"""Tests for Gitea credential generation.""" import pytest from dagster import build_op_context from simpl_open_credential_delivery.ops.generate_gitea_credentials import ( generate_gitea_credentials, ) class FakeResponse: def __init__(self, status_code, payload=None, text="OK"): self.status_code = status_code self._payload = payload or {} self.text = text self.content = b"{}" if payload is not None else b"" def json(self): return self._payload def test_generate_gitea_credentials_success(monkeypatch, op_context): responses = [ FakeResponse(201), FakeResponse(201, {"sha1": "token-123"}), FakeResponse(204), ] def fake_request(*args, **kwargs): return responses.pop(0) monkeypatch.setenv("GITEA_BASE_URL", "https://gitea.example.local") monkeypatch.setenv("GITEA_ADMIN_TOKEN", "admin-token") monkeypatch.setenv("GITEA_DEFAULT_OWNER", "simpl-open") monkeypatch.setattr("requests.request", fake_request) op_context = build_op_context( op_config={ "repository_identifier": "platform-automation", "consumer_email": "consumer@example.local", "access_level": "read", "token_name_prefix": "simpl-open", "token_expiration_days": 30, } ) result = generate_gitea_credentials(op_context) assert result["username"] == "consumer" assert result["access_token"] == "token-123" assert result["repository_access_url"] == "https://gitea.example.local/simpl-open/platform-automation" def test_generate_gitea_credentials_requires_gitea_base_url(monkeypatch, op_context): monkeypatch.delenv("GITEA_BASE_URL", raising=False) monkeypatch.setenv("GITEA_ADMIN_TOKEN", "admin-token") op_context = build_op_context( op_config={ "repository_identifier": "simpl-open/platform-automation", "consumer_email": "consumer@example.local", "access_level": "read", "token_name_prefix": "simpl-open", "token_expiration_days": 30, } ) with pytest.raises(ValueError, match="GITEA_BASE_URL"): generate_gitea_credentials(op_context)