Skip to content

Commit

Permalink
cleanup tests (#415)
Browse files Browse the repository at this point in the history
  • Loading branch information
mikkonie committed Apr 24, 2024
1 parent 46030b9 commit b91586b
Showing 1 changed file with 51 additions and 118 deletions.
169 changes: 51 additions & 118 deletions adminalerts/tests/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,18 @@ def test_get(self):
class TestAdminAlertCreateView(AdminalertsViewTestBase):
"""Tests for AdminAlertCreateView"""

def _get_post_data(self, **kwargs):
ret = {
'message': ALERT_MSG,
'description': ALERT_DESC,
'date_expire': self.expiry_str,
'active': True,
'require_auth': True,
'send_email': True,
}
ret.update(**kwargs)
return ret

def setUp(self):
super().setUp()
self.url = reverse('adminalerts:create')
Expand All @@ -106,16 +118,9 @@ def test_post(self):
"""Test POST"""
self.assertEqual(AdminAlert.objects.all().count(), 0)
self.assertEqual(len(mail.outbox), 0)
post_data = {
'message': ALERT_MSG,
'description': ALERT_DESC,
'date_expire': self.expiry_str,
'active': True,
'require_auth': True,
'send_email': True,
}
data = self._get_post_data()
with self.login(self.superuser):
response = self.client.post(self.url, post_data)
response = self.client.post(self.url, data)
self.assertEqual(response.status_code, 302)
self.assertEqual(response.url, reverse('adminalerts:list'))
self.assertEqual(AdminAlert.objects.all().count(), 1)
Expand All @@ -137,16 +142,9 @@ def test_post(self):
def test_post_no_description(self):
"""Test POST with no description"""
self.assertEqual(len(mail.outbox), 0)
post_data = {
'message': ALERT_MSG,
'description': '',
'date_expire': self.expiry_str,
'active': True,
'require_auth': True,
'send_email': True,
}
data = self._get_post_data(description='')
with self.login(self.superuser):
response = self.client.post(self.url, post_data)
response = self.client.post(self.url, data)
self.assertEqual(response.status_code, 302)
self.assertEqual(len(mail.outbox), 1)
self.assertIn(ALERT_MSG, mail.outbox[0].body)
Expand All @@ -155,16 +153,9 @@ def test_post_no_description(self):
def test_post_markdown_description(self):
"""Test POST with markdown description"""
self.assertEqual(len(mail.outbox), 0)
post_data = {
'message': ALERT_MSG,
'description': ALERT_DESC_MARKDOWN,
'date_expire': self.expiry_str,
'active': True,
'require_auth': True,
'send_email': True,
}
data = self._get_post_data(description=ALERT_DESC_MARKDOWN)
with self.login(self.superuser):
response = self.client.post(self.url, post_data)
response = self.client.post(self.url, data)
self.assertEqual(response.status_code, 302)
self.assertEqual(len(mail.outbox), 1)
self.assertIn(ALERT_MSG, mail.outbox[0].body)
Expand All @@ -175,49 +166,28 @@ def test_post_markdown_description(self):
def test_post_no_email(self):
"""Test POST with no email to be sent"""
self.assertEqual(len(mail.outbox), 0)
post_data = {
'message': ALERT_MSG,
'description': ALERT_DESC,
'date_expire': self.expiry_str,
'active': True,
'require_auth': True,
'send_email': False,
}
data = self._get_post_data(send_email=False)
with self.login(self.superuser):
response = self.client.post(self.url, post_data)
response = self.client.post(self.url, data)
self.assertEqual(response.status_code, 302)
self.assertEqual(len(mail.outbox), 0)

def test_post_inactive(self):
"""Test POST with inactive state"""
self.assertEqual(len(mail.outbox), 0)
post_data = {
'message': ALERT_MSG,
'description': ALERT_DESC,
'date_expire': self.expiry_str,
'active': False,
'require_auth': True,
'send_email': True,
}
data = self._get_post_data(active=False)
with self.login(self.superuser):
response = self.client.post(self.url, post_data)
response = self.client.post(self.url, data)
self.assertEqual(response.status_code, 302)
self.assertEqual(len(mail.outbox), 0)

def test_post_multiple_users(self):
"""Test POST with multiple users"""
user_new = self.make_user('user_new')
self.assertEqual(len(mail.outbox), 0)
post_data = {
'message': ALERT_MSG,
'description': ALERT_DESC,
'date_expire': self.expiry_str,
'active': True,
'require_auth': True,
'send_email': True,
}
data = self._get_post_data()
with self.login(self.superuser):
response = self.client.post(self.url, post_data)
response = self.client.post(self.url, data)
self.assertEqual(response.status_code, 302)
self.assertEqual(len(mail.outbox), 1)
self.assertEqual(
Expand All @@ -239,16 +209,9 @@ def test_post_alt_email_regular_user(self):
user=self.user_regular,
)
self.assertEqual(len(mail.outbox), 0)
post_data = {
'message': ALERT_MSG,
'description': ALERT_DESC,
'date_expire': self.expiry_str,
'active': True,
'require_auth': True,
'send_email': True,
}
data = self._get_post_data()
with self.login(self.superuser):
response = self.client.post(self.url, post_data)
response = self.client.post(self.url, data)
self.assertEqual(response.status_code, 302)
self.assertEqual(len(mail.outbox), 1)
self.assertEqual(
Expand All @@ -272,16 +235,9 @@ def test_post_alt_email_superuser(self):
user=self.superuser,
)
self.assertEqual(len(mail.outbox), 0)
post_data = {
'message': ALERT_MSG,
'description': ALERT_DESC,
'date_expire': self.expiry_str,
'active': True,
'require_auth': True,
'send_email': True,
}
data = self._get_post_data()
with self.login(self.superuser):
response = self.client.post(self.url, post_data)
response = self.client.post(self.url, data)
self.assertEqual(response.status_code, 302)
self.assertEqual(len(mail.outbox), 1)
# Superuser alt emails should not be included
Expand All @@ -295,26 +251,31 @@ def test_post_alt_email_superuser(self):
def test_post_expired(self):
"""Test POST with old expiry date (should fail)"""
self.assertEqual(AdminAlert.objects.all().count(), 0)
expiry_fail = (timezone.now() + timezone.timedelta(days=-1)).strftime(
expire_fail = (timezone.now() + timezone.timedelta(days=-1)).strftime(
'%Y-%m-%d'
)
post_data = {
'message': ALERT_MSG,
'description': ALERT_DESC,
'date_expire': expiry_fail,
'active': True,
'require_auth': True,
'send_email': True,
}
data = self._get_post_data(date_expire=expire_fail)
with self.login(self.superuser):
response = self.client.post(self.url, post_data)
response = self.client.post(self.url, data)
self.assertEqual(response.status_code, 200)
self.assertEqual(AdminAlert.objects.all().count(), 0)


class TestAdminAlertUpdateView(AdminalertsViewTestBase):
"""Tests for AdminAlertUpdateView"""

def _get_post_data(self, **kwargs):
ret = {
'message': ALERT_MSG_UPDATED,
'description': ALERT_DESC_UPDATED,
'date_expire': self.expiry_str,
'active': False,
'require_auth': True,
'send_email': False,
}
ret.update(kwargs)
return ret

def setUp(self):
super().setUp()
self.alert = self._make_alert()
Expand All @@ -333,16 +294,9 @@ def test_post(self):
"""Test POST"""
self.assertEqual(AdminAlert.objects.all().count(), 1)
self.assertEqual(len(mail.outbox), 0)
post_data = {
'message': ALERT_MSG_UPDATED,
'description': ALERT_DESC_UPDATED,
'date_expire': self.expiry_str,
'active': False,
'require_auth': True,
'send_email': False,
}
data = self._get_post_data()
with self.login(self.superuser):
response = self.client.post(self.url, post_data)
response = self.client.post(self.url, data)
self.assertEqual(response.status_code, 302)
self.assertEqual(response.url, reverse('adminalerts:list'))
self.assertEqual(AdminAlert.objects.all().count(), 1)
Expand All @@ -355,16 +309,9 @@ def test_post(self):
def test_post_email(self):
"""Test POST with email update enabled"""
self.assertEqual(len(mail.outbox), 0)
post_data = {
'message': ALERT_MSG_UPDATED,
'description': ALERT_DESC_UPDATED,
'date_expire': self.expiry_str,
'active': True,
'require_auth': True,
'send_email': True,
}
data = self._get_post_data(active=True, send_email=True)
with self.login(self.superuser):
response = self.client.post(self.url, post_data)
response = self.client.post(self.url, data)
self.assertEqual(response.status_code, 302)
self.assertEqual(len(mail.outbox), 1)
self.assertIn(
Expand All @@ -379,16 +326,9 @@ def test_post_email(self):
def test_post_email_inactive(self):
"""Test POST with email update enabled and inactive alert"""
self.assertEqual(len(mail.outbox), 0)
post_data = {
'message': ALERT_MSG_UPDATED,
'description': ALERT_DESC_UPDATED,
'date_expire': self.expiry_str,
'active': False,
'require_auth': True,
'send_email': True,
}
data = self._get_post_data(send_email=True)
with self.login(self.superuser):
response = self.client.post(self.url, post_data)
response = self.client.post(self.url, data)
self.assertEqual(response.status_code, 302)
self.assertEqual(len(mail.outbox), 0) # No email for inactive event

Expand All @@ -399,16 +339,9 @@ def test_post_user(self):
superuser2 = self.make_user('superuser2')
superuser2.is_superuser = True
superuser2.save()
post_data = {
'message': ALERT_MSG_UPDATED,
'description': ALERT_DESC_UPDATED,
'date_expire': self.expiry_str,
'active': False,
'require_auth': True,
'send_email': False,
}
data = self._get_post_data()
with self.login(superuser2):
response = self.client.post(self.url, post_data)
response = self.client.post(self.url, data)
self.assertEqual(response.status_code, 302)
self.assertEqual(response.url, reverse('adminalerts:list'))
self.alert.refresh_from_db()
Expand Down

0 comments on commit b91586b

Please sign in to comment.