def test_generate_reminder_with_long_name_and_location(self): # Make sure we generate a reminder less than 4 days before now = timezone.now() sg = StudyGroup.objects.get(pk=1) sg.timezone = now.strftime("%Z") sg.venue_name = 'This is a very long venue name to test for sms reminders that are longer than a 160 caracters long' sg.venue_details = 'This is an even longer venue detail field. I am almost 100% certain this will be cut off. What do you think' sg.start_date = now.date() + datetime.timedelta(days=3) sg.meeting_time = now.time() sg.end_date = now.date() + datetime.timedelta(weeks=5, days=3) sg.save() sg = StudyGroup.objects.get(pk=1) generate_all_meetings(sg) self.assertEqual(sg.meeting_set.active().count(), 6) self.assertTrue(sg.next_meeting().meeting_datetime() - now < datetime.timedelta(days=4)) generate_reminder(sg) self.assertEqual(Reminder.objects.all().count(), 1) reminder = Reminder.objects.all()[0] #TODO check that email was sent to site admin #TODO test with unicode in generated email subject # Make sure we do it only once generate_reminder(sg) self.assertEqual(Reminder.objects.all().count(), 1)
def test_send_malicious_reminder_email(self, send_message): now = timezone.now() sg = StudyGroup.objects.get(pk=1) sg.timezone = now.strftime("%Z") sg.start_date = now - datetime.timedelta(days=5) sg.meeting_time = sg.start_date.time() sg.end_date = sg.start_date + datetime.timedelta(weeks=2) sg.save() sg = StudyGroup.objects.get(pk=1) data = self.APPLICATION_DATA data['study_group'] = sg application = Application(**data) accept_application(application) application.save() mail.outbox = [] generate_all_meetings(sg) meeting = sg.meeting_set.filter(meeting_date__lte=now).last() feedback = Feedback.objects.create( study_group_meeting=meeting, feedback='<a href="https://evil.ink/">this awesome link</a>', attendance=3, rating='3') generate_reminder(sg) self.assertEqual(Reminder.objects.all().count(), 1) reminder = Reminder.objects.all()[0] self.assertEqual(len(mail.outbox), 1) send_reminder(reminder) self.assertEqual(len(mail.outbox), 3) # should be sent to facilitator & application self.assertEqual(mail.outbox[1].to[0], data['email']) self.assertFalse(send_message.called) self.assertIn( '{0}/{1}/rsvp/?user=test%40mail.com&study_group=1&meeting_date={2}&attending=yes&sig=' .format( settings.DOMAIN, get_language(), urllib.parse.quote( sg.next_meeting().meeting_datetime().isoformat())), mail.outbox[1].alternatives[0][0]) self.assertIn( '{0}/{1}/rsvp/?user=test%40mail.com&study_group=1&meeting_date={2}&attending=no&sig=' .format( settings.DOMAIN, get_language(), urllib.parse.quote( sg.next_meeting().meeting_datetime().isoformat())), mail.outbox[1].alternatives[0][0]) self.assertIn( '{0}/{1}/optout/confirm/?user='******'<a href="https://evil.ink/">this awesome link</a>', mail.outbox[1].alternatives[0][0])
def test_dont_send_automatic_reminder_for_old_message(self, send_message): now = timezone.now() sg = StudyGroup.objects.get(pk=1) sg.timezone = now.strftime("%Z") sg.start_date = datetime.date(2010, 3, 10) sg.meeting_time = datetime.time(18, 0) sg.end_date = sg.start_date + datetime.timedelta(weeks=5) sg.save() sg = StudyGroup.objects.get(pk=1) generate_all_meetings(sg) data = self.APPLICATION_DATA data['study_group'] = sg application = Application(**data) application.save() accept_application(application) mail.outbox = [] with freeze_time("2010-03-06 18:55:34"): generate_reminder(sg) self.assertEqual(len(mail.outbox), 1) self.assertEqual(Reminder.objects.all().count(), 1) mail.outbox = [] with freeze_time("2010-03-08 18:55:34"): send_reminders() self.assertEqual(len(mail.outbox), 2) self.assertEqual(mail.outbox[0].to[0], data['email']) self.assertEqual(mail.outbox[1].to[0], '*****@*****.**') self.assertFalse(send_message.called) self.assertEqual( Reminder.objects.filter(sent_at__isnull=True).count(), 0) mail.outbox = [] with freeze_time("2010-03-13 18:55:34"): generate_reminder(sg) self.assertEqual(len(mail.outbox), 1) self.assertEqual(Reminder.objects.all().count(), 2) self.assertEqual( Reminder.objects.filter(sent_at__isnull=True).count(), 1) reminder = Reminder.objects.filter(sent_at__isnull=True).first() self.assertEqual(reminder.study_group_meeting.meeting_date, datetime.date(2010, 3, 17)) mail.outbox = [] with freeze_time("2010-03-18 18:55:34"): send_reminders() self.assertEqual(len(mail.outbox), 0) self.assertEqual( Reminder.objects.filter(sent_at__isnull=True).count(), 1)
def test_dont_generate_reminder_4days_before(self): # Make sure we don't generate a reminder more than 4 days before now = timezone.now() self.assertEqual(Reminder.objects.all().count(), 0) sg = StudyGroup.objects.get(pk=1) sg.timezone = now.strftime("%Z") sg.start_date = now + datetime.timedelta(days=4, hours=1) sg.meeting_time = sg.start_date.time() sg.end_date = sg.start_date + datetime.timedelta(weeks=5) sg.save() sg = StudyGroup.objects.get(pk=1) generate_all_meetings(sg) generate_reminder(sg) self.assertEqual(Reminder.objects.all().count(), 0)
def test_no_reminders_for_future_studygroups(self): now = timezone.now() self.assertEqual(Reminder.objects.all().count(), 0) # Make sure we don't generate a reminder for future study groups sg = StudyGroup.objects.get(pk=1) sg.timezone = now.strftime("%Z") sg.start_date = now + datetime.timedelta(days=2, weeks=1) sg.meeting_time = sg.start_date.time() sg.end_date = sg.start_date + datetime.timedelta(weeks=5) sg.save() sg = StudyGroup.objects.get(pk=1) generate_all_meetings(sg) generate_reminder(sg) self.assertEqual(Reminder.objects.all().count(), 0)
def test_send_learner_reminder_ics(self, send_message): now = timezone.now() sg = StudyGroup.objects.get(pk=1) sg.timezone = now.strftime("%Z") sg.start_date = now - datetime.timedelta(days=5) sg.meeting_time = sg.start_date.time() sg.end_date = sg.start_date + datetime.timedelta(weeks=5) sg.attach_ics = True sg.save() sg = StudyGroup.objects.get(pk=1) data = self.APPLICATION_DATA data['study_group'] = sg application = Application(**data) accept_application(application) application.save() mail.outbox = [] generate_all_meetings(sg) generate_reminder(sg) self.assertEqual(Reminder.objects.all().count(), 1) reminder = Reminder.objects.all()[0] self.assertEqual(len(mail.outbox), 1) send_reminder(reminder) self.assertEqual(len(mail.outbox), 3) # should be sent to facilitator & application self.assertEqual(mail.outbox[1].to[0], data['email']) self.assertFalse(send_message.called) self.assertIn( '{0}/{1}/rsvp/?user=test%40mail.com&study_group=1&meeting_date={2}&attending=yes&sig=' .format( settings.DOMAIN, get_language(), urllib.parse.quote( sg.next_meeting().meeting_datetime().isoformat())), mail.outbox[1].alternatives[0][0]) self.assertIn( '{0}/{1}/rsvp/?user=test%40mail.com&study_group=1&meeting_date={2}&attending=no&sig=' .format( settings.DOMAIN, get_language(), urllib.parse.quote( sg.next_meeting().meeting_datetime().isoformat())), mail.outbox[1].alternatives[0][0]) self.assertIn( '{0}/{1}/optout/confirm/?user='******'VEVENT', mail.outbox[1].attachments[0].get_payload())
def test_send_reminder_sms(self, send_message): now = timezone.now() sg = StudyGroup.objects.get(pk=1) sg.timezone = now.strftime("%Z") sg.start_date = now - datetime.timedelta(days=5) sg.meeting_time = sg.start_date.time() sg.end_date = sg.start_date + datetime.timedelta(weeks=5) sg.save() sg = StudyGroup.objects.get(pk=1) data = self.APPLICATION_DATA data['study_group'] = sg application = Application(**data) application.save() mail.outbox = [] generate_all_meetings(sg) generate_reminder(sg) self.assertEqual(Reminder.objects.all().count(), 1) reminder = Reminder.objects.all()[0] send_reminder(reminder) self.assertEqual(len(mail.outbox), 2) #self.assertEqual(mail.outbox[0].subject, mail_data['email_subject']) self.assertFalse(send_message.called)
def test_generate_reminder_4days_before(self): # Make sure we generate a reminder less than 4 days before now = timezone.now() sg = StudyGroup.objects.get(pk=1) sg.timezone = now.strftime("%Z") sg.start_date = now.date() + datetime.timedelta(days=3) sg.meeting_time = now.time() sg.end_date = now.date() + datetime.timedelta(weeks=5, days=3) sg.save() sg = StudyGroup.objects.get(pk=1) generate_all_meetings(sg) self.assertEqual(sg.meeting_set.active().count(), 6) self.assertTrue(sg.next_meeting().meeting_datetime() - now < datetime.timedelta(days=4)) generate_reminder(sg) self.assertEqual(Reminder.objects.all().count(), 1) reminder = Reminder.objects.all()[0] #TODO check that email was sent to site admin #TODO test with unicode in generated email subject # Make sure we do it only once generate_reminder(sg) self.assertEqual(Reminder.objects.all().count(), 1)
def test_facilitator_reminder_email_links(self, send_message): now = timezone.now() sg = StudyGroup.objects.get(pk=1) sg.timezone = now.strftime("%Z") sg.start_date = now - datetime.timedelta(days=5) sg.meeting_time = sg.start_date.time() sg.end_date = sg.start_date + datetime.timedelta(weeks=5) sg.save() sg = StudyGroup.objects.get(pk=1) data = self.APPLICATION_DATA data['study_group'] = sg application = Application(**data) accept_application(application) application.save() mail.outbox = [] generate_all_meetings(sg) generate_reminder(sg) self.assertEqual(Reminder.objects.all().count(), 1) reminder = Reminder.objects.all()[0] self.assertEqual(len(mail.outbox), 1) mail.outbox = [] send_reminder(reminder) self.assertEqual(len(mail.outbox), 2) # should be sent to facilitator & application self.assertEqual(mail.outbox[0].to[0], data['email']) self.assertEqual(mail.outbox[1].to[0], sg.facilitator.email) self.assertFalse(send_message.called) self.assertNotIn( '{0}/{1}/rsvp/'.format(settings.DOMAIN, get_language()), mail.outbox[1].alternatives[0][0]) self.assertIn('{0}/{1}/'.format(settings.DOMAIN, get_language()), mail.outbox[1].alternatives[0][0]) self.assertNotIn( '{0}/{1}/optout/confirm/?user='.format(settings.DOMAIN, get_language()), mail.outbox[1].alternatives[0][0])
def test_edit_meeting(self, send_meeting_change_notification): # Create studygroup # generate reminder # edit meeting # make sure reminder was deleted # send reminders # edit meeting in the past to be in the future now # test reminder in now orphaned user = create_user('*****@*****.**', 'bob', 'test', 'password', False) confirm_user_email(user) c = Client() c.login(username='******', password='******') data = self.STUDY_GROUP_DATA.copy() data['start_date'] = '12/25/2018' data['meeting_time'] = '07:00 PM' resp = c.post('/en/studygroup/create/legacy/', data) sg = StudyGroup.objects.last() self.assertRedirects(resp, '/en/studygroup/{}/'.format(sg.pk)) study_groups = StudyGroup.objects.filter(facilitator=user) self.assertEquals(study_groups.count(), 1) lc = study_groups.first() self.assertEquals(lc.meeting_set.count(), 0) resp = c.post('/en/studygroup/{0}/publish/'.format(lc.pk)) self.assertRedirects(resp, '/en/studygroup/{}/'.format(lc.pk)) study_group = StudyGroup.objects.get(pk=lc.pk) self.assertEqual(study_group.draft, False) self.assertEqual(study_group.meeting_set.count(), 6) # generate reminder for first meeting (< 4 days before 25 Dec) with freeze_time('2018-12-22'): generate_reminder(study_group) self.assertEqual( Reminder.objects.filter(study_group=study_group).count(), 1) # update meeting with unsent reminder reminder = Reminder.objects.filter(study_group=study_group).first() meeting = reminder.study_group_meeting meeting_id = meeting.pk update = { "meeting_date": "2018-12-27", "meeting_time": "07:00 PM", "study_group": study_group.pk, } resp = c.post( '/en/studygroup/{0}/meeting/{1}/edit/'.format( study_group.pk, meeting.pk), update) self.assertRedirects(resp, '/en/studygroup/{}/'.format(study_group.pk)) meeting.refresh_from_db() self.assertEqual(meeting.meeting_date, datetime.date(2018, 12, 27)) # make sure the reminder was deleted self.assertEqual( Reminder.objects.filter(study_group=study_group).count(), 0) # generate a reminder for the updated meeting with freeze_time('2018-12-24'): generate_reminder(study_group) self.assertEqual( Reminder.objects.filter(study_group=study_group).count(), 1) # send it with freeze_time('2018-12-26'): self.assertEqual( Reminder.objects.filter(study_group=study_group, sent_at__isnull=False).count(), 0) send_reminders() self.assertEqual( Reminder.objects.filter(study_group=study_group, sent_at__isnull=False).count(), 1) # and then update it afterwards to be in the future again with freeze_time('2018-12-28'): meeting.refresh_from_db() self.assertTrue(meeting.meeting_datetime() < timezone.now()) self.assertEquals(Reminder.objects.count(), 1) update['meeting_date'] = '2018-12-30' resp = c.post( '/en/studygroup/{0}/meeting/{1}/edit/'.format( study_group.pk, meeting.pk), update) self.assertRedirects(resp, '/en/studygroup/{}/'.format(study_group.pk)) meeting.refresh_from_db() self.assertFalse(send_meeting_change_notification.delay.called) # old reminder should be orphaned - not linked to this meeting self.assertEquals(meeting.reminder_set.count(), 0) generate_reminder(study_group) # now generate a reminder again meeting.refresh_from_db() self.assertEquals(meeting.reminder_set.count(), 1) self.assertEquals(Reminder.objects.count(), 2) # update it before the meeting to be further in the future with freeze_time('2018-12-29'): meeting.refresh_from_db() self.assertTrue(meeting.meeting_datetime() > timezone.now()) self.assertEqual( Reminder.objects.filter(study_group_meeting=meeting, sent_at__isnull=False).count(), 0) send_reminders() self.assertEqual( Reminder.objects.filter(study_group_meeting=meeting, sent_at__isnull=False).count(), 1) update['meeting_date'] = '2019-01-02' resp = c.post( '/en/studygroup/{0}/meeting/{1}/edit/'.format( study_group.pk, meeting.pk), update) self.assertRedirects(resp, '/en/studygroup/{}/'.format(study_group.pk)) meeting.refresh_from_db() self.assertTrue(send_meeting_change_notification.delay.called) # old reminder should be orphaned - not linked to this meeting self.assertEquals(meeting.reminder_set.count(), 0)