-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
252 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
from announcements.models import Announcement, Audience | ||
from django.test import TestCase | ||
from django.utils import timezone | ||
|
||
|
||
class AudienceTestCase(TestCase): | ||
def setUp(self): | ||
self.audience_name = "CLUBS" | ||
self.audience = Audience.objects.create(name=Audience.AUDIENCE_CLUBS) | ||
|
||
def test_str(self): | ||
self.assertEqual(str(self.audience), self.audience_name) | ||
|
||
|
||
class AnnouncementTestCase(TestCase): | ||
def setUp(self): | ||
self.audience_name = "CLUBS" | ||
self.title = "Test Announcement" | ||
self.message = "This is a test" | ||
self.announcement_type = "Issue" | ||
self.release_time = timezone.datetime( | ||
year=3000, month=12, day=31, tzinfo=timezone.get_current_timezone() | ||
) | ||
self.end_time = timezone.datetime( | ||
year=3001, month=1, day=1, tzinfo=timezone.get_current_timezone() | ||
) | ||
self.audience = Audience.objects.create(name=Audience.AUDIENCE_CLUBS) | ||
self.announcement = Announcement.objects.create( | ||
title=self.title, | ||
message=self.message, | ||
announcement_type=Announcement.ANNOUNCEMENT_ISSUE, | ||
release_time=self.release_time, | ||
end_time=self.end_time, | ||
) | ||
self.announcement.audiences.add(self.audience) | ||
|
||
def test_str(self): | ||
self.assertEqual( | ||
str(self.announcement), | ||
f"[{self.announcement_type} for {self.audience_name}] \ | ||
starting at {self.release_time.strftime('%m-%d-%Y %H:%M:%S')} to \ | ||
{self.end_time.strftime('%m-%d-%Y %H:%M:%S')} | {self.title}: {self.message}", | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
from announcements.models import Audience | ||
from django.core.management import call_command | ||
from django.test import TestCase | ||
|
||
|
||
class PopulateAudiencesTestCase(TestCase): | ||
def test_populate_audiences(self): | ||
call_command("populate_audiences") | ||
self.assertTrue(Audience.objects.all().count() > 0) | ||
|
||
def test_populate_twice(self): | ||
call_command("populate_audiences") | ||
count = Audience.objects.all().count() | ||
call_command("populate_audiences") | ||
self.assertEqual(Audience.objects.all().count(), count) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
from announcements.models import Announcement, Audience | ||
from announcements.serializers import AnnouncementSerializer, AudienceSerializer | ||
from django.test import TestCase | ||
from django.utils import timezone | ||
|
||
|
||
class AudienceSerializerTestCase(TestCase): | ||
def setUp(self): | ||
self.audience = Audience.objects.create(name=Audience.AUDIENCE_CLUBS) | ||
self.serializer = AudienceSerializer(self.audience) | ||
|
||
def test_serializer(self): | ||
data = {"name": self.audience.name} | ||
self.assertEqual(self.serializer.data, data) | ||
|
||
|
||
class AnnouncementSerializerTestCase(TestCase): | ||
def setUp(self): | ||
self.audience_clubs = Audience.objects.create(name=Audience.AUDIENCE_CLUBS) | ||
self.audience_ohq = Audience.objects.create(name=Audience.AUDIENCE_OHQ) | ||
self.announcement = Announcement.objects.create( | ||
title="Test message", | ||
message="This is a test", | ||
announcement_type=Announcement.ANNOUNCEMENT_NOTICE, | ||
release_time=timezone.datetime( | ||
year=3000, month=12, day=31, tzinfo=timezone.get_current_timezone() | ||
), | ||
) | ||
self.announcement.audiences.add(self.audience_clubs) | ||
self.announcement.audiences.add(self.audience_ohq) | ||
self.serializer = AnnouncementSerializer(self.announcement) | ||
|
||
def test_serializer(self): | ||
data = { | ||
"id": self.announcement.id, | ||
"title": self.announcement.title, | ||
"message": self.announcement.message, | ||
"announcement_type": self.announcement.announcement_type, | ||
"release_time": self.announcement.release_time.isoformat(), | ||
"end_time": None, | ||
"audiences": [Audience.AUDIENCE_CLUBS, Audience.AUDIENCE_OHQ], | ||
} | ||
self.assertEqual(self.serializer.data, data) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,134 @@ | ||
from announcements.models import Announcement, Audience | ||
from announcements.serializers import AnnouncementSerializer | ||
from django.contrib.auth import get_user_model | ||
from django.test import Client, TestCase | ||
from django.utils import timezone | ||
|
||
|
||
class AnnouncementsFilterTestCase(TestCase): | ||
def setUp(self): | ||
self.client = Client() | ||
self.audience_clubs = Audience.objects.create(name=Audience.AUDIENCE_CLUBS) | ||
self.audience_ohq = Audience.objects.create(name=Audience.AUDIENCE_OHQ) | ||
self.announcement1 = Announcement.objects.create( | ||
title="Test message", | ||
message="This is a test", | ||
announcement_type=Announcement.ANNOUNCEMENT_NOTICE, | ||
release_time=timezone.datetime( | ||
year=1000, month=12, day=31, tzinfo=timezone.get_current_timezone() | ||
), | ||
end_time=timezone.datetime( | ||
year=1001, month=12, day=31, tzinfo=timezone.get_current_timezone() | ||
), | ||
) | ||
self.announcement2 = Announcement.objects.create( | ||
title="Test message 2", | ||
message="This is also a test", | ||
announcement_type=Announcement.ANNOUNCEMENT_NOTICE, | ||
end_time=timezone.datetime( | ||
year=3000, month=12, day=31, tzinfo=timezone.get_current_timezone() | ||
), | ||
) | ||
self.announcement1.audiences.add(self.audience_clubs) | ||
self.announcement2.audiences.add(self.audience_ohq) | ||
|
||
def test_get_no_filter(self): | ||
response = self.client.get("/announcements/") | ||
self.assertIn(AnnouncementSerializer(self.announcement1).data, response.json()) | ||
self.assertIn(AnnouncementSerializer(self.announcement2).data, response.json()) | ||
|
||
def test_filter_active(self): | ||
response = self.client.get("/announcements/?active=true") | ||
self.assertNotIn( | ||
AnnouncementSerializer(self.announcement1).data, response.json() | ||
) | ||
self.assertIn(AnnouncementSerializer(self.announcement2).data, response.json()) | ||
|
||
def test_filter_audience(self): | ||
response = self.client.get("/announcements/?audience=clubs") | ||
self.assertIn(AnnouncementSerializer(self.announcement1).data, response.json()) | ||
self.assertNotIn( | ||
AnnouncementSerializer(self.announcement2).data, response.json() | ||
) | ||
|
||
|
||
class AnnouncementsPermissionTestCase(TestCase): | ||
def setUp(self): | ||
self.client = Client() | ||
|
||
def test_invalid_permission(self): | ||
response = self.client.post( | ||
"/announcements/", | ||
{ | ||
"title": "Maintenance Alert", | ||
"message": "We apologize for any inconvenience caused.", | ||
"audiences": [ | ||
"CLUBS", | ||
"COURSE_PLAN", | ||
"COURSE_ALERT" | ||
], | ||
}, | ||
) | ||
self.assertEqual(response.status_code, 403) | ||
|
||
|
||
class AnnouncementsModifyTestCase(TestCase): | ||
def setUp(self): | ||
self.client = Client() | ||
for x in [ | ||
"MOBILE", | ||
"OHQ", | ||
"CLUBS", | ||
"COURSE_PLAN", | ||
"COURSE_REVIEW", | ||
"COURSE_ALERT", | ||
]: | ||
Audience.objects.get_or_create(name=x) | ||
self.announcement = Announcement.objects.create( | ||
title="Test message", | ||
message="This is a test", | ||
announcement_type=Announcement.ANNOUNCEMENT_NOTICE, | ||
release_time=timezone.datetime( | ||
year=1000, month=12, day=31, tzinfo=timezone.get_current_timezone() | ||
), | ||
end_time=timezone.datetime( | ||
year=1001, month=12, day=31, tzinfo=timezone.get_current_timezone() | ||
), | ||
) | ||
self.user = get_user_model().objects.create( | ||
pennid=1, | ||
username="student", | ||
password="secret", | ||
first_name="First", | ||
last_name="Last", | ||
email="[email protected]", | ||
is_superuser=1, | ||
) | ||
self.client.force_login(self.user) | ||
|
||
def test_create_announcement(self): | ||
response = self.client.post( | ||
"/announcements/", | ||
{ | ||
"title": "Maintenance Alert", | ||
"message": "We apologize for any inconvenience caused.", | ||
"audiences": ["CLUBS", "COURSE_PLAN", "COURSE_ALERT"], | ||
}, | ||
) | ||
self.assertEqual(response.status_code, 201) | ||
self.assertTrue(Announcement.objects.filter(title="Maintenance Alert").exists()) | ||
|
||
def test_update_announcement(self): | ||
response = self.client.patch( | ||
f"/announcements/{self.announcement.id}/", | ||
{"title": "Wow!"}, | ||
content_type="application/json", | ||
) | ||
self.assertEqual(response.status_code, 200) | ||
self.assertTrue(Announcement.objects.filter(title="Wow!").exists()) | ||
self.assertFalse(Announcement.objects.filter(title="Test message").exists()) | ||
|
||
def test_delete_announcement(self): | ||
response = self.client.delete(f"/announcements/{self.announcement.id}/") | ||
self.assertEqual(response.status_code, 204) | ||
self.assertFalse(Announcement.objects.filter(title="Test message").exists()) |