Skip to content

Commit 877909b

Browse files
committed
feat(api_token): Add tests for api_tokens method
1 parent 5808c2e commit 877909b

File tree

2 files changed

+79
-0
lines changed

2 files changed

+79
-0
lines changed

tests/test_client.py

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
MULTI_DOCUMENT_LIMIT,
2424
)
2525
from pygitguardian.models import (
26+
ApiTokensResponse,
2627
Detail,
2728
HoneytokenResponse,
2829
HoneytokenWithContextResponse,
@@ -867,6 +868,64 @@ def test_versions_from_headers(client: GGClient, method):
867868
assert other_client.secrets_engine_version == secrets_engine_version_value
868869

869870

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+
870929
@responses.activate
871930
def test_create_honeytoken(
872931
client: GGClient,

tests/test_models.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
import pytest
44

55
from pygitguardian.models import (
6+
ApiTokensResponse,
7+
ApiTokensResponseSchema,
68
Detail,
79
DetailSchema,
810
Document,
@@ -63,6 +65,24 @@ def test_document_handle_surrogates(self):
6365
OrderedDict,
6466
{"detail": "hello", "status_code": 200},
6567
),
68+
(
69+
ApiTokensResponseSchema,
70+
ApiTokensResponse,
71+
{
72+
"id": "5ddaad0c-5a0c-4674-beb5-1cd198d13360",
73+
"name": "myTokenName",
74+
"workspace_id": 42,
75+
"type": "personal_access_token",
76+
"status": "revoked",
77+
"created_at": "2023-05-20T12:40:55.662949Z",
78+
"last_used_at": "2023-05-24T12:40:55.662949Z",
79+
"expire_at": None,
80+
"revoked_at": "2023-05-27T12:40:55.662949Z",
81+
"member_id": 22015,
82+
"creator_id": 22015,
83+
"scopes": ["incidents:read", "scan"],
84+
},
85+
),
6686
(MatchSchema, Match, {"match": "hello", "type": "hello"}),
6787
(
6888
MultiScanResultSchema,

0 commit comments

Comments
 (0)