5
from Publicity.models import CommsOfficer
6
from Groups.models import Group, GroupCategory
7
from django.contrib.auth.models import User
8
from Email.models import EmailTemplate, EmailRecipient
9
from django.core import mail
10
from django.test import TestCase
12
class CommsOfficerTests(TestCase):
16
self.anna = User.objects.create(
20
email = "abz222@cam.ac.uk")
21
self.bobo = User.objects.create(
24
last_name = "The Bear",
25
email = "bobothebear@cam.ac.uk")
26
self.charliferous = User.objects.create(
28
first_name = "Charliferous",
30
email = "cIII@cam.ac.uk")
33
self.college, created = GroupCategory.objects.get_or_create(name = "College", defaults = {'small_name': "COL", 'all_can_create': False})
34
# College Student Union Group Type
35
self.college_union, created = GroupCategory.objects.get_or_create(name = "College Students' Union", defaults = {'small_name': "CSU", 'all_can_create': False})
38
self.binary = Group.objects.create(official_name = "Binary College", category = self.college)
39
self.porterhouse = Group.objects.create(official_name = "Porterhouse", category = self.college)
41
# Create three SUs - two notionally at one college
42
self.binary_juniors = Group.objects.create(official_name = "Binary Juniors", category = self.college_union)
43
self.binary_seniors = Group.objects.create(official_name = "Binary Seniors", category = self.college_union)
44
self.porterhouse_six_club = Group.objects.create(official_name = "Porterhouse Six Club", category = self.college_union)
46
# Anna is comms officer of binary juniors
47
self.anna_work = CommsOfficer.objects.create(
48
college = self.binary,
49
body = self.binary_juniors,
51
position_name = "Comms Officer",
52
position_email = "anna@binaryjs.com",
55
# Bobo is secretary of binary seniors
56
self.bobo_work = CommsOfficer.objects.create(
57
college = self.binary,
58
body = self.binary_seniors,
60
position_name = "Secretary",
61
position_email = "sec@binaryseniorroom.com",
64
# Charliferous is an officer of porterhouse six club
65
self.char_work = CommsOfficer.objects.create(
66
college = self.porterhouse,
67
body = self.porterhouse_six_club,
69
contact = self.charliferous
72
# And an emailtemplate
73
self.theemail = EmailTemplate.objects.create(
74
name = "Fergus' Email",
75
email_reply_to = "friverlous@gmail.com",
76
email_reply_to_name = "Fergus McQueen",
77
email_from = "fergus@mypidge.com",
78
email_from_name = "Fergus McQueen",
79
subject = "{{ user.firstname }}, this is it",
80
body = "Dear {{ user.firstname }}, here's an exciting email for you."
84
def testCommsOfficerStringRep(self):
85
''' CommsOfficer has a suitable string representation '''
86
self.failUnlessEqual(unicode(self.char_work), "Charliferous III - Porterhouse Six Club relevant officer")
88
def testSendOne(self):
89
''' Send a mailing to one particular comms officer. '''
90
CommsOfficer.objects.send(self.theemail, [self.char_work])
91
# Queues one recipient
92
self.assertEquals(EmailRecipient.objects.filter(email = self.theemail).count(), 1)
93
# And it's to Charliferous to his personal email address
94
self.assertEquals(EmailRecipient.objects.get(email = self.theemail).alternate_address, '')
96
def testSendAll(self):
97
''' Send a mailing to all SU's comms officers. '''
98
CommsOfficer.objects.send_all(self.theemail)
99
# Queues three recipients
100
self.assertEquals(EmailRecipient.objects.filter(email = self.theemail).count(), 3)
101
# Each message has one of their names etc
103
def testSendUndergrad(self):
104
''' Send a mailing to undergrad SU's comms officers. '''
105
CommsOfficer.objects.send_undergrad(self.theemail)
106
# Creates 2 recipients - one to Anna and one to Char
107
self.assertEquals(EmailRecipient.objects.filter(email = self.theemail).count(), 2)
109
def testSendPostgrad(self):
110
''' Send a mailing to postgrad SU's comms officers. '''
111
CommsOfficer.objects.send_postgrad(self.theemail)
112
# Creates 1 email message
113
self.assertEquals(EmailRecipient.objects.filter(email = self.theemail).count(), 1)
115
self.assertEquals(EmailRecipient.objects.get(email = self.theemail).alternate_address, 'sec@binaryseniorroom.com')