def form_valid(self, form): email = form.cleaned_data["email"] district = form.cleaned_data["district"] user = self.get_user(email) if not user: user = self.create_user(email, district) if AuthCode.send_auth_code(user): self.success_url += f"?email={user.username}" return super().form_valid(form) else: form.add_error( None, _("Please check your inbox and spam folder for a previously-sent code." ), ) return super().form_invalid(form)
def test_send_auth_code(self, m_create_code_for_user): auth_code = AuthCode(code="123", user=self.u) m_create_code_for_user.return_value = auth_code self.assertTrue( AuthCode.send_auth_code(self.u, "http://site/code?code=123")) self.assertEqual(len(mail.outbox), 1) self.assertEqual(mail.outbox[0].subject, _(f"Your {settings.SITE_NAME} log in code")) context = { "code": auth_code.code, "code_uri": "http://site/code?code=123", "email": auth_code.user.email, "site_name": settings.SITE_NAME, } assert_that(mail.outbox[0].body, equal_to(render_to_string("log-in-email.txt", context))) assert_that(mail.outbox[0].alternatives[0][0], equal_to(render_to_string("log-in-email.html", context)))
def test_send_auth_code_returns_false_when_auth_code_not_created( self, m_create_code_for_user): m_create_code_for_user.return_value = False self.assertFalse(AuthCode.send_auth_code(self.u, ""))