Skip to content

Commit 872ac34

Browse files
committed
Add Incident model tests.
1 parent a9fa044 commit 872ac34

File tree

1 file changed

+241
-0
lines changed

1 file changed

+241
-0
lines changed

tests/test_incident_models.py

Lines changed: 241 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,241 @@
1+
import pytest
2+
3+
from pygitguardian.incident_models import Detector, Incident, Match, Occurrence
4+
5+
6+
class TestModel:
7+
@pytest.mark.parametrize(
8+
"klass, instance_data",
9+
[
10+
(
11+
Detector,
12+
{
13+
"name": "slack_bot_token",
14+
"display_name": "Slack Bot Token",
15+
"nature": "specific",
16+
"family": "apikey",
17+
"detector_group_name": "slackbot_token",
18+
"detector_group_display_name": "Slack Bot Token",
19+
},
20+
),
21+
(
22+
Match,
23+
{
24+
"name": "apikey",
25+
"indice_start": 32,
26+
"indice_end": 79,
27+
"pre_line_start": None,
28+
"pre_line_end": None,
29+
"post_line_start": 1,
30+
"post_line_end": 1,
31+
},
32+
),
33+
(
34+
Occurrence,
35+
{
36+
"id": 4421,
37+
"incident_id": 3759,
38+
"kind": "realtime",
39+
"sha": "d670460b4b4aece5915caf5c68d12f560a9fe3e4",
40+
"source": {
41+
"id": 6531,
42+
"url": "https://github.com/GitGuardian/gg-shield",
43+
"type": "github",
44+
"full_name": "gitguardian/gg-shield",
45+
"health": "at_risk",
46+
"open_incidents_count": 3,
47+
"closed_incidents_count": 2,
48+
"visibility": "public",
49+
"external_id": "125",
50+
"last_scan": {
51+
"date": "2021-05-20T12:40:55.662949Z",
52+
"status": "finished",
53+
},
54+
},
55+
"author_name": "Eric",
56+
"author_info": "[email protected]",
57+
"date": "2021-05-20T12:40:55.662949Z",
58+
"presence": "present",
59+
"url": (
60+
"https://github.com/prm-dev-team/QATest_staging/commit/"
61+
"76dd18a2a8d27eaf00a45851cc7731c53b59ed19"
62+
"#diff-0f372f3171c8f13a15a22a1081487ed54fa70ad088e17c6c6386196a179a04ffR1"
63+
),
64+
"matches": [
65+
{
66+
"name": "apikey",
67+
"indice_start": 32,
68+
"indice_end": 79,
69+
"pre_line_start": None,
70+
"pre_line_end": None,
71+
"post_line_start": 1,
72+
"post_line_end": 1,
73+
}
74+
],
75+
"filepath": "test_data/12123testfile.txt",
76+
},
77+
),
78+
(
79+
# Unresolved incident without ocurrences - returned from list endpoint
80+
Incident,
81+
{
82+
"id": 3759,
83+
"date": "2019-08-22T14:15:22Z",
84+
"detector": {
85+
"name": "slack_bot_token",
86+
"display_name": "Slack Bot Token",
87+
"nature": "specific",
88+
"family": "apikey",
89+
"detector_group_name": "slackbot_token",
90+
"detector_group_display_name": "Slack Bot Token",
91+
},
92+
"secret_hash": "Ri9FjVgdOlPnBmujoxP4XPJcbe82BhJXB/SAngijw/juCISuOMgPzYhV28m6OG24",
93+
"gitguardian_url": "https://dashboard.gitguardian.com/workspace/1/incidents/3899",
94+
"regression": False,
95+
"status": "IGNORED",
96+
"assignee_email": "[email protected]",
97+
"occurrences_count": 4,
98+
"occurrences": None,
99+
"ignore_reason": "test_credential",
100+
"ignored_at": "2019-08-24T14:15:22Z",
101+
"secret_revoked": False,
102+
"severity": "high",
103+
"validity": "valid",
104+
"resolved_at": None,
105+
"share_url": (
106+
"https://dashboard.gitguardian.com"
107+
"/share/incidents/11111111-1111-1111-1111-111111111111"
108+
),
109+
"tags": ["FROM_HISTORICAL_SCAN", "SENSITIVE_FILE"],
110+
},
111+
),
112+
(
113+
# Resolved incident without ocurrences - returned from list endpoint
114+
Incident,
115+
{
116+
"id": 3759,
117+
"date": "2019-08-22T14:15:22Z",
118+
"detector": {
119+
"name": "slack_bot_token",
120+
"display_name": "Slack Bot Token",
121+
"nature": "specific",
122+
"family": "apikey",
123+
"detector_group_name": "slackbot_token",
124+
"detector_group_display_name": "Slack Bot Token",
125+
},
126+
"secret_hash": "Ri9FjVgdOlPnBmujoxP4XPJcbe82BhJXB/SAngijw/juCISuOMgPzYhV28m6OG24",
127+
"gitguardian_url": "https://dashboard.gitguardian.com/workspace/1/incidents/3899",
128+
"regression": False,
129+
"status": "IGNORED",
130+
"assignee_email": "[email protected]",
131+
"occurrences_count": 4,
132+
"occurrences": None,
133+
"ignore_reason": "test_credential",
134+
"ignored_at": "2019-08-24T14:15:22Z",
135+
"secret_revoked": False,
136+
"severity": "high",
137+
"validity": "valid",
138+
"resolved_at": None,
139+
"share_url": (
140+
"https://dashboard.gitguardian.com"
141+
"/share/incidents/11111111-1111-1111-1111-111111111111"
142+
),
143+
"tags": ["FROM_HISTORICAL_SCAN", "SENSITIVE_FILE"],
144+
},
145+
),
146+
(
147+
# Ignored incident with ocurrences - returned from
148+
# /v1/incidents/secrets/{incident_id}
149+
Incident,
150+
{
151+
"id": 3759,
152+
"date": "2019-08-22T14:15:22Z",
153+
"detector": {
154+
"name": "slack_bot_token",
155+
"display_name": "Slack Bot Token",
156+
"nature": "specific",
157+
"family": "apikey",
158+
"detector_group_name": "slackbot_token",
159+
"detector_group_display_name": "Slack Bot Token",
160+
},
161+
"secret_hash": "Ri9FjVgdOlPnBmujoxP4XPJcbe82BhJXB/SAngijw/juCISuOMgPzYhV28m6OG24",
162+
"gitguardian_url": "https://dashboard.gitguardian.com/workspace/1/incidents/3899",
163+
"regression": False,
164+
"status": "IGNORED",
165+
"assignee_id": 309,
166+
"assignee_email": "[email protected]",
167+
"occurrences_count": 4,
168+
"occurrences": [
169+
{
170+
"id": 4421,
171+
"incident_id": 3759,
172+
"kind": "realtime",
173+
"sha": "d670460b4b4aece5915caf5c68d12f560a9fe3e4",
174+
"source": {
175+
"id": 6531,
176+
"url": "https://github.com/GitGuardian/gg-shield",
177+
"type": "github",
178+
"full_name": "gitguardian/gg-shield",
179+
"health": "at_risk",
180+
"open_incidents_count": 3,
181+
"closed_incidents_count": 2,
182+
"visibility": "public",
183+
"external_id": "125",
184+
"last_scan": {
185+
"date": "2021-05-20T12:40:55.662949Z",
186+
"status": "finished",
187+
},
188+
},
189+
"author_name": "Eric",
190+
"author_info": "[email protected]",
191+
"date": "2021-05-20T12:40:55.662949Z",
192+
"presence": "present",
193+
"url": (
194+
"https://github.com/prm-dev-team/QATest_staging/commit/"
195+
"76dd18a2a8d27eaf00a45851cc7731c53b59ed19"
196+
"#diff-0f372f3171c8f13a15a22a1081487ed54fa70ad088e17c6c6386196a179a04ffR1"
197+
),
198+
"matches": [
199+
{
200+
"name": "apikey",
201+
"indice_start": 32,
202+
"indice_end": 79,
203+
"pre_line_start": None,
204+
"pre_line_end": None,
205+
"post_line_start": 1,
206+
"post_line_end": 1,
207+
}
208+
],
209+
"filepath": "test_data/12123testfile.txt",
210+
}
211+
],
212+
"ignore_reason": "test_credential",
213+
"severity": "high",
214+
"validity": "valid",
215+
"ignored_at": "2019-08-24T14:15:22Z",
216+
"ignorer_id": 309,
217+
"ignorer_api_token_id": "fdf075f9-1662-4cf1-9171-af50568158a8",
218+
"resolver_id": 395,
219+
"resolver_api_token_id": "fdf075f9-1662-4cf1-9171-af50568158a8",
220+
"secret_revoked": False,
221+
"resolved_at": None,
222+
"share_url": (
223+
"https://dashboard.gitguardian.com"
224+
"/share/incidents/11111111-1111-1111-1111-111111111111"
225+
),
226+
"tags": ["FROM_HISTORICAL_SCAN", "SENSITIVE_FILE"],
227+
},
228+
),
229+
],
230+
)
231+
def test_schema_loads(self, klass, instance_data):
232+
"""
233+
GIVEN the right kwargs and an extra field in dict format
234+
WHEN loading using the schema
235+
THEN the extra field is not taken into account
236+
AND the result should be an instance of the expected class
237+
"""
238+
data = {**instance_data, "field": "extra"}
239+
240+
obj = klass.from_dict(data)
241+
assert isinstance(obj, klass)

0 commit comments

Comments
 (0)