1
|
|
|
from django.contrib.sessions.middleware import SessionMiddleware |
|
|
|
|
2
|
|
|
from rest_framework import status |
|
|
|
|
3
|
|
|
|
4
|
|
|
from djet import assertions, restframework |
|
|
|
|
5
|
|
|
import djoser.social.views |
6
|
|
|
from social_core.exceptions import AuthException |
|
|
|
|
7
|
|
|
|
8
|
|
|
from ..common import create_user, mock |
9
|
|
|
|
10
|
|
|
|
11
|
|
|
class ProviderAuthViewTestCase(restframework.APIViewTestCase, |
|
|
|
|
12
|
|
|
assertions.StatusCodeAssertionsMixin): |
13
|
|
|
view_class = djoser.social.views.ProviderAuthView |
14
|
|
|
middleware = [SessionMiddleware] |
15
|
|
|
|
16
|
|
|
def test_get_facebook_provider_provides_valid_authorization_url(self): |
|
|
|
|
17
|
|
|
request = self.factory.get() |
18
|
|
|
response = self.view(request, provider='facebook') |
19
|
|
|
|
20
|
|
|
self.assert_status_equal(response, status.HTTP_200_OK) |
21
|
|
|
self.assertIn('authorization_url', response.data) |
22
|
|
|
|
23
|
|
|
def test_post_facebook_provider_success_returns_token(self): |
|
|
|
|
24
|
|
|
data = {'code': 'XYZ', 'state': 'ABC'} |
25
|
|
|
|
26
|
|
|
mock.patch( |
27
|
|
|
'social_core.backends.facebook.FacebookOAuth2.auth_complete', |
28
|
|
|
return_value=create_user() |
29
|
|
|
).start() |
30
|
|
|
mock.patch( |
31
|
|
|
'social_core.backends.oauth.OAuthAuth.get_session_state', |
32
|
|
|
return_value=data['state'] |
33
|
|
|
).start() |
34
|
|
|
|
35
|
|
|
request = self.factory.post(data=data) |
36
|
|
|
response = self.view(request, provider='facebook') |
37
|
|
|
self.assert_status_equal(response, status.HTTP_201_CREATED) |
38
|
|
|
self.assertEqual(set(response.data.keys()), {'token', 'user'}) |
39
|
|
|
|
40
|
|
|
def test_post_facebook_provider_code_validation_fails(self): |
|
|
|
|
41
|
|
|
data = {'code': 'XYZ', 'state': 'ABC'} |
42
|
|
|
|
43
|
|
|
mock.patch( |
44
|
|
|
'social_core.backends.facebook.FacebookOAuth2.auth_complete', |
45
|
|
|
side_effect=AuthException(backend=None) |
46
|
|
|
).start() |
47
|
|
|
mock.patch( |
48
|
|
|
'social_core.backends.oauth.OAuthAuth.get_session_state', |
49
|
|
|
return_value=data['state'] |
50
|
|
|
).start() |
51
|
|
|
|
52
|
|
|
request = self.factory.post(data=data) |
53
|
|
|
response = self.view(request, provider='facebook') |
54
|
|
|
self.assert_status_equal(response, status.HTTP_400_BAD_REQUEST) |
55
|
|
|
|
56
|
|
|
def test_post_facebook_provider_validation_fails_if_invalid_state(self): |
|
|
|
|
57
|
|
|
data = {'code': 'XYZ', 'state': 'ABC'} |
58
|
|
|
|
59
|
|
|
mock.patch( |
60
|
|
|
'social_core.backends.oauth.OAuthAuth.get_session_state', |
61
|
|
|
return_value=data['state'][::-1] |
62
|
|
|
).start() |
63
|
|
|
|
64
|
|
|
request = self.factory.post(data=data) |
65
|
|
|
response = self.view(request, provider='facebook') |
66
|
|
|
self.assert_status_equal(response, status.HTTP_400_BAD_REQUEST) |
67
|
|
|
|
The coding style of this project requires that you add a docstring to this code element. Below, you find an example for methods:
If you would like to know more about docstrings, we recommend to read PEP-257: Docstring Conventions.