|
| 1 | +"""Tests for the generic fallback OIDC detector.""" |
| 2 | + |
| 3 | +from unittest import mock |
| 4 | + |
| 5 | +import pytest |
| 6 | + |
| 7 | +from cloudsmith_cli.core.credentials.models import CredentialContext |
| 8 | +from cloudsmith_cli.core.credentials.oidc.detectors import detect_environment |
| 9 | +from cloudsmith_cli.core.credentials.oidc.detectors.generic import GenericDetector |
| 10 | + |
| 11 | + |
| 12 | +@pytest.fixture |
| 13 | +def generic_env(): |
| 14 | + env = { |
| 15 | + "CLOUDSMITH_OIDC_TOKEN": "the-jwt", |
| 16 | + } |
| 17 | + with mock.patch.dict("os.environ", env, clear=True): |
| 18 | + yield env |
| 19 | + |
| 20 | + |
| 21 | +class TestDetect: |
| 22 | + def test_detects_when_token_present(self, generic_env): |
| 23 | + detector = GenericDetector(context=CredentialContext()) |
| 24 | + assert detector.detect() is True |
| 25 | + |
| 26 | + def test_not_detected_when_unset(self): |
| 27 | + with mock.patch.dict("os.environ", {}, clear=True): |
| 28 | + detector = GenericDetector(context=CredentialContext()) |
| 29 | + assert detector.detect() is False |
| 30 | + |
| 31 | + def test_not_detected_when_token_empty(self, generic_env): |
| 32 | + generic_env["CLOUDSMITH_OIDC_TOKEN"] = "" |
| 33 | + with mock.patch.dict("os.environ", generic_env, clear=True): |
| 34 | + detector = GenericDetector(context=CredentialContext()) |
| 35 | + assert detector.detect() is False |
| 36 | + |
| 37 | + def test_not_detected_when_token_whitespace_only(self, generic_env): |
| 38 | + generic_env["CLOUDSMITH_OIDC_TOKEN"] = " \t\n" |
| 39 | + with mock.patch.dict("os.environ", generic_env, clear=True): |
| 40 | + detector = GenericDetector(context=CredentialContext()) |
| 41 | + assert detector.detect() is False |
| 42 | + |
| 43 | + |
| 44 | +class TestGetToken: |
| 45 | + def test_returns_token(self, generic_env): |
| 46 | + detector = GenericDetector(context=CredentialContext()) |
| 47 | + assert detector.get_token() == "the-jwt" |
| 48 | + |
| 49 | + def test_strips_surrounding_whitespace(self, generic_env): |
| 50 | + generic_env["CLOUDSMITH_OIDC_TOKEN"] = " the-jwt\n" |
| 51 | + with mock.patch.dict("os.environ", generic_env, clear=True): |
| 52 | + detector = GenericDetector(context=CredentialContext()) |
| 53 | + assert detector.get_token() == "the-jwt" |
| 54 | + |
| 55 | + def test_raises_when_token_missing(self): |
| 56 | + with mock.patch.dict("os.environ", {}, clear=True): |
| 57 | + detector = GenericDetector(context=CredentialContext()) |
| 58 | + with pytest.raises(ValueError, match="CLOUDSMITH_OIDC_TOKEN"): |
| 59 | + detector.get_token() |
| 60 | + |
| 61 | + def test_raises_when_token_whitespace_only(self, generic_env): |
| 62 | + generic_env["CLOUDSMITH_OIDC_TOKEN"] = " " |
| 63 | + with mock.patch.dict("os.environ", generic_env, clear=True): |
| 64 | + detector = GenericDetector(context=CredentialContext()) |
| 65 | + with pytest.raises(ValueError, match="CLOUDSMITH_OIDC_TOKEN"): |
| 66 | + detector.get_token() |
| 67 | + |
| 68 | + |
| 69 | +class TestIntegration: |
| 70 | + def test_detect_environment_selects_generic(self, generic_env): |
| 71 | + detector = detect_environment(CredentialContext()) |
| 72 | + assert isinstance(detector, GenericDetector) |
0 commit comments