1
1
import json
2
- from typing import Dict , List
2
+ from typing import Dict , List , Type
3
3
from unittest .mock import patch
4
4
5
5
import pytest
6
6
from marshmallow import ValidationError
7
7
8
8
from pygitguardian import GGClient
9
+ from pygitguardian .client import is_ok , load_detail
9
10
from pygitguardian .config import (
10
11
DEFAULT_BASE_URI ,
11
12
DOCUMENT_SIZE_THRESHOLD_BYTES ,
183
184
],
184
185
)
185
186
def test_client_creation (
186
- api_key : str , uri : str , user_agent : str , timeout : float , exception : Exception
187
+ api_key : str , uri : str , user_agent : str , timeout : float , exception : Type [ Exception ]
187
188
):
188
189
if exception is not None :
189
190
with pytest .raises (exception ):
@@ -299,7 +300,7 @@ def test_multi_content_scan(
299
300
],
300
301
)
301
302
def test_content_scan_exceptions (
302
- client : GGClient , to_scan : str , exception : Exception , regex : str
303
+ client : GGClient , to_scan : str , exception : Type [ Exception ] , regex : str
303
304
):
304
305
with pytest .raises (exception , match = regex ):
305
306
client .content_scan (to_scan )
@@ -313,7 +314,7 @@ def test_content_scan_exceptions(
313
314
],
314
315
)
315
316
def test_multi_content_exceptions (
316
- client : GGClient , to_scan : List , exception : Exception
317
+ client : GGClient , to_scan : List , exception : Type [ Exception ]
317
318
):
318
319
with pytest .raises (exception ):
319
320
client .multi_content_scan (to_scan )
@@ -326,7 +327,7 @@ def test_multi_content_not_ok():
326
327
327
328
obj = client .multi_content_scan (req )
328
329
329
- assert obj .status_code , 401
330
+ assert obj .status_code == 401
330
331
assert isinstance (obj , Detail )
331
332
assert obj .detail == "Invalid API key."
332
333
@@ -338,7 +339,7 @@ def test_content_not_ok():
338
339
339
340
obj = client .content_scan (** req )
340
341
341
- assert obj .status_code , 401
342
+ assert obj .status_code == 401
342
343
assert isinstance (obj , Detail )
343
344
assert obj .detail == "Invalid API key."
344
345
@@ -401,5 +402,17 @@ def test_content_scan(
401
402
402
403
@my_vcr .use_cassette
403
404
def test_assert_content_type (client : GGClient ):
404
- with pytest .raises (TypeError ):
405
- client .get (endpoint = "/docs/static/logo.png" , version = None )
405
+ """
406
+ GIVEN a response that's 200 but the content is not JSON
407
+ WHEN is_ok is called
408
+ THEN is_ok should be false
409
+ WHEN load_detail is called
410
+ THEN is should return a Detail object
411
+ """
412
+ resp = client .get (endpoint = "/docs/static/logo.png" , version = None )
413
+ assert is_ok (resp ) is False
414
+ obj = load_detail (resp )
415
+ obj .status_code = resp .status_code
416
+ assert obj .status_code == 200
417
+ assert isinstance (obj , Detail )
418
+ assert str (obj ).startswith ("200:" ), str (obj )
0 commit comments