Skip to content

cbsd: add pytest and some test cases#32

Open
jecluis wants to merge 1 commit intomainfrom
wip/cbsd-pytest
Open

cbsd: add pytest and some test cases#32
jecluis wants to merge 1 commit intomainfrom
wip/cbsd-pytest

Conversation

@jecluis
Copy link
Contributor

@jecluis jecluis commented Feb 22, 2026

Adds pytest support to the project, and introduces some tests for cbsd/.

This way we're moving away from kludge tests in modules' __main__ functions, and into something a lot more predictable and usable.

This patch set was generated mostly by Claude Opus 4.6, with guidance by the submitter.

Signed-off-by: Joao Eduardo Luis <joao@clyso.com>

Co-authored-by: Claude Opus 4.6 <noreply@anthropic.com>
Signed-off-by: Joao Eduardo Luis <joao@clyso.com>
@jecluis jecluis requested a review from UweSchwaeke February 22, 2026 10:47
"""Valid PASETO envelope but non-JSON payload."""

def test_non_json_payload_raises(self, mock_config: Config) -> None:
assert mock_config.server is not None
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do we need to assert the mock_config.server? mock_config.server is always not None

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can't recall, but I'm pretty sure this was to make the linter happy.

Comment on lines +124 to +147
class TestCapsSerialization:
"""Caps -> JSON -> back roundtrip preserves the value."""

def test_auth_caps_roundtrip(self) -> None:
original = _AuthCapsModel(
caps=AuthorizationCaps.BUILDS_CREATE | AuthorizationCaps.BUILDS_LIST_OWN,
)
json_str = original.model_dump_json()
restored = _AuthCapsModel.model_validate_json(json_str)
assert original.caps == restored.caps

def test_routes_caps_roundtrip(self) -> None:
original = _RoutesCapsModel(
caps=RoutesCaps.ROUTES_BUILDS_NEW | RoutesCaps.ROUTES_AUTH_LOGIN,
)
json_str = original.model_dump_json()
restored = _RoutesCapsModel.model_validate_json(json_str)
assert original.caps == restored.caps

def test_auth_caps_all_roundtrip(self) -> None:
original = _AuthCapsModel.model_validate({"caps": ["all"]})
json_str = original.model_dump_json()
restored = _AuthCapsModel.model_validate_json(json_str)
assert original.caps == restored.caps
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we test a library functions? (model_dump_json and model_validate_json)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

these tests were auto-generated, but regardless this is a valid test hence why it was submitted. This is not validating model_dump_json nor model_validate_json, but the serialization and deserialization of all -- this is a meta flag that gets translated by _caps_from_str_lst() into the various AuthorizationCaps, which is itself an enum.

Comment on lines +257 to +259
@pytest.fixture
def default_perms() -> Permissions:
return permissions_from_yaml(_DEFAULT_YAML)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should this go into conftest.py, because it is a fixture?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure, not quite as proficient with pytest as I used to be. This seemed fine, given this was meant for permissions solely, not for all the tests?

Comment on lines +482 to +540
@pytest.fixture
def basic_perms(self) -> Permissions:
auth = Permissions(groups={}, rules=[])
auth.groups["admin"] = AuthorizationGroup(
name="admin",
authorized_for=[
ProjectAuthorizationEntry(
pattern=r".*",
caps=AuthorizationCaps.PROJECT_MANAGE
| AuthorizationCaps.PROJECT_LIST
| AuthorizationCaps.BUILDS_CREATE
| AuthorizationCaps.BUILDS_REVOKE_ANY
| AuthorizationCaps.BUILDS_LIST_ANY,
),
RegistryAuthorizationEntry(pattern=r".*"),
RepositoryAuthorizationEntry(pattern=r".*"),
],
)
auth.groups["development"] = AuthorizationGroup(
name="development",
authorized_for=[
ProjectAuthorizationEntry(
pattern=r"^dev/.*$",
caps=AuthorizationCaps.PROJECT_LIST
| AuthorizationCaps.BUILDS_CREATE
| AuthorizationCaps.BUILDS_LIST_OWN
| AuthorizationCaps.BUILDS_LIST_ANY
| AuthorizationCaps.BUILDS_REVOKE_OWN,
),
RegistryAuthorizationEntry(pattern=r"^registry\.example\.com/dev/.*$"),
RepositoryAuthorizationEntry(pattern=r"https?://github\.com/clyso/.*"),
],
)
auth.groups["all"] = AuthorizationGroup(
name="all",
authorized_for=[
ProjectAuthorizationEntry(
pattern=r".*",
caps=AuthorizationCaps.PROJECT_LIST
| AuthorizationCaps.BUILDS_LIST_OWN
| AuthorizationCaps.BUILDS_LIST_ANY,
),
],
)
auth.rules.extend(
[
UserAuthorizationRule(user_pattern=r"^.*@domain\.tld$", groups=["all"]),
UserAuthorizationRule(
user_pattern=r"^foo.bar@domain.tld$",
groups=["development"],
),
UserAuthorizationRule(
user_pattern=r"^foo.baz@domain.tld$",
groups=["admin"],
),
]
)
return auth

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

shouldn't this fixture be part of the conftest.py?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

but it's only used for this test? Again, not as proficient with pytest as I used to be, but I would assume conftest.py would only hold fixtures that are reused across the tests, instead of being the go-to place to dump even one-time-use fixtures.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants