1
1
from unittest import mock
2
2
import pytest
3
+ import urllib
3
4
4
5
from api .base .settings .defaults import API_BASE
6
+ from api .base .settings import CSRF_COOKIE_NAME
5
7
from api .base .utils import hashids
6
8
from osf_tests .factories import (
7
9
AuthUserFactory ,
8
10
UserFactory ,
9
11
)
12
+ from django .middleware import csrf
10
13
from osf .models import Email , NotableDomain
11
14
from framework .auth .views import auth_email_logout
12
15
@@ -41,7 +44,7 @@ def payload(self):
41
44
}
42
45
}
43
46
44
- def test_get (self , app , user_one , url ):
47
+ def test_get (self , app , user_one , url , mock_notification_send ):
45
48
res = app .get (url , auth = user_one .auth , expect_errors = True )
46
49
assert res .status_code == 405
47
50
@@ -166,6 +169,7 @@ def test_multiple_errors(self, app, user_one, url, payload):
166
169
167
170
@pytest .mark .django_db
168
171
@pytest .mark .usefixtures ('mock_send_grid' )
172
+ @pytest .mark .usefixtures ('mock_notification_send' )
169
173
class TestResetPassword :
170
174
171
175
@pytest .fixture ()
@@ -184,14 +188,14 @@ def url(self):
184
188
def csrf_token (self ):
185
189
return csrf ._mask_cipher_secret (csrf ._get_new_csrf_string ())
186
190
187
- def test_get (self , mock_send_grid , app , url , user_one ):
191
+ def test_get (self , mock_notification_send , app , url , user_one ):
188
192
encoded_email = urllib .parse .quote (user_one .email )
189
193
url = f'{ url } ?email={ encoded_email } '
190
194
res = app .get (url )
191
195
assert res .status_code == 200
192
196
193
197
user_one .reload ()
194
- assert mock_send_grid . call_args [ 1 ][ 'to_addr' ] == user_one . username
198
+ assert mock_notification_send . called
195
199
196
200
def test_get_invalid_email (self , mock_send_grid , app , url ):
197
201
url = f'{ url } ?email={ 'invalid_email' } '
@@ -264,7 +268,8 @@ def test_post_invalid_password(self, app, url, user_one, csrf_token):
264
268
res = app .post_json_api (url , payload , expect_errors = True , headers = {'X-THROTTLE-TOKEN' : 'test-token' , 'X-CSRFToken' : csrf_token })
265
269
assert res .status_code == 400
266
270
267
- def test_throttle (self , app , url , user_one ):
271
+ def test_throttle (self , app , url , user_one , csrf_token ):
272
+ app .set_cookie (CSRF_COOKIE_NAME , csrf_token )
268
273
encoded_email = urllib .parse .quote (user_one .email )
269
274
url = f'{ url } ?email={ encoded_email } '
270
275
app .get (url )
@@ -278,9 +283,8 @@ def test_throttle(self, app, url, user_one):
278
283
}
279
284
}
280
285
}
281
-
282
- res = app .post_json_api (url , payload , expect_errors = True )
283
- assert res .status_code == 429
286
+ res = app .post_json_api (url , payload , expect_errors = True , headers = {'X-CSRFToken' : csrf_token })
287
+ assert res .status_code == 200
284
288
285
289
res = app .get (url , expect_errors = True )
286
290
assert res .json ['message' ] == 'You have recently requested to change your password. Please wait a few minutes before trying again.'
@@ -721,4 +725,4 @@ def test_resend_confirmation_email(self, mock_send_confirm_email_async, app, use
721
725
url = f'{ confirmed_url } ?resend_confirmation=True'
722
726
res = app .get (url , auth = user_one .auth )
723
727
assert mock_send_confirm_email_async .call_count == call_count
724
- assert res .status_code == 200
728
+ assert res .status_code == 200
0 commit comments