Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix tests to pass without cassettes #137

Merged
merged 2 commits into from
Feb 25, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 0 additions & 65 deletions tests/cassettes/filename.yaml

This file was deleted.

68 changes: 0 additions & 68 deletions tests/cassettes/filename_secret.yaml

This file was deleted.

71 changes: 30 additions & 41 deletions tests/cassettes/with_breaks.yaml
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
interactions:
- request:
body: '[{"filename": ".env", "document": "\n import urllib.request\n url
= ''http://jen_barber:[email protected]/isreal.json''\n response
= urllib.request.urlopen(url)\n consume(response.read())\"\n"}, {"document":
"\n import urllib.request\n url = ''http://jen_barber:[email protected]/isreal.json''\n response
body:
'[{"document": "\n import urllib.request\n url = ''http://jen_barber:[email protected]/isreal.json''\n response
= urllib.request.urlopen(url)\n consume(response.read())\"\n"}, {"filename":
"normal", "document": "normal"}]'
headers:
Expand All @@ -14,63 +12,54 @@ interactions:
Connection:
- keep-alive
Content-Length:
- '492'
- '259'
Content-Type:
- application/json
User-Agent:
- pygitguardian/1.3.4 (Linux;py3.8.10)
- pygitguardian/1.19.0 (Linux;py3.10.12)
method: POST
uri: https://api.gitguardian.com/v1/multiscan
response:
body:
string:
'[{"policy_break_count":2,"policies":["File extensions","Filenames","Secrets
detection"],"policy_breaks":[{"type":".env","policy":"Filenames","matches":[{"type":"filename","match":".env"}]},{"type":"Basic
Auth String","policy":"Secrets detection","matches":[{"type":"username","match":"jen_barber","index_start":40,"index_end":49,"line_start":2,"line_end":2},{"type":"password","match":"correcthorsebatterystaple","index_start":51,"index_end":75,"line_start":2,"line_end":2},{"type":"host","match":"cake.gitguardian.com","index_start":77,"index_end":96,"line_start":2,"line_end":2}],"validity":"no_checker"}]},{"policy_break_count":1,"policies":["File
extensions","Filenames","Secrets detection"],"policy_breaks":[{"type":"Basic
Auth String","policy":"Secrets detection","matches":[{"type":"username","match":"jen_barber","index_start":40,"index_end":49,"line_start":2,"line_end":2},{"type":"password","match":"correcthorsebatterystaple","index_start":51,"index_end":75,"line_start":2,"line_end":2},{"type":"host","match":"cake.gitguardian.com","index_start":77,"index_end":96,"line_start":2,"line_end":2}],"validity":"no_checker"}]},{"policy_break_count":0,"policies":["File
extensions","Filenames","Secrets detection"],"policy_breaks":[]}]'
'[{"policy_break_count":1,"policies":["Secrets detection"],"policy_breaks":[{"type":"Basic
Auth String","policy":"Secrets detection","matches":[{"type":"username","match":"jen_barber","index_start":45,"index_end":54,"line_start":3,"line_end":3},{"type":"password","match":"correcthorsebatterystaple","index_start":56,"index_end":80,"line_start":3,"line_end":3},{"type":"host","match":"cake.gitguardian.com","index_start":82,"index_end":101,"line_start":3,"line_end":3}],"is_excluded":false,"exclude_reason":null,"incident_url":"https://dashboard.gitguardian.com/workspace/8/incidents/10114685","known_secret":true,"validity":"no_checker","diff_kind":null}],"is_diff":false},{"policy_break_count":0,"policies":["Secrets
detection"],"policy_breaks":[],"is_diff":false}]'
headers:
Access-Control-Expose-Headers:
access-control-expose-headers:
- X-App-Version
Allow:
allow:
- POST, OPTIONS
Connection:
- keep-alive
Content-Type:
content-length:
- '766'
content-type:
- application/json
Date:
- Thu, 30 Jun 2022 10:29:32 GMT
Referrer-Policy:
cross-origin-opener-policy:
- same-origin
date:
- Mon, 24 Feb 2025 12:49:30 GMT
referrer-policy:
- strict-origin-when-cross-origin
Server:
- nginx
Set-Cookie:
- AWSALB=0NfNYw3UeyBFpnge2V9Eg/1mGjFw092I9Sz6A9XTSEcEwwY9QOaglYydWG0j9lP08vyA+Lbr2GKW5wEsC4ps3gWBR4xR/t2cWAZs9ce2iFiqaodevlC5KyXGb6kC;
Expires=Thu, 07 Jul 2022 10:29:32 GMT; Path=/
- AWSALBCORS=0NfNYw3UeyBFpnge2V9Eg/1mGjFw092I9Sz6A9XTSEcEwwY9QOaglYydWG0j9lP08vyA+Lbr2GKW5wEsC4ps3gWBR4xR/t2cWAZs9ce2iFiqaodevlC5KyXGb6kC;
Expires=Thu, 07 Jul 2022 10:29:32 GMT; Path=/; SameSite=None; Secure
Strict-Transport-Security:
server:
- istio-envoy
strict-transport-security:
- max-age=31536000; includeSubDomains
Transfer-Encoding:
- chunked
Vary:
- Accept-Encoding
vary:
- Cookie
X-App-Version:
- v2.8.1
X-Content-Type-Options:
x-app-version:
- v2.155.0
x-content-type-options:
- nosniff
- nosniff
X-Frame-Options:
x-envoy-upstream-service-time:
- '490'
x-frame-options:
- DENY
- SAMEORIGIN
X-Secrets-Engine-Version:
- 2.70.0
X-XSS-Protection:
x-secrets-engine-version:
- 2.132.2
x-xss-protection:
- 1; mode=block
content-length:
- '1240'
status:
code: 200
message: OK
Expand Down
110 changes: 21 additions & 89 deletions tests/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,56 +72,6 @@
"""
EXAMPLE_RESPONSE = """
[{
"policy_break_count": 2,
"policies": [
"Filenames",
"File extensions",
"Secrets detection"
],
"policy_breaks": [
{
"type": ".env",
"policy": "Filenames",
"matches": [
{
"type": "filename",
"match": ".env"
}
]
},
{
"type": "Basic Auth String",
"policy": "Secrets detection",
"matches": [
{
"type": "username",
"match": "jen_barber",
"index_start": 36,
"index_end": 45,
"line_start": 2,
"line_end": 2
},
{
"type": "password",
"match": "correcthorsebatterystaple",
"index_start": 47,
"index_end": 71,
"line_start": 2,
"line_end": 2
},
{
"type": "host",
"match": "cake.gitguardian.com",
"index_start": 73,
"index_end": 92,
"line_start": 2,
"line_end": 2
}
]
}
]
},
{
"policy_break_count": 1,
"policies": [
"Filenames",
Expand Down Expand Up @@ -322,7 +272,6 @@ def test_health_check(client: GGClient):
pytest.param(
"with_breaks",
[
{"filename": FILENAME, "document": DOCUMENT},
{"document": DOCUMENT},
{"filename": "normal", "document": "normal"},
],
Expand All @@ -341,7 +290,6 @@ def test_health_check(client: GGClient):
),
],
)
@my_vcr.use_cassette
def test_multi_content_scan(
client: GGClient,
name: str,
Expand All @@ -353,28 +301,23 @@ def test_multi_content_scan(
with my_vcr.use_cassette(name + ".yaml"):
multiscan = client.multi_content_scan(to_scan)

assert multiscan.status_code == 200
if not isinstance(multiscan, MultiScanResult):
pytest.fail("multiscan is not a MultiScanResult")
return

assert type(multiscan.to_dict()) == OrderedDict
assert type(multiscan.to_json()) == str
assert type(repr(multiscan)) == str
assert type(str(multiscan)) == str
assert multiscan.has_secrets == has_secrets
assert multiscan.has_policy_breaks == has_policy_breaks

for i, scan_result in enumerate(multiscan.scan_results):
if expected:
example_dict = json.loads(expected)
assert all(
elem in example_dict[i]["policies"] for elem in scan_result.policies
)
assert (
scan_result.policy_break_count
== example_dict[i]["policy_break_count"]
)
assert multiscan.status_code == 200
assert isinstance(multiscan, MultiScanResult)

assert type(multiscan.to_dict()) == OrderedDict
assert type(multiscan.to_json()) == str
assert type(repr(multiscan)) == str
assert type(str(multiscan)) == str
assert multiscan.has_secrets == has_secrets
assert multiscan.has_policy_breaks == has_policy_breaks

if not expected:
return

example_dict = json.loads(expected)
for i, scan_result in enumerate(multiscan.scan_results):
assert all(elem in example_dict[i]["policies"] for elem in scan_result.policies)
assert scan_result.policy_break_count == example_dict[i]["policy_break_count"]


@patch("pygitguardian.config.DOCUMENT_SIZE_THRESHOLD_BYTES", 20)
Expand Down Expand Up @@ -438,15 +381,12 @@ def test_content_not_ok():
"name, to_scan, policy_break_count, has_secrets, has_policy_breaks",
[
pytest.param(
"filename_secret",
{"filename": FILENAME, "document": DOCUMENT},
2,
"secret",
{"document": DOCUMENT},
1,
True,
True,
id="filename_secret",
),
pytest.param(
"secret", {"document": DOCUMENT}, 1, True, True, id="secret (deprecated)"
id="secret (deprecated)",
),
pytest.param(
"secret_validity",
Expand All @@ -464,14 +404,6 @@ def test_content_not_ok():
False,
id="Document containing a 0 byte",
),
pytest.param(
"filename",
{"filename": FILENAME, "document": "normal"},
1,
False,
True,
id="filename",
),
pytest.param(
"no_breaks",
{"filename": "normal", "document": "normal"},
Expand Down
Loading