-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
32 changed files
with
40 additions
and
134 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
from django.contrib import admin | ||
from django.contrib import admin # noqa: F401 | ||
|
||
# Register your models here. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,6 +2,7 @@ | |
from django.contrib.auth import get_user_model | ||
from accounts.authentication import PasswordlessAuthenticationBackend | ||
from accounts.models import Token | ||
|
||
User = get_user_model() | ||
|
||
|
||
|
@@ -13,15 +14,13 @@ def test_returns_None_if_no_such_token(self): | |
) | ||
self.assertIsNone(result) | ||
|
||
|
||
def test_returns_new_user_with_correct_email_if_token_exists(self): | ||
email = '[email protected]' | ||
token = Token.objects.create(email=email) | ||
user = PasswordlessAuthenticationBackend().authenticate(token.uid) | ||
new_user = User.objects.get(email=email) | ||
self.assertEqual(user, new_user) | ||
|
||
|
||
def test_returns_existing_user_with_correct_email_if_token_exists(self): | ||
email = '[email protected]' | ||
existing_user = User.objects.create(email=email) | ||
|
@@ -30,7 +29,6 @@ def test_returns_existing_user_with_correct_email_if_token_exists(self): | |
self.assertEqual(user, existing_user) | ||
|
||
|
||
|
||
class GetUserTest(TestCase): | ||
|
||
def test_gets_user_by_email(self): | ||
|
@@ -41,9 +39,7 @@ def test_gets_user_by_email(self): | |
) | ||
self.assertEqual(found_user, desired_user) | ||
|
||
|
||
def test_returns_None_if_no_user_with_that_email(self): | ||
self.assertIsNone( | ||
PasswordlessAuthenticationBackend().get_user('[email protected]') | ||
) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,7 @@ | ||
from django.test import TestCase | ||
from django.contrib import auth | ||
from accounts.models import Token | ||
|
||
User = auth.get_user_model() | ||
|
||
|
||
|
@@ -10,19 +11,16 @@ def test_user_is_valid_with_email_only(self): | |
user = User(email='[email protected]') | ||
user.full_clean() # should not raise | ||
|
||
|
||
def test_no_problem_with_auth_login(self): | ||
user = User.objects.create(email='[email protected]') | ||
user.backend = '' | ||
request = self.client.request().wsgi_request | ||
auth.login(request, user) # should not raise | ||
|
||
|
||
|
||
class TokenModelTest(TestCase): | ||
|
||
def test_links_user_with_auto_generated_uid(self): | ||
token1 = Token.objects.create(email='[email protected]') | ||
token2 = Token.objects.create(email='[email protected]') | ||
self.assertNotEqual(token1.uid, token2.uid) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,7 +11,6 @@ def test_redirects_to_home_page(self): | |
}) | ||
self.assertRedirects(response, '/') | ||
|
||
|
||
def test_adds_success_message(self): | ||
response = self.client.post('/accounts/send_login_email', data={ | ||
'email': '[email protected]' | ||
|
@@ -24,7 +23,6 @@ def test_adds_success_message(self): | |
) | ||
self.assertEqual(message.tags, "success") | ||
|
||
|
||
@patch('accounts.views.send_mail') | ||
def test_sends_mail_to_address_from_post(self, mock_send_mail): | ||
self.client.post('/accounts/send_login_email', data={ | ||
|
@@ -37,15 +35,13 @@ def test_sends_mail_to_address_from_post(self, mock_send_mail): | |
self.assertEqual(from_email, 'noreply@superlists') | ||
self.assertEqual(to_list, ['[email protected]']) | ||
|
||
|
||
def test_creates_token_associated_with_email(self): | ||
self.client.post('/accounts/send_login_email', data={ | ||
'email': '[email protected]' | ||
}) | ||
token = Token.objects.first() | ||
self.assertEqual(token.email, '[email protected]') | ||
|
||
|
||
@patch('accounts.views.send_mail') | ||
def test_sends_link_to_login_using_token_uid(self, mock_send_mail): | ||
self.client.post('/accounts/send_login_email', data={ | ||
|
@@ -60,33 +56,28 @@ def test_sends_link_to_login_using_token_uid(self, mock_send_mail): | |
self.assertIn(expected_url, body) | ||
|
||
|
||
|
||
@patch('accounts.views.auth') | ||
class LoginViewTest(TestCase): | ||
|
||
def test_redirects_to_home_page(self, mock_auth): | ||
response = self.client.get('/accounts/login?token=abcd123') | ||
self.assertRedirects(response, '/') | ||
|
||
|
||
def test_calls_authenticate_with_uid_from_get_request(self, mock_auth): | ||
self.client.get('/accounts/login?token=abcd123') | ||
self.assertEqual( | ||
mock_auth.authenticate.call_args, | ||
call(uid='abcd123') | ||
) | ||
|
||
|
||
def test_calls_auth_login_with_user_if_there_is_one(self, mock_auth): | ||
response = self.client.get('/accounts/login?token=abcd123') | ||
self.assertEqual( | ||
mock_auth.login.call_args, | ||
call(response.wsgi_request, mock_auth.authenticate.return_value) | ||
) | ||
|
||
|
||
def test_does_not_login_if_user_is_not_authenticated(self, mock_auth): | ||
mock_auth.authenticate.return_value = None | ||
self.client.get('/accounts/login?token=abcd123') | ||
self.assertEqual(mock_auth.login.called, False) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -31,4 +31,3 @@ def login(request): | |
if user: | ||
auth.login(request, user) | ||
return redirect('/') | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.