|
| 1 | +import base64 |
| 2 | +import hashlib |
| 3 | + |
| 4 | +import pytest |
| 5 | + |
| 6 | +from socketdev import socketdev |
| 7 | +from socketdev.core.api import API |
| 8 | +from socketdev.core.retired_token import ( |
| 9 | + RETIRED_TOKEN_ERROR, |
| 10 | + _reject_basic_auth_token, |
| 11 | + _reject_retired_token, |
| 12 | +) |
| 13 | +from socketdev.core.token_selection import TOKEN_ENVIRONMENT_VARIABLES, select_token |
| 14 | + |
| 15 | + |
| 16 | +def test_retired_token_rejection_redacts_token_and_digest(): |
| 17 | + token = "synthetic-retired-token" |
| 18 | + digest = hashlib.sha256(token.encode()).hexdigest() |
| 19 | + |
| 20 | + with pytest.raises(ValueError) as exc_info: |
| 21 | + _reject_retired_token(token, digest) |
| 22 | + |
| 23 | + assert str(exc_info.value) == RETIRED_TOKEN_ERROR |
| 24 | + assert token not in str(exc_info.value) |
| 25 | + assert digest not in str(exc_info.value) |
| 26 | + |
| 27 | + |
| 28 | +def test_sdk_constructor_accepts_safe_explicit_token_without_network_access(): |
| 29 | + sdk = socketdev(token="synthetic-safe-token") |
| 30 | + |
| 31 | + assert sdk.token == "synthetic-safe-token:" |
| 32 | + |
| 33 | + |
| 34 | +def test_api_encode_key_preserves_each_input_byte_sequence(): |
| 35 | + bare_api = API() |
| 36 | + suffixed_api = API() |
| 37 | + bare_token = "synthetic-safe-token" |
| 38 | + suffixed_token = "synthetic-safe-token:" |
| 39 | + |
| 40 | + bare_api.encode_key(bare_token) |
| 41 | + suffixed_api.encode_key(suffixed_token) |
| 42 | + |
| 43 | + assert bare_api.encoded_key == base64.b64encode(bare_token.encode()).decode("ascii") |
| 44 | + assert suffixed_api.encoded_key == base64.b64encode(suffixed_token.encode()).decode("ascii") |
| 45 | + |
| 46 | + |
| 47 | +def test_api_rejects_retired_basic_username_with_password_suffix(): |
| 48 | + token = "synthetic-retired-token" |
| 49 | + digest = hashlib.sha256(token.encode()).hexdigest() |
| 50 | + |
| 51 | + with pytest.raises(ValueError, match="token has been retired"): |
| 52 | + _reject_basic_auth_token(f"{token}:password", digest) |
| 53 | + |
| 54 | + |
| 55 | +def test_token_selection_preserves_explicit_and_environment_precedence(): |
| 56 | + environment = { |
| 57 | + name: f"token-{index}" |
| 58 | + for index, name in enumerate(TOKEN_ENVIRONMENT_VARIABLES) |
| 59 | + } |
| 60 | + |
| 61 | + assert select_token("", environment) == "" |
| 62 | + assert select_token("explicit", environment) == "explicit" |
| 63 | + assert select_token(None, environment) == "token-0" |
| 64 | + assert select_token( |
| 65 | + None, |
| 66 | + {TOKEN_ENVIRONMENT_VARIABLES[0]: "", TOKEN_ENVIRONMENT_VARIABLES[1]: "next"}, |
| 67 | + ) == "next" |
| 68 | + assert select_token(None, {}) is None |
| 69 | + |
| 70 | + |
| 71 | +@pytest.mark.parametrize("token", [None, "different-token"]) |
| 72 | +def test_nonretired_token_is_accepted(token): |
| 73 | + digest = hashlib.sha256(b"synthetic-retired-token").hexdigest() |
| 74 | + _reject_retired_token(token, digest) |
| 75 | + |
| 76 | + |
| 77 | +def test_retired_token_comparison_trims_whitespace(): |
| 78 | + digest = hashlib.sha256(b"synthetic-retired-token").hexdigest() |
| 79 | + with pytest.raises(ValueError): |
| 80 | + _reject_retired_token(" \tsynthetic-retired-token\n", digest) |
| 81 | + |
| 82 | + |
| 83 | +@pytest.mark.parametrize("name", TOKEN_ENVIRONMENT_VARIABLES) |
| 84 | +def test_each_environment_source_is_supported(name): |
| 85 | + assert select_token(None, {name: "selected-token"}) == "selected-token" |
0 commit comments