Skip to content
Snippets Groups Projects
Commit 9a5eff34 authored by Tanner Prestegard's avatar Tanner Prestegard Committed by GraceDB
Browse files

Add some unit tests for alert signup views

parent 443ccd73
No related branches found
No related tags found
No related merge requests found
......@@ -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')
......
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'
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment