Skip to content

Commit 773fedc

Browse files
Replacing Fixtures with Constants
1 parent 70b4da3 commit 773fedc

File tree

9 files changed

+39
-42
lines changed

9 files changed

+39
-42
lines changed

tests/conftest.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -68,8 +68,8 @@
6868
from .common.db import Session
6969
from .common.db.accounts import EmailFactory, UserFactory
7070
from .common.db.ip_addresses import IpAddressFactory
71+
from .common.constants import REMOTE_ADDR, REMOTE_ADDR_HASHED
7172

72-
from common.constants import REMOTE_ADDR, REMOTE_ADDR_HASHED, REMOTE_ADDR_SALTED
7373

7474
_HERE = Path(__file__).parent.resolve()
7575
_FIXTURES = _HERE / "_fixtures"
@@ -214,7 +214,7 @@ def pyramid_services(
214214

215215

216216
@pytest.fixture
217-
def pyramid_request(pyramid_services, jinja, remote_addr, remote_addr_hashed):
217+
def pyramid_request(pyramid_services, jinja):
218218
pyramid.testing.setUp()
219219
dummy_request = pyramid.testing.DummyRequest()
220220
dummy_request.find_service = pyramid_services.find_service
@@ -438,7 +438,7 @@ def db_session(app_config):
438438

439439

440440
@pytest.fixture
441-
def user_service(db_session, metrics, remote_addr):
441+
def user_service(db_session, metrics):
442442
return account_services.DatabaseUserService(
443443
db_session, metrics=metrics, remote_addr=REMOTE_ADDR
444444
)
@@ -745,7 +745,7 @@ def tm():
745745

746746

747747
@pytest.fixture
748-
def webtest(app_config_dbsession_from_env, remote_addr, tm):
748+
def webtest(app_config_dbsession_from_env, tm):
749749
"""
750750
This fixture yields a test app with an alternative Pyramid configuration,
751751
injecting the database session and transaction manager into the app.

tests/unit/accounts/test_forms.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,8 @@
3333
from warehouse.captcha import recaptcha
3434
from warehouse.events.tags import EventTag
3535
from warehouse.utils.webauthn import AuthenticationRejectedError
36-
from common.constants import REMOTE_ADDR
36+
from ...common.constants import REMOTE_ADDR
37+
3738

3839
class TestLoginForm:
3940
def test_validate(self):

tests/unit/accounts/test_security_policy.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@
2121
from warehouse.accounts import UserContext, security_policy
2222
from warehouse.accounts.interfaces import IUserService
2323
from warehouse.utils.security_policy import AuthenticationMethod
24-
from common.constants import REMOTE_ADDR
24+
from ...common.constants import REMOTE_ADDR
25+
2526

2627
class TestBasicAuthSecurityPolicy:
2728
def test_verify(self):

tests/unit/accounts/test_services.py

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

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

5455
class TestDatabaseUserService:
5556
def test_verify_service(self):
5657
assert verifyClass(IUserService, services.DatabaseUserService)
5758

58-
def test_service_creation(self, monkeypatch, remote_addr):
59+
def test_service_creation(self, monkeypatch):
5960
crypt_context_obj = pretend.stub()
6061
crypt_context_cls = pretend.call_recorder(lambda **kwargs: crypt_context_obj)
6162
monkeypatch.setattr(services, "CryptContext", crypt_context_cls)
@@ -84,7 +85,7 @@ def test_service_creation(self, monkeypatch, remote_addr):
8485
)
8586
]
8687

87-
def test_service_creation_ratelimiters(self, monkeypatch, remote_addr):
88+
def test_service_creation_ratelimiters(self, monkeypatch):
8889
crypt_context_obj = pretend.stub()
8990
crypt_context_cls = pretend.call_recorder(lambda **kwargs: crypt_context_obj)
9091
monkeypatch.setattr(services, "CryptContext", crypt_context_cls)
@@ -119,7 +120,7 @@ def test_service_creation_ratelimiters(self, monkeypatch, remote_addr):
119120
)
120121
]
121122

122-
def test_skips_ip_rate_limiter(self, user_service, metrics, remote_addr):
123+
def test_skips_ip_rate_limiter(self, user_service, metrics):
123124
user = UserFactory.create()
124125
resets = pretend.stub()
125126
limiter = pretend.stub(
@@ -214,7 +215,7 @@ def test_check_password_user_rate_limited(self, user_service, metrics):
214215
),
215216
]
216217

217-
def test_check_password_ip_rate_limited(self, user_service, metrics, remote_addr):
218+
def test_check_password_ip_rate_limited(self, user_service, metrics):
218219
user = UserFactory.create()
219220
resets = pretend.stub()
220221
limiter = pretend.stub(
@@ -382,7 +383,7 @@ def test_add_email_defaults_to_primary(self, user_service):
382383
assert not new_email2.primary
383384
assert not new_email2.verified
384385

385-
def test_add_email_rate_limited(self, user_service, metrics, remote_addr):
386+
def test_add_email_rate_limited(self, user_service, metrics):
386387
resets = pretend.stub()
387388
limiter = pretend.stub(
388389
hit=pretend.call_recorder(lambda ip: None),
@@ -1128,7 +1129,7 @@ def test_unsafe_load_payload_signature_invalid(self, token_service):
11281129
assert token_service.unsafe_load_payload(token) is None
11291130

11301131

1131-
def test_database_login_factory(monkeypatch, pyramid_services, metrics, remote_addr):
1132+
def test_database_login_factory(monkeypatch, pyramid_services, metrics):
11321133
service_obj = pretend.stub()
11331134
service_cls = pretend.call_recorder(lambda *a, **kw: service_obj)
11341135
monkeypatch.setattr(services, "DatabaseUserService", service_cls)

tests/unit/email/test_init.py

Lines changed: 2 additions & 2 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-
from common.constants import REMOTE_ADDR
29+
from ...common.constants import REMOTE_ADDR
3030

3131
@pytest.mark.parametrize(
3232
("user", "address", "expected"),
@@ -74,7 +74,7 @@ def test_compute_recipient(user, address, expected):
7474
(None, None, "127.0.0.1", True),
7575
],
7676
)
77-
def test_redact_ip(unauthenticated_userid, user, remote_addr, expected):
77+
def test_redact_ip(unauthenticated_userid, user, expected):
7878
user_email = pretend.stub(user_id="the_users_id")
7979

8080
request = pretend.stub(

tests/unit/forklift/test_legacy.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@
6666
ReleaseFactory,
6767
RoleFactory,
6868
)
69-
from common.constants import REMOTE_ADDR
69+
from ...common.constants import REMOTE_ADDR
7070

7171

7272
def _get_tar_testdata(compression_type=""):
@@ -4398,7 +4398,6 @@ def test_upload_new_project_fails_ratelimited(
43984398
db_request,
43994399
project_service,
44004400
failing_limiter,
4401-
remote_addr,
44024401
):
44034402
user = UserFactory.create()
44044403
EmailFactory.create(user=user)

tests/unit/manage/test_forms.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
from warehouse.manage import forms
2424

2525
from ...common.db.packaging import ProjectFactory
26-
from common.constants import REMOTE_ADDR
26+
from ...common.constants import REMOTE_ADDR
2727

2828

2929
class TestCreateRoleForm:

tests/unit/oidc/test_views.py

Lines changed: 11 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@
4040
from warehouse.packaging import services
4141
from warehouse.packaging.models import Project
4242
from warehouse.rate_limiting.interfaces import IRateLimiter
43-
from common.constants import DUMMY_GITHUB_OIDC_JWT, DUMMY_ACTIVESTATE_OIDC_JWT
43+
from ...common.constants import DUMMY_GITHUB_OIDC_JWT, DUMMY_ACTIVESTATE_OIDC_JWT
4444

4545

4646
def test_ratelimiters():
@@ -193,7 +193,7 @@ def find_service(self, *a, **kw):
193193

194194

195195
def test_mint_token_from_oidc_jwt_decode_leaky_exception(
196-
monkeypatch, dummy_github_oidc_jwt: str
196+
monkeypatch
197197
):
198198
class Request:
199199
def __init__(self):
@@ -309,7 +309,6 @@ def test_mint_token_from_oidc_creates_expected_service(
309309

310310

311311
def test_mint_token_from_trusted_publisher_verify_jwt_signature_fails(
312-
dummy_github_oidc_jwt,
313312
):
314313
oidc_service = pretend.stub(
315314
verify_jwt_signature=pretend.call_recorder(lambda token: None),
@@ -337,7 +336,7 @@ def test_mint_token_from_trusted_publisher_verify_jwt_signature_fails(
337336
]
338337

339338

340-
def test_mint_token_trusted_publisher_lookup_fails(dummy_github_oidc_jwt):
339+
def test_mint_token_trusted_publisher_lookup_fails():
341340
claims = pretend.stub()
342341
message = "some message"
343342
oidc_service = pretend.stub(
@@ -375,7 +374,7 @@ def test_mint_token_trusted_publisher_lookup_fails(dummy_github_oidc_jwt):
375374
]
376375

377376

378-
def test_mint_token_duplicate_token(dummy_github_oidc_jwt):
377+
def test_mint_token_duplicate_token():
379378
def find_publishers_mockup(_, pending: bool = False):
380379
if pending is False:
381380
raise errors.ReusedTokenError("some message")
@@ -407,7 +406,7 @@ def find_publishers_mockup(_, pending: bool = False):
407406

408407

409408
def test_mint_token_pending_publisher_project_already_exists(
410-
db_request, dummy_github_oidc_jwt
409+
db_request
411410
):
412411
project = ProjectFactory.create()
413412
pending_publisher = PendingGitHubPublisherFactory.create(
@@ -445,8 +444,7 @@ def test_mint_token_pending_publisher_project_already_exists(
445444

446445
def test_mint_token_from_oidc_pending_publisher_ok(
447446
monkeypatch,
448-
db_request,
449-
dummy_github_oidc_jwt,
447+
db_request
450448
):
451449
user = UserFactory.create()
452450

@@ -501,7 +499,7 @@ def test_mint_token_from_oidc_pending_publisher_ok(
501499

502500

503501
def test_mint_token_from_pending_trusted_publisher_invalidates_others(
504-
monkeypatch, db_request, dummy_github_oidc_jwt
502+
monkeypatch, db_request
505503
):
506504
time = pretend.stub(time=pretend.call_recorder(lambda: 0))
507505
monkeypatch.setattr(views, "time", time)
@@ -595,7 +593,7 @@ def test_mint_token_from_pending_trusted_publisher_invalidates_others(
595593
],
596594
)
597595
def test_mint_token_no_pending_publisher_ok(
598-
monkeypatch, db_request, claims_in_token, claims_input, dummy_github_oidc_jwt
596+
monkeypatch, db_request, claims_in_token, claims_input
599597
):
600598
time = pretend.stub(time=pretend.call_recorder(lambda: 0))
601599
monkeypatch.setattr(views, "time", time)
@@ -684,7 +682,7 @@ def find_service(iface, **kw):
684682

685683

686684
def test_mint_token_warn_constrain_environment(
687-
monkeypatch, db_request, dummy_github_oidc_jwt
685+
monkeypatch, db_request
688686
):
689687
claims_in_token = {"ref": "someref", "sha": "somesha", "environment": "fakeenv"}
690688
claims_input = {"ref": "someref", "sha": "somesha"}
@@ -798,8 +796,7 @@ def find_service(iface, **kw):
798796

799797
def test_mint_token_with_prohibited_name_fails(
800798
monkeypatch,
801-
db_request,
802-
dummy_github_oidc_jwt,
799+
db_request
803800
):
804801
prohibited_project_name = ProhibitedProjectFactory.create()
805802
user = UserFactory.create()
@@ -841,8 +838,7 @@ def test_mint_token_with_prohibited_name_fails(
841838

842839
def test_mint_token_with_invalid_name_fails(
843840
monkeypatch,
844-
db_request,
845-
dummy_github_oidc_jwt,
841+
db_request
846842
):
847843
user = UserFactory.create()
848844
pending_publisher = PendingGitHubPublisherFactory.create(
@@ -917,7 +913,6 @@ def test_mint_token_github_reusable_workflow_metrics(
917913
claims_in_token,
918914
is_reusable,
919915
is_github,
920-
dummy_github_oidc_jwt,
921916
metrics,
922917
):
923918
time = pretend.stub(time=pretend.call_recorder(lambda: 0))

tests/unit/utils/test_wsgi.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
from warehouse.utils import wsgi
2323

2424
from ...common.db.ip_addresses import IpAddressFactory as DBIpAddressFactory
25-
from common.constants import REMOTE_ADDR
25+
from ...common.constants import REMOTE_ADDR, REMOTE_ADDR_SALTED, REMOTE_ADDR_HASHED
2626

2727

2828
class TestProxyFixer:
@@ -117,7 +117,7 @@ def test_missing_headers(self):
117117
assert resp is response
118118
assert app.calls == [pretend.call({}, start_response)]
119119

120-
def test_accepts_x_forwarded_headers(self, remote_addr_salted):
120+
def test_accepts_x_forwarded_headers(self):
121121
response = pretend.stub()
122122
app = pretend.call_recorder(lambda e, s: response)
123123

@@ -137,7 +137,7 @@ def test_accepts_x_forwarded_headers(self, remote_addr_salted):
137137
{
138138
"HTTP_SOME_OTHER_HEADER": "woop",
139139
"REMOTE_ADDR": REMOTE_ADDR,
140-
"REMOTE_ADDR_HASHED": remote_addr_salted,
140+
"REMOTE_ADDR_HASHED": REMOTE_ADDR_SALTED,
141141
"HTTP_HOST": "example.com",
142142
"wsgi.url_scheme": "http",
143143
},
@@ -161,7 +161,7 @@ def test_skips_x_forwarded_when_not_enough(self):
161161
pretend.call({"HTTP_SOME_OTHER_HEADER": "woop"}, start_response)
162162
]
163163

164-
def test_selects_right_x_forwarded_value(self, remote_addr_salted):
164+
def test_selects_right_x_forwarded_value(self):
165165
response = pretend.stub()
166166
app = pretend.call_recorder(lambda e, s: response)
167167

@@ -183,7 +183,7 @@ def test_selects_right_x_forwarded_value(self, remote_addr_salted):
183183
{
184184
"HTTP_SOME_OTHER_HEADER": "woop",
185185
"REMOTE_ADDR": REMOTE_ADDR,
186-
"REMOTE_ADDR_HASHED": remote_addr_salted,
186+
"REMOTE_ADDR_HASHED": REMOTE_ADDR_SALTED,
187187
"HTTP_HOST": "example.com",
188188
"wsgi.url_scheme": "http",
189189
},
@@ -246,11 +246,11 @@ def test_ip_address_created(db_request):
246246
assert ip_address.geoip_info == {"city": "Anytown, ST"}
247247

248248

249-
def test_remote_addr_hashed(remote_addr_hashed):
250-
environ = {"REMOTE_ADDR_HASHED": remote_addr_hashed}
249+
def test_remote_addr_hashed():
250+
environ = {"REMOTE_ADDR_HASHED": REMOTE_ADDR_HASHED}
251251
request = pretend.stub(environ=environ)
252252

253-
assert wsgi._remote_addr_hashed(request) == remote_addr_hashed
253+
assert wsgi._remote_addr_hashed(request) == REMOTE_ADDR_HASHED
254254

255255

256256
def test_remote_addr_hashed_missing():

0 commit comments

Comments
 (0)