Skip to content

Commit 6a70040

Browse files
fix unit tests
1 parent 409f5d5 commit 6a70040

File tree

5 files changed

+91
-47
lines changed

5 files changed

+91
-47
lines changed

api_tests/institutions/views/test_institution_auth.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616

1717
from osf.models import OSFUser, InstitutionAffiliation, InstitutionStorageRegion
1818
from osf.models.institution import SsoFilterCriteriaAction
19-
from osf_tests.factories import InstitutionFactory, ProjectFactory, UserFactory, RegionFactory
19+
from osf_tests.factories import InstitutionFactory, ProjectFactory, UserFactory, RegionFactory, NotificationTypeFactory
2020

2121
from tests.base import capture_signals
2222

@@ -183,8 +183,14 @@ def type_2_ineligible_user_roles():
183183

184184

185185
@pytest.mark.django_db
186+
@pytest.mark.usefixtures('mock_notification_send')
186187
class TestInstitutionAuth:
187188

189+
@pytest.fixture()
190+
def notification_type(self):
191+
NotificationTypeFactory(name='password_reset', notification_interval_choices=['instantly'])
192+
return
193+
188194
def test_invalid_payload(self, app, url_auth_institution):
189195
res = app.post(url_auth_institution, 'INVALID_PAYLOAD', expect_errors=True)
190196
assert res.status_code == 403
@@ -280,7 +286,7 @@ def test_new_user_names_used_when_provided(self, app, institution, url_auth_inst
280286
assert user.given_name == 'Foo'
281287
assert user.family_name == 'Bar'
282288

283-
def test_user_active(self, app, institution, url_auth_institution):
289+
def test_user_active(self, notification_type, mock_notification_send, app, institution, url_auth_institution):
284290

285291
username, fullname, password = '[email protected]', 'Foo Bar', 'FuAsKeEr'
286292
user = make_user(username, fullname)
@@ -399,7 +405,7 @@ def test_user_unconfirmed(self, app, institution, url_auth_institution):
399405
# Confirm affiliation
400406
assert institution in user.get_affiliated_institutions()
401407

402-
def test_user_inactive(self, app, institution, url_auth_institution):
408+
def test_user_inactive(self, notification_type, mock_notification_send, app, institution, url_auth_institution):
403409

404410
username, fullname, password = '[email protected]', 'Foo Bar', 'FuAsKeEr'
405411
user = make_user(username, fullname)

api_tests/users/views/test_user_external_login.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,22 @@
55
from api.base.settings.defaults import API_BASE
66
from api.base.settings import CSRF_COOKIE_NAME
77
from osf.models import OSFUser
8-
from osf_tests.factories import UserFactory
8+
from osf_tests.factories import UserFactory, NotificationTypeFactory
99
from importlib import import_module
1010
from django.conf import settings as django_conf_settings
1111

1212
SessionStore = import_module(django_conf_settings.SESSION_ENGINE).SessionStore
1313

1414

1515
@pytest.mark.django_db
16+
@pytest.mark.usefixtures('mock_notification_send')
1617
class TestExternalLogin:
1718

19+
@pytest.fixture()
20+
def notification_type(self):
21+
NotificationTypeFactory(name='password_reset', notification_interval_choices=['instantly'])
22+
return
23+
1824
@pytest.fixture()
1925
def user_one(self):
2026
user = UserFactory()
@@ -73,7 +79,7 @@ def test_invalid_payload(self, app, url, session_data, csrf_token):
7379
res = app.post_json_api(url, payload, expect_errors=True, headers={'X-CSRFToken': csrf_token})
7480
assert res.status_code == 400
7581

76-
def test_existing_user(self, app, payload, url, user_one, session_data, csrf_token):
82+
def test_existing_user(self, notification_type, mock_notification_send, app, payload, url, user_one, session_data, csrf_token):
7783
app.set_cookie(CSRF_COOKIE_NAME, csrf_token)
7884
app.set_cookie(settings.COOKIE_NAME, str(session_data))
7985
payload['data']['attributes']['email'] = user_one.username

api_tests/users/views/test_user_settings.py

Lines changed: 30 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -29,13 +29,20 @@ def unconfirmed_address():
2929

3030

3131
@pytest.mark.django_db
32+
@pytest.mark.usefixtures('mock_notification_send')
3233
@pytest.mark.usefixtures('mock_send_grid')
3334
class TestUserRequestExport:
3435

3536
@pytest.fixture()
3637
def url(self, user_one):
3738
return f'/{API_BASE}users/{user_one._id}/settings/export/'
3839

40+
@pytest.fixture()
41+
def notification_type(self):
42+
NotificationTypeFactory(name='forgot_password', notification_interval_choices=['instantly'])
43+
NotificationTypeFactory(name='password_reset', notification_interval_choices=['instantly'])
44+
return
45+
3946
@pytest.fixture()
4047
def payload(self):
4148
return {
@@ -45,11 +52,11 @@ def payload(self):
4552
}
4653
}
4754

48-
def test_get(self, app, user_one, url):
55+
def test_get(self, notification_type, app, user_one, url):
4956
res = app.get(url, auth=user_one.auth, expect_errors=True)
5057
assert res.status_code == 405
5158

52-
def test_post(self, mock_send_grid, app, user_one, user_two, url, payload):
59+
def test_post(self, notification_type, mock_send_grid, mock_notification_send, app, user_one, user_two, url, payload):
5360
# Logged out
5461
res = app.post_json_api(url, payload, expect_errors=True)
5562
assert res.status_code == 401
@@ -66,16 +73,16 @@ def test_post(self, mock_send_grid, app, user_one, user_two, url, payload):
6673
assert user_one.email_last_sent is not None
6774
assert mock_send_grid.call_count == 1
6875

69-
def test_post_invalid_type(self, mock_send_grid, app, user_one, url, payload):
76+
def test_post_invalid_type(self, notification_type, mock_notification_send, app, user_one, url, payload):
7077
assert user_one.email_last_sent is None
7178
payload['data']['type'] = 'Invalid Type'
7279
res = app.post_json_api(url, payload, auth=user_one.auth, expect_errors=True)
7380
assert res.status_code == 409
7481
user_one.reload()
7582
assert user_one.email_last_sent is None
76-
assert mock_send_grid.call_count == 0
83+
assert mock_notification_send.call_count == 0
7784

78-
def test_exceed_throttle(self, app, user_one, url, payload):
85+
def test_exceed_throttle(self, notification_type, app, user_one, url, payload):
7986
assert user_one.email_last_sent is None
8087
res = app.post_json_api(url, payload, auth=user_one.auth)
8188
assert res.status_code == 204
@@ -88,6 +95,7 @@ def test_exceed_throttle(self, app, user_one, url, payload):
8895

8996

9097
@pytest.mark.django_db
98+
@pytest.mark.usefixtures('mock_notification_send')
9199
class TestUserChangePassword:
92100

93101
@pytest.fixture()
@@ -98,6 +106,12 @@ def user_one(self):
98106
user.save()
99107
return user
100108

109+
@pytest.fixture()
110+
def notification_type(self):
111+
NotificationTypeFactory(name='forgot_password', notification_interval_choices=['instantly'])
112+
NotificationTypeFactory(name='password_reset', notification_interval_choices=['instantly'])
113+
return
114+
101115
@pytest.fixture()
102116
def url(self, user_one):
103117
return f'/{API_BASE}users/{user_one._id}/settings/password/'
@@ -115,11 +129,11 @@ def payload(self, user_one):
115129
}
116130
}
117131

118-
def test_get(self, app, user_one, url):
132+
def test_get(self, notification_type, app, user_one, url):
119133
res = app.get(url, auth=user_one.auth, expect_errors=True)
120134
assert res.status_code == 405
121135

122-
def test_post(self, app, user_one, user_two, url, payload):
136+
def test_post(self, notification_type, app, user_one, user_two, url, payload):
123137
# Logged out
124138
res = app.post_json_api(url, payload, expect_errors=True)
125139
assert res.status_code == 401
@@ -134,12 +148,12 @@ def test_post(self, app, user_one, user_two, url, payload):
134148
user_one.reload()
135149
assert user_one.check_password('password2')
136150

137-
def test_post_invalid_type(self, app, user_one, url, payload):
151+
def test_post_invalid_type(self, notification_type, app, user_one, url, payload):
138152
payload['data']['type'] = 'Invalid Type'
139153
res = app.post_json_api(url, payload, auth=user_one.auth, expect_errors=True)
140154
assert res.status_code == 409
141155

142-
def test_exceed_throttle_failed_attempts(self, app, user_one, url, payload):
156+
def test_exceed_throttle_failed_attempts(self, notification_type, app, user_one, url, payload):
143157
payload['data']['attributes']['existing_password'] = 'wrong password'
144158
payload['data']['attributes']['new_password'] = 'password2'
145159
res = app.post_json_api(url, payload, auth=user_one.auth, expect_errors=True)
@@ -159,7 +173,7 @@ def test_exceed_throttle_failed_attempts(self, app, user_one, url, payload):
159173
# Expected time is omitted to prevent probabilistic failures.
160174
assert 'Request was throttled. Expected available in ' in res.json['errors'][0]['detail']
161175

162-
def test_multiple_errors(self, app, user_one, url, payload):
176+
def test_multiple_errors(self, notification_type, app, user_one, url, payload):
163177
payload['data']['attributes']['existing_password'] = 'wrong password'
164178
payload['data']['attributes']['new_password'] = '!'
165179
res = app.post_json_api(url, payload, auth=user_one.auth, expect_errors=True)
@@ -203,13 +217,13 @@ def test_get(self, mock_notification_send, notification_type, app, url, user_one
203217
user_one.reload()
204218
assert mock_notification_send.called
205219

206-
def test_get_invalid_email(self, mock_notification_send, app, url, notification_type):
220+
def test_get_invalid_email(self, notification_type, mock_notification_send, app, url):
207221
url = f'{url}?email={'invalid_email'}'
208222
res = app.get(url)
209223
assert res.status_code == 200
210224
assert not mock_notification_send.called
211225

212-
def test_post(self, mock_notification_send, app, url, user_one, csrf_token, notification_type):
226+
def test_post(self, notification_type, mock_notification_send, app, url, user_one, csrf_token):
213227
app.set_cookie(CSRF_COOKIE_NAME, csrf_token)
214228
encoded_email = urllib.parse.quote(user_one.email)
215229
url = f'{url}?email={encoded_email}'
@@ -231,7 +245,7 @@ def test_post(self, mock_notification_send, app, url, user_one, csrf_token, noti
231245
assert user_one.check_password('password2')
232246
assert mock_notification_send.called
233247

234-
def test_post_empty_payload(self, app, url, csrf_token):
248+
def test_post_empty_payload(self, notification_type, app, url, csrf_token):
235249
app.set_cookie(CSRF_COOKIE_NAME, csrf_token)
236250
payload = {
237251
'data': {
@@ -242,7 +256,7 @@ def test_post_empty_payload(self, app, url, csrf_token):
242256
res = app.post_json_api(url, payload, expect_errors=True, headers={'X-CSRFToken': csrf_token})
243257
assert res.status_code == 400
244258

245-
def test_post_invalid_token(self, app, url, user_one, csrf_token):
259+
def test_post_invalid_token(self, notification_type, app, url, user_one, csrf_token):
246260
app.set_cookie(CSRF_COOKIE_NAME, csrf_token)
247261
payload = {
248262
'data': {
@@ -256,7 +270,7 @@ def test_post_invalid_token(self, app, url, user_one, csrf_token):
256270
res = app.post_json_api(url, payload, expect_errors=True, headers={'X-THROTTLE-TOKEN': 'test-token', 'X-CSRFToken': csrf_token})
257271
assert res.status_code == 400
258272

259-
def test_post_invalid_password(self, app, url, user_one, csrf_token, notification_type):
273+
def test_post_invalid_password(self, notification_type, app, url, user_one, csrf_token):
260274
app.set_cookie(CSRF_COOKIE_NAME, csrf_token)
261275
encoded_email = urllib.parse.quote(user_one.email)
262276
url = f'{url}?email={encoded_email}'
@@ -275,7 +289,7 @@ def test_post_invalid_password(self, app, url, user_one, csrf_token, notificatio
275289
res = app.post_json_api(url, payload, expect_errors=True, headers={'X-THROTTLE-TOKEN': 'test-token', 'X-CSRFToken': csrf_token})
276290
assert res.status_code == 400
277291

278-
def test_throrrle(self, app, url, user_one, notification_type):
292+
def test_throrrle(self, notification_type, app, url, user_one):
279293
encoded_email = urllib.parse.quote(user_one.email)
280294
url = f'{url}?email={encoded_email}'
281295
res = app.get(url)

osf_tests/test_auth_utils.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,24 @@
22

33
from framework.auth.core import get_user
44

5-
from .factories import UserFactory
5+
from .factories import UserFactory, NotificationTypeFactory
66

77
# from tests/test_auth
88
@pytest.mark.django_db
9+
@pytest.mark.usefixtures('mock_notification_send')
910
class TestGetUser:
1011

12+
@pytest.fixture()
13+
def notification_type(self):
14+
NotificationTypeFactory(name='password_reset', notification_interval_choices=['instantly'])
15+
return
16+
1117
def test_get_user_by_email(self):
1218
user = UserFactory()
1319
assert get_user(email=user.username) == user
1420
assert get_user(email=user.username.upper()) == user
1521

16-
def test_get_user_with_wrong_password_returns_false(self):
22+
def test_get_user_with_wrong_password_returns_false(self, mock_notification_send, notification_type):
1723
user = UserFactory.build()
1824
user.set_password('killerqueen')
1925
assert bool(

0 commit comments

Comments
 (0)