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)))
示例#2
0
 def _validate_and_get_redirect_uri(cls, email, code):
     """
     Validates an code and returns a URI to redirect to, if the code is valid.
     :param email: The email address associated with the auth code.
     :param code: The code associated with the auth code.
     :return: A URI to redirect to if the code is valid, otherwise None.
     """
     auth_code = AuthCode.get_auth_code(email, code)
     if not auth_code:
         return None
     auth_code.delete()
     return auth_code.next_page or "/"
示例#3
0
    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_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, ""))
 def test_generate_code_creates_code_of_length_defined_in_settings(self):
     code = AuthCode._create_code_for_user(self.u).code
     assert_that(len(code), equal_to(19))
 def test_generate_code_creates_code_of_default_length(self):
     code = AuthCode._create_code_for_user(self.u).code
     assert_that(len(code), equal_to(DEFAULT_CODE_LENGTH))
 def test_create_code_for_user_creates_new_code(self):
     expected_auth_code = AuthCode._create_code_for_user(self.u)
     actual_auth_code = AuthCode.objects.get(user=self.u,
                                             code=expected_auth_code.code)
     assert_that(expected_auth_code.code, equal_to(actual_auth_code.code))
 def test_create_code_for_user_returns_none_for_inactive_user(self):
     self.u.is_active = False
     self.u.save()
     assert_that(AuthCode._create_code_for_user(self.u), none())