Example #1
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_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": "a"}
        view.site = Mock()
        self.assertEquals(
            [
                {
                    "color": "",
                    "path": "path1",
                    "results": [],
                    "admins": [{"username": "a"}],
                    "members": [{"username": "a"}],
                    "title": "Title1",
                }
            ],
            [x for x in view.teams],
        )
Example #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"]
Example #4
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')
Example #5
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])
Example #6
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)
Example #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")
Example #8
0
 def test_help(self, abort):
     app = Flask(__name__)
     with app.app_context():
         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)
Example #9
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")
Example #10
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_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)
Example #12
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)
Example #13
0
    def test_migrate_reply_queue_excl_argument(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)
        fake_broker.delQueue.assert_called_with(Services.REPLY_QUEUE)
        fake_broker.addQueue.assert_called_with(Services.REPLY_QUEUE, durable=True)
Example #14
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)
Example #15
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)
    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)
Example #17
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)