1
0
mirror of https://github.com/peeringdb/peeringdb.git synced 2024-05-11 05:55:09 +00:00
Files
peeringdb-peeringdb/tests/test_search.py

188 lines
6.1 KiB
Python
Raw Normal View History

2018-11-08 19:45:21 +00:00
"""
Unit-tests for quick search functionality - note that advanced search is not
tested here as that is using the PDB API entirely.
"""
import re
import datetime
2018-11-08 19:45:21 +00:00
import pytest
import unidecode
2018-11-08 19:45:21 +00:00
from django.test import TestCase, RequestFactory
2018-11-08 19:45:21 +00:00
import peeringdb_server.search as search
import peeringdb_server.models as models
import peeringdb_server.views as views
2018-11-08 19:45:21 +00:00
class SearchTests(TestCase):
"""
Test quick-search functionality
"""
@classmethod
def create_instance(cls, model, org, asn=1, prefix="Test", accented=False):
kwargs = {}
if model.handleref.tag == "net":
kwargs = {"asn": asn}
July updates (#762) * Change label from primary ASN to ASN * Raise validation error when trying to update ASN * first steps for dotf importer procotol (#697) * migrations (#697) * Add translation to error meessage * Make ASN readonly in table * Add test now that ASN should not be able to update * Set fac.rencode to '' for all entries and make it readonly in serializer * Add unique constraints to network ixlan ip addresses * Add migration to null out duplicate ipaddresses for deleted netixlans * Add unique constraints to network ixlan ip addresses * Add migration to null out duplicate ipaddresses for deleted netixlans * remove old migrations (#697) * fix netixlan ipaddr dedupe migration (#268) add netixlan ipaddr unique constraint migration (#268) * ixf_member_data migrations (#697) * fix table name (#697) * importer protocol (#697) * fix netixlan ipaddr dedupe migration (#268) add netixlan ipaddr unique constraint migration (#268) * ixf proposed changes notifications (#697) * Delete repeated query * Add a test to show rencode is readonly * Blank out rencode when mocking data * Remove validator now that constraint exists * Add back unique field validator w Check Deleted true * conflict resolving (#697) * UniqueFieldValidator raise error with code "unique" (#268) * conflict resolution (#697) * Add fixme comment to tests * conflict resolution (#697) * Remove now invalid undelete tests * UniqueFieldValidator raise error with code "unique" (#268) * delete admin tools for duplicate ip addresses * Make migration to delete duplicateipnetworkixlan * Add ixlan-ixpfx status matching validation, add corresponding test * delete redundant checking in test * resolve conflict ui (#697) * fix migrations hierarchy * squash migrations for ixf member data * clean up preview and post-mortem tools * remove non-sensical permission check when undeleting soft-deleted objects through unique integrity error handling * only include the ix-f data url in notifications to admincom (#697) * resolve on --skip-import (#697) * ac conflict resolution (#697) * Define more accurately the incompatible statuses for ixlan and ixpfx * Add another status test * Preventing disrupting changes (#697) * fix tests (#697) * Stop allow_ixp_update from being write only and add a global stat for automated networks * Add tests for global stats that appear in footer * Change how timezone is called with datetime, to get test_stats.py/test_generate_for_current_date to pass * test for protected entities (#697) * admincom conflict resolution refine readonly fields (#697) network notifications only if the problem is actually actionable by the network (#697) * ixp / ac notifcation when ix-f source cannot be parsed (#697) fix issue with ixlan prefix protection (#697) * migrations (#697) * code documentation (#697) * ux tweaks (#697) * UX tweaks (#697) * Fix typo * fix netixlan returned in IXFMemberData.apply when adding a new one (#697) * fix import log incosistencies (#697) * Add IXFMemberData to test * Update test data * Add protocol tests * Add tests for views * always persist changes to remote data on set_conflict (#697) * More tests * always persist changes to remote data on set_conflict (#697) * suggest-add test * net_present_at_ix should check status (#697) * Add more protocol tests * Edit language of some tests * django-peeringdb to 2.1.1 relock pipfile, pin django-ratelimit to <3 as it breaks stuff * Add net_count_ixf field to ix object (#683) * Add the IX-F Member Export URL to the ixlan API endpoint (#249) * Lock some objects from being deleted by the owner (#696) * regenerate api docs (#249) * always persist changes to remote data on set_add and set_update (#697) * IXFMemberData: always persist remote data changes during set_add and set_update, also allow for saving without touching the updated field * always persist changes to remote data on set_add and set_update (#697) * Fix suggest-add tests * IXFMemberData: always persist remote data changes during set_add and set_update, also allow for saving without touching the updated field * IXFMemberData: always persist remote data changes during set_add and set_update, also allow for saving without touching the updated field * fix issue with deletion when ixfmemberdata for entry existed previously (#697) * fix test_suggest_delete_local_ixf_no_flag (#697 tests) * fix issue with deletion when ixfmemberdata for entry existed previously (#697) * invalid ips get logged and notified to the ix via notify_error (#697) * Fix more tests * issue with previous_data when running without save (#697) properly track speed errors (#697) * reset errors on ixfmemberdata that go into pending_save (#697) * add remote_data to admin view (#697) * fix error reset inconsistency (#697) * Refine invalid data tests * remove debug output * for notifications to ac include contact points for net and ix in the message (#697) * settings to toggle ix-f tickets / emails (#697) * allow turning off ix-f notifications for net and ix separately (#697) * add jsonschema test * Add idempotent tests to updater * remove old ixf member tests * Invalid data tests when ixp_updates are enabled * fix speed error validation (#697) * fix issue with rollback (#697) * fix migration hierarchy * fix ixfmemberdata _email * django-peeringdb to 2.2 and relock * add ixf rollback tests * ixf email notifications off by default * black formatted * pyupgrade Co-authored-by: egfrank <egfrank@20c.com> Co-authored-by: Stefan Pratter <stefan@20c.com>
2020-07-15 02:07:01 -05:00
kwargs.update(status="ok", name=f"{prefix} {model.handleref.tag}")
if accented:
July updates (#762) * Change label from primary ASN to ASN * Raise validation error when trying to update ASN * first steps for dotf importer procotol (#697) * migrations (#697) * Add translation to error meessage * Make ASN readonly in table * Add test now that ASN should not be able to update * Set fac.rencode to '' for all entries and make it readonly in serializer * Add unique constraints to network ixlan ip addresses * Add migration to null out duplicate ipaddresses for deleted netixlans * Add unique constraints to network ixlan ip addresses * Add migration to null out duplicate ipaddresses for deleted netixlans * remove old migrations (#697) * fix netixlan ipaddr dedupe migration (#268) add netixlan ipaddr unique constraint migration (#268) * ixf_member_data migrations (#697) * fix table name (#697) * importer protocol (#697) * fix netixlan ipaddr dedupe migration (#268) add netixlan ipaddr unique constraint migration (#268) * ixf proposed changes notifications (#697) * Delete repeated query * Add a test to show rencode is readonly * Blank out rencode when mocking data * Remove validator now that constraint exists * Add back unique field validator w Check Deleted true * conflict resolving (#697) * UniqueFieldValidator raise error with code "unique" (#268) * conflict resolution (#697) * Add fixme comment to tests * conflict resolution (#697) * Remove now invalid undelete tests * UniqueFieldValidator raise error with code "unique" (#268) * delete admin tools for duplicate ip addresses * Make migration to delete duplicateipnetworkixlan * Add ixlan-ixpfx status matching validation, add corresponding test * delete redundant checking in test * resolve conflict ui (#697) * fix migrations hierarchy * squash migrations for ixf member data * clean up preview and post-mortem tools * remove non-sensical permission check when undeleting soft-deleted objects through unique integrity error handling * only include the ix-f data url in notifications to admincom (#697) * resolve on --skip-import (#697) * ac conflict resolution (#697) * Define more accurately the incompatible statuses for ixlan and ixpfx * Add another status test * Preventing disrupting changes (#697) * fix tests (#697) * Stop allow_ixp_update from being write only and add a global stat for automated networks * Add tests for global stats that appear in footer * Change how timezone is called with datetime, to get test_stats.py/test_generate_for_current_date to pass * test for protected entities (#697) * admincom conflict resolution refine readonly fields (#697) network notifications only if the problem is actually actionable by the network (#697) * ixp / ac notifcation when ix-f source cannot be parsed (#697) fix issue with ixlan prefix protection (#697) * migrations (#697) * code documentation (#697) * ux tweaks (#697) * UX tweaks (#697) * Fix typo * fix netixlan returned in IXFMemberData.apply when adding a new one (#697) * fix import log incosistencies (#697) * Add IXFMemberData to test * Update test data * Add protocol tests * Add tests for views * always persist changes to remote data on set_conflict (#697) * More tests * always persist changes to remote data on set_conflict (#697) * suggest-add test * net_present_at_ix should check status (#697) * Add more protocol tests * Edit language of some tests * django-peeringdb to 2.1.1 relock pipfile, pin django-ratelimit to <3 as it breaks stuff * Add net_count_ixf field to ix object (#683) * Add the IX-F Member Export URL to the ixlan API endpoint (#249) * Lock some objects from being deleted by the owner (#696) * regenerate api docs (#249) * always persist changes to remote data on set_add and set_update (#697) * IXFMemberData: always persist remote data changes during set_add and set_update, also allow for saving without touching the updated field * always persist changes to remote data on set_add and set_update (#697) * Fix suggest-add tests * IXFMemberData: always persist remote data changes during set_add and set_update, also allow for saving without touching the updated field * IXFMemberData: always persist remote data changes during set_add and set_update, also allow for saving without touching the updated field * fix issue with deletion when ixfmemberdata for entry existed previously (#697) * fix test_suggest_delete_local_ixf_no_flag (#697 tests) * fix issue with deletion when ixfmemberdata for entry existed previously (#697) * invalid ips get logged and notified to the ix via notify_error (#697) * Fix more tests * issue with previous_data when running without save (#697) properly track speed errors (#697) * reset errors on ixfmemberdata that go into pending_save (#697) * add remote_data to admin view (#697) * fix error reset inconsistency (#697) * Refine invalid data tests * remove debug output * for notifications to ac include contact points for net and ix in the message (#697) * settings to toggle ix-f tickets / emails (#697) * allow turning off ix-f notifications for net and ix separately (#697) * add jsonschema test * Add idempotent tests to updater * remove old ixf member tests * Invalid data tests when ixp_updates are enabled * fix speed error validation (#697) * fix issue with rollback (#697) * fix migration hierarchy * fix ixfmemberdata _email * django-peeringdb to 2.2 and relock * add ixf rollback tests * ixf email notifications off by default * black formatted * pyupgrade Co-authored-by: egfrank <egfrank@20c.com> Co-authored-by: Stefan Pratter <stefan@20c.com>
2020-07-15 02:07:01 -05:00
kwargs.update(name=f"ãccented {model.handleref.tag}")
if model.handleref.tag != "org":
kwargs.update(org=org)
instance = model.objects.create(**kwargs)
if model.handleref.tag == "org":
instance.org_id = instance.id
return instance
2018-11-08 19:45:21 +00:00
@classmethod
def setUpTestData(cls):
# in case other tests updated the search index through object
# creation we need to flush it
search.SEARCH_CACHE["search_index"] = {}
cls.instances = {}
cls.instances_accented = {}
cls.instances_sponsored = {}
2018-11-08 19:45:21 +00:00
# create an instance of each searchable model, so we have something
# to search for
cls.org = models.Organization.objects.create(name="Parent org")
2018-11-08 19:45:21 +00:00
for model in search.searchable_models:
cls.instances[model.handleref.tag] = cls.create_instance(model, cls.org)
cls.instances_accented[model.handleref.tag] = cls.create_instance(
model, cls.org, asn=2, accented=True
2019-12-05 16:57:52 +00:00
)
# we also need to test that sponsor ship status comes through
# accordingly
2019-12-05 16:57:52 +00:00
cls.org_w_sponsorship = models.Organization.objects.create(
name="Sponsor Parent org", status="ok"
2019-12-05 16:57:52 +00:00
)
now = datetime.datetime.now().replace(tzinfo=models.UTC())
cls.sponsorship = models.Sponsorship.objects.create(
start_date=now - datetime.timedelta(days=1),
end_date=now + datetime.timedelta(days=1),
2019-12-05 16:57:52 +00:00
level=1,
)
models.SponsorshipOrganization.objects.create(
org=cls.org_w_sponsorship, sponsorship=cls.sponsorship
)
for model in search.searchable_models:
cls.instances_sponsored[model.handleref.tag] = cls.create_instance(
model, cls.org_w_sponsorship, asn=3, prefix="Sponsor"
2019-12-05 16:57:52 +00:00
)
2018-11-08 19:45:21 +00:00
def test_search(self):
"""
search for entities containing 'Test' - this should return all
instances we created during setUp
"""
rv = search.search("Test")
for k, inst in list(self.instances.items()):
2018-11-08 19:45:21 +00:00
assert k in rv
assert len(rv[k]) == 1
assert rv[k][0]["name"] == inst.search_result_name
assert rv[k][0]["org_id"] == inst.org_id
2018-11-08 19:45:21 +00:00
rv = search.search("as1")
assert len(rv["net"]) == 1
assert rv["net"][0]["name"] == self.instances["net"].search_result_name
assert rv["net"][0]["org_id"] == self.instances["net"].org_id
2018-11-08 19:45:21 +00:00
rv = search.search("asn1")
assert len(rv["net"]) == 1
assert rv["net"][0]["name"] == self.instances["net"].search_result_name
assert rv["net"][0]["org_id"] == self.instances["net"].org_id
def test_sponsor_badges(self):
"""
Test that the sponsor badges show up in search result
"""
factory = RequestFactory()
2019-12-05 16:57:52 +00:00
request = factory.get("/search", {"q": "Sponsor"})
response = views.request_search(request)
2019-12-05 16:57:52 +00:00
m = re.findall(
re.escape('<a href="/sponsors" class="sponsor silver">'),
response.content.decode(),
2019-12-05 16:57:52 +00:00
)
assert len(m) == 4
2018-11-08 19:45:21 +00:00
def test_search_case(self):
"""
search for entities containing 'test' - this should return all
instances we created during setUp since matching is case-insensitive
"""
rv = search.search("test")
for k, inst in list(self.instances.items()):
2018-11-08 19:45:21 +00:00
assert k in rv
assert len(rv[k]) == 1
assert rv[k][0]["name"] == inst.search_result_name
def test_index_updates(self):
"""
test that indexes get updated correctly when objects are created
or deleted or updated from pending to ok
"""
# this object will be status pending and should not be returned in the search
# results
new_ix_p = models.InternetExchange.objects.create(
2019-12-05 16:57:52 +00:00
status="pending", org=self.org, name="Test IU ix"
)
2018-11-08 19:45:21 +00:00
self.test_search()
# this object will be status ok, and should show up in the index
new_ix_o = models.InternetExchange.objects.create(
2019-12-05 16:57:52 +00:00
status="ok", org=self.org, name="Test IU P ix"
)
2018-11-08 19:45:21 +00:00
rv = search.search("test")
assert len(rv["ix"]) == 2
# now we switch the first object to ok as well and it as well should show up in the
# index
new_ix_p.status = "ok"
new_ix_p.save()
rv = search.search("test")
assert len(rv["ix"]) == 3
2019-12-05 16:57:52 +00:00
# finally we delete both and they should disappear again
2018-11-08 19:45:21 +00:00
new_ix_p.delete()
new_ix_o.delete()
self.test_search()
def test_search_unaccent(self):
"""
search for entities containing 'ãccented' using accented and unaccented
terms
"""
rv = search.search("accented")
for k, inst in list(self.instances_accented.items()):
assert k in rv
assert len(rv[k]) == 1
2019-12-05 16:57:52 +00:00
assert unidecode.unidecode(rv[k][0]["name"]) == unidecode.unidecode(
inst.search_result_name
)
rv = search.search("ãccented")
for k, inst in list(self.instances_accented.items()):
assert k in rv
assert len(rv[k]) == 1
2019-12-05 16:57:52 +00:00
assert unidecode.unidecode(rv[k][0]["name"]) == unidecode.unidecode(
inst.search_result_name
)