3
3
from oauthadmin .views import destroy_session , login , callback , logout
4
4
from oauthlib .oauth2 .rfc6749 .errors import MismatchingStateError , InvalidGrantError
5
5
from django .test .client import RequestFactory
6
+ from django .core .urlresolvers import reverse
6
7
7
8
8
9
SESSION_VARIABLES = ['oauth_state' , 'oauth_token' , 'uid' , 'user' ]
@@ -40,16 +41,69 @@ def test_login(app_setting, OAuth2Session, request_factory):
40
41
OAuth2Session .return_value = mock .Mock (
41
42
authorization_url = mock .Mock (return_value = ('https://foo' , 'state-variable' ))
42
43
)
43
- request = request_factory .post ( '/' )
44
+ request = request_factory .get ( reverse ( 'oauthadmin.views.login' ) )
44
45
request .session = {}
45
46
request .build_absolute_uri = mock .Mock (return_value = 'https://test.com/construct-redirect' )
46
47
47
48
app_setting .return_value = 'app-setting'
48
49
49
50
resp = login (request )
50
51
assert resp .status_code == 302
52
+ assert resp ['location' ] == 'https://foo'
51
53
assert request .session .get ('oauth_state' ) == 'state-variable'
52
54
55
+ @mock .patch ('oauthadmin.views.OAuth2Session' )
56
+ def test_login_redirect_uri (OAuth2Session , request_factory ):
57
+ OAuth2Session .return_value = mock .Mock (
58
+ authorization_url = mock .Mock (return_value = ('https://foo' , 'state-variable' ))
59
+ )
60
+ request = request_factory .get (reverse ('oauthadmin.views.login' ))
61
+ request .session = {}
62
+ request .build_absolute_uri = mock .Mock (return_value = 'https://test.com/construct-redirect' )
63
+
64
+ resp = login (request )
65
+
66
+ OAuth2Session .assert_called_once_with (
67
+ client_id = 'test-client-id' ,
68
+ redirect_uri = u'https://test.com/construct-redirect' ,
69
+ scope = ['default' ],
70
+ )
71
+
72
+ @mock .patch ('oauthadmin.views.OAuth2Session' )
73
+ def test_login_redirect_uri_with_next_from_url (OAuth2Session , request_factory ):
74
+ OAuth2Session .return_value = mock .Mock (
75
+ authorization_url = mock .Mock (return_value = ('https://foo' , 'state-variable' ))
76
+ )
77
+ request = request_factory .get (reverse ('oauthadmin.views.login' ) + '?next=/admin/content/' )
78
+ request .session = {}
79
+ request .build_absolute_uri = mock .Mock (return_value = 'https://test.com/construct-redirect' )
80
+
81
+ resp = login (request )
82
+
83
+ OAuth2Session .assert_called_once_with (
84
+ redirect_uri = u'https://test.com/construct-redirect?next=/admin/content/' ,
85
+ client_id = mock .ANY ,
86
+ scope = mock .ANY ,
87
+ )
88
+
89
+ @mock .patch ('oauthadmin.views.OAuth2Session' )
90
+ def test_login_redirect_uri_with_next_as_current_url (OAuth2Session , request_factory ):
91
+ OAuth2Session .return_value = mock .Mock (
92
+ authorization_url = mock .Mock (return_value = ('https://foo' , 'state-variable' ))
93
+ )
94
+ request = request_factory .get ('/admin/content/' )
95
+ request .session = {}
96
+ request .build_absolute_uri = mock .Mock (return_value = 'https://test.com/construct-redirect' )
97
+
98
+ resp = login (request )
99
+
100
+ OAuth2Session .assert_called_once_with (
101
+ redirect_uri = u'https://test.com/construct-redirect?next=/admin/content/' ,
102
+ client_id = mock .ANY ,
103
+ scope = mock .ANY ,
104
+ )
105
+
106
+
53
107
@mock .patch ('oauthadmin.views.OAuth2Session' )
54
108
@mock .patch ('oauthadmin.views.app_setting' )
55
109
@mock .patch ('oauthadmin.views.import_by_path' )
@@ -89,7 +143,7 @@ def test_callback_with_invalid_grant(import_by_path, app_setting, OAuth2Session,
89
143
@mock .patch ('oauthadmin.views.app_setting' )
90
144
@mock .patch ('oauthadmin.views.import_by_path' )
91
145
def test_callback (import_by_path , app_setting , OAuth2Session , request_factory ):
92
- request = request_factory .get ('/' )
146
+ request = request_factory .get (reverse ( 'oauthadmin.views.callback' ) )
93
147
request .session = {'oauth_state' : 'state-variable' }
94
148
OAuth2Session .return_value = mock .Mock (
95
149
fetch_token = mock .Mock (return_value = 'token' )
@@ -101,9 +155,28 @@ def test_callback(import_by_path, app_setting, OAuth2Session, request_factory):
101
155
102
156
resp = callback (request )
103
157
assert resp .status_code == 302
158
+ assert resp ['location' ] == 'http://testserver/admin'
104
159
assert request .session .get ('oauth_token' ) == 'token'
105
160
assert request .session .get ('user' ) == 'test-user'
106
161
162
+ @mock .patch ('oauthadmin.views.OAuth2Session' )
163
+ @mock .patch ('oauthadmin.views.app_setting' )
164
+ @mock .patch ('oauthadmin.views.import_by_path' )
165
+ def test_callback_redirect_to_next (import_by_path , app_setting , OAuth2Session , request_factory ):
166
+ request = request_factory .get (reverse ('oauthadmin.views.callback' ) + '?next=/admin/content/' )
167
+ request .session = {'oauth_state' : 'state-variable' }
168
+ OAuth2Session .return_value = mock .Mock (
169
+ fetch_token = mock .Mock (return_value = 'token' )
170
+ )
171
+ app_setting .return_value = 'app-setting'
172
+ ibp = mock .Mock ()
173
+ ibp .return_value = 'test-user'
174
+ import_by_path .return_value = ibp
175
+
176
+ resp = callback (request )
177
+ assert resp .status_code == 302
178
+ assert resp ['location' ] == 'http://testserver/admin/content/'
179
+
107
180
@mock .patch ('oauthadmin.views.OAuth2Session' )
108
181
@mock .patch ('oauthadmin.views.app_setting' )
109
182
def test_logout (app_setting , OAuth2Session , request_factory ):
0 commit comments