def test_user_delete_authorizations(self): """ Verify that deleted user authorizations are expired and contain no user links """ delete_url = reverse("users:delete_data", kwargs={"pk": self.user_editor.pk}) # Need a password so we can login self.user_editor.set_password("editor") self.user_editor.save() self.client = Client() session = self.client.session self.client.login(username=self.username1, password="******") partner = PartnerFactory() user_auth = Authorization( user=self.user_editor, partner=partner, date_authorized=date.today(), date_expires=date.today() + timedelta(days=30), ) user_auth.save() submit = self.client.post(delete_url) user_auth.refresh_from_db() self.assertEqual(user_auth.date_expires, date.today() - timedelta(days=1))
class UserRenewalNoticeTest(TestCase): def setUp(self): super(UserRenewalNoticeTest, self).setUp() editor = EditorFactory(user__email="*****@*****.**") self.user = editor.user self.coordinator = EditorFactory().user coordinators = get_coordinators() coordinators.user_set.add(self.coordinator) self.partner = PartnerFactory() self.authorization = Authorization() self.authorization.user = self.user self.authorization.authorizer = self.coordinator self.authorization.date_expires = datetime.today() + timedelta(weeks=1) self.authorization.save() self.authorization.partners.add(self.partner) def test_single_user_renewal_notice(self): """ Given one authorization that expires in two weeks, ensure that our email task sends an email to that user. """ call_command("user_renewal_notice") self.assertEqual(len(mail.outbox), 1) self.assertEqual(mail.outbox[0].to, [self.user.email]) def test_user_renewal_notice_disabled(self): """ Users have the option to disable renewal notices. If users have disabled emails, we shouldn't send them one. """ self.user.userprofile.send_renewal_notices = False self.user.userprofile.save() call_command("user_renewal_notice") self.assertEqual(len(mail.outbox), 0) def test_user_renewal_notice_doesnt_duplicate(self): """ If we run the command a second time, the same user shouldn't receive a second email. """ call_command("user_renewal_notice") self.assertEqual(len(mail.outbox), 1) call_command("user_renewal_notice") self.assertEqual(len(mail.outbox), 1) def test_user_renewal_notice_past_date(self): """ If the authorization expired before today, the user shouldn't receive a notice. """ self.authorization.date_expires = datetime.today() - timedelta(weeks=1) self.authorization.save() call_command("user_renewal_notice") self.assertEqual(len(mail.outbox), 0) def test_user_renewal_notice_future_date(self): """ If the authorization doesn't expire for months, the user shouldn't receive a notice. """ self.authorization.date_expires = datetime.today() + timedelta(weeks=8) self.authorization.save() call_command("user_renewal_notice") self.assertEqual(len(mail.outbox), 0) def test_user_renewal_notice_future_date_1(self): """ If we have multiple authorizations to send emails for, let's make sure we send distinct emails to the right places. """ editor2 = EditorFactory(user__email="*****@*****.**") authorization2 = Authorization() authorization2.user = editor2.user authorization2.authorizer = self.coordinator authorization2.date_expires = datetime.today() + timedelta(weeks=1) authorization2.save() authorization2.partners.add(self.partner) call_command("user_renewal_notice") self.assertEqual(len(mail.outbox), 2) # Make sure that the two emails went to the two expected # email addresses. # This looks a little complicated because mail.outbox[0].to is a # (one element) list, and we need to compare sets to ensure we've # got 1 of each email. self.assertEqual( {mail.outbox[0].to[0], mail.outbox[1].to[0]}, {"*****@*****.**", "*****@*****.**"}, ) def test_user_renewal_notice_after_renewal(self): """ If a user renews their authorization, we want to remind them again when it runs out. """ call_command("user_renewal_notice") self.assertEqual(len(mail.outbox), 1) self.authorization.refresh_from_db() self.assertTrue(self.authorization.reminder_email_sent) # We already have an authorization, so let's setup up # an application that 'corresponds' to it. application = ApplicationFactory( editor=self.user.editor, sent_by=self.coordinator, partner=self.partner, status=Application.SENT, requested_access_duration=1, ) application.save() # File a renewal, approve it, and send it. self.partner.renewals_available = True self.partner.save() renewed_app = application.renew() renewed_app.status = application.APPROVED renewed_app.save() renewed_app.status = application.SENT renewed_app.sent_by = self.coordinator renewed_app.save() # Sending this renewal notice will have sent the user # an email, so we expect 2 emails now. self.assertEqual(len(mail.outbox), 2) # We've correctly marked reminder_email_sent as False self.authorization.refresh_from_db() self.assertFalse(self.authorization.reminder_email_sent) # And calling the command should send a third email. call_command("user_renewal_notice") self.assertEqual(len(mail.outbox), 3)