def compose_mock_response(gmp_name): gmp_plural = pluralize_name(gmp_name) xml_response = f''' <get_{gmp_plural}_response> <{gmp_name} id="08b69003-5fc2-4037-a479-93b440211c73"> <owner> <name>admin</name> </owner> <name>foo</name> <comment>bar</comment> <creation_time>2019-07-19T13:33:21Z</creation_time> <modification_time>2019-07-19T13:33:21Z</modification_time> <writable>1</writable> <in_use>1</in_use> <permissions> <permission> <name>Everything</name> </permission> </permissions> <user_tags> <count>2</count> <tag id="480cdbd9-1af2-4be6-a195-163748b80381"> <name>test tag 1</name> <value/> <comment/> </tag> <tag id="82a7864f-b9ab-4063-9153-c84e37e5c213"> <name>test tag 2</name> <value/> <comment/> </tag> </user_tags> </{gmp_name}> <{gmp_name} id="6b2db524-9fb0-45b8-9b56-d958f84cb546"> <owner> <name>admin</name> </owner> <name>lorem</name> <comment>ipsum</comment> <creation_time>2019-07-19T13:33:21Z</creation_time> <modification_time>2019-07-19T13:33:21Z</modification_time> <writable>1</writable> <in_use>0</in_use> <permissions> <permission> <name>{gmp_name}</name> </permission> <permission> <name>{gmp_name}</name> </permission> </permissions> </{gmp_name}> </get_{gmp_plural}_response> ''' return xml_response
def make_test_edges( gmp_name: str, *, selene_name: str = None, edge_name: str = None, gmp_cmd: str = None, plural_selene_name: str = None, no_plural: bool = False, ): # no_plural for e.g. info where there is no plural - I wanted # to use this tests for that entity too # was a little hacky ... if not selene_name: selene_name = gmp_name # for special gmp commands like "get_info_list" if not gmp_cmd: gmp_cmd = compose_mock_command(gmp_name) if edge_name is None: edge_name = selene_name.lower() # for special plurals of irregulars like policy if not plural_selene_name: plural_selene_name = pluralize_name(selene_name) @unittest.mock.patch('selene.views.Gmp', new_callable=GmpMockFactory) def test(self, mock_gmp: GmpMockFactory): mock_gmp.mock_response(gmp_cmd, compose_mock_response(gmp_name, no_plural)) self.login('foo', 'bar') response = self.query(compose_mock_query(plural_selene_name)) self.assertResponseNoErrors(response) json = response.json() edges = json['data'][plural_selene_name]['edges'] edge1 = edges[0] edge2 = edges[1] self.assertEqual(edge1['cursor'], get_cursor(edge_name, 0)) self.assertEqual(edge2['cursor'], get_cursor(edge_name, 1)) entity1 = edge1['node'] entity2 = edge2['node'] self.assertEqual(entity1['id'], 'e9b98e26-9fff-4ee8-9378-bc44fe3d6f2b') self.assertEqual(entity2['id'], '85787cbb-a737-463d-94b8-fcc348225f3b') return test
def make_test_counts( gmp_name: str, *, selene_name: str = None, gmp_cmd: str = None, plural_selene_name: str = None, no_plural: bool = False, ): # no_plural for e.g. info where there is no plural - I wanted # to use this tests for that entity too # was a little hacky ... but this way no old tests need to be adjusted ... if not selene_name: selene_name = gmp_name # for special gmp commands like "get_info_list" if not gmp_cmd: gmp_cmd = compose_mock_command(gmp_name) # for special plurals of irregulars like policy if not plural_selene_name: plural_selene_name = pluralize_name(selene_name) @unittest.mock.patch('selene.views.Gmp', new_callable=GmpMockFactory) def test(self, mock_gmp: GmpMockFactory): mock_gmp.mock_response( gmp_cmd, compose_mock_response(gmp_name, no_plural) ) self.login('foo', 'bar') response = self.query(compose_mock_query(plural_selene_name)) self.assertResponseNoErrors(response) json = response.json() entities = json['data'][plural_selene_name]['nodes'] self.assertEqual(len(entities), 2) counts = json['data'][plural_selene_name]['counts'] self.assertEqual(counts['filtered'], 2) self.assertEqual(counts['total'], 2) self.assertEqual(counts['offset'], 0) self.assertEqual(counts['limit'], 10) self.assertEqual(counts['length'], 2) return test
def compose_mock_response(gmp_name): gmp_plural = pluralize_name(gmp_name) xml_response = f''' <get_{gmp_plural}_response> <{gmp_name} id="08b69003-5fc2-4037-a479-93b440211c73"> <name>a</name> </{gmp_name}> <{gmp_name} id="6b2db524-9fb0-45b8-9b56-d958f84cb546"> <name>b</name> </{gmp_name}> <{gmp_plural} start="1" max="10"/> <{gmp_name}_count>2<filtered>2</filtered> <page>2</page> </{gmp_name}_count> </get_{gmp_plural}_response> ''' return xml_response
def compose_mock_response(entity_name, no_plural): entities_name = pluralize_name(entity_name) xml_response = f''' <get_{entities_name}_response status="200" status_text="OK"> <{entity_name} id="e9b98e26-9fff-4ee8-9378-bc44fe3d6f2b"> <name>foo</name> </{entity_name}> <{entity_name} id="85787cbb-a737-463d-94b8-fcc348225f3b"> <name>bar</name> </{entity_name}> <{entity_name if no_plural else entities_name} start="1" max="10"/> <{entity_name}_count>2<filtered>2</filtered> <page>2</page> </{entity_name}_count> </get_{entities_name}_response> ''' return xml_response
def make_test_after_first( gmp_name: str, *, selene_name: str = None, gmp_cmd: str = None, plural_selene_name: str = None, **kwargs, ): if not selene_name: selene_name = gmp_name # for special gmp commands like "get_info_list" if not gmp_cmd: gmp_cmd = compose_mock_command(gmp_name) # for special plurals of irregulars like policy if not plural_selene_name: plural_selene_name = pluralize_name(selene_name) @unittest.mock.patch('selene.views.Gmp', new_callable=GmpMockFactory) def test(self, mock_gmp: GmpMockFactory): gmp_commands = return_gmp_methods(mock_gmp.gmp_protocol) mock_gmp.mock_response(gmp_cmd, compose_mock_response(gmp_name)) self.login('foo', 'bar') response = self.query(compose_mock_query(plural_selene_name)) json = response.json() self.assertResponseNoErrors(response) get_entities = gmp_commands[gmp_cmd] get_entities.assert_called_with( filter='lorem rows=10 first=125', **kwargs ) entities = json['data'][plural_selene_name]['nodes'] self.assertEqual(len(entities), 2) return test
def make_test_page_info( gmp_name: str, *, selene_name: str = None, query=None, gmp_cmd: str = None, plural_selene_name: str = None, no_plural: bool = False, ): # no_plural for e.g. info where there is no plural - I wanted # to use this tests for that entity too # was a little hacky ... if not selene_name: selene_name = gmp_name # for special gmp commands like "get_info_list" if not gmp_cmd: gmp_cmd = compose_mock_command(gmp_name) # for special plurals of irregulars like policy if not plural_selene_name: plural_selene_name = pluralize_name(selene_name) @unittest.mock.patch('selene.views.Gmp', new_callable=GmpMockFactory) def test(self, mock_gmp: GmpMockFactory): mock_gmp.mock_response(gmp_cmd, compose_mock_response(gmp_name, no_plural)) self.login('foo', 'bar') response = self.query(compose_mock_query(plural_selene_name)) json = response.json() self.assertResponseNoErrors(response) page_info = json['data'][plural_selene_name]['pageInfo'] self.assertTrue(page_info['hasNextPage']) self.assertTrue(page_info['hasPreviousPage']) edge_class = query().type._meta.edge self.assertEqual(page_info['startCursor'], edge_class.get_cursor(2)) self.assertEqual(page_info['endCursor'], edge_class.get_cursor(3)) return test
def compose_mock_response(entity_name, no_plural): entities_name = pluralize_name(entity_name) xml_response = f''' <get_{entities_name}_response> <{entity_name} id="f650a1c0-3d23-11ea-8540-e790e17c1b00"> <name>a</name> </{entity_name}> <{entity_name} id="0778ac90-3d24-11ea-b722-fff755412c48"> <name>b</name> </{entity_name}> <{entity_name}_count> 20 <filtered>11</filtered> </{entity_name}_count> <{entity_name if no_plural else entities_name} max="10" start="3"/> </get_{entities_name}_response> ''' return xml_response
def make_test_get_entities( gmp_name: str, *, selene_name: str = None, gmp_cmd: str = None, plural_selene_name: str = None, **kwargs, ): if not selene_name: selene_name = gmp_name # for special gmp commands like "get_info_list" if not gmp_cmd: gmp_cmd = compose_mock_command(gmp_name) # for special plurals of irregulars like policy if not plural_selene_name: plural_selene_name = pluralize_name(selene_name) @unittest.mock.patch('selene.views.Gmp', new_callable=GmpMockFactory) def test(self, mock_gmp: GmpMockFactory): # get the gmp_commands gmp_commands = return_gmp_methods(mock_gmp.gmp_protocol) # create the mock response with the gmp_command and the gmp_name mock_gmp.mock_response(gmp_cmd, compose_mock_response(gmp_name)) self.login('foo', 'bar') response = self.query(compose_mock_query(plural_selene_name)) json = response.json() self.assertResponseNoErrors(response) get_entities = gmp_commands[gmp_cmd] get_entities.assert_called_with(filter='lorem', **kwargs) entities = json['data'][plural_selene_name]['nodes'] self.assertEqual(len(entities), 2) entity1 = entities[0] entity2 = entities[1] # Entity 1 self.assertEqual(entity1['id'], '08b69003-5fc2-4037-a479-93b440211c73') self.assertEqual(entity1['name'], 'foo') self.assertEqual(entity1['comment'], "bar") self.assertEqual(entity1['owner'], 'admin') self.assertEqual(entity1['creationTime'], '2019-07-19T13:33:21+00:00') self.assertEqual(entity1['modificationTime'], '2019-07-19T13:33:21+00:00') self.assertTrue(entity1['inUse']) self.assertTrue(entity1['writable']) permissions = entity1['permissions'] self.assertEqual(len(permissions), 1) self.assertEqual(permissions[0]['name'], 'Everything') user_tags = entity1['userTags'] self.assertEqual(user_tags['count'], 2) tags = user_tags['tags'] self.assertEqual(len(tags), 2) tag1 = tags[0] self.assertEqual(tag1['name'], 'test tag 1') self.assertEqual(tag1['id'], '480cdbd9-1af2-4be6-a195-163748b80381') self.assertIsNone(tag1['value']) self.assertIsNone(tag1['comment']) # Entity 2 self.assertEqual(entity2['id'], '6b2db524-9fb0-45b8-9b56-d958f84cb546') self.assertEqual(entity2['name'], 'lorem') self.assertEqual(entity2['comment'], 'ipsum') self.assertEqual(entity2['owner'], 'admin') self.assertEqual(entity2['creationTime'], '2019-07-19T13:33:21+00:00') self.assertEqual(entity2['modificationTime'], '2019-07-19T13:33:21+00:00') self.assertFalse(entity2['inUse']) self.assertTrue(entity2['writable']) permissions = entity2['permissions'] self.assertEqual(len(permissions), 2) self.assertEqual(permissions[0]['name'], gmp_name) self.assertIsNone(entity2['userTags']) return test