1
1
from __future__ import annotations
2
2
3
+ from gidgethub import sansio
4
+
3
5
from django_github_app .commands import CommandScope
4
6
from django_github_app .commands import check_event_for_mention
5
7
from django_github_app .commands import check_event_scope
@@ -161,37 +163,51 @@ def test_special_character_command(self):
161
163
162
164
class TestCheckMentionMatches :
163
165
def test_match_with_command (self ):
164
- event = {"comment" : {"body" : "@bot help" }}
166
+ event = sansio .Event (
167
+ {"comment" : {"body" : "@bot help" }}, event = "issue_comment" , delivery_id = "123"
168
+ )
165
169
166
170
assert check_event_for_mention (event , "help" , "bot" ) is True
167
171
assert check_event_for_mention (event , "deploy" , "bot" ) is False
168
172
169
173
def test_match_without_command (self ):
170
- event = {"comment" : {"body" : "@bot help" }}
174
+ event = sansio .Event (
175
+ {"comment" : {"body" : "@bot help" }}, event = "issue_comment" , delivery_id = "123"
176
+ )
171
177
172
178
assert check_event_for_mention (event , None , "bot" ) is True
173
179
174
- event = {"comment" : {"body" : "no mention here" }}
180
+ event = sansio .Event (
181
+ {"comment" : {"body" : "no mention here" }},
182
+ event = "issue_comment" ,
183
+ delivery_id = "124" ,
184
+ )
175
185
176
186
assert check_event_for_mention (event , None , "bot" ) is False
177
187
178
188
def test_no_comment_body (self ):
179
- event = {}
189
+ event = sansio . Event ({}, event = "issue_comment" , delivery_id = "123" )
180
190
181
191
assert check_event_for_mention (event , "help" , "bot" ) is False
182
192
183
- event = {"comment" : {}}
193
+ event = sansio . Event ( {"comment" : {}}, event = "issue_comment" , delivery_id = "124" )
184
194
185
195
assert check_event_for_mention (event , "help" , "bot" ) is False
186
196
187
197
def test_case_insensitive_command_match (self ):
188
- event = {"comment" : {"body" : "@bot HELP" }}
198
+ event = sansio .Event (
199
+ {"comment" : {"body" : "@bot HELP" }}, event = "issue_comment" , delivery_id = "123"
200
+ )
189
201
190
202
assert check_event_for_mention (event , "help" , "bot" ) is True
191
203
assert check_event_for_mention (event , "HELP" , "bot" ) is True
192
204
193
205
def test_multiple_mentions (self ):
194
- event = {"comment" : {"body" : "@bot help @bot deploy" }}
206
+ event = sansio .Event (
207
+ {"comment" : {"body" : "@bot help @bot deploy" }},
208
+ event = "issue_comment" ,
209
+ delivery_id = "123" ,
210
+ )
195
211
196
212
assert check_event_for_mention (event , "help" , "bot" ) is True
197
213
assert check_event_for_mention (event , "deploy" , "bot" ) is True
@@ -201,82 +217,92 @@ def test_multiple_mentions(self):
201
217
class TestCheckEventScope :
202
218
def test_no_scope_allows_all_events (self ):
203
219
# When no scope is specified, all events should pass
204
- assert check_event_scope ("issue_comment" , {"issue" : {}}, None ) is True
205
- assert check_event_scope ("pull_request_review_comment" , {}, None ) is True
206
- assert check_event_scope ("commit_comment" , {}, None ) is True
220
+ event1 = sansio .Event ({"issue" : {}}, event = "issue_comment" , delivery_id = "1" )
221
+ assert check_event_scope (event1 , None ) is True
222
+
223
+ event2 = sansio .Event ({}, event = "pull_request_review_comment" , delivery_id = "2" )
224
+ assert check_event_scope (event2 , None ) is True
225
+
226
+ event3 = sansio .Event ({}, event = "commit_comment" , delivery_id = "3" )
227
+ assert check_event_scope (event3 , None ) is True
207
228
208
229
def test_issue_scope_on_issue_comment (self ):
209
230
# Issue comment on an actual issue (no pull_request field)
210
- issue_event = {"issue" : {"title" : "Bug report" }}
211
- assert (
212
- check_event_scope ("issue_comment" , issue_event , CommandScope .ISSUE ) is True
231
+ issue_event = sansio .Event (
232
+ {"issue" : {"title" : "Bug report" }}, event = "issue_comment" , delivery_id = "1"
213
233
)
234
+ assert check_event_scope (issue_event , CommandScope .ISSUE ) is True
214
235
215
236
# Issue comment on a pull request (has pull_request field)
216
- pr_event = {"issue" : {"title" : "PR title" , "pull_request" : {"url" : "..." }}}
217
- assert check_event_scope ("issue_comment" , pr_event , CommandScope .ISSUE ) is False
237
+ pr_event = sansio .Event (
238
+ {"issue" : {"title" : "PR title" , "pull_request" : {"url" : "..." }}},
239
+ event = "issue_comment" ,
240
+ delivery_id = "2" ,
241
+ )
242
+ assert check_event_scope (pr_event , CommandScope .ISSUE ) is False
218
243
219
244
def test_pr_scope_on_issue_comment (self ):
220
245
# Issue comment on an actual issue (no pull_request field)
221
- issue_event = {"issue" : {"title" : "Bug report" }}
222
- assert check_event_scope ("issue_comment" , issue_event , CommandScope .PR ) is False
246
+ issue_event = sansio .Event (
247
+ {"issue" : {"title" : "Bug report" }}, event = "issue_comment" , delivery_id = "1"
248
+ )
249
+ assert check_event_scope (issue_event , CommandScope .PR ) is False
223
250
224
251
# Issue comment on a pull request (has pull_request field)
225
- pr_event = {"issue" : {"title" : "PR title" , "pull_request" : {"url" : "..." }}}
226
- assert check_event_scope ("issue_comment" , pr_event , CommandScope .PR ) is True
252
+ pr_event = sansio .Event (
253
+ {"issue" : {"title" : "PR title" , "pull_request" : {"url" : "..." }}},
254
+ event = "issue_comment" ,
255
+ delivery_id = "2" ,
256
+ )
257
+ assert check_event_scope (pr_event , CommandScope .PR ) is True
227
258
228
259
def test_pr_scope_allows_pr_specific_events (self ):
229
260
# PR scope should allow pull_request_review_comment
230
- assert (
231
- check_event_scope ("pull_request_review_comment" , {}, CommandScope .PR )
232
- is True
233
- )
261
+ event1 = sansio .Event ({}, event = "pull_request_review_comment" , delivery_id = "1" )
262
+ assert check_event_scope (event1 , CommandScope .PR ) is True
234
263
235
264
# PR scope should allow pull_request_review
236
- assert check_event_scope ("pull_request_review" , {}, CommandScope .PR ) is True
265
+ event2 = sansio .Event ({}, event = "pull_request_review" , delivery_id = "2" )
266
+ assert check_event_scope (event2 , CommandScope .PR ) is True
237
267
238
268
# PR scope should not allow commit_comment
239
- assert check_event_scope ("commit_comment" , {}, CommandScope .PR ) is False
269
+ event3 = sansio .Event ({}, event = "commit_comment" , delivery_id = "3" )
270
+ assert check_event_scope (event3 , CommandScope .PR ) is False
240
271
241
272
def test_commit_scope_allows_commit_comment_only (self ):
242
273
# Commit scope should allow commit_comment
243
- assert check_event_scope ("commit_comment" , {}, CommandScope .COMMIT ) is True
274
+ event1 = sansio .Event ({}, event = "commit_comment" , delivery_id = "1" )
275
+ assert check_event_scope (event1 , CommandScope .COMMIT ) is True
244
276
245
277
# Commit scope should not allow issue_comment
246
- assert (
247
- check_event_scope ("issue_comment" , {"issue" : {}}, CommandScope .COMMIT )
248
- is False
249
- )
278
+ event2 = sansio .Event ({"issue" : {}}, event = "issue_comment" , delivery_id = "2" )
279
+ assert check_event_scope (event2 , CommandScope .COMMIT ) is False
250
280
251
281
# Commit scope should not allow PR events
252
- assert (
253
- check_event_scope ("pull_request_review_comment" , {}, CommandScope .COMMIT )
254
- is False
255
- )
282
+ event3 = sansio .Event ({}, event = "pull_request_review_comment" , delivery_id = "3" )
283
+ assert check_event_scope (event3 , CommandScope .COMMIT ) is False
256
284
257
285
def test_issue_scope_disallows_non_issue_events (self ):
258
286
# Issue scope should not allow pull_request_review_comment
259
- assert (
260
- check_event_scope ("pull_request_review_comment" , {}, CommandScope .ISSUE )
261
- is False
262
- )
287
+ event1 = sansio .Event ({}, event = "pull_request_review_comment" , delivery_id = "1" )
288
+ assert check_event_scope (event1 , CommandScope .ISSUE ) is False
263
289
264
290
# Issue scope should not allow commit_comment
265
- assert check_event_scope ("commit_comment" , {}, CommandScope .ISSUE ) is False
291
+ event2 = sansio .Event ({}, event = "commit_comment" , delivery_id = "2" )
292
+ assert check_event_scope (event2 , CommandScope .ISSUE ) is False
266
293
267
294
def test_pull_request_field_none_treated_as_issue (self ):
268
295
# If pull_request field exists but is None, treat as issue
269
- event_with_none_pr = {"issue" : {"title" : "Issue" , "pull_request" : None }}
270
- assert (
271
- check_event_scope ("issue_comment" , event_with_none_pr , CommandScope .ISSUE )
272
- is True
273
- )
274
- assert (
275
- check_event_scope ("issue_comment" , event_with_none_pr , CommandScope .PR )
276
- is False
296
+ event = sansio .Event (
297
+ {"issue" : {"title" : "Issue" , "pull_request" : None }},
298
+ event = "issue_comment" ,
299
+ delivery_id = "1" ,
277
300
)
301
+ assert check_event_scope (event , CommandScope .ISSUE ) is True
302
+ assert check_event_scope (event , CommandScope .PR ) is False
278
303
279
304
def test_missing_issue_data (self ):
280
305
# If issue data is missing entirely, default behavior
281
- assert check_event_scope ("issue_comment" , {}, CommandScope .ISSUE ) is True
282
- assert check_event_scope ("issue_comment" , {}, CommandScope .PR ) is False
306
+ event = sansio .Event ({}, event = "issue_comment" , delivery_id = "1" )
307
+ assert check_event_scope (event , CommandScope .ISSUE ) is True
308
+ assert check_event_scope (event , CommandScope .PR ) is False
0 commit comments