Passed
Push — master ( 8aa213...c2c2f1 )
by Piotr
01:15
created

ProviderAuthViewTestCase   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 68
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 6
c 2
b 0
f 0
dl 0
loc 68
rs 10
1
from django.contrib.sessions.middleware import SessionMiddleware
0 ignored issues
show
Bug introduced by
There seems to be a cyclic import (djoser.urls.base -> djoser.views).

Cyclic imports may cause partly loaded modules to be returned. This might lead to unexpected runtime behavior which is hard to debug.

Loading history...
Bug introduced by
There seems to be a cyclic import (djoser.urls.authtoken -> djoser.views).

Cyclic imports may cause partly loaded modules to be returned. This might lead to unexpected runtime behavior which is hard to debug.

Loading history...
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_fails_if_no_redirect_uri(self):
17
        request = self.factory.get()
18
        response = self.view(request, provider='facebook')
19
20
        self.assert_status_equal(response, status.HTTP_400_BAD_REQUEST)
21
22
    def test_get_facebook_provider_fails_if_wrong_redirect_uri(self):
23
        request = self.factory.get(data={'redirect_uri': 'http://yolo.com/'})
24
        response = self.view(request, provider='facebook')
25
26
        self.assert_status_equal(response, status.HTTP_400_BAD_REQUEST)
27
28
    def test_get_facebook_provider_provides_valid_authorization_url(self):
29
        request = self.factory.get(data={
30
            'redirect_uri': 'http://test.localhost/'
31
        })
32
        response = self.view(request, provider='facebook')
33
34
        self.assert_status_equal(response, status.HTTP_200_OK)
35
        self.assertIn('authorization_url', response.data)
36
37
    def test_post_facebook_provider_success_returns_token(self):
38
        data = {'code': 'XYZ', 'state': 'ABC'}
39
40
        mock.patch(
41
            'social_core.backends.facebook.FacebookOAuth2.auth_complete',
42
            return_value=create_user()
43
        ).start()
44
        mock.patch(
45
            'social_core.backends.oauth.OAuthAuth.get_session_state',
46
            return_value=data['state']
47
        ).start()
48
49
        request = self.factory.post()
50
        request.GET = {**data}
0 ignored issues
show
introduced by
invalid syntax (<string>, line 50)
Loading history...
51
        response = self.view(request, provider='facebook')
52
        self.assert_status_equal(response, status.HTTP_201_CREATED)
53
        self.assertEqual(set(response.data.keys()), {'token', 'user'})
54
55
    def test_post_facebook_provider_code_validation_fails(self):
56
        data = {'code': 'XYZ', 'state': 'ABC'}
57
58
        mock.patch(
59
            'social_core.backends.facebook.FacebookOAuth2.auth_complete',
60
            side_effect=AuthException(backend=None)
61
        ).start()
62
        mock.patch(
63
            'social_core.backends.oauth.OAuthAuth.get_session_state',
64
            return_value=data['state']
65
        ).start()
66
67
        request = self.factory.post()
68
        request.GET = {**data}
69
        response = self.view(request, provider='facebook')
70
        self.assert_status_equal(response, status.HTTP_400_BAD_REQUEST)
71
72
    def test_post_facebook_provider_validation_fails_if_invalid_state(self):
73
        data = {'code': 'XYZ', 'state': 'ABC'}
74
75
        mock.patch(
76
            'social_core.backends.oauth.OAuthAuth.get_session_state',
77
            return_value=data['state'][::-1]
78
        ).start()
79
80
        request = self.factory.post()
81
        request.GET = {**data}
82
        response = self.view(request, provider='facebook')
83
        self.assert_status_equal(response, status.HTTP_400_BAD_REQUEST)
84