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 setUp(self): self.union1 = Union( name="union1", region="S", streetname="", housenumber="1", zipcode="1234", city="", ) self.union1.save() self.union2 = Union( name="union2", region="S", streetname="", housenumber="1", zipcode="1234", city="", ) self.union2.save() self.department = Department( name="", union=self.union1, streetname="", housenumber="1", zipcode="1234", city="", ) self.department.save()
class TestModelWaitinglist(TestCase): def setUp(self): self.union = Union() self.union.save() self.department = Department(union=self.union) self.department.save() self.family = Family() self.family.save() self.person1 = Person(family=self.family) self.person1.save() self.person1_waitinglist = WaitingList(person=self.person1, department=self.department) self.person1_waitinglist.save() # Artifically wait 60 seconds with adding person2 to the waitinglist with freeze_time(datetime.now() + timedelta(seconds=60)): self.person2 = Person(family=self.family) self.person2.save() self.person2_waitinglist = WaitingList(person=self.person2, department=self.department) self.person2_waitinglist.save() def test_waiting_list_position(self): self.assertEqual(self.person1_waitinglist.number_on_waiting_list(), 1) self.assertEqual(self.person2_waitinglist.number_on_waiting_list(), 2)
class TestModelActivityParticipant(TestCase): # ToDo: Maybe test payment 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() self.member = Member( department=self.department, person=self.person, is_active=True ) self.member.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 def test_save_waiting_list(self): self.participant = ActivityParticipant( activity=self.activity, member=self.member ) self.participant.save() self.assertFalse(WaitingList.objects.filter(pk=self.waitinglist_id).exists())
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.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() self.member = Member( department=self.department, person=self.person, is_active=True ) self.member.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
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 setUp(self): self.union = Union() self.union.save() self.department = Department(union=self.union) self.department.save() self.family = Family() self.family.save() self.person1 = Person(family=self.family) self.person1.save() self.person1_waitinglist = WaitingList(person=self.person1, department=self.department) self.person1_waitinglist.save() # Artifically wait 60 seconds with adding person2 to the waitinglist with freeze_time(datetime.now() + timedelta(seconds=60)): self.person2 = Person(family=self.family) self.person2.save() self.person2_waitinglist = WaitingList(person=self.person2, department=self.department) self.person2_waitinglist.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 TestModelEquipment(TestCase): def setUp(self): self.union1 = Union( name="union1", region="S", streetname="", housenumber="1", zipcode="1234", city="", ) self.union1.save() self.union2 = Union( name="union2", region="S", streetname="", housenumber="1", zipcode="1234", city="", ) self.union2.save() self.department = Department( name="", union=self.union1, streetname="", housenumber="1", zipcode="1234", city="", ) self.department.save() def test_clean_no_owner(self): # Validation should fail if their isn't a owner equipment = Equipment() with self.assertRaises(ValidationError): equipment.clean() equipment.save() def test_clean_department_owner(self): # Test model validation with only department as owner equipment = Equipment(department=self.department) equipment.clean() equipment.save() def test_clean_union_owner(self): # Test model validation with only union as owner equipment = Equipment(union=self.union1) equipment.clean() equipment.save() def test_clean_department_and_union(self): # Test the mode validation with both department and union set self.department.union = self.union1 self.department.save() equipment = Equipment(union=self.union1, department=self.department) # Test validation with department union matching union in equipment equipment.clean() equipment.save() self.department.union = self.union2 self.department.save() # Test validation with department union not matching union in equipment with self.assertRaises(ValidationError): equipment.clean() equipment.save()
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()