Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor: fixtures with static values to constants #16913

Open
miketheman opened this issue Oct 17, 2024 · 0 comments
Open

Refactor: fixtures with static values to constants #16913

miketheman opened this issue Oct 17, 2024 · 0 comments
Labels
developer experience Anything that improves the experience for Warehouse devs testing Test infrastructure and individual tests

Comments

@miketheman
Copy link
Member

We have a bunch of things in conftest.py that are declared as fixtures, but behave as constants.

A few examples:

warehouse/tests/conftest.py

Lines 118 to 137 in 694286e

@pytest.fixture
def remote_addr():
return "1.2.3.4"
@pytest.fixture
def remote_addr_hashed():
"""
Static output of `hashlib.sha256(remote_addr.encode("utf8")).hexdigest()`
Created statically to prevent needing to calculate it every run.
"""
return "6694f83c9f476da31f5df6bcc520034e7e57d421d247b9d34f49edbfc84a764c"
@pytest.fixture
def remote_addr_salted():
"""
Output of `hashlib.sha256((remote_addr + "pepa").encode("utf8")).hexdigest()`
"""
return "a69a49383d81404e4b1df297c7baa28e1cd6c4ee1495ed5d0ab165a63a147763"

warehouse/tests/conftest.py

Lines 453 to 535 in 694286e

@pytest.fixture
def dummy_github_oidc_jwt():
# {
# "jti": "6e67b1cb-2b8d-4be5-91cb-757edb2ec970",
# "sub": "repo:foo/bar",
# "aud": "pypi",
# "ref": "fake",
# "sha": "fake",
# "repository": "foo/bar",
# "repository_owner": "foo",
# "repository_owner_id": "123",
# "run_id": "fake",
# "run_number": "fake",
# "run_attempt": "1",
# "repository_id": "fake",
# "actor_id": "fake",
# "actor": "foo",
# "workflow": "fake",
# "head_ref": "fake",
# "base_ref": "fake",
# "event_name": "fake",
# "ref_type": "fake",
# "environment": "fake",
# "job_workflow_ref": "foo/bar/.github/workflows/example.yml@fake",
# "iss": "https://token.actions.githubusercontent.com",
# "nbf": 1650663265,
# "exp": 1650664165,
# "iat": 1650663865
# }
return (
"eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJqdGkiOiI2ZTY3YjFjYi0yYjhkLTRiZ"
"TUtOTFjYi03NTdlZGIyZWM5NzAiLCJzdWIiOiJyZXBvOmZvby9iYXIiLCJhdWQiOiJweXB"
"pIiwicmVmIjoiZmFrZSIsInNoYSI6ImZha2UiLCJyZXBvc2l0b3J5IjoiZm9vL2JhciIsI"
"nJlcG9zaXRvcnlfb3duZXIiOiJmb28iLCJyZXBvc2l0b3J5X293bmVyX2lkIjoiMTIzIiw"
"icnVuX2lkIjoiZmFrZSIsInJ1bl9udW1iZXIiOiJmYWtlIiwicnVuX2F0dGVtcHQiOiIxI"
"iwicmVwb3NpdG9yeV9pZCI6ImZha2UiLCJhY3Rvcl9pZCI6ImZha2UiLCJhY3RvciI6ImZ"
"vbyIsIndvcmtmbG93IjoiZmFrZSIsImhlYWRfcmVmIjoiZmFrZSIsImJhc2VfcmVmIjoiZ"
"mFrZSIsImV2ZW50X25hbWUiOiJmYWtlIiwicmVmX3R5cGUiOiJmYWtlIiwiZW52aXJvbm1"
"lbnQiOiJmYWtlIiwiam9iX3dvcmtmbG93X3JlZiI6ImZvby9iYXIvLmdpdGh1Yi93b3JrZ"
"mxvd3MvZXhhbXBsZS55bWxAZmFrZSIsImlzcyI6Imh0dHBzOi8vdG9rZW4uYWN0aW9ucy5"
"naXRodWJ1c2VyY29udGVudC5jb20iLCJuYmYiOjE2NTA2NjMyNjUsImV4cCI6MTY1MDY2N"
"DE2NSwiaWF0IjoxNjUwNjYzODY1fQ.f-FMv5FF5sdxAWeUilYDt9NoE7Et0vbdNhK32c2o"
"C-E"
)
@pytest.fixture
def dummy_activestate_oidc_jwt():
# {
# "jti": "6e67b1cb-2b8d-4be5-91cb-757edb2ec970",
# "sub": "org:fakeorg:project:fakeproject",
# "aud": "pypi",
# "actor_id": "fake",
# "actor": "foo",
# "oraganization_id": "7e67b1cb-2b8d-4be5-91cb-757edb2ec970",
# "organization": "fakeorg",
# "project_visibility": "private",
# "project_id": "8e67b1cb-2b8d-4be5-91cb-757edb2ec970",
# "project_path": "fakeorg/fakeproject",
# "project": "fakeproject",
# "builder": "pypi_builder",
# "ingredient_name": "fakeingredient",
# "artifact_id": "9e67b1cb-2b8d-4be5-91cb-757edb2ec970",
# "iss":"https://platform.activestate.com/api/v1/oauth/oidc",
# "nbf": 1650663265,
# "exp": 1650664165,
# "iat": 1650663865
# }
return (
"eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJqdGkiOiI2ZTY3YjFjYi0yYjhkLTRi"
"ZTUtOTFjYi03NTdlZGIyZWM5NzAiLCJzdWIiOiJvcmc6ZmFrZW9yZzpwcm9qZWN0OmZha"
"2Vwcm9qZWN0IiwiYXVkIjoicHlwaSIsImFjdG9yX2lkIjoiZmFrZSIsImFjdG9yIjoiZm"
"9vIiwib3JhZ2FuaXphdGlvbl9pZCI6IjdlNjdiMWNiLTJiOGQtNGJlNS05MWNiLTc1N2V"
"kYjJlYzk3MCIsIm9yZ2FuaXphdGlvbiI6ImZha2VvcmciLCJwcm9qZWN0X3Zpc2liaWxp"
"dHkiOiJwcml2YXRlIiwicHJvamVjdF9pZCI6IjhlNjdiMWNiLTJiOGQtNGJlNS05MWNiL"
"Tc1N2VkYjJlYzk3MCIsInByb2plY3RfcGF0aCI6ImZha2VvcmcvZmFrZXByb2plY3QiLC"
"Jwcm9qZWN0IjoiZmFrZXByb2plY3QiLCJidWlsZGVyIjoicHlwaV9idWlsZGVyIiwiaW5"
"ncmVkaWVudF9uYW1lIjoiZmFrZWluZ3JlZGllbnQiLCJhcnRpZmFjdF9pZCI6IjllNjdi"
"MWNiLTJiOGQtNGJlNS05MWNiLTc1N2VkYjJlYzk3MCIsImlzcyI6Imh0dHBzOi8vcGxhd"
"GZvcm0uYWN0aXZlc3RhdGUuY29tL2FwaS92MS9vYXV0aC9vaWRjIiwibmJmIjoxNjUwNj"
"YzMjY1LCJleHAiOjE2NTA2NjQxNjUsImlhdCI6MTY1MDY2Mzg2NX0.R4q-vWAFXHrBSBK"
"AZuHHIsGOkqlirPxEtLfjLIDiLr0"
)

As far as I can gather, these are used to provide some well-known value to other test cases, and since it's using the pytest fixtures decorator, that allows a test function to use them as part of their function signature, and not import them (magic! 🪄🐰🎩).

Considering this, I'm contemplating a tests/common/constants.py (provide your naming choice!) and relocating these functions there, as CONSTANT-type values, and importing them in the test modules that use them.

This change will:

  • raise the visibility of the specific variable used as a constant, making it clearer to the developer that they are operating on some value
  • remove things that aren't fixture-like from conftest.py
  • establish a pattern of test-centric constant values
  • increase imports at the top of test modules that use constants (today they don't have to import it)
  • reduce test function signature input args
@miketheman miketheman added testing Test infrastructure and individual tests developer experience Anything that improves the experience for Warehouse devs labels Oct 17, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
developer experience Anything that improves the experience for Warehouse devs testing Test infrastructure and individual tests
Projects
None yet
Development

No branches or pull requests

1 participant