def test_meeting_change_notification(self, send_message): sg = StudyGroup.objects.first() data = self.APPLICATION_DATA data['study_group'] = sg Application(**{**data, "email": "*****@*****.**"}).save() Application(**{**data, "email": "*****@*****.**"}).save() Application(**{**data, "email": "", "mobile": "+27713213213"}).save() list( map(lambda app: accept_application(app), Application.objects.all())) self.assertEqual(Application.objects.all().count(), 3) generate_all_meetings(sg) meeting = sg.meeting_set.first() meeting.meeting_date = datetime.date(2019, 4, 1) old_meeting = sg.meeting_set.first() mail.outbox = [] send_meeting_change_notification(old_meeting, meeting) self.assertEqual(len(mail.outbox), 1) self.assertEqual(len(mail.outbox[0].bcc), 2) self.assertIn('*****@*****.**', mail.outbox[0].bcc) self.assertIn('*****@*****.**', mail.outbox[0].bcc) self.assertEqual(meeting.meeting_time, datetime.time(18, 30)) self.assertEqual( mail.outbox[0].subject, 'Test learning circle at Harold Washington now meets Monday, 1 April, 6:30PM.' ) send_message.assert_called_with( '+27713213213', 'Your learning circle on Monday, 23 March, 6:30PM has been rescheduled. Reply STOP to unsubscribe.' )
def test_send_custom_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=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) reminder = Reminder( study_group=sg, email_subject='Custom reminder', email_body='Test for extra reminders sent by facilitators', sms_body='Nothing to say here') reminder.save() self.assertEqual(Reminder.objects.all().count(), 1) reminder = Reminder.objects.all()[0] self.assertEqual(len(mail.outbox), 0) send_reminder(reminder) self.assertEqual(len(mail.outbox), 1) self.assertEqual(len(mail.outbox[0].bcc), 2) self.assertEqual(mail.outbox[0].bcc[0], data['email']) self.assertFalse(send_message.called)
def test_send_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=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() 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), 2) self.assertEqual(mail.outbox[1].to[0], data['email']) self.assertFalse(send_message.called) self.assertIn('https://example.net/{0}/rsvp/?user=test%40mail.com&study_group=1&meeting_date={1}&attending=yes&sig='.format(get_language(), urllib.quote(sg.next_meeting().meeting_datetime().isoformat())), mail.outbox[1].body) self.assertIn('https://example.net/{0}/rsvp/?user=test%40mail.com&study_group=1&meeting_date={1}&attending=no&sig='.format(get_language(), urllib.quote(sg.next_meeting().meeting_datetime().isoformat())), mail.outbox[1].body) self.assertIn('https://example.net/{0}/optout/confirm/?user='.format(get_language()), mail.outbox[1].body)
def test_create_rsvp(self): data = self.APPLICATION_DATA data['study_group'] = StudyGroup.objects.get(pk=1) application = Application(**data) application.save() sg = StudyGroup.objects.get(pk=1) meeting_date = timezone.now() sgm = StudyGroupMeeting( study_group=sg, meeting_time=meeting_date.time(), meeting_date=meeting_date.date() ) sgm.save() sgm = StudyGroupMeeting( study_group=sg, meeting_time=meeting_date.time(), meeting_date=meeting_date.date() + datetime.timedelta(weeks=1) ) sgm.save() # Test creating an RSVP self.assertEqual(Rsvp.objects.all().count(), 0) create_rsvp('*****@*****.**', sg.id, meeting_date, 'yes') self.assertEqual(Rsvp.objects.all().count(), 1) self.assertTrue(Rsvp.objects.all().first().attending) # Test updating an RSVP create_rsvp('*****@*****.**', sg.id, meeting_date, 'no') self.assertEqual(Rsvp.objects.all().count(), 1) self.assertFalse(Rsvp.objects.all().first().attending)
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() generate_all_meetings(sg) self.assertEqual(Reminder.objects.all().count(), 5) reminder = Reminder.objects.all()[0] 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_study_group_final_report_with_no_responses(self): facilitator = create_user('*****@*****.**', 'bowie', 'wowie', 'password') course_data = dict(title='Course 1011', provider='CourseMagick', link='https://course.magick/test', caption='learn by means of magic', on_demand=True, topics='html,test', language='en', created_by=facilitator) course = Course.objects.create(**course_data) sg = StudyGroup.objects.get(pk=1) sg.course = course sg.facilitator = facilitator sg.save() data = dict(self.APPLICATION_DATA) data['study_group'] = sg data['email'] = '*****@*****.**' application = Application(**data) application.save() accept_application(application) c = Client() report = '/en/studygroup/{}/report/'.format(sg.pk) response = c.get(report) self.assertEqual(response.status_code, 200) self.assertEqual(response.context_data['study_group'], sg) self.assertEqual(response.context_data['registrations'], 1) self.assertEqual(response.context_data['learner_survey_responses'], 0) self.assertEqual(response.context_data['facilitator_survey_responses'], 0) self.assertNotIn('courses', response.context_data)
def test_accept_application(self): # TODO remove this test self.assertEqual(Application.objects.active().count(), 0) data = self.APPLICATION_DATA data['study_group'] = StudyGroup.objects.get(pk=1) application = Application(**data) application.save() accept_application(application) self.assertEqual(Application.objects.active().count(), 1)
def test_unapply_signing(self): data = self.APPLICATION_DATA data['study_group'] = StudyGroup.objects.all().first() application = Application(**data) application.save() qs = application.unapply_link() qs = qs[qs.index('?')+1:] sig = urlparse.parse_qs(qs).get('sig')[0] self.assertTrue(check_unsubscribe_signature(application.pk, sig)) self.assertFalse(check_unsubscribe_signature(application.pk+1, sig))
def post(self, request): signup_questions = { "goals": schema.text(required=True), "support": schema.text(required=True), "computer_access": schema.text(required=True), "use_internet": schema.text(required=True) } post_schema = { "study_group": schema.chain([ schema.integer(), lambda x: (None, 'No matching learning circle exists') if not StudyGroup.objects.filter(pk=int(x)).exists() else (StudyGroup.objects.get(pk=int(x)), None), ], required=True), "name": schema.text(required=True), "email": schema.email(required=True), "mobile": schema.mobile(), "signup_questions": schema.schema(signup_questions, required=True) } data = json.loads(request.body) clean_data, errors = schema.validate(post_schema, data) if errors != {}: return json_response(request, { "status": "error", "errors": errors }) study_group = StudyGroup.objects.get(pk=data.get('study_group')) if Application.objects.active().filter( email__iexact=data.get('email'), study_group=study_group).exists(): application = Application.objects.active().get( email__iexact=data.get('email'), study_group=study_group) else: application = Application(study_group=study_group, name=data.get('name'), email=data.get('email'), signup_questions=json.dumps( data.get('signup_questions')), accepted_at=timezone.now()) application.name = data.get('name') application.signup_questions = json.dumps(data.get('signup_questions')) if data.get('mobile'): application.mobile = data.get('mobile') application.save() return json_response(request, {"status": "created"})
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"): tasks.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"): tasks.send_reminders() self.assertEqual(len(mail.outbox), 0) self.assertEqual( Reminder.objects.filter(sent_at__isnull=True).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_meeting(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) with freeze_time("2010-03-06 18:55:34"): generate_all_meetings(sg) data = self.APPLICATION_DATA data['study_group'] = sg application = Application(**data) application.save() accept_application(application) # There should be 6 unsent reminders self.assertEqual( Reminder.objects.filter(sent_at__isnull=True).count(), 6) 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) # Only 5 unsent reminders now self.assertEqual( Reminder.objects.filter(sent_at__isnull=True).count(), 5) reminder = Reminder.objects.filter(sent_at__isnull=True).order_by( 'study_group_meeting__meeting_date').first() self.assertEqual(reminder.study_group_meeting.meeting_date, datetime.date(2010, 3, 17)) # Make sure we don't send unsent reminders after a meeting has happened 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(), 5)
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_study_group_final_report_with_responses(self): # TODO return facilitator = create_user('*****@*****.**', 'bowie', 'wowie', 'password') course_data = dict(title='Course 1011', provider='CourseMagick', link='https://course.magick/test', caption='learn by means of magic', on_demand=True, topics='html,test', language='en', created_by=facilitator) course = Course.objects.create(**course_data) sg = StudyGroup.objects.get(pk=1) sg.course = course sg.facilitator = facilitator sg.save() data = dict(self.APPLICATION_DATA) data['study_group'] = sg data['email'] = '*****@*****.**' application = Application(**data) application.save() accept_application(application) self.create_learner_survey_response(sg, application) c = Client() report = reverse('studygroups_final_report', kwargs={'study_group_id': sg.pk}) response = c.get(report) self.assertEqual(response.status_code, 200) self.assertEqual(response.context_data['study_group'], sg) self.assertEqual(response.context_data['registrations'], 1) self.assertEqual(response.context_data['learner_survey_responses'], 1) self.assertEqual(response.context_data['facilitator_survey_responses'], 0) self.assertEqual(response.context_data['course'], course) self.assertEqual(response.context_data['goals_met_chart'], "image")
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() 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), 1) #self.assertEqual(mail.outbox[0].subject, mail_data['email_subject']) self.assertFalse(send_message.called)
def test_send_learner_surveys(self): now = timezone.now() sg = StudyGroup.objects.get(pk=1) sg.timezone = now.strftime("%Z") sg.start_date = datetime.date(2021, 5, 14) sg.meeting_time = datetime.time(10, 0) sg.save() meeting = Meeting.objects.create(study_group=sg, meeting_date=datetime.date( 2021, 5, 14), meeting_time=datetime.time(18, 0)) sg = StudyGroup.objects.get(pk=1) data = dict(self.APPLICATION_DATA) data['study_group'] = sg data['email'] = '*****@*****.**' application = Application(**data) application.save() accept_application(application) data = dict(self.APPLICATION_DATA) data['study_group'] = sg data['email'] = '*****@*****.**' application = Application(**data) accept_application(application) application.save() mail.outbox = [] last_meeting = sg.meeting_set.active().order_by( 'meeting_date', 'meeting_time').last() self.assertEqual(last_meeting.meeting_date, datetime.date(2021, 5, 14)) self.assertEqual(last_meeting.meeting_time, datetime.time(18)) self.assertEqual(sg.meeting_set.active().count(), 1) self.assertEqual(sg.application_set.active().count(), 2) # too soon with freeze_time("2021-05-14 16:30:00"): send_learner_surveys(sg) self.assertEqual(len(mail.outbox), 0) # way too late with freeze_time("2021-07-12 18:30:00"): send_learner_surveys(sg) self.assertEqual(len(mail.outbox), 0) # send when intended with freeze_time("2021-05-14 17:30:00"): self.assertEquals(sg.learner_survey_sent_at, None) send_learner_surveys(sg) self.assertEqual(len(mail.outbox), 2) self.assertIn('*****@*****.**', mail.outbox[0].to + mail.outbox[1].to) self.assertIn('*****@*****.**', mail.outbox[0].to + mail.outbox[1].to) a1 = Application.objects.get(email=mail.outbox[0].to[0]) self.assertIn( '{0}/en/studygroup/{1}/survey/?learner={2}'.format( settings.DOMAIN, sg.uuid, a1.uuid), mail.outbox[0].body) self.assertNotEquals(sg.learner_survey_sent_at, None) mail.outbox = [] # no resend with freeze_time("2021-05-14 19:00:00"): send_learner_surveys(sg) self.assertEqual(len(mail.outbox), 0) # three weeks later mail.outbox = [] sg.learner_survey_sent_at = None sg.save() with freeze_time("2021-06-04 16:30:00"): self.assertEquals(sg.learner_survey_sent_at, None) send_learner_surveys(sg) self.assertEqual(len(mail.outbox), 2) self.assertIn('*****@*****.**', mail.outbox[0].to + mail.outbox[1].to) self.assertIn('*****@*****.**', mail.outbox[0].to + mail.outbox[1].to) a1 = Application.objects.get(email=mail.outbox[0].to[0]) self.assertIn( '{0}/en/studygroup/{1}/survey/?learner={2}'.format( settings.DOMAIN, sg.uuid, a1.uuid), mail.outbox[0].body) self.assertNotEquals(sg.learner_survey_sent_at, None)
def test_send_learner_surveys(self): now = timezone.now() sg = StudyGroup.objects.get(pk=1) sg.timezone = now.strftime("%Z") sg.start_date = datetime.date(2010, 1, 1) sg.meeting_time = datetime.time(18, 0) sg.end_date = sg.start_date + datetime.timedelta(weeks=6) sg.save() sg = StudyGroup.objects.get(pk=1) generate_all_meetings(sg) data = dict(self.APPLICATION_DATA) data['study_group'] = sg data['email'] = '*****@*****.**' application = Application(**data) application.save() accept_application(application) data = dict(self.APPLICATION_DATA) data['study_group'] = sg data['email'] = '*****@*****.**' application = Application(**data) accept_application(application) application.save() mail.outbox = [] last_meeting = sg.meeting_set.active().order_by( 'meeting_date', 'meeting_time').last() self.assertEqual(last_meeting.meeting_date, datetime.date(2010, 2, 12)) self.assertEqual(last_meeting.meeting_time, datetime.time(18)) self.assertEqual(sg.meeting_set.active().count(), 7) self.assertEqual(sg.application_set.active().count(), 2) # freeze time to 5 minutes before with freeze_time("2010-02-05 17:55:34"): send_learner_surveys(sg) self.assertEqual(len(mail.outbox), 0) # 10 minutes after start with freeze_time("2010-02-05 18:10:34"): send_learner_surveys(sg) self.assertEqual(len(mail.outbox), 0) # 25 minutes after start with freeze_time("2010-02-05 18:25:34"): send_learner_surveys(sg) self.assertEqual(len(mail.outbox), 2) self.assertIn('*****@*****.**', mail.outbox[0].to + mail.outbox[1].to) self.assertIn('*****@*****.**', mail.outbox[0].to + mail.outbox[1].to) a1 = Application.objects.get(email=mail.outbox[0].to[0]) self.assertIn( 'https://example.net/en/studygroup/{}/survey/?learner={}'. format(sg.uuid, a1.uuid), mail.outbox[0].body) mail.outbox = [] # 40 minutes after start with freeze_time("2010-02-05 18:40:34"): send_learner_surveys(sg) self.assertEqual(len(mail.outbox), 0)
def test_send_learner_surveys(self): now = timezone.now() sg = StudyGroup.objects.get(pk=1) sg.timezone = now.strftime("%Z") sg.start_date = datetime.date(2010, 1, 1) sg.meeting_time = datetime.time(18, 0) sg.end_date = sg.start_date + datetime.timedelta(weeks=6) sg.save() sg = StudyGroup.objects.get(pk=1) generate_all_meetings(sg) data = dict(self.APPLICATION_DATA) data['study_group'] = sg data['email'] = '*****@*****.**' application = Application(**data) application.save() accept_application(application) data = dict(self.APPLICATION_DATA) data['study_group'] = sg data['email'] = '*****@*****.**' application = Application(**data) accept_application(application) application.save() mail.outbox = [] last_meeting = sg.meeting_set.active().order_by( 'meeting_date', 'meeting_time').last() self.assertEqual(last_meeting.meeting_date, datetime.date(2010, 2, 12)) self.assertEqual(last_meeting.meeting_time, datetime.time(18)) self.assertEqual(sg.meeting_set.active().count(), 7) self.assertEqual(sg.application_set.active().count(), 2) # too soon with freeze_time("2010-02-12 16:30:00"): send_learner_surveys(sg) self.assertEqual(len(mail.outbox), 0) # too late with freeze_time("2010-02-12 18:30:00"): send_learner_surveys(sg) self.assertEqual(len(mail.outbox), 0) with freeze_time("2010-02-12 17:30:00"): send_learner_surveys(sg) self.assertEqual(len(mail.outbox), 2) self.assertIn('*****@*****.**', mail.outbox[0].to + mail.outbox[1].to) self.assertIn('*****@*****.**', mail.outbox[0].to + mail.outbox[1].to) a1 = Application.objects.get(email=mail.outbox[0].to[0]) self.assertIn( '{0}/en/studygroup/{1}/survey/?learner={2}'.format( settings.DOMAIN, sg.uuid, a1.uuid), mail.outbox[0].body) mail.outbox = [] # way too late with freeze_time("2010-02-13 19:00:00"): send_learner_surveys(sg) self.assertEqual(len(mail.outbox), 0)
def test_send_final_learning_circle_report_email(self): organizer = create_user('*****@*****.**', 'organ', 'test', '1234', False) facilitator = create_user('*****@*****.**', 'faci', 'test', 'password', False) StudyGroup.objects.filter(pk=1).update(facilitator=facilitator) team = Team.objects.create(name='test team') TeamMembership.objects.create(team=team, user=organizer, role=TeamMembership.ORGANIZER) TeamMembership.objects.create(team=team, user=facilitator, role=TeamMembership.MEMBER) now = timezone.now() sg = StudyGroup.objects.get(pk=1) sg.timezone = now.strftime("%Z") sg.start_date = datetime.date(2010, 1, 1) 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 = dict(self.APPLICATION_DATA) data['study_group'] = sg data['email'] = '*****@*****.**' application = Application(**data) application.save() accept_application(application) mail.outbox = [] last_meeting = sg.meeting_set.active().order_by( 'meeting_date', 'meeting_time').last() self.assertEqual(last_meeting.meeting_date, datetime.date(2010, 2, 5)) self.assertEqual(last_meeting.meeting_time, datetime.time(18, 0)) self.assertEqual(sg.meeting_set.active().count(), 6) self.assertEqual(Reminder.objects.all().count(), 0) # send time is 7 days after the last meeting with freeze_time("2010-02-12 17:30:00"): send_final_learning_circle_report(sg) self.assertEqual(len(mail.outbox), 0) # freeze time to 2 hours after send time with freeze_time("2010-02-12 19:30:00"): send_final_learning_circle_report(sg) self.assertEqual(len(mail.outbox), 0) # freeze time to 30 minutes after send time with freeze_time("2010-02-12 18:30:00"): send_final_learning_circle_report(sg) self.assertIn(application.email, mail.outbox[0].bcc) self.assertIn(facilitator.email, mail.outbox[0].bcc) self.assertIn(organizer.email, mail.outbox[0].bcc) self.assertEqual(len(mail.outbox[0].bcc), 3) self.assertIn( '{0}/en/studygroup/{1}/report/'.format(settings.DOMAIN, sg.id), mail.outbox[0].body)