Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test cover authentication with falsy email, fixes #141 #142

Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 28 additions & 1 deletion tests/test_auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -201,9 +201,11 @@ def test_successful_authentication_new_user(self, token_mock, request_mock, algo
'redirect_uri': 'http://site-url.com/callback/',
}
self.assertEqual(User.objects.all().count(), 0)
self.backend.authenticate(request=auth_request)
result = self.backend.authenticate(request=auth_request)
self.assertEqual(User.objects.all().count(), 1)
user = User.objects.all()[0]
self.assertEquals(user, result)

self.assertEquals(user.email, '[email protected]')
self.assertEquals(user.username, 'username_algo')

Expand All @@ -216,6 +218,31 @@ def test_successful_authentication_new_user(self, token_mock, request_mock, algo
headers={'Authorization': 'Bearer access_granted'}
)

@patch('mozilla_django_oidc.auth.requests')
@patch('mozilla_django_oidc.auth.OIDCAuthenticationBackend.verify_token')
def test_successful_authentication_no_email(self, token_mock, request_mock):
"""What happens if the auth "works" but it doesn't have an email?"""
auth_request = RequestFactory().get('/foo', {'code': 'foo',
'state': 'bar'})
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Single line please

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's a a repeated pattern.

auth_request.session = {}

token_mock.return_value = True
get_json_mock = Mock()
get_json_mock.json.return_value = {
'nickname': 'a_username',
'email': ''
}
request_mock.get.return_value = get_json_mock
post_json_mock = Mock()
post_json_mock.json.return_value = {
'id_token': 'id_token',
'access_token': 'access_granted'
}
request_mock.post.return_value = post_json_mock
result = self.backend.authenticate(request=auth_request)
assert result is None
self.assertEqual(User.objects.all().count(), 0)

def test_authenticate_no_code_no_state(self):
"""Test authenticate with wrong parameters."""

Expand Down