|
23 | 23 | MULTI_DOCUMENT_LIMIT,
|
24 | 24 | )
|
25 | 25 | from pygitguardian.models import (
|
| 26 | + ApiTokensResponse, |
26 | 27 | Detail,
|
27 | 28 | HoneytokenResponse,
|
28 | 29 | HoneytokenWithContextResponse,
|
@@ -867,6 +868,64 @@ def test_versions_from_headers(client: GGClient, method):
|
867 | 868 | assert other_client.secrets_engine_version == secrets_engine_version_value
|
868 | 869 |
|
869 | 870 |
|
| 871 | +@responses.activate |
| 872 | +@pytest.mark.parametrize("token", ["self", "token"]) |
| 873 | +def test_api_tokens(client: GGClient, token): |
| 874 | + """ |
| 875 | + GIVEN a ggclient |
| 876 | + WHEN calling api_tokens with or without a token |
| 877 | + THEN the method returns the token details |
| 878 | + """ |
| 879 | + mock_response = responses.get( |
| 880 | + url=client._url_from_endpoint(f"api_tokens/{token}", "v1"), |
| 881 | + content_type="application/json", |
| 882 | + status=201, |
| 883 | + json={ |
| 884 | + "id": "5ddaad0c-5a0c-4674-beb5-1cd198d13360", |
| 885 | + "name": "myTokenName", |
| 886 | + "workspace_id": 42, |
| 887 | + "type": "personal_access_token", |
| 888 | + "status": "revoked", |
| 889 | + "created_at": "2023-05-20T12:40:55.662949Z", |
| 890 | + "last_used_at": "2023-05-24T12:40:55.662949Z", |
| 891 | + "expire_at": None, |
| 892 | + "revoked_at": "2023-05-27T12:40:55.662949Z", |
| 893 | + "member_id": 22015, |
| 894 | + "creator_id": 22015, |
| 895 | + "scopes": ["incidents:read", "scan"], |
| 896 | + }, |
| 897 | + ) |
| 898 | + |
| 899 | + result = client.api_tokens(token) |
| 900 | + |
| 901 | + assert mock_response.call_count == 1 |
| 902 | + assert isinstance(result, ApiTokensResponse) |
| 903 | + |
| 904 | + |
| 905 | +@responses.activate |
| 906 | +def test_api_tokens_error( |
| 907 | + client: GGClient, |
| 908 | +): |
| 909 | + """ |
| 910 | + GIVEN a ggclient |
| 911 | + WHEN calling api_tokens with an invalid token |
| 912 | + THEN the method returns a Detail object containing the error detail |
| 913 | + """ |
| 914 | + mock_response = responses.get( |
| 915 | + url=client._url_from_endpoint("api_tokens/invalid", "v1"), |
| 916 | + content_type="application/json", |
| 917 | + status=400, |
| 918 | + json={ |
| 919 | + "detail": "Not authorized", |
| 920 | + }, |
| 921 | + ) |
| 922 | + |
| 923 | + result = client.api_tokens(token="invalid") |
| 924 | + |
| 925 | + assert mock_response.call_count == 1 |
| 926 | + assert isinstance(result, Detail) |
| 927 | + |
| 928 | + |
870 | 929 | @responses.activate
|
871 | 930 | def test_create_honeytoken(
|
872 | 931 | client: GGClient,
|
|
0 commit comments