def setUp(self): self.template = EmailTemplate(idname="TEST", name="TEMPLATE NAME", description="TEMPLATE DESCRIPTION", subject="TEMPLATE SUBJECT", body_html="TEMPLATE HTML", body_text="TEMPLATE TEXT") self.template.save() self.family = Family(email="*****@*****.**") self.family.save() self.person = Person(family=self.family) self.person.save() self.union = Union(name="", region="S", streetname="", housenumber="1", zipcode="1234", city="") self.union.save() self.department = Department( union=self.union, name="", streetname="", housenumber="1", zipcode="1234", city="", responsible_contact="*****@*****.**") self.department.save()
class TestModelFamily(TestCase): def test_saving_and_retrieving_family(self): family = FamilyFactory() PersonFactory(family=family) PersonFactory(family=family, membertype=Person.CHILD) self.assertEqual(family, Family.objects.first()) self.assertEqual(1, Family.objects.count()) self.assertEqual(2, Person.objects.count()) def test_defaults_to_dont_send_mails(self): family = FamilyFactory() self.assertFalse(family.dont_send_mails) def test_get_first_parent_none_when_no_parents(self): family = FamilyFactory() PersonFactory(family=family, membertype=Person.CHILD) self.assertIsNone(family.get_first_parent()) def test_get_first_parent(self): family = FamilyFactory() p1 = PersonFactory(family=family, membertype=Person.PARENT) PersonFactory(family=family, membertype=Person.PARENT) PersonFactory(family=family, membertype=Person.CHILD) self.assertEqual(p1, family.get_first_parent()) def test_get_first_parent_when_guardian(self): family = FamilyFactory() p1 = PersonFactory(family=family, membertype=Person.GUARDIAN) PersonFactory(family=family, membertype=Person.PARENT) PersonFactory(family=family, membertype=Person.PARENT) self.assertEqual(p1, family.get_first_parent()) def test_cannot_create_two_families_with_same_email(self): FamilyFactory(email="*****@*****.**") with self.assertRaises(IntegrityError): FamilyFactory(email="*****@*****.**") def test_send_login_link_email(self): self.template = EmailTemplate( idname="LINK", name="TEMPLATE NAME", description="TEMPLATE DESCRIPTION", subject="TEMPLATE SUBJECT", body_html="TEMPLATE HTML", body_text="TEMPLATE TEXT" ) self.template.save() family = FamilyFactory() family.send_link_email() EmailSendCronJob().do() self.assertEqual(1, len(mail.outbox)) self.assertEqual("TEMPLATE SUBJECT", mail.outbox[0].subject) def test_string_representation(self): family = FamilyFactory(email="*****@*****.**") self.assertEqual("*****@*****.**", str(family))
class TestModelFamily(TestCase): def test_saving_and_retrieving_family(self): family = FamilyFactory() PersonFactory(family=family) PersonFactory(family=family, membertype=Person.CHILD) self.assertEqual(family, Family.objects.first()) self.assertEqual(1, Family.objects.count()) self.assertEqual(2, Person.objects.count()) def test_defaults_to_dont_send_mails(self): family = FamilyFactory() self.assertFalse(family.dont_send_mails) def test_get_first_parent_none_when_no_parents(self): family = FamilyFactory() PersonFactory(family=family, membertype=Person.CHILD) self.assertIsNone(family.get_first_parent()) def test_get_first_parent(self): family = FamilyFactory() p1 = PersonFactory(family=family, membertype=Person.PARENT) PersonFactory(family=family, membertype=Person.PARENT) PersonFactory(family=family, membertype=Person.CHILD) self.assertEqual(p1, family.get_first_parent()) def test_get_first_parent_when_guardian(self): family = FamilyFactory() p1 = PersonFactory(family=family, membertype=Person.GUARDIAN) PersonFactory(family=family, membertype=Person.PARENT) PersonFactory(family=family, membertype=Person.PARENT) self.assertEqual(p1, family.get_first_parent()) def test_cannot_create_two_families_with_same_email(self): FamilyFactory(email="*****@*****.**") with self.assertRaises(IntegrityError): FamilyFactory(email="*****@*****.**") def test_send_login_link_email(self): self.template = EmailTemplate( idname="LINK", name="TEMPLATE NAME", description="TEMPLATE DESCRIPTION", subject="TEMPLATE SUBJECT", body_html="TEMPLATE HTML", body_text="TEMPLATE TEXT", ) self.template.save() family = FamilyFactory() family.send_link_email() EmailSendCronJob().do() self.assertEqual(1, len(mail.outbox)) self.assertEqual("TEMPLATE SUBJECT", mail.outbox[0].subject) def test_string_representation(self): family = FamilyFactory(email="*****@*****.**") self.assertEqual("*****@*****.**", str(family))
class TestModelDepartment(TestCase): def setUp(self): self.union = Union() self.union.save() self.department = Department( union=self.union, streetname="Prins Jørgens Gård", housenumber="1", zipcode="1218", city="København" ) self.department.save() self.emailtemplate = EmailTemplate( idname="VOL_NEW", name="", description="", from_address="*****@*****.**", subject="[TEST] new volunteer email" ) self.emailtemplate.save() def util_calc_distance(self, coord1, coord2): # Implements the Haversine formula # Ref: https://stackoverflow.com/a/19412565/1689680 R = 6373.0 lat1 = math.radians(coord1[0]) lon1 = math.radians(coord1[1]) lat2 = math.radians(coord2[0]) lon2 = math.radians(coord2[1]) dlon = lon2 - lon1 dlat = lat2 - lat1 a = math.sin(dlat / 2) ** 2 + math.cos(lat1) * math.cos(lat2) * math.sin(dlon / 2) ** 2 c = 2 * math.atan2(math.sqrt(a), math.sqrt(1 - a)) return R * c def test_get_long_lat(self): self.assertLess( self.util_calc_distance( self.department.getLatLon(), (55.67680271, 12.57994743) ), 1.0 # Calculated distance from the returned value and the expected value can be no more than 1 km ) def test_new_volunteer_email(self): self.department.new_volunteer_email("") EmailSendCronJob().do() self.assertEqual(len(mail.outbox), 1)
def test_send_login_link_email(self): self.template = EmailTemplate( idname="LINK", name="TEMPLATE NAME", description="TEMPLATE DESCRIPTION", subject="TEMPLATE SUBJECT", body_html="TEMPLATE HTML", body_text="TEMPLATE TEXT", ) self.template.save() family = FamilyFactory() family.send_link_email() EmailSendCronJob().do() self.assertEqual(1, len(mail.outbox)) self.assertEqual("TEMPLATE SUBJECT", mail.outbox[0].subject)
class TestModelDepartment(TestCase): def setUp(self): self.union = Union() self.union.save() self.department = Department(union=self.union, streetname="Prins Jørgens Gård", housenumber="1", zipcode="1218", city="København") self.department.save() self.emailtemplate = EmailTemplate( idname="VOL_NEW", name="", description="", from_address="*****@*****.**", subject="[TEST] new volunteer email") self.emailtemplate.save() def util_calc_distance(self, coord1, coord2): # Implements the Haversine formula # Ref: https://stackoverflow.com/a/19412565/1689680 R = 6373.0 lat1 = math.radians(coord1[0]) lon1 = math.radians(coord1[1]) lat2 = math.radians(coord2[0]) lon2 = math.radians(coord2[1]) dlon = lon2 - lon1 dlat = lat2 - lat1 a = math.sin(dlat / 2)**2 + math.cos(lat1) * math.cos(lat2) * math.sin( dlon / 2)**2 c = 2 * math.atan2(math.sqrt(a), math.sqrt(1 - a)) return R * c def test_get_long_lat(self): self.assertLess( self.util_calc_distance(self.department.getLatLon(), (55.67680271, 12.57994743)), 1.0 # Calculated distance from the returned value and the expected value can be no more than 1 km ) def test_new_volunteer_email(self): self.department.new_volunteer_email("") EmailSendCronJob().do() self.assertEqual(len(mail.outbox), 1)
def setUp(self): self.union = Union() self.union.save() self.department = Department(union=self.union, streetname="Prins Jørgens Gård", housenumber="1", zipcode="1218", city="København") self.department.save() self.emailtemplate = EmailTemplate( idname="VOL_NEW", name="", description="", from_address="*****@*****.**", subject="[TEST] new volunteer email") self.emailtemplate.save()
def setUp(self): self.template = EmailTemplate( idname="TEST", name="TEMPLATE NAME", description="TEMPLATE DESCRIPTION", subject="TEMPLATE SUBJECT", body_html="TEMPLATE HTML", body_text="TEMPLATE TEXT", ) self.template.save() self.family = Family(email="*****@*****.**") self.family.save() self.person = Person(family=self.family) self.person.save() self.union = UnionFactory() self.department = DepartmentFactory( union=self.union, department_email="*****@*****.**" )
def setUp(self): self.union = Union() self.union.save() self.department = Department( union=self.union ) self.department.save() self.activity = Activity( start_date=datetime.now(), end_date=datetime.now() + timedelta(days=365), # Has to be long enough to be a season department=self.department ) self.activity.save() self.assertTrue(self.activity.is_season()) # If this fail increase the end_date self.family = Family( email='*****@*****.**' ) self.family.save() self.person = Person( family=self.family ) self.person.save() waitinglist = WaitingList( person=self.person, department=self.department, on_waiting_list_since=datetime.now() - timedelta(days=1) ) waitinglist.save() self.waitinglist_id = waitinglist.id self.emailtemplate = EmailTemplate( idname='ACT_INVITE', from_address='*****@*****.**' ) self.emailtemplate.save()
def test_send_login_link_email(self): self.template = EmailTemplate( idname="LINK", name="TEMPLATE NAME", description="TEMPLATE DESCRIPTION", subject="TEMPLATE SUBJECT", body_html="TEMPLATE HTML", body_text="TEMPLATE TEXT" ) self.template.save() family = FamilyFactory() family.send_link_email() EmailSendCronJob().do() self.assertEqual(1, len(mail.outbox)) self.assertEqual("TEMPLATE SUBJECT", mail.outbox[0].subject)
def setUp(self): self.union = Union() self.union.save() self.department = Department( union=self.union, streetname="Prins Jørgens Gård", housenumber="1", zipcode="1218", city="København" ) self.department.save() self.emailtemplate = EmailTemplate( idname="VOL_NEW", name="", description="", from_address="*****@*****.**", subject="[TEST] new volunteer email" ) self.emailtemplate.save()
class TestEmailTemplate(TestCase): def setUp(self): self.template = EmailTemplate(idname="TEST", name="TEMPLATE NAME", description="TEMPLATE DESCRIPTION", subject="TEMPLATE SUBJECT", body_html="TEMPLATE HTML", body_text="TEMPLATE TEXT") self.template.save() self.family = Family(email="*****@*****.**") self.family.save() self.person = Person(family=self.family) self.person.save() self.union = Union(name="", region="S", streetname="", housenumber="1", zipcode="1234", city="") self.union.save() self.department = Department( union=self.union, name="", streetname="", housenumber="1", zipcode="1234", city="", responsible_contact="*****@*****.**") self.department.save() def util_check_email(self, receivers): self.assertEqual(len(mail.outbox), len(receivers)) self.assertEqual(mail.outbox[0].to, receivers, msg="Email receiver is incorrect") self.assertEqual(mail.outbox[0].from_email, "TEST <*****@*****.**>", msg="Email from address is incorrect") # Note: breaks if Django decides to swap plain text and html self.assertEqual( mail.outbox[0].content_subtype, "plain", msg= "Django changed how they handle mail with multiple types (Plain text version broke)" ) self.assertEqual(mail.outbox[0].body, "TEMPLATE TEXT") self.assertEqual( mail.outbox[0].alternatives[0][1], "text/html", msg= "Django changed how they handle mail with multiple types (HTML version broke)" ) self.assertEqual(mail.outbox[0].alternatives[0][0], "TEMPLATE HTML") @override_settings(SITE_CONTACT="TEST <*****@*****.**>") def test_send_email_family(self): self.template.makeEmail(self.family, {}) EmailSendCronJob().do() self.util_check_email(["*****@*****.**"]) @override_settings(SITE_CONTACT="TEST <*****@*****.**>") def test_send_email_family_no_to_email(self): self.family.dont_send_mails = True self.family.save() self.template.makeEmail(self.family, {}) EmailSendCronJob().do() self.assertEqual(len(mail.outbox), 0) @override_settings(SITE_CONTACT="TEST <*****@*****.**>") def test_send_email_person(self): self.person.email = "*****@*****.**" self.person.save() self.template.makeEmail(self.person, {}) EmailSendCronJob().do() self.util_check_email(["*****@*****.**"]) @override_settings(SITE_CONTACT="TEST <*****@*****.**>") def test_send_email_person_no_to_email(self): self.family.dont_send_mails = True self.family.save() self.template.makeEmail(self.person, {}) EmailSendCronJob().do() self.assertEqual(len(mail.outbox), 0) @override_settings(SITE_CONTACT="TEST <*****@*****.**>") def test_send_email_department(self): self.template.makeEmail(self.department, {}) EmailSendCronJob().do() self.util_check_email(["*****@*****.**"])
class TestModelActivityInvite(TestCase): fixtures = ['templates'] def setUp(self): self.union = Union() self.union.save() self.department = Department( union=self.union ) self.department.save() self.activity = Activity( start_date=datetime.now(), end_date=datetime.now() + timedelta(days=365), # Has to be long enough to be a season department=self.department ) self.activity.save() self.assertTrue(self.activity.is_season()) # If this fail increase the end_date self.family = Family( email='*****@*****.**' ) self.family.save() self.person = Person( family=self.family ) self.person.save() waitinglist = WaitingList( person=self.person, department=self.department, on_waiting_list_since=datetime.now() - timedelta(days=1) ) waitinglist.save() self.waitinglist_id = waitinglist.id self.emailtemplate = EmailTemplate( idname='ACT_INVITE', from_address='*****@*****.**' ) self.emailtemplate.save() def test_own_email(self): self.person.email = '*****@*****.**' self.person.save() self.invite = ActivityInvite( activity=self.activity, person=self.person ) self.invite.save() EmailSendCronJob().do() self.assertEqual(len(mail.outbox), 2) self.assertEqual(len(list(filter(lambda email: email.to == ['*****@*****.**'], mail.outbox))), 1, msg="No email was send to the persons email address") self.assertEqual(len(list(filter(lambda email: email.to == ['*****@*****.**'], mail.outbox))), 1, msg="No email wes send to the families email address") def test_family_email(self): self.person.email = '' self.person.save() self.invite = ActivityInvite( activity=self.activity, person=self.person ) self.invite.save() EmailSendCronJob().do() self.assertEqual(len(mail.outbox), 1) self.assertEqual(mail.outbox[0].to, ['*****@*****.**']) def test_email_only_once(self): self.invite = ActivityInvite( activity=self.activity, person=self.person ) self.invite.save() EmailSendCronJob().do() mail.outbox.clear() self.invite.save() EmailSendCronJob().do() self.assertEqual(len(mail.outbox), 0) def test_waiting_list(self): self.invite = ActivityInvite( activity=self.activity, person=self.person ) self.invite.save() self.assertFalse(WaitingList.objects.filter(pk=self.waitinglist_id).exists()) def test_clean_age_limit_min(self): self.person.birthday = datetime.now() - timedelta(days=365 * 2) self.activity.min_age = 4 self.activity.max_age = 8 self.invite = ActivityInvite( activity=self.activity, person=self.person ) with self.assertRaises(ValidationError): self.invite.clean() def test_clean_age_limit_max(self): self.person.birthday = datetime.now() - timedelta(days=365 * 10) self.activity.min_age = 4 self.activity.max_age = 6 self.invite = ActivityInvite( activity=self.activity, person=self.person ) with self.assertRaises(ValidationError): self.invite.clean() def test_clean_age_limit_inbounds(self): self.person.birthday = datetime.now() - timedelta(days=365 * 6) self.activity.min_age = 4 self.activity.max_age = 8 self.invite = ActivityInvite( activity=self.activity, person=self.person ) self.invite.clean()