diff --git a/gracedb/alerts/tests/test_access.py b/gracedb/alerts/tests/test_access.py index a00ea4b0bcdc78f1a54518deb98b115b26ed836b..cbd068b1f788227ec91cb188314980cf4e93e019 100644 --- a/gracedb/alerts/tests/test_access.py +++ b/gracedb/alerts/tests/test_access.py @@ -142,12 +142,28 @@ class TestContactDeleteView(GraceDbTestBase): class TestNotificationCreateView(GraceDbTestBase): """Test user access to notification creation view""" - def test_internal_user_get(self): - """Internal user can get notification creation view""" + def test_internal_user_get_with_verified_contact(self): + """ + Internal user can get notification creation view if they have a + verified contact + """ url = reverse('alerts:create-notification') + self.internal_user.contact_set.create(phone='12345678901', + phone_method=Contact.CONTACT_PHONE_TEXT, verified=True, + description='test') response = self.request_as_user(url, "GET", self.internal_user) self.assertEqual(response.status_code, 200) + def test_internal_user_get_with_no_verified_contact(self): + """ + Internal user can't get notification creation view if they don't have + a verified contact + """ + url = reverse('alerts:create-notification') + response = self.request_as_user(url, "GET", self.internal_user) + self.assertEqual(response.status_code, 302) + self.assertEqual(response.url, reverse('alerts:index')) + def test_lvem_user_get(self): """LV-EM user can't get notification creation view""" url = reverse('alerts:create-notification') diff --git a/gracedb/alerts/tests/test_views.py b/gracedb/alerts/tests/test_views.py index 966b3ed6e3b1b7962248d315c61761f1bc24d434..ae284c5e00a474ab321e25b0021d780ccef07a6b 100644 --- a/gracedb/alerts/tests/test_views.py +++ b/gracedb/alerts/tests/test_views.py @@ -1,4 +1,5 @@ import mock +import pytest from django.conf import settings from django.contrib.auth.models import Group as AuthGroup @@ -77,3 +78,69 @@ class TestUpdateContactView(GraceDbTestBase): self.assertNotEqual(self.phone_contact.phone, data['phone']) self.assertEqual(self.phone_contact.phone, original_phone) self.assertEqual(self.phone_contact.phone_method, data['phone_method']) + + +@pytest.mark.parametrize("notif_exists", [True, False]) +@pytest.mark.django_db +def test_delete_contact(notif_exists, internal_user, client): + client.force_login(internal_user) + + # Manually create verified contact for user + contact = internal_user.contact_set.create(phone='12345678901', + verified=True, phone_method=Contact.CONTACT_PHONE_TEXT, + description='test') + # Optionally create a notification + if notif_exists: + notif = internal_user.notification_set.create(description='test', + category=Notification.NOTIFICATION_CATEGORY_SUPEREVENT) + notif.contacts.add(contact) + + # Try to delete contact + response = client.get(reverse('alerts:delete-contact', args=[contact.pk])) + + # Assert results + if notif_exists: + # Redirect to main alerts page + assert response.status_code == 302 + assert response.url == reverse('alerts:index') + # Message displayed by message framework + assert "cannot be deleted" in response.cookies['messages'].value + # Contact still exists + assert Contact.objects.filter(pk=contact.pk).exists() + else: + # Redirect to main alerts page + assert response.status_code == 302 + assert response.url == reverse('alerts:index') + # Contact does not exist + assert not Contact.objects.filter(pk=contact.pk).exists() + + +@pytest.mark.django_db +def test_create_notification_no_contact(internal_user, client): + client.force_login(internal_user) + + # Manually create verified contact for user + contact = internal_user.contact_set.create(phone='12345678901', + verified=True, phone_method=Contact.CONTACT_PHONE_TEXT, + description='test') + + # Try to create a notification with no contact + url = reverse('alerts:create-notification') + data = { + 'description': 'test', + 'key_field': 'superevent', + } + response = client.post(url, data=data) + + # This should call the forms_invalid() method of the CBV, + # resulting in a TemplateResponse to render the form with + # errors + # Get form + form = [form for form in response.context['forms'] + if form.key == 'superevent'][0] + + # Check form status and errors + assert form.is_bound == True + assert form.is_valid() == False + assert 'contacts' in form.errors + assert form.errors.as_data()['contacts'][0].code == 'required'