From ac17461258c659b78d6cc742274d883d7fd2e466 Mon Sep 17 00:00:00 2001 From: Jennifer Richards Date: Thu, 24 Oct 2024 21:07:29 -0300 Subject: [PATCH] test: validate PersonalApiKey when used in tests --- ietf/api/tests.py | 44 ++++++++++++++++++++-------------------- ietf/person/factories.py | 10 ++++++++- 2 files changed, 31 insertions(+), 23 deletions(-) diff --git a/ietf/api/tests.py b/ietf/api/tests.py index 97e928dc51..0699d438b0 100644 --- a/ietf/api/tests.py +++ b/ietf/api/tests.py @@ -33,7 +33,7 @@ from ietf.meeting.models import Session from ietf.nomcom.models import Volunteer from ietf.nomcom.factories import NomComFactory, nomcom_kwargs_for_year -from ietf.person.factories import PersonFactory, random_faker, EmailFactory +from ietf.person.factories import PersonFactory, random_faker, EmailFactory, PersonalApiKeyFactory from ietf.person.models import Email, User from ietf.person.models import PersonalApiKey from ietf.stats.models import MeetingRegistration @@ -71,7 +71,7 @@ def test_deprecated_api_set_session_video_url(self): meeting = MeetingFactory(type_id='ietf') session = SessionFactory(group__type_id='wg', meeting=meeting) group = session.group - apikey = PersonalApiKey.objects.create(endpoint=url, person=recman) + apikey = PersonalApiKeyFactory(endpoint=url, person=recman) video = 'https://foo.example.com/bar/beer/' # error cases @@ -79,7 +79,7 @@ def test_deprecated_api_set_session_video_url(self): self.assertContains(r, "Missing apikey parameter", status_code=400) badrole = RoleFactory(group__type_id='ietf', name_id='ad') - badapikey = PersonalApiKey.objects.create(endpoint=url, person=badrole.person) + badapikey = PersonalApiKeyFactory(endpoint=url, person=badrole.person) badrole.person.user.last_login = timezone.now() badrole.person.user.save() r = self.client.post(url, {'apikey': badapikey.hash()} ) @@ -151,7 +151,7 @@ def test_api_set_session_video_url(self): recman = recmanrole.person meeting = MeetingFactory(type_id="ietf") session = SessionFactory(group__type_id="wg", meeting=meeting) - apikey = PersonalApiKey.objects.create(endpoint=url, person=recman) + apikey = PersonalApiKeyFactory(endpoint=url, person=recman) video = "https://foo.example.com/bar/beer/" # error cases @@ -159,7 +159,7 @@ def test_api_set_session_video_url(self): self.assertContains(r, "Missing apikey parameter", status_code=400) badrole = RoleFactory(group__type_id="ietf", name_id="ad") - badapikey = PersonalApiKey.objects.create(endpoint=url, person=badrole.person) + badapikey = PersonalApiKeyFactory(endpoint=url, person=badrole.person) badrole.person.user.last_login = timezone.now() badrole.person.user.save() r = self.client.post(url, {"apikey": badapikey.hash()}) @@ -228,7 +228,7 @@ def test_api_set_meetecho_recording_name(self): recman = recmanrole.person meeting = MeetingFactory(type_id="ietf") session = SessionFactory(group__type_id="wg", meeting=meeting) - apikey = PersonalApiKey.objects.create(endpoint=url, person=recman) + apikey = PersonalApiKeyFactory(endpoint=url, person=recman) name = "testname" # error cases @@ -236,7 +236,7 @@ def test_api_set_meetecho_recording_name(self): self.assertContains(r, "Missing apikey parameter", status_code=400) badrole = RoleFactory(group__type_id="ietf", name_id="ad") - badapikey = PersonalApiKey.objects.create(endpoint=url, person=badrole.person) + badapikey = PersonalApiKeyFactory(endpoint=url, person=badrole.person) badrole.person.user.last_login = timezone.now() badrole.person.user.save() r = self.client.post(url, {"apikey": badapikey.hash()}) @@ -295,10 +295,10 @@ def test_api_add_session_attendees_deprecated(self): recman = recmanrole.person meeting = MeetingFactory(type_id='ietf') session = SessionFactory(group__type_id='wg', meeting=meeting) - apikey = PersonalApiKey.objects.create(endpoint=url, person=recman) + apikey = PersonalApiKeyFactory(endpoint=url, person=recman) badrole = RoleFactory(group__type_id='ietf', name_id='ad') - badapikey = PersonalApiKey.objects.create(endpoint=url, person=badrole.person) + badapikey = PersonalApiKeyFactory(endpoint=url, person=badrole.person) badrole.person.user.last_login = timezone.now() badrole.person.user.save() @@ -361,10 +361,10 @@ def test_api_add_session_attendees(self): recman = recmanrole.person meeting = MeetingFactory(type_id="ietf") session = SessionFactory(group__type_id="wg", meeting=meeting) - apikey = PersonalApiKey.objects.create(endpoint=url, person=recman) + apikey = PersonalApiKeyFactory(endpoint=url, person=recman) badrole = RoleFactory(group__type_id="ietf", name_id="ad") - badapikey = PersonalApiKey.objects.create(endpoint=url, person=badrole.person) + badapikey = PersonalApiKeyFactory(endpoint=url, person=badrole.person) badrole.person.user.last_login = timezone.now() badrole.person.user.save() @@ -517,8 +517,8 @@ def test_api_upload_polls_and_chatlog(self): ), ): url = urlreverse(f"ietf.meeting.views.api_upload_{type_id}") - apikey = PersonalApiKey.objects.create(endpoint=url, person=recmanrole.person) - badapikey = PersonalApiKey.objects.create(endpoint=url, person=badrole.person) + apikey = PersonalApiKeyFactory(endpoint=url, person=recmanrole.person) + badapikey = PersonalApiKeyFactory(endpoint=url, person=badrole.person) r = self.client.post(url, {}) self.assertContains(r, "Missing apikey parameter", status_code=400) @@ -562,7 +562,7 @@ def test_deprecated_api_upload_bluesheet(self): meeting = MeetingFactory(type_id='ietf') session = SessionFactory(group__type_id='wg', meeting=meeting) group = session.group - apikey = PersonalApiKey.objects.create(endpoint=url, person=recman) + apikey = PersonalApiKeyFactory(endpoint=url, person=recman) people = [ {"name": "Andrea Andreotti", "affiliation": "Azienda"}, @@ -579,7 +579,7 @@ def test_deprecated_api_upload_bluesheet(self): self.assertContains(r, "Missing apikey parameter", status_code=400) badrole = RoleFactory(group__type_id='ietf', name_id='ad') - badapikey = PersonalApiKey.objects.create(endpoint=url, person=badrole.person) + badapikey = PersonalApiKeyFactory(endpoint=url, person=badrole.person) badrole.person.user.last_login = timezone.now() badrole.person.user.save() r = self.client.post(url, {'apikey': badapikey.hash()}) @@ -654,7 +654,7 @@ def test_api_upload_bluesheet(self): meeting = MeetingFactory(type_id="ietf") session = SessionFactory(group__type_id="wg", meeting=meeting) group = session.group - apikey = PersonalApiKey.objects.create(endpoint=url, person=recman) + apikey = PersonalApiKeyFactory(endpoint=url, person=recman) people = [ {"name": "Andrea Andreotti", "affiliation": "Azienda"}, @@ -671,7 +671,7 @@ def test_api_upload_bluesheet(self): self.assertContains(r, "Missing apikey parameter", status_code=400) badrole = RoleFactory(group__type_id="ietf", name_id="ad") - badapikey = PersonalApiKey.objects.create(endpoint=url, person=badrole.person) + badapikey = PersonalApiKeyFactory(endpoint=url, person=badrole.person) badrole.person.user.last_login = timezone.now() badrole.person.user.save() r = self.client.post(url, {"apikey": badapikey.hash()}) @@ -781,14 +781,14 @@ def test_api_v2_person_export_view(self): url = urlreverse('ietf.api.views.ApiV2PersonExportView') robot = PersonFactory(user__is_staff=True) RoleFactory(name_id='robot', person=robot, email=robot.email(), group__acronym='secretariat') - apikey = PersonalApiKey.objects.create(endpoint=url, person=robot) + apikey = PersonalApiKeyFactory(endpoint=url, person=robot) # error cases r = self.client.post(url, {}) self.assertContains(r, "Missing apikey parameter", status_code=400) badrole = RoleFactory(group__type_id='ietf', name_id='ad') - badapikey = PersonalApiKey.objects.create(endpoint=url, person=badrole.person) + badapikey = PersonalApiKeyFactory(endpoint=url, person=badrole.person) badrole.person.user.last_login = timezone.now() badrole.person.user.save() r = self.client.post(url, {'apikey': badapikey.hash()}) @@ -827,7 +827,7 @@ def test_api_new_meeting_registration(self): oidcp = PersonFactory(user__is_staff=True) # Make sure 'oidcp' has an acceptable role RoleFactory(name_id='robot', person=oidcp, email=oidcp.email(), group__acronym='secretariat') - key = PersonalApiKey.objects.create(person=oidcp, endpoint=url) + key = PersonalApiKeyFactory(person=oidcp, endpoint=url) reg['apikey'] = key.hash() # # Test valid POST @@ -911,7 +911,7 @@ def test_api_new_meeting_registration_nomcom_volunteer(self): oidcp = PersonFactory(user__is_staff=True) # Make sure 'oidcp' has an acceptable role RoleFactory(name_id='robot', person=oidcp, email=oidcp.email(), group__acronym='secretariat') - key = PersonalApiKey.objects.create(person=oidcp, endpoint=url) + key = PersonalApiKeyFactory(person=oidcp, endpoint=url) reg['apikey'] = key.hash() # first test is_nomcom_volunteer False @@ -947,7 +947,7 @@ def test_api_version(self): def test_api_appauth(self): url = urlreverse('ietf.api.views.app_auth') person = PersonFactory() - apikey = PersonalApiKey.objects.create(endpoint=url, person=person) + apikey = PersonalApiKeyFactory(endpoint=url, person=person) self.client.login(username=person.user.username,password=f'{person.user.username}+password') self.client.logout() diff --git a/ietf/person/factories.py b/ietf/person/factories.py index 2012483c0d..e91aafeff9 100644 --- a/ietf/person/factories.py +++ b/ietf/person/factories.py @@ -159,9 +159,17 @@ class Meta: class PersonalApiKeyFactory(factory.django.DjangoModelFactory): person = factory.SubFactory(PersonFactory) endpoint = FuzzyChoice(PERSON_API_KEY_ENDPOINTS) - + class Meta: model = PersonalApiKey + skip_postgeneration_save = True + + @factory.post_generation + def do_full_clean(obj, create, extracted, **kwargs): + do_clean = True if extracted is None else extracted + if do_clean: + obj.full_clean() + class PersonApiKeyEventFactory(factory.django.DjangoModelFactory): key = factory.SubFactory(PersonalApiKeyFactory)