Beispiel #1
0
def test_interactive_function_success():
    """Ensure the function is called when it passes inspection."""
    def good_function(session):
        return session.run("who")

    runner = Bladerunner()
    mock_hosts = Mock()
    # we need to mock out the interactive_hosts dict b/c dict.values will
    # return a new object every time, making the assert_called_once_with fail
    runner.interactive_hosts = mock_hosts

    map_mock = patch.object(
        base.ThreadPoolExecutor,
        "map",
        return_value=["totes fake"],
    )

    with patch.object(runner, "setup_interactive") as mock_setup:
        with map_mock as mock_map:
            res = runner.run_interactive_function(good_function, "some host")

    mock_setup.assert_called_once_with("some host")
    assert mock_map.call_count == 1
    mock_map.assert_called_once_with(
        good_function,
        mock_hosts.values(),
    )

    assert res == ["totes fake"]
Beispiel #2
0
 def bad_choice():
     parser = RequestParser()
     parser.add_argument('foo', choices=['one', 'two'])
     req = Mock(['values'])
     req.values = MultiDict([('foo', 'three')])
     parser.parse_args(req)
     abort.assert_called_with(400, message='three is not a valid choice')
Beispiel #3
0
def test_interactive_function_success():
    """Ensure the function is called when it passes inspection."""

    def good_function(session):
        return session.run("who")

    runner = Bladerunner()
    mock_hosts = Mock()
    # we need to mock out the interactive_hosts dict b/c dict.values will
    # return a new object every time, making the assert_called_once_with fail
    runner.interactive_hosts = mock_hosts

    map_mock = patch.object(
        base.ThreadPoolExecutor,
        "map",
        return_value=["totes fake"],
    )

    with patch.object(runner, "setup_interactive") as mock_setup:
        with map_mock as mock_map:
            res = runner.run_interactive_function(good_function, "some host")

    mock_setup.assert_called_once_with("some host")
    assert mock_map.call_count == 1
    mock_map.assert_called_once_with(
        good_function,
        mock_hosts.values(),
    )

    assert res == ["totes fake"]
 def bad_choice():
     parser = RequestParser()
     parser.add_argument("foo", choices=["one", "two"])
     req = Mock(["values"])
     req.values = MultiDict([("foo", "three")])
     parser.parse_args(req)
     abort.assert_called_with(400, message="three is not a valid choice")
Beispiel #5
0
 def bad_choice():
     parser = RequestParser()
     parser.add_argument('foo', choices=['one', 'two'])
     req = Mock(['values'])
     req.values = MultiDict([('foo', 'three')])
     parser.parse_args(req)
     abort.assert_called_with(400, message='three is not a valid choice')
 def test_help(self, abort):
     parser = RequestParser()
     parser.add_argument('foo', choices=['one', 'two'], help='Bad choice')
     req = Mock(['values'])
     req.values = MultiDict([('foo', 'three')])
     parser.parse_args(req)
     expected = '[foo]: (Bad choice) three is not a valid choice'
     abort.assert_called_with(400, message=expected)
Beispiel #7
0
 def test_help(self, abort):
     from werkzeug.datastructures import MultiDict
     parser = RequestParser()
     parser.add_argument('foo', choices=['one', 'two'], help='Bad choice')
     req = Mock(['values'])
     req.values = MultiDict([('foo', 'three')])
     parser.parse_args(req)
     abort.assert_called_with(400, message='Bad choice')
Beispiel #8
0
 def test_help(self, abort):
     from werkzeug.datastructures import MultiDict
     parser = RequestParser()
     parser.add_argument('foo', choices=['one', 'two'], help='Bad choice')
     req = Mock(['values'])
     req.values = MultiDict([('foo', 'three')])
     parser.parse_args(req)
     abort.assert_called_with(400, message='Bad choice')
Beispiel #9
0
    def test_multiple_nested_keys(self):
        parser = RequestParser()
        parser.add_argument("baz", nesting=['foo', 'bar'])

        req = Mock(['values'])
        req.values = {"foo": {"bar": {"baz": "qux"}}}
        args = parser.parse_args(req)
        self.assertEquals(args['baz'], "qux")
Beispiel #10
0
 def test_classes(self, _isclass):
     func_globals = Mock()
     func_globals.values.return_value = [1, 2, 3]
     _isclass.side_effect = [True, False, True]
     collator = Collator()
     classes = collator._Collator__classes(func_globals)
     _isclass.assert_has_calls([call(c) for c in func_globals.values()])
     self.assertEqual(classes, [1, 3])
Beispiel #11
0
 def test_classes(self, _isclass):
     func_globals = Mock()
     func_globals.values.return_value = [1, 2, 3]
     _isclass.side_effect = [True, False, True]
     collator = Collator()
     classes = collator._Collator__classes(func_globals)
     _isclass.assert_has_calls([call(c) for c in func_globals.values()])
     self.assertEqual(classes, [1, 3])
Beispiel #12
0
 def test_help(self, abort):
     parser = RequestParser()
     parser.add_argument('foo', choices=['one', 'two'], help='Bad choice')
     req = Mock(['values'])
     req.values = MultiDict([('foo', 'three')])
     parser.parse_args(req)
     expected = '[foo]: (Bad choice) three is not a valid choice'
     abort.assert_called_with(400, message=expected)
    def test_help(self, abort):
        from werkzeug.datastructures import MultiDict

        parser = RequestParser()
        parser.add_argument("foo", choices=["one", "two"], help="Bad choice")
        req = Mock(["values"])
        req.values = MultiDict([("foo", "three")])
        parser.parse_args(req)
        abort.assert_called_with(400, message="Bad choice")
Beispiel #14
0
 def test_no_help(self, abort):
     with self.assertRaises(exceptions.BadRequest):
         from werkzeug.datastructures import MultiDict
         parser = RequestParser()
         parser.add_argument('foo', choices=['one', 'two'])
         req = Mock(['values'])
         req.values = MultiDict([('foo', 'three')])
         parser.parse_args(req)
         abort.assert_called_with(400, message='three is not a valid choice')
 def test_help_with_unicode_error_msg(self, abort):
     app = Flask(__name__)
     with app.app_context():
         parser = RequestParser()
         parser.add_argument("foo", choices=("one", "two"), help=u"Bad choice: {error_msg}")
         req = Mock(["values"])
         req.values = MultiDict([("foo", u"\xf0\x9f\x8d\x95")])
         parser.parse_args(req)
         expected = {"foo": u"Bad choice: \xf0\x9f\x8d\x95 is not a valid choice"}
         abort.assert_called_with(400, message=expected)
Beispiel #16
0
 def test_help_no_error_msg(self, abort):
     app = Flask(__name__)
     with app.app_context():
         parser = RequestParser()
         parser.add_argument('foo', choices=['one', 'two'], help='Please select a valid choice')
         req = Mock(['values'])
         req.values = MultiDict([('foo', 'three')])
         parser.parse_args(req)
         expected = {'foo': 'Please select a valid choice'}
         abort.assert_called_with(400, message=expected)
Beispiel #17
0
 def test_help_no_error_msg(self, abort):
     app = Flask(__name__)
     with app.app_context():
         parser = RequestParser()
         parser.add_argument('foo', choices=['one', 'two'], help='Please select a valid choice')
         req = Mock(['values'])
         req.values = MultiDict([('foo', 'three')])
         parser.parse_args(req)
         expected = {'foo': 'Please select a valid choice'}
         abort.assert_called_with(400, message=expected)
 def test_help_no_error_msg(self, abort):
     app = Flask(__name__)
     with app.app_context():
         parser = RequestParser()
         parser.add_argument("foo", choices=["one", "two"], help="Please select a valid choice")
         req = Mock(["values"])
         req.values = MultiDict([("foo", "three")])
         parser.parse_args(req)
         expected = {"foo": "Please select a valid choice"}
         abort.assert_called_with(400, message=expected)
Beispiel #19
0
 def test_help_with_unicode_error_msg(self, abort):
     app = Flask(__name__)
     with app.app_context():
         parser = RequestParser()
         parser.add_argument('foo', choices=('one', 'two'), help=u'Bad choice: {error_msg}')
         req = Mock(['values'])
         req.values = MultiDict([('foo', u'\xf0\x9f\x8d\x95')])
         self.assertRaises(exceptions.BadRequest, parser.parse_args, req)
         expected = {'foo': u'Bad choice: \xf0\x9f\x8d\x95 is not a valid choice'}
         abort.assert_called_with(400, message=expected)
 def test_help_with_unicode_error_msg(self, abort):
     app = Flask(__name__)
     with app.app_context():
         parser = RequestParser()
         parser.add_argument('foo',
                             choices=('one', 'two'),
                             help=u'Bad choice: {error_msg}')
         req = Mock(['values'])
         req.values = MultiDict([('foo', u'\xf0\x9f\x8d\x95')])
         parser.parse_args(req)
         expected = {
             'foo': u'Bad choice: \xf0\x9f\x8d\x95 is not a valid choice'
         }
         abort.assert_called_with(400, message=expected)
Beispiel #21
0
    def test_migrate_reply_queue_not_exclusive_does_not_delete_or_add_reply_queue(
            self):
        fake_queue = Mock()
        fake_queue.values = {'exclusive': False, 'arguments': {}}
        fake_broker = Mock()
        fake_broker.getQueue.return_value = fake_queue

        # test
        migration = MigrationModule(MIGRATION)._module
        migration._migrate_reply_queue(fake_broker)

        # validation
        fake_broker.getQueue.assert_called_with(Services.REPLY_QUEUE)
        self.assertTrue(not self.mock_del_queue_catch_queue_in_use_exc.called)
        self.assertTrue(not fake_broker.addQueue.called)
Beispiel #22
0
    def test_viewargs(self):
        req = Mock()
        req.view_args = {"foo": "bar"}
        parser = RequestParser()
        parser.add_argument("foo", location=["view_args"], type=str)
        args = parser.parse_args(req)
        self.assertEquals(args['foo'], "bar")

        req = Mock()
        req.values = ()
        req.view_args = {"foo": "bar"}
        parser = RequestParser()
        parser.add_argument("foo", type=str)
        args = parser.parse_args(req)
        self.assertEquals(args["foo"], None)
Beispiel #23
0
    def test_viewargs(self):
        req = Mock()
        req.view_args = {"foo": "bar"}
        parser = RequestParser()
        parser.add_argument("foo", location=["view_args"], type=str)
        args = parser.parse_args(req)
        self.assertEquals(args['foo'], "bar")

        req = Mock()
        req.values = ()
        req.view_args = {"foo": "bar"}
        parser = RequestParser()
        parser.add_argument("foo", type=str)
        args = parser.parse_args(req)
        self.assertEquals(args["foo"], None)
    def test_viewargs(self):
        req = Request.from_values()
        req.view_args = {"foo": "bar"}
        parser = RequestParser()
        parser.add_argument("foo", location=["view_args"])
        args = parser.parse_args(req)
        self.assertEquals(args['foo'], "bar")

        req = Mock()
        req.values = ()
        req.json = None
        req.view_args = {"foo": "bar"}
        parser = RequestParser()
        parser.add_argument("foo", store_missing=True)
        args = parser.parse_args(req)
        self.assertEquals(args["foo"], None)
Beispiel #25
0
    def test_viewargs(self):
        req = Request.from_values()
        req.view_args = {"foo": "bar"}
        parser = RequestParser()
        parser.add_argument("foo", location=["view_args"])
        args = parser.parse_args(req)
        self.assertEquals(args['foo'], "bar")

        req = Mock()
        req.values = ()
        req.json = None
        req.view_args = {"foo": "bar"}
        parser = RequestParser()
        parser.add_argument("foo", store_missing=True)
        args = parser.parse_args(req)
        self.assertEquals(args["foo"], None)
Beispiel #26
0
    def test_migrate_reply_queue(self):
        fake_queue = Mock()
        fake_queue.values = {
            'exclusive': True,
            'arguments': {}
        }
        fake_broker = Mock()
        fake_broker.getQueue.return_value = fake_queue

        # test
        migration = MigrationModule(MIGRATION)._module
        migration._migrate_reply_queue(fake_broker)

        # validation
        fake_broker.getQueue.assert_called_with(Services.REPLY_QUEUE)
        fake_broker.delQueue.assert_called_with(Services.REPLY_QUEUE)
        fake_broker.addQueue.assert_called_with(Services.REPLY_QUEUE, durable=True)
Beispiel #27
0
    def test_migrate_reply_queue_not_exclusive(self):
        fake_queue = Mock()
        fake_queue.values = {
            'exclusive': False,
            'arguments': {}
        }
        fake_broker = Mock()
        fake_broker.getQueue.return_value = fake_queue

        # test
        migration = MigrationModule(MIGRATION)._module
        migration._migrate_reply_queue(fake_broker)

        # validation
        fake_broker.getQueue.assert_called_with(Services.REPLY_QUEUE)
        self.assertFalse(fake_broker.called)
        self.assertFalse(fake_broker.called)
    def test_marshal_valid_with_time(self):
        command = Mock()
        command.is_valid.return_value = True
        command.plugin = 'my'
        command.plugin_instance = 'plugin'
        command.type_ = 'mytype'
        command.type_instance = 'myinstance'
        command.interval = '1'
        command.values = ('2', '3')
        command.time = 42

        result = self.marshaler.marshal_message(command)

        expected = 'PUTVAL {uuid}/my-plugin/mytype-myinstance interval=1 42:2:3'.format(
            uuid=self.uuid)

        assert_that(result, equal_to(expected))
Beispiel #29
0
    def test_migrate_reply_queue_not_exclusive_does_not_delete_or_add_reply_queue(self):
        fake_queue = Mock()
        fake_queue.values = {
            'exclusive': False,
            'arguments': {}
        }
        fake_broker = Mock()
        fake_broker.getQueue.return_value = fake_queue

        # test
        migration = MigrationModule(MIGRATION)._module
        migration._migrate_reply_queue(fake_broker)

        # validation
        fake_broker.getQueue.assert_called_with(Services.REPLY_QUEUE)
        self.assertTrue(not self.mock_del_queue_catch_queue_in_use_exc.called)
        self.assertTrue(not fake_broker.addQueue.called)
Beispiel #30
0
    def test_migrate_reply_queue_arguments_is_exclusive_deletes_and_add_reply_queue(self):
        fake_queue = Mock()
        fake_queue.values = {
            'exclusive': False,
            'arguments': {'exclusive': True}
        }
        fake_broker = Mock()
        fake_broker.getQueue.return_value = fake_queue

        # test
        migration = MigrationModule(MIGRATION)._module
        migration._migrate_reply_queue(fake_broker)

        # validation
        fake_broker.getQueue.assert_called_with(Services.REPLY_QUEUE)
        expected_call = call(fake_broker, Services.REPLY_QUEUE)
        self.mock_del_queue_catch_queue_in_use_exc.assert_has_calls(expected_call)
        fake_broker.addQueue.assert_called_with(Services.REPLY_QUEUE, durable=True)
Beispiel #31
0
    def test_migrate_reply_queue_checks_deleted_and_recreates_reply_queue(
            self):
        fake_queue = Mock()
        fake_queue.values = {'exclusive': True, 'arguments': {}}
        fake_broker = Mock()
        fake_broker.getQueue.return_value = fake_queue

        # test
        migration = MigrationModule(MIGRATION)._module
        migration._migrate_reply_queue(fake_broker)

        # validation
        fake_broker.getQueue.assert_called_once_with(Services.REPLY_QUEUE)
        expected_call = call(fake_broker, Services.REPLY_QUEUE)
        self.mock_del_queue_catch_queue_in_use_exc.assert_has_calls(
            expected_call)
        fake_broker.addQueue.assert_called_once_with(Services.REPLY_QUEUE,
                                                     durable=True)
 def test_teams(self):
     from collective.team.browser import intranet
     context = Mock()
     folder_contents = [Mock()]
     folder_contents[0].absolute_url = lambda: 'path1'
     folder_contents[0].title = 'Title1'
     folder_contents[0].managers = 'a'
     folder_contents[0].members = 'a'
     folder_contents[0].portal_type = 'team'
     context.values = lambda: folder_contents
     view = intranet.StartPage(context, None)
     view.get_member_data = lambda x:{'username': '******'}
     view.site = Mock()
     self.assertEquals([{'color': '',
                         'path': 'path1',
                         'results': [],
                         'admins': [{'username': '******'}],
                         'members': [{'username': '******'}],
                         'title': 'Title1'}],
                       [x for x in view.teams])
Beispiel #33
0
def _build_mock_config_child(key, value):
    mock_config_child = Mock()
    mock_config_child.key = key
    mock_config_child.values = [value]

    return mock_config_child
Beispiel #34
0
 def mock_celery_get_stats(self):
     stats = Mock()
     stats.values = MagicMock(return_value=True)
     return stats