| 
									
										
										
										
											2018-11-08 19:45:21 +00:00
										 |  |  | """
 | 
					
						
							|  |  |  | Test peeringdb data export views | 
					
						
							|  |  |  | """
 | 
					
						
							|  |  |  | import os | 
					
						
							|  |  |  | import pytest | 
					
						
							|  |  |  | import json | 
					
						
							|  |  |  | import difflib | 
					
						
							| 
									
										
										
										
											2020-01-08 13:29:58 -06:00
										 |  |  | import re | 
					
						
							| 
									
										
										
										
											2018-11-08 19:45:21 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | from django.test import Client | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-01-08 13:29:58 -06:00
										 |  |  | from .util import ClientCase | 
					
						
							| 
									
										
										
										
											2018-11-08 19:45:21 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-12-05 16:57:52 +00:00
										 |  |  | from peeringdb_server.models import ( | 
					
						
							|  |  |  |     Organization, | 
					
						
							|  |  |  |     Network, | 
					
						
							|  |  |  |     InternetExchange, | 
					
						
							|  |  |  |     Facility, | 
					
						
							|  |  |  |     NetworkFacility, | 
					
						
							|  |  |  |     NetworkIXLan, | 
					
						
							|  |  |  |     IXLan, | 
					
						
							|  |  |  | ) | 
					
						
							| 
									
										
										
										
											2018-11-08 19:45:21 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | class AdvancedSearchExportTest(ClientCase): | 
					
						
							|  |  |  |     """
 | 
					
						
							|  |  |  |     Tests advanced search result exports | 
					
						
							|  |  |  |     """
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     @classmethod | 
					
						
							|  |  |  |     def setUpTestData(cls): | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         ClientCase.setUpTestData() | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-01-08 13:29:58 -06:00
										 |  |  |         entity_count = list(range(1, 4)) | 
					
						
							| 
									
										
										
										
											2018-11-08 19:45:21 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |         countries = ["US", "FI", ""] | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-01-08 13:29:58 -06:00
										 |  |  |         # create orgs | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         # create exchanges | 
					
						
							|  |  |  |         cls.org = [ | 
					
						
							|  |  |  |             Organization.objects.create( | 
					
						
							|  |  |  |                 name="Organization {}".format(i), | 
					
						
							|  |  |  |                 country=countries[i - 1], | 
					
						
							|  |  |  |                 city="City {}".format(i), | 
					
						
							|  |  |  |                 status="ok", | 
					
						
							|  |  |  |             ) | 
					
						
							|  |  |  |             for i in entity_count | 
					
						
							|  |  |  |         ] | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-11-08 19:45:21 +00:00
										 |  |  |         # create networks | 
					
						
							|  |  |  |         cls.net = [ | 
					
						
							|  |  |  |             Network.objects.create( | 
					
						
							| 
									
										
										
										
											2019-12-05 16:57:52 +00:00
										 |  |  |                 name="Network {}".format(i), | 
					
						
							|  |  |  |                 status="ok", | 
					
						
							|  |  |  |                 aka="AKA {}".format(i), | 
					
						
							|  |  |  |                 policy_general="Open", | 
					
						
							| 
									
										
										
										
											2020-05-21 03:53:49 -05:00
										 |  |  |                 info_traffic="0-20Mbps", | 
					
						
							| 
									
										
										
										
											2019-12-05 16:57:52 +00:00
										 |  |  |                 asn=i, | 
					
						
							| 
									
										
										
										
											2020-01-08 13:29:58 -06:00
										 |  |  |                 org=cls.org[i - 1], | 
					
						
							| 
									
										
										
										
											2019-12-05 16:57:52 +00:00
										 |  |  |             ) | 
					
						
							| 
									
										
										
										
											2018-11-08 19:45:21 +00:00
										 |  |  |             for i in entity_count | 
					
						
							|  |  |  |         ] | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         # create exchanges | 
					
						
							|  |  |  |         cls.ix = [ | 
					
						
							|  |  |  |             InternetExchange.objects.create( | 
					
						
							| 
									
										
										
										
											2019-12-05 16:57:52 +00:00
										 |  |  |                 name="Exchange {}".format(i), | 
					
						
							|  |  |  |                 media="Ethernet", | 
					
						
							|  |  |  |                 country=countries[i - 1], | 
					
						
							|  |  |  |                 city="City {}".format(i), | 
					
						
							|  |  |  |                 status="ok", | 
					
						
							| 
									
										
										
										
											2020-01-08 13:29:58 -06:00
										 |  |  |                 org=cls.org[i - 1], | 
					
						
							| 
									
										
										
										
											2019-12-05 16:57:52 +00:00
										 |  |  |             ) | 
					
						
							|  |  |  |             for i in entity_count | 
					
						
							| 
									
										
										
										
											2018-11-08 19:45:21 +00:00
										 |  |  |         ] | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         # create facilities | 
					
						
							|  |  |  |         cls.fac = [ | 
					
						
							|  |  |  |             Facility.objects.create( | 
					
						
							| 
									
										
										
										
											2019-12-05 16:57:52 +00:00
										 |  |  |                 name="Facility {}".format(i), | 
					
						
							|  |  |  |                 status="ok", | 
					
						
							|  |  |  |                 city="City {}".format(i), | 
					
						
							|  |  |  |                 clli="CLLI{}".format(i), | 
					
						
							|  |  |  |                 state="State {}".format(i), | 
					
						
							|  |  |  |                 npanxx="{}-{}".format(i, i), | 
					
						
							|  |  |  |                 country=countries[i - 1], | 
					
						
							|  |  |  |                 zipcode=i, | 
					
						
							| 
									
										
										
										
											2020-01-08 13:29:58 -06:00
										 |  |  |                 org=cls.org[i - 1], | 
					
						
							| 
									
										
										
										
											2019-12-05 16:57:52 +00:00
										 |  |  |             ) | 
					
						
							| 
									
										
										
										
											2018-11-08 19:45:21 +00:00
										 |  |  |             for i in entity_count | 
					
						
							|  |  |  |         ] | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         # create network facility relationships | 
					
						
							|  |  |  |         cls.netfac = [ | 
					
						
							|  |  |  |             NetworkFacility.objects.create( | 
					
						
							| 
									
										
										
										
											2019-12-05 16:57:52 +00:00
										 |  |  |                 network=cls.net[i - 1], facility=cls.fac[i - 1], status="ok" | 
					
						
							|  |  |  |             ) | 
					
						
							| 
									
										
										
										
											2018-11-08 19:45:21 +00:00
										 |  |  |             for i in entity_count | 
					
						
							|  |  |  |         ] | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         # create ixlans | 
					
						
							| 
									
										
										
										
											2020-02-05 21:25:25 -06:00
										 |  |  |         cls.ixlan = [ix.ixlan for ix in cls.ix] | 
					
						
							| 
									
										
										
										
											2018-11-08 19:45:21 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |         # create netixlans | 
					
						
							|  |  |  |         cls.netixlan = [ | 
					
						
							| 
									
										
										
										
											2019-12-05 16:57:52 +00:00
										 |  |  |             NetworkIXLan.objects.create( | 
					
						
							|  |  |  |                 ixlan=cls.ixlan[i - 1], | 
					
						
							|  |  |  |                 network=cls.net[i - 1], | 
					
						
							|  |  |  |                 asn=i, | 
					
						
							|  |  |  |                 speed=0, | 
					
						
							|  |  |  |                 status="ok", | 
					
						
							|  |  |  |             ) | 
					
						
							|  |  |  |             for i in entity_count | 
					
						
							| 
									
										
										
										
											2018-11-08 19:45:21 +00:00
										 |  |  |         ] | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def expected_data(self, tag, fmt): | 
					
						
							|  |  |  |         path = os.path.join( | 
					
						
							| 
									
										
										
										
											2019-12-05 16:57:52 +00:00
										 |  |  |             os.path.dirname(__file__), | 
					
						
							|  |  |  |             "data", | 
					
						
							|  |  |  |             "export", | 
					
						
							|  |  |  |             "advancedsearch", | 
					
						
							|  |  |  |             "{}.{}".format(tag, fmt), | 
					
						
							|  |  |  |         ) | 
					
						
							| 
									
										
										
										
											2018-11-08 19:45:21 +00:00
										 |  |  |         with open(path, "r") as fh: | 
					
						
							|  |  |  |             data = fh.read().rstrip() | 
					
						
							|  |  |  |         return data | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def test_export_net_json(self): | 
					
						
							|  |  |  |         """ test json export of network search """ | 
					
						
							|  |  |  |         client = Client() | 
					
						
							| 
									
										
										
										
											2019-12-05 16:57:52 +00:00
										 |  |  |         response = client.get("/export/advanced-search/net/json?name_search=Network") | 
					
						
							| 
									
										
										
										
											2020-01-08 13:29:58 -06:00
										 |  |  |         assert json.loads(response.content) == json.loads( | 
					
						
							|  |  |  |             self.expected_data("net", "json") | 
					
						
							| 
									
										
										
										
											2019-12-05 16:57:52 +00:00
										 |  |  |         ) | 
					
						
							| 
									
										
										
										
											2018-11-08 19:45:21 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |     def test_export_net_json_pretty(self): | 
					
						
							|  |  |  |         """ test pretty json export of network search """ | 
					
						
							|  |  |  |         client = Client() | 
					
						
							|  |  |  |         response = client.get( | 
					
						
							| 
									
										
										
										
											2019-12-05 16:57:52 +00:00
										 |  |  |             "/export/advanced-search/net/json-pretty?name_search=Network" | 
					
						
							|  |  |  |         ) | 
					
						
							| 
									
										
										
										
											2020-01-08 13:29:58 -06:00
										 |  |  | 
 | 
					
						
							|  |  |  |         assert json.loads(response.content) == json.loads( | 
					
						
							|  |  |  |             self.expected_data("net", "jsonpretty") | 
					
						
							|  |  |  |         ) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         # TODO: better check for pretty json formatting | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         assert len(re.findall(r"\n  ", response.content.decode("utf-8"))) > 0 | 
					
						
							| 
									
										
										
										
											2018-11-08 19:45:21 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |     def test_export_net_csv(self): | 
					
						
							|  |  |  |         """ test csv export of network search """ | 
					
						
							|  |  |  |         client = Client() | 
					
						
							| 
									
										
										
										
											2019-12-05 16:57:52 +00:00
										 |  |  |         response = client.get("/export/advanced-search/net/csv?name_search=Network") | 
					
						
							| 
									
										
										
										
											2020-01-08 13:29:58 -06:00
										 |  |  |         assert response.content.decode("utf-8").replace( | 
					
						
							|  |  |  |             "\r\n", "\n" | 
					
						
							|  |  |  |         ).rstrip() == self.expected_data("net", "csv") | 
					
						
							| 
									
										
										
										
											2018-11-08 19:45:21 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |     def test_export_fac_json(self): | 
					
						
							|  |  |  |         """ test json export of facility search """ | 
					
						
							|  |  |  |         client = Client() | 
					
						
							|  |  |  |         response = client.get( | 
					
						
							| 
									
										
										
										
											2019-12-05 16:57:52 +00:00
										 |  |  |             "/export/advanced-search/fac/json?name__contains=Facility" | 
					
						
							|  |  |  |         ) | 
					
						
							| 
									
										
										
										
											2020-01-08 13:29:58 -06:00
										 |  |  |         assert json.loads(response.content) == json.loads( | 
					
						
							|  |  |  |             self.expected_data("fac", "json") | 
					
						
							| 
									
										
										
										
											2019-12-05 16:57:52 +00:00
										 |  |  |         ) | 
					
						
							| 
									
										
										
										
											2018-11-08 19:45:21 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |     def test_export_fac_json_pretty(self): | 
					
						
							|  |  |  |         """ test pretty json export of facility search """ | 
					
						
							|  |  |  |         client = Client() | 
					
						
							|  |  |  |         response = client.get( | 
					
						
							| 
									
										
										
										
											2019-12-05 16:57:52 +00:00
										 |  |  |             "/export/advanced-search/fac/json-pretty?name__contains=Facility" | 
					
						
							|  |  |  |         ) | 
					
						
							| 
									
										
										
										
											2020-01-08 13:29:58 -06:00
										 |  |  | 
 | 
					
						
							|  |  |  |         assert json.loads(response.content) == json.loads( | 
					
						
							|  |  |  |             self.expected_data("fac", "jsonpretty") | 
					
						
							|  |  |  |         ) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         # TODO: better check for pretty json formatting | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         assert len(re.findall(r"\n  ", response.content.decode("utf-8"))) > 0 | 
					
						
							| 
									
										
										
										
											2018-11-08 19:45:21 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |     def test_export_fac_csv(self): | 
					
						
							|  |  |  |         """ test csv export of facility search """ | 
					
						
							|  |  |  |         client = Client() | 
					
						
							| 
									
										
										
										
											2019-12-05 16:57:52 +00:00
										 |  |  |         response = client.get("/export/advanced-search/fac/csv?name__contains=Facility") | 
					
						
							| 
									
										
										
										
											2020-01-08 13:29:58 -06:00
										 |  |  | 
 | 
					
						
							|  |  |  |         assert response.content.decode("utf-8").replace( | 
					
						
							|  |  |  |             "\r\n", "\n" | 
					
						
							|  |  |  |         ).rstrip() == self.expected_data("fac", "csv") | 
					
						
							| 
									
										
										
										
											2018-11-08 19:45:21 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |     def test_export_ix_json(self): | 
					
						
							|  |  |  |         """ test json export of exchange search """ | 
					
						
							|  |  |  |         client = Client() | 
					
						
							| 
									
										
										
										
											2019-12-05 16:57:52 +00:00
										 |  |  |         response = client.get("/export/advanced-search/ix/json?name__contains=Exchange") | 
					
						
							| 
									
										
										
										
											2018-11-08 19:45:21 +00:00
										 |  |  |         self.assertEqual( | 
					
						
							| 
									
										
										
										
											2019-12-05 16:57:52 +00:00
										 |  |  |             json.loads(response.content), json.loads(self.expected_data("ix", "json")) | 
					
						
							|  |  |  |         ) | 
					
						
							| 
									
										
										
										
											2018-11-08 19:45:21 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |     def test_export_ix_json_pretty(self): | 
					
						
							|  |  |  |         """ test pretty json export of exchange search """ | 
					
						
							|  |  |  |         client = Client() | 
					
						
							|  |  |  |         response = client.get( | 
					
						
							| 
									
										
										
										
											2019-12-05 16:57:52 +00:00
										 |  |  |             "/export/advanced-search/ix/json-pretty?name__contains=Exchange" | 
					
						
							|  |  |  |         ) | 
					
						
							| 
									
										
										
										
											2019-05-02 18:03:21 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-01-08 13:29:58 -06:00
										 |  |  |         assert json.loads(response.content) == json.loads( | 
					
						
							|  |  |  |             self.expected_data("ix", "jsonpretty") | 
					
						
							|  |  |  |         ) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         # TODO: better check for pretty json formatting | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         assert len(re.findall(r"\n  ", response.content.decode("utf-8"))) > 0 | 
					
						
							| 
									
										
										
										
											2018-11-08 19:45:21 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |     def test_export_ix_csv(self): | 
					
						
							|  |  |  |         """ test csv export of exchange search """ | 
					
						
							|  |  |  |         client = Client() | 
					
						
							| 
									
										
										
										
											2019-12-05 16:57:52 +00:00
										 |  |  |         response = client.get("/export/advanced-search/ix/csv?name__contains=Exchange") | 
					
						
							| 
									
										
										
										
											2020-01-08 13:29:58 -06:00
										 |  |  |         assert response.content.decode("utf-8").replace( | 
					
						
							|  |  |  |             "\r\n", "\n" | 
					
						
							|  |  |  |         ).rstrip() == self.expected_data("ix", "csv") | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def test_export_org_json(self): | 
					
						
							|  |  |  |         """ test json export of organization search """ | 
					
						
							|  |  |  |         client = Client() | 
					
						
							|  |  |  |         response = client.get( | 
					
						
							|  |  |  |             "/export/advanced-search/org/json?name_search=Organization" | 
					
						
							|  |  |  |         ) | 
					
						
							| 
									
										
										
										
											2018-11-08 19:45:21 +00:00
										 |  |  |         self.assertEqual( | 
					
						
							| 
									
										
										
										
											2020-01-08 13:29:58 -06:00
										 |  |  |             json.loads(response.content), json.loads(self.expected_data("org", "json")) | 
					
						
							|  |  |  |         ) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def test_export_org_json_pretty(self): | 
					
						
							|  |  |  |         """ test pretty json export of organization search """ | 
					
						
							|  |  |  |         client = Client() | 
					
						
							|  |  |  |         response = client.get( | 
					
						
							|  |  |  |             "/export/advanced-search/org/json-pretty?name_search=Organization" | 
					
						
							|  |  |  |         ) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         assert json.loads(response.content) == json.loads( | 
					
						
							|  |  |  |             self.expected_data("org", "jsonpretty") | 
					
						
							|  |  |  |         ) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         # TODO: better check for pretty json formatting | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         assert len(re.findall(r"\n  ", response.content.decode("utf-8"))) > 0 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def test_export_org_csv(self): | 
					
						
							|  |  |  |         """ test csv export of organization search """ | 
					
						
							|  |  |  |         client = Client() | 
					
						
							|  |  |  |         response = client.get( | 
					
						
							|  |  |  |             "/export/advanced-search/org/csv?name_search=Organization" | 
					
						
							| 
									
										
										
										
											2019-12-05 16:57:52 +00:00
										 |  |  |         ) | 
					
						
							| 
									
										
										
										
											2020-01-08 13:29:58 -06:00
										 |  |  |         assert response.content.decode("utf-8").replace( | 
					
						
							|  |  |  |             "\r\n", "\n" | 
					
						
							|  |  |  |         ).rstrip() == self.expected_data("org", "csv") |