def test_join_one_app_should_produce_one_app_not_a_list(self, fixture): """ Um POST em /v2/apps, apesar de receber no body apens uma app ({...}), após o request.join(), restá produzindo um request com uma lista de apps: [{...}], e a API do marathon não aceita lista no POST apenas no PUT. O problema parece ser no request.join():89, onde fazemos if self.is_list_app_request(). Precisamos olhar se é PUT ou POST e gerar list() ou dict() apropriadamente. """ with application.test_request_context("/v2/apps/", method="POST", data=json.dumps(fixture)) as ctx: ctx.request.user = self.user request_parser = Request(ctx.request) mock_app = get_fixture("single_full_app.json") mock_apps = [(MarathonApp.from_json(mock_app), Mock())] joined_request = request_parser.join(mock_apps) self.assertIsInstance(joined_request, HollowmanRequest) joined_request_data = json.loads(joined_request.data) self.assertFalse( isinstance(joined_request_data, list), "Body não deveria ser uma lista", ) self.assertEqual("/foo", joined_request_data["id"])
def test_change_request_path_if_is_write_on_one_app(self, fixture): """ Quando fazemos WRITE em cima de uma app específica, devemos ajustar o request.path para que o `upstream_request` seja feito no endpoint correto. """ user = User(tx_name="User One", tx_email="*****@*****.**") user.current_account = Account(name="Dev", namespace="dev", owner="company") full_app_with_name_space = deepcopy(fixture) full_app_with_name_space['id'] = "/dev/foo" with application.test_request_context('/v2/apps//foo', method='PUT', data=json.dumps(fixture)) as ctx: with RequestsMock() as rsps: rsps.add(method='GET', url=conf.MARATHON_ADDRESSES[0] + '/v2/apps//dev/foo', body=json.dumps({'app': full_app_with_name_space}), status=200) ctx.request.user = user request_parser = Request(ctx.request) apps = list(request_parser.split()) request = request_parser.join(apps) self.assertIsInstance(request, HollowmanRequest) self.assertEqual("/v2/apps/dev/foo", request.path)
def test_join_apps_read_empty_list(self): with application.test_request_context('/v2/apps', method='GET') as ctx: ctx.request.user = self.user request = Request(ctx.request) with RequestsMock() as rsps: rsps.add(method='GET', url=conf.MARATHON_ADDRESSES[0] + '/v2/apps', status=200, body='''{"apps":[]}''') apps = list(request.split()) joined_request = request.join(apps) self.assertEqual("/v2/apps", joined_request.path) self.assertEqual(b"", joined_request.data)
def test_change_request_path_if_is_read_single_app( self, single_full_app_fixture): with application.test_request_context('/v2/apps/foo', method='GET') as ctx: ctx.request.user = self.user request_parser = Request(ctx.request) single_full_app_fixture['id'] = "/dev/foo" apps = [(MarathonApp.from_json(single_full_app_fixture), MarathonApp.from_json(single_full_app_fixture))] request = request_parser.join(apps) self.assertIsInstance(request, HollowmanRequest) self.assertEqual("/v2/apps/dev/foo", request.path)
def test_it_recreates_a_post_request_for_a_single_app(self, fixture): with application.test_request_context('/v2/apps//foo', method='POST', data=json.dumps(fixture)) as ctx: ctx.request.user = self.user request_parser = Request(ctx.request) with patch.object(request_parser, 'marathon_client') as client: client.get_app.return_value = MarathonApp.from_json(fixture) apps = list(request_parser.split()) request = request_parser.join(apps) self.assertIsInstance(request, HollowmanRequest) self.assertEqual(request.get_json()['id'], '/foo')
def test_join_queue_should_return_original_request_if_GET(self): """ Quando fazemos GET em /v2/queue, temos apenas que deixar request passar. """ with application.test_request_context('/v2/queue', method='GET') as ctx: ctx.request.user = self.user request_parser = Request(ctx.request) joined_request = request_parser.join([]) self.assertIsInstance(joined_request, HollowmanRequest) self.assertEqual(b'', joined_request.data, "Body deveria estar vazio") self.assertEqual("/v2/queue", joined_request.path)
def test_it_call_dispatch_using_user_from_request(self): """ Certificamos que o user preenchido no request é repassado para o dispatch """ with application.test_request_context('/v2/apps/foo', method='GET') as ctx: with patch('hollowman.request_handlers.upstream_request'), \ patch('hollowman.request_handlers.dispatch') as dispatch_mock: user = MagicMock() ctx.request.user = user request_parser = Request(ctx.request) request_parser.split = MagicMock(return_value=[self.request_apps[0]]) request_parser.join = MagicMock() response = new(request_parser) dispatch_mock.assert_called_once_with(user=user, request=ANY)
def test_it_recreates_a_get_request_for_a_single_app(self, fixture): with application.test_request_context("/v2/apps//foo", method="GET", data=b"") as ctx: ctx.request.user = self.user request_parser = Request(ctx.request) with patch.object(request_parser, "marathon_client") as client: client.get_app.return_value = MarathonApp.from_json(fixture) apps = list(request_parser.split()) request = request_parser.join(apps) self.assertIsInstance(request, HollowmanRequest) self.assertEqual(request, ctx.request) self.assertEqual(request.data, b"")
def test_join_group_read_root_group(self, group_dev_namespace_fixture): with application.test_request_context('/v2/groups', method='GET') as ctx: ctx.request.user = self.user request = Request(ctx.request) with RequestsMock() as rsps: rsps.add(method='GET', url=conf.MARATHON_ADDRESSES[0] + '/v2/groups//dev/', body=json.dumps(group_dev_namespace_fixture), status=200) apps = list(request.split()) joined_request = request.join(apps) self.assertEqual("/v2/groups/dev", joined_request.path) self.assertEqual(b"", joined_request.data)
def test_it_recreates_a_put_request_for_multiple_apps(self, fixture): with application.test_request_context('/v2/apps/', method='PUT', data=json.dumps(fixture)) as ctx: ctx.request.user = self.user request_parser = Request(ctx.request) mock_app = get_fixture('single_full_app.json') mock_apps = [(MarathonApp.from_json(mock_app), Mock()) for _ in range(2)] request = request_parser.join(mock_apps) self.assertIsInstance(request, HollowmanRequest) self.assertCountEqual( [app['id'] for app in json.loads(request.data)], [app.id for app, _ in mock_apps])
def test_join_apps_read_empty_list(self): with application.test_request_context("/v2/apps", method="GET") as ctx: ctx.request.user = self.user request = Request(ctx.request) with RequestsMock() as rsps: rsps.add( method="GET", url=conf.MARATHON_ADDRESSES[0] + "/v2/groups//dev/", status=200, body=json.dumps({"apps": []}), ) apps = list(request.split()) joined_request = request.join(apps) self.assertEqual("/v2/apps", joined_request.path) self.assertEqual(b"", joined_request.data)
def test_join_queue_should_return_original_request_with_path_adjusted( self): """ No momento d join() devemos ajustar o app_id para adicionar o namespace. """ with application.test_request_context( '/v2/queue/myapp/multi/path/delay', method='DELETE') as ctx: ctx.request.user = self.user request_parser = Request(ctx.request) joined_request = request_parser.join([]) self.assertIsInstance(joined_request, HollowmanRequest) self.assertEqual(b'', joined_request.data, "Body deveria estar vazio") self.assertEqual("/v2/queue/dev/myapp/multi/path/delay", joined_request.path)
def test_join_v2_apps_on_DELETE_method(self): """ O request de DELETE tem o corpo vazio. Isso significa que o split() não retornou nada, o que faz o join ser chamado assim: .join([]). É isso que esse teste trata. """ with application.test_request_context('/v2/apps/group', method='DELETE') as ctx: ctx.request.user = self.user request_parser = Request(ctx.request) joined_request = request_parser.join([]) self.assertIsInstance(joined_request, HollowmanRequest) self.assertEqual(b'', joined_request.data, "Body deveria estar vazio") self.assertEqual("/v2/apps/dev/group", joined_request.path)
def test_join_tasks_POST(self, tasks_post_fixture): """ O join deve juntar apenas o id das tasks. O body do request é apenas: {"ids": ["...", "...", ...]} """ with application.test_request_context( '/v2/tasks/delete', method='POST', data=json.dumps(tasks_post_fixture)) as ctx: ctx.request.user = self.user request_parser = Request(ctx.request) joined_request = request_parser.join(list(request_parser.split())) joined_request_data = json.loads(joined_request.data) self.assertEqual( [task_id for task_id in joined_request_data['ids']], [task_id for task_id in tasks_post_fixture['ids']])
def test_join_group_read_non_root_empty_group( self, non_root_group_empty_fixture): with application.test_request_context("/v2/groups/group-c", method="GET") as ctx: ctx.request.user = self.user request = Request(ctx.request) with RequestsMock() as rsps: rsps.add( method="GET", url=conf.MARATHON_ADDRESSES[0] + "/v2/groups//dev/group-c", body=json.dumps(non_root_group_empty_fixture), status=200, ) apps = list(request.split()) joined_request = request.join(apps) self.assertEqual("/v2/groups/dev/group-c", joined_request.path) self.assertEqual(b"", joined_request.data)