|
| 1 | +import hashlib |
| 2 | + |
| 3 | +import pytest |
| 4 | +from socketdev import socketdev |
| 5 | + |
| 6 | +from socketsecurity.config import ( |
| 7 | + API_TOKEN_ENVIRONMENT_VARIABLES, |
| 8 | + CliConfig, |
| 9 | + select_api_token, |
| 10 | +) |
| 11 | +from socketsecurity.core.cli_client import CliClient |
| 12 | +from socketsecurity.core.socket_config import SocketConfig |
| 13 | +from socketsecurity.core.tools.reachability import ReachabilityAnalyzer |
| 14 | +from socketsecurity.retired_api_token import ( |
| 15 | + RETIRED_API_TOKEN_ERROR, |
| 16 | + _matches_retired_digest, |
| 17 | + _reject_retired_api_token, |
| 18 | +) |
| 19 | + |
| 20 | + |
| 21 | +def test_retired_api_token_rejection_redacts_token_and_digest(): |
| 22 | + token = "synthetic-retired-token" |
| 23 | + digest = hashlib.sha256(token.encode()).hexdigest() |
| 24 | + |
| 25 | + with pytest.raises(ValueError) as exc_info: |
| 26 | + _reject_retired_api_token(token, digest) |
| 27 | + |
| 28 | + assert str(exc_info.value) == RETIRED_API_TOKEN_ERROR |
| 29 | + assert token not in str(exc_info.value) |
| 30 | + assert digest not in str(exc_info.value) |
| 31 | + assert _matches_retired_digest(f" {token} ", digest) |
| 32 | + |
| 33 | + |
| 34 | +def test_cli_config_preserves_token_environment_precedence(monkeypatch): |
| 35 | + for name in API_TOKEN_ENVIRONMENT_VARIABLES: |
| 36 | + monkeypatch.delenv(name, raising=False) |
| 37 | + monkeypatch.setenv(API_TOKEN_ENVIRONMENT_VARIABLES[0], "first-env-token") |
| 38 | + monkeypatch.setenv(API_TOKEN_ENVIRONMENT_VARIABLES[1], "second-env-token") |
| 39 | + |
| 40 | + config = CliConfig.from_args(["--api-token", "argument-token"]) |
| 41 | + |
| 42 | + assert config.api_token == "first-env-token" |
| 43 | + |
| 44 | + |
| 45 | +def test_cli_config_uses_argument_when_token_environment_is_empty(monkeypatch): |
| 46 | + for name in API_TOKEN_ENVIRONMENT_VARIABLES: |
| 47 | + monkeypatch.delenv(name, raising=False) |
| 48 | + |
| 49 | + config = CliConfig.from_args(["--api-token", "argument-token"]) |
| 50 | + |
| 51 | + assert config.api_token == "argument-token" |
| 52 | + |
| 53 | + |
| 54 | +def test_cli_http_and_reachability_construction_accept_safe_token_without_network(): |
| 55 | + token = "synthetic-safe-token" |
| 56 | + config = SocketConfig(api_key=token) |
| 57 | + client = CliClient(config) |
| 58 | + sdk = socketdev(token=token) |
| 59 | + analyzer = ReachabilityAnalyzer(sdk, token) |
| 60 | + |
| 61 | + assert client.config.api_key == token |
| 62 | + assert analyzer.api_token == token |
| 63 | + |
| 64 | + |
| 65 | +@pytest.mark.parametrize("token", [None, "different-token"]) |
| 66 | +def test_nonretired_api_token_is_accepted(token): |
| 67 | + digest = hashlib.sha256(b"synthetic-retired-token").hexdigest() |
| 68 | + _reject_retired_api_token(token, digest) |
| 69 | + |
| 70 | + |
| 71 | +@pytest.mark.parametrize("name", API_TOKEN_ENVIRONMENT_VARIABLES) |
| 72 | +def test_each_cli_environment_source_is_supported(name): |
| 73 | + assert select_api_token({name: "selected-token"}, "argument-token") == "selected-token" |
0 commit comments