Skip to content

Commit 70b4da3

Browse files
WIP
1 parent 7068a28 commit 70b4da3

File tree

10 files changed

+120
-75
lines changed

10 files changed

+120
-75
lines changed

tests/common/constants.py

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
# Pytest Fixture Constants
2+
3+
REMOTE_ADDR = "1.2.3.4"
4+
5+
REMOTE_ADDR_HASHED = "6694f83c9f476da31f5df6bcc520034e7e57d421d247b9d34f49edbfc84a764c"
6+
7+
REMOTE_ADDR_SALTED = "a69a49383d81404e4b1df297c7baa28e1cd6c4ee1495ed5d0ab165a63a147763"
8+
9+
DUMMY_GITHUB_OIDC_JWT = (
10+
"eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJqdGkiOiI2ZTY3YjFjYi0yYjhkLTRiZ"
11+
"TUtOTFjYi03NTdlZGIyZWM5NzAiLCJzdWIiOiJyZXBvOmZvby9iYXIiLCJhdWQiOiJweXB"
12+
"pIiwicmVmIjoiZmFrZSIsInNoYSI6ImZha2UiLCJyZXBvc2l0b3J5IjoiZm9vL2JhciIsI"
13+
"nJlcG9zaXRvcnlfb3duZXIiOiJmb28iLCJyZXBvc2l0b3J5X293bmVyX2lkIjoiMTIzIiw"
14+
"icnVuX2lkIjoiZmFrZSIsInJ1bl9udW1iZXIiOiJmYWtlIiwicnVuX2F0dGVtcHQiOiIxI"
15+
"iwicmVwb3NpdG9yeV9pZCI6ImZha2UiLCJhY3Rvcl9pZCI6ImZha2UiLCJhY3RvciI6ImZ"
16+
"vbyIsIndvcmtmbG93IjoiZmFrZSIsImhlYWRfcmVmIjoiZmFrZSIsImJhc2VfcmVmIjoiZ"
17+
"mFrZSIsImV2ZW50X25hbWUiOiJmYWtlIiwicmVmX3R5cGUiOiJmYWtlIiwiZW52aXJvbm1"
18+
"lbnQiOiJmYWtlIiwiam9iX3dvcmtmbG93X3JlZiI6ImZvby9iYXIvLmdpdGh1Yi93b3JrZ"
19+
"mxvd3MvZX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C9vaWRjIiwibmJmIjoxNjUwNj"
37+
"YzMjY1LCJleHAiOjE2NTA2NjQxNjUsImlhdCI6MTY1MDY2Mzg2NX0.R4q-vWAFXHrBSBK"
38+
"AZuHHIsGOkqlirPxEtLfjLIDiLr0"
39+
)

tests/conftest.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,8 @@
6969
from .common.db.accounts import EmailFactory, UserFactory
7070
from .common.db.ip_addresses import IpAddressFactory
7171

72+
from common.constants import REMOTE_ADDR, REMOTE_ADDR_HASHED, REMOTE_ADDR_SALTED
73+
7274
_HERE = Path(__file__).parent.resolve()
7375
_FIXTURES = _HERE / "_fixtures"
7476

@@ -216,8 +218,8 @@ def pyramid_request(pyramid_services, jinja, remote_addr, remote_addr_hashed):
216218
pyramid.testing.setUp()
217219
dummy_request = pyramid.testing.DummyRequest()
218220
dummy_request.find_service = pyramid_services.find_service
219-
dummy_request.remote_addr = remote_addr
220-
dummy_request.remote_addr_hashed = remote_addr_hashed
221+
dummy_request.remote_addr = REMOTE_ADDR
222+
dummy_request.remote_addr_hashed = REMOTE_ADDR_HASHED
221223
dummy_request.authentication_method = pretend.stub()
222224
dummy_request._unauthenticated_userid = None
223225
dummy_request.user = None
@@ -438,7 +440,7 @@ def db_session(app_config):
438440
@pytest.fixture
439441
def user_service(db_session, metrics, remote_addr):
440442
return account_services.DatabaseUserService(
441-
db_session, metrics=metrics, remote_addr=remote_addr
443+
db_session, metrics=metrics, remote_addr=REMOTE_ADDR
442444
)
443445

444446

@@ -768,7 +770,7 @@ def webtest(app_config_dbsession_from_env, remote_addr, tm):
768770
"warehouse.db_session": _db_session,
769771
"tm.active": True, # disable pyramid_tm
770772
"tm.manager": tm, # pass in our own tm for the app to use
771-
"REMOTE_ADDR": remote_addr, # set the same address for all requests
773+
"REMOTE_ADDR": REMOTE_ADDR, # set the same address for all requests
772774
},
773775
)
774776
yield testapp

tests/unit/accounts/test_forms.py

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -33,12 +33,12 @@
3333
from warehouse.captcha import recaptcha
3434
from warehouse.events.tags import EventTag
3535
from warehouse.utils.webauthn import AuthenticationRejectedError
36-
36+
from common.constants import REMOTE_ADDR
3737

3838
class TestLoginForm:
3939
def test_validate(self):
4040
request = pretend.stub(
41-
remote_addr="1.2.3.4",
41+
remote_addr=REMOTE_ADDR,
4242
banned=pretend.stub(
4343
by_ip=lambda ip_address: False,
4444
),
@@ -119,7 +119,7 @@ def test_validate_username_with_user(self, input_username, expected_username):
119119

120120
def test_validate_password_no_user(self):
121121
request = pretend.stub(
122-
remote_addr="1.2.3.4",
122+
remote_addr=REMOTE_ADDR,
123123
banned=pretend.stub(
124124
by_ip=lambda ip_address: False,
125125
),
@@ -145,7 +145,7 @@ def test_validate_password_no_user(self):
145145

146146
def test_validate_password_disabled_for_compromised_pw(self, db_session):
147147
request = pretend.stub(
148-
remote_addr="1.2.3.4", banned=pretend.stub(by_ip=lambda ip_address: False)
148+
remote_addr=REMOTE_ADDR, banned=pretend.stub(by_ip=lambda ip_address: False)
149149
)
150150
user_service = pretend.stub(
151151
find_userid=pretend.call_recorder(lambda userid: 1),
@@ -176,7 +176,7 @@ def test_validate_password_disabled_for_compromised_pw(self, db_session):
176176

177177
def test_validate_password_ok(self):
178178
request = pretend.stub(
179-
remote_addr="1.2.3.4",
179+
remote_addr=REMOTE_ADDR,
180180
banned=pretend.stub(
181181
by_ip=lambda ip_address: False,
182182
),
@@ -216,7 +216,7 @@ def test_validate_password_ok(self):
216216

217217
def test_validate_password_notok(self, db_session):
218218
request = pretend.stub(
219-
remote_addr="1.2.3.4",
219+
remote_addr=REMOTE_ADDR,
220220
banned=pretend.stub(
221221
by_ip=lambda ip_address: False,
222222
),
@@ -259,7 +259,7 @@ def test_validate_password_notok(self, db_session):
259259

260260
def test_validate_password_too_many_failed(self):
261261
request = pretend.stub(
262-
remote_addr="1.2.3.4",
262+
remote_addr=REMOTE_ADDR,
263263
banned=pretend.stub(
264264
by_ip=lambda ip_address: False,
265265
),
@@ -297,7 +297,7 @@ def test_password_breached(self, monkeypatch):
297297

298298
user = pretend.stub(id=1)
299299
request = pretend.stub(
300-
remote_addr="1.2.3.4",
300+
remote_addr=REMOTE_ADDR,
301301
banned=pretend.stub(
302302
by_ip=lambda ip_address: False,
303303
),
@@ -334,7 +334,7 @@ def test_password_breached(self, monkeypatch):
334334

335335
def test_validate_password_ok_ip_banned(self):
336336
request = pretend.stub(
337-
remote_addr="1.2.3.4",
337+
remote_addr=REMOTE_ADDR,
338338
banned=pretend.stub(
339339
by_ip=lambda ip_address: True,
340340
),
@@ -368,7 +368,7 @@ def test_validate_password_ok_ip_banned(self):
368368

369369
def test_validate_password_notok_ip_banned(self, db_session):
370370
request = pretend.stub(
371-
remote_addr="1.2.3.4",
371+
remote_addr=REMOTE_ADDR,
372372
banned=pretend.stub(
373373
by_ip=lambda ip_address: True,
374374
),
@@ -980,7 +980,7 @@ class TestTOTPAuthenticationForm:
980980
def test_validate(self, totp_value):
981981
user = pretend.stub(record_event=pretend.call_recorder(lambda *a, **kw: None))
982982
get_user = pretend.call_recorder(lambda userid: user)
983-
request = pretend.stub(remote_addr="1.2.3.4")
983+
request = pretend.stub(remote_addr=REMOTE_ADDR)
984984

985985
form = forms.TOTPAuthenticationForm(
986986
formdata=MultiDict({"totp_value": totp_value}),
@@ -1003,7 +1003,7 @@ def test_validate(self, totp_value):
10031003
def test_totp_secret_not_valid(self, pyramid_config, totp_value, expected_error):
10041004
user = pretend.stub(record_event=pretend.call_recorder(lambda *a, **kw: None))
10051005
get_user = pretend.call_recorder(lambda userid: user)
1006-
request = pretend.stub(remote_addr="1.2.3.4")
1006+
request = pretend.stub(remote_addr=REMOTE_ADDR)
10071007

10081008
form = forms.TOTPAuthenticationForm(
10091009
formdata=MultiDict({"totp_value": totp_value}),
@@ -1028,7 +1028,7 @@ def test_totp_secret_raises(
10281028
):
10291029
user = pretend.stub(record_event=pretend.call_recorder(lambda *a, **kw: None))
10301030
get_user = pretend.call_recorder(lambda userid: user)
1031-
request = pretend.stub(remote_addr="1.2.3.4")
1031+
request = pretend.stub(remote_addr=REMOTE_ADDR)
10321032

10331033
user_service = pretend.stub(
10341034
check_totp_value=pretend.raiser(exception),
@@ -1163,7 +1163,7 @@ def test_validate(self):
11631163

11641164
class TestRecoveryCodeForm:
11651165
def test_validate(self, monkeypatch):
1166-
request = pretend.stub(remote_addr="1.2.3.4")
1166+
request = pretend.stub(remote_addr=REMOTE_ADDR)
11671167
user = pretend.stub(id=pretend.stub(), username="foobar")
11681168
user_service = pretend.stub(
11691169
check_recovery_code=pretend.call_recorder(lambda *a, **kw: True),

tests/unit/accounts/test_security_policy.py

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
from warehouse.accounts import UserContext, security_policy
2222
from warehouse.accounts.interfaces import IUserService
2323
from warehouse.utils.security_policy import AuthenticationMethod
24-
24+
from common.constants import REMOTE_ADDR
2525

2626
class TestBasicAuthSecurityPolicy:
2727
def test_verify(self):
@@ -99,12 +99,12 @@ def test_identity_credentials_fail(self, monkeypatch):
9999
pretend.stub(
100100
matched_route=None,
101101
banned=pretend.stub(by_ip=lambda ip_address: False),
102-
remote_addr="1.2.3.4",
102+
remote_addr=REMOTE_ADDR,
103103
),
104104
pretend.stub(
105105
matched_route=pretend.stub(name="an.invalid.route"),
106106
banned=pretend.stub(by_ip=lambda ip_address: False),
107-
remote_addr="1.2.3.4",
107+
remote_addr=REMOTE_ADDR,
108108
),
109109
],
110110
)
@@ -203,7 +203,7 @@ def test_identity_missing_route(self, monkeypatch):
203203
add_response_callback=pretend.call_recorder(lambda cb: None),
204204
matched_route=None,
205205
banned=pretend.stub(by_ip=lambda ip_address: False),
206-
remote_addr="1.2.3.4",
206+
remote_addr=REMOTE_ADDR,
207207
)
208208

209209
assert policy.identity(request) is None
@@ -237,7 +237,7 @@ def test_identity_invalid_route(self, route_name, monkeypatch):
237237
add_response_callback=pretend.call_recorder(lambda cb: None),
238238
matched_route=pretend.stub(name=route_name),
239239
banned=pretend.stub(by_ip=lambda ip_address: False),
240-
remote_addr="1.2.3.4",
240+
remote_addr=REMOTE_ADDR,
241241
)
242242

243243
assert policy.identity(request) is None
@@ -266,7 +266,7 @@ def test_identity_no_userid(self, monkeypatch):
266266
add_response_callback=pretend.call_recorder(lambda cb: None),
267267
matched_route=pretend.stub(name="a.permitted.route"),
268268
banned=pretend.stub(by_ip=lambda ip_address: False),
269-
remote_addr="1.2.3.4",
269+
remote_addr=REMOTE_ADDR,
270270
)
271271

272272
assert policy.identity(request) is None
@@ -299,7 +299,7 @@ def test_identity_no_user(self, monkeypatch):
299299
matched_route=pretend.stub(name="a.permitted.route"),
300300
find_service=pretend.call_recorder(lambda i, **kw: user_service),
301301
banned=pretend.stub(by_ip=lambda ip_address: False),
302-
remote_addr="1.2.3.4",
302+
remote_addr=REMOTE_ADDR,
303303
)
304304

305305
assert policy.identity(request) is None
@@ -345,7 +345,7 @@ def test_identity_password_outdated(self, monkeypatch):
345345
flash=pretend.call_recorder(lambda *a, **kw: None),
346346
),
347347
banned=pretend.stub(by_ip=lambda ip_address: False),
348-
remote_addr="1.2.3.4",
348+
remote_addr=REMOTE_ADDR,
349349
)
350350

351351
assert policy.identity(request) is None
@@ -397,7 +397,7 @@ def test_identity_is_disabled(self, monkeypatch):
397397
flash=pretend.call_recorder(lambda *a, **kw: None),
398398
),
399399
banned=pretend.stub(by_ip=lambda ip_address: False),
400-
remote_addr="1.2.3.4",
400+
remote_addr=REMOTE_ADDR,
401401
)
402402

403403
assert policy.identity(request) is None
@@ -448,7 +448,7 @@ def test_identity(self, monkeypatch):
448448
password_outdated=pretend.call_recorder(lambda ts: False)
449449
),
450450
banned=pretend.stub(by_ip=lambda ip_address: False),
451-
remote_addr="1.2.3.4",
451+
remote_addr=REMOTE_ADDR,
452452
)
453453

454454
assert policy.identity(request).user is user
@@ -493,7 +493,7 @@ def test_identity_ip_banned(self, monkeypatch):
493493
password_outdated=pretend.call_recorder(lambda ts: False)
494494
),
495495
banned=pretend.stub(by_ip=lambda ip_address: True),
496-
remote_addr="1.2.3.4",
496+
remote_addr=REMOTE_ADDR,
497497
)
498498

499499
assert policy.identity(request) is None

tests/unit/accounts/test_services.py

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@
4949

5050
from ...common.db.accounts import EmailFactory, UserFactory
5151
from ...common.db.ip_addresses import IpAddressFactory
52-
52+
from common.constants import REMOTE_ADDR
5353

5454
class TestDatabaseUserService:
5555
def test_verify_service(self):
@@ -62,7 +62,7 @@ def test_service_creation(self, monkeypatch, remote_addr):
6262

6363
session = pretend.stub()
6464
service = services.DatabaseUserService(
65-
session, metrics=NullMetrics(), remote_addr=remote_addr
65+
session, metrics=NullMetrics(), remote_addr=REMOTE_ADDR
6666
)
6767

6868
assert service.db is session
@@ -95,7 +95,7 @@ def test_service_creation_ratelimiters(self, monkeypatch, remote_addr):
9595
service = services.DatabaseUserService(
9696
session,
9797
metrics=NullMetrics(),
98-
remote_addr=remote_addr,
98+
remote_addr=REMOTE_ADDR,
9999
ratelimiters=ratelimiters,
100100
)
101101

@@ -227,8 +227,8 @@ def test_check_password_ip_rate_limited(self, user_service, metrics, remote_addr
227227
user_service.check_password(user.id, None)
228228

229229
assert excinfo.value.resets_in is resets
230-
assert limiter.test.calls == [pretend.call(remote_addr)]
231-
assert limiter.resets_in.calls == [pretend.call(remote_addr)]
230+
assert limiter.test.calls == [pretend.call(REMOTE_ADDR)]
231+
assert limiter.resets_in.calls == [pretend.call(REMOTE_ADDR)]
232232
assert metrics.increment.calls == [
233233
pretend.call(
234234
"warehouse.authentication.start", tags=["mechanism:check_password"]
@@ -397,8 +397,8 @@ def test_add_email_rate_limited(self, user_service, metrics, remote_addr):
397397
user_service.add_email(user.id, user.email)
398398

399399
assert excinfo.value.resets_in is resets
400-
assert limiter.test.calls == [pretend.call(remote_addr)]
401-
assert limiter.resets_in.calls == [pretend.call(remote_addr)]
400+
assert limiter.test.calls == [pretend.call(REMOTE_ADDR)]
401+
assert limiter.resets_in.calls == [pretend.call(REMOTE_ADDR)]
402402
assert metrics.increment.calls == [
403403
pretend.call(
404404
"warehouse.email.add.ratelimited", tags=["ratelimiter:email.add"]
@@ -1165,15 +1165,15 @@ def find_service(iface, name=None, context=None):
11651165

11661166
context = pretend.stub()
11671167
request = pretend.stub(
1168-
db=pretend.stub(), find_service=find_service, remote_addr=remote_addr
1168+
db=pretend.stub(), find_service=find_service, remote_addr=REMOTE_ADDR
11691169
)
11701170

11711171
assert services.database_login_factory(context, request) is service_obj
11721172
assert service_cls.calls == [
11731173
pretend.call(
11741174
request.db,
11751175
metrics=metrics,
1176-
remote_addr=remote_addr,
1176+
remote_addr=REMOTE_ADDR,
11771177
ratelimiters={
11781178
"global.login": global_login_ratelimiter,
11791179
"user.login": user_login_ratelimiter,

tests/unit/email/test_init.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626

2727
from ...common.db.accounts import EmailFactory, UserFactory
2828
from ...common.db.organizations import TeamFactory
29-
29+
from common.constants import REMOTE_ADDR
3030

3131
@pytest.mark.parametrize(
3232
("user", "address", "expected"),
@@ -66,11 +66,11 @@ def test_compute_recipient(user, address, expected):
6666
@pytest.mark.parametrize(
6767
("unauthenticated_userid", "user", "remote_addr", "expected"),
6868
[
69-
("the_users_id", None, "1.2.3.4", False),
70-
("some_other_id", None, "1.2.3.4", True),
71-
(None, pretend.stub(id="the_users_id"), "1.2.3.4", False),
72-
(None, pretend.stub(id="some_other_id"), "1.2.3.4", True),
73-
(None, None, "1.2.3.4", False),
69+
("the_users_id", None, REMOTE_ADDR, False),
70+
("some_other_id", None, REMOTE_ADDR, True),
71+
(None, pretend.stub(id="the_users_id"), REMOTE_ADDR, False),
72+
(None, pretend.stub(id="some_other_id"), REMOTE_ADDR, True),
73+
(None, None, REMOTE_ADDR, False),
7474
(None, None, "127.0.0.1", True),
7575
],
7676
)
@@ -85,7 +85,7 @@ def test_redact_ip(unauthenticated_userid, user, remote_addr, expected):
8585
filter=lambda a: pretend.stub(one=lambda: user_email)
8686
)
8787
),
88-
remote_addr=remote_addr,
88+
remote_addr=REMOTE_ADDR,
8989
)
9090
assert email._redact_ip(request, user_email) == expected
9191

0 commit comments

Comments
 (0)