7
7
"""Tests for our webhooks: HTTP handling and helper methods."""
8
8
9
9
import json
10
- import logging
11
10
import os
12
11
import unittest
13
12
from unittest .mock import patch
21
20
from webcompat .db import Site
22
21
from webcompat .helpers import to_bytes
23
22
from webcompat .webhooks import helpers
24
- from webcompat .webhooks .helpers import process_issue_action
25
23
from webcompat .webhooks .model import WebHookIssue
26
24
27
25
28
26
# The key is being used for testing and computing the signature.
29
27
# The key needs to be a bytes object
30
28
key = to_bytes (webcompat .app .config ['HOOK_SECRET_KEY' ])
31
29
32
- # Some expected responses as tuples
33
- accepted = ('Moderated issue accepted' , 200 , {'Content-Type' : 'text/plain' })
34
- rejected = ('Moderated issue rejected' , 200 , {'Content-Type' : 'text/plain' })
35
- boring = ('Not an interesting hook' , 403 , {'Content-Type' : 'text/plain' })
36
- gracias = ('gracias, amigo.' , 200 , {'Content-Type' : 'text/plain' })
37
- wrong_repo = ('Wrong repository' , 403 , {'Content-Type' : 'text/plain' })
38
- oops = ('oops' , 400 , {'Content-Type' : 'text/plain' })
39
- comment_added = ('public url added' , 200 , {'Content-Type' : 'text/plain' })
40
-
41
30
42
31
# Some machinery for opening our test files
43
32
def event_data (filename ):
@@ -422,6 +411,32 @@ def test_repo_scope_unknown(self):
422
411
actual = helpers .repo_scope (url )
423
412
self .assertEqual (expected , actual )
424
413
414
+ def test_prepare_incomplete_issue (self ):
415
+ """Test we prepare the right payload for the incomplete issue."""
416
+ expected = {'body' : '<p>Thanks for the report. Unfortunately without any\n detail about the issue you experienced, we cannot help with this bug.\n Please leave a comment with more detail, or file a new report and we will\n gladly investigate this further.</p>' , # noqa
417
+ 'milestone' : 7 ,
418
+ 'state' : 'closed' ,
419
+ 'title' : 'Test incomplete title!' }
420
+ actual = helpers .prepare_incomplete_issue ('Test incomplete title!' )
421
+ self .assertEqual (type (actual ), dict )
422
+ self .assertEqual (actual , expected )
423
+
424
+ def test_prepare_invalid_issue (self ):
425
+ """Test we prepare the right payload for the invalid issue."""
426
+ expected = {'body' : '<p>Thanks for the report, but this is not a Compatibility\n issue.</p><p>For this project we try to focus our effort on layouts, features\n or content that works as expected in one browser but not in another.\n Closing the issue as Invalid.</p>' , # noqa
427
+ 'milestone' : 8 ,
428
+ 'state' : 'closed' ,
429
+ 'title' : 'Test invalid title!' }
430
+ actual = helpers .prepare_invalid_issue ('Test invalid title!' )
431
+ self .assertEqual (type (actual ), dict )
432
+ self .assertEqual (actual , expected )
433
+
434
+ def test_prepare_issue_no_title (self ):
435
+ """Test we raise for missing title arguements."""
436
+ with pytest .raises (ValueError ) as excinfo :
437
+ helpers .prepare_invalid_issue ()
438
+ helpers .prepare_incomplete_issue ()
439
+
425
440
def test_prepare_rejected_issue (self ):
426
441
"""Test we prepare the right payload for the rejected issue."""
427
442
expected = {'body' : "<p>The content of this issue doesn't meet our\n "
@@ -436,132 +451,5 @@ def test_prepare_rejected_issue(self):
436
451
self .assertEqual (actual , expected )
437
452
438
453
439
- @patch ('webcompat.webhooks.model.make_request' )
440
- def test_process_issue_action_right_repo (mock_mr ):
441
- """Test that repository_url matches the CONFIG for public repo."""
442
- mock_mr .return_value .status_code == 200
443
- json_event , signature = event_data ('new_event_valid.json' )
444
- payload = json .loads (json_event )
445
- issue = WebHookIssue .from_dict (payload )
446
- with webcompat .app .test_request_context ():
447
- rv = process_issue_action (issue )
448
- assert rv == gracias
449
-
450
-
451
- def test_process_issue_action_wrong_repo ():
452
- """Test when repository_url differs from the CONFIG for public repo."""
453
- json_event , signature = event_data ('wrong_repo.json' )
454
- payload = json .loads (json_event )
455
- issue = WebHookIssue .from_dict (payload )
456
- with webcompat .app .test_request_context ():
457
- rv = process_issue_action (issue )
458
- assert rv == wrong_repo
459
-
460
-
461
- def test_process_issue_action_wrong_repo ():
462
- """Test for issues in the wrong repo."""
463
- json_event , signature = event_data (
464
- 'private_milestone_accepted_wrong_repo.json' )
465
- payload = json .loads (json_event )
466
- issue = WebHookIssue .from_dict (payload )
467
- with webcompat .app .test_request_context ():
468
- rv = process_issue_action (issue )
469
- assert rv == wrong_repo
470
-
471
-
472
- @patch ('webcompat.webhooks.model.make_request' )
473
- def test_process_issue_action_acceptable_issue (mock_mr ):
474
- """Test for acceptable issues from private repo."""
475
- mock_mr .return_value .status_code == 200
476
- json_event , signature = event_data ('private_milestone_accepted.json' )
477
- payload = json .loads (json_event )
478
- issue = WebHookIssue .from_dict (payload )
479
- with webcompat .app .test_request_context ():
480
- rv = process_issue_action (issue )
481
- assert rv == accepted
482
-
483
-
484
- @patch ('webcompat.webhooks.model.make_request' )
485
- def test_process_issue_action_private_issue_moderated_ok (mock_mr ):
486
- """Test for private issue successfully moderated."""
487
- mock_mr .return_value .status_code == 200
488
- json_event , signature = event_data ('private_milestone_accepted.json' )
489
- payload = json .loads (json_event )
490
- issue = WebHookIssue .from_dict (payload )
491
- with webcompat .app .test_request_context ():
492
- rv = process_issue_action (issue )
493
- assert rv == accepted
494
-
495
-
496
- @patch ('webcompat.webhooks.model.make_request' )
497
- def test_process_issue_action_reject_issue (mock_mr ):
498
- """Test for rejected issues from private repo."""
499
- mock_mr .return_value .status_code == 200
500
- json_event , signature = event_data ('private_milestone_closed.json' )
501
- payload = json .loads (json_event )
502
- issue = WebHookIssue .from_dict (payload )
503
- with webcompat .app .test_request_context ():
504
- rv = process_issue_action (issue )
505
- assert rv == rejected
506
-
507
-
508
- @patch ('webcompat.webhooks.model.make_request' )
509
- def test_process_issue_action_acceptable_issue_closed (mock_mr ):
510
- """Test for accepted issues being closed."""
511
- mock_mr .return_value .status_code == 200
512
- json_event , signature = event_data (
513
- 'private_milestone_accepted_closed.json' )
514
- payload = json .loads (json_event )
515
- issue = WebHookIssue .from_dict (payload )
516
- with webcompat .app .test_request_context ():
517
- rv = process_issue_action (issue )
518
- assert rv == boring
519
-
520
-
521
- @patch ('webcompat.webhooks.model.make_request' )
522
- def test_process_issue_action_comment_public_uri (mock_mr ):
523
- """Test we are getting the right message on public uri comment."""
524
- mock_mr .return_value .status_code == 200
525
- json_event , signature = event_data ('private_issue_opened.json' )
526
- payload = json .loads (json_event )
527
- issue = WebHookIssue .from_dict (payload )
528
- with webcompat .app .test_request_context ():
529
- rv = process_issue_action (issue )
530
- assert rv == comment_added
531
-
532
-
533
- @patch ('webcompat.webhooks.model.make_request' )
534
- def test_process_issue_action_github_api_exception (mock_mr , caplog ):
535
- """Test GitHub API exception handling.
536
-
537
- Each of the test scenarios have the following:
538
- issue_payload, expected_log, method
539
- method is unused in the test, but is meant to provide context to
540
- the reader for where the exception is happening.
541
- """
542
- caplog .set_level (logging .INFO )
543
- mock_mr .side_effect = HTTPError ()
544
- mock_mr .status_code = 400
545
- scenarios = [
546
- ('private_milestone_accepted.json' ,
547
- 'private:moving to public failed' , 'moderate_private_issue' ),
548
- ('private_issue_no_source.json' , 'comment failed' ,
549
- 'comment_public_uri' ),
550
- ('new_event_valid.json' , 'public:opened labels failed' ,
551
- 'tag_as_public' ),
552
- ('private_milestone_closed.json' ,
553
- 'public rejection failed' , 'reject_private_issue' )
554
- ]
555
- for scenario in scenarios :
556
- issue_payload , expected_log , method = scenario
557
- json_event , signature = event_data (issue_payload )
558
- payload = json .loads (json_event )
559
- issue = WebHookIssue .from_dict (payload )
560
- with webcompat .app .test_request_context ():
561
- rv = process_issue_action (issue )
562
- assert rv == oops
563
- assert expected_log in caplog .text
564
-
565
-
566
454
if __name__ == '__main__' :
567
455
unittest .main ()
0 commit comments