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

Fixes #3445 - Add 2 more moderation milestones #3452

Merged
merged 6 commits into from
Aug 21, 2020
Merged
Show file tree
Hide file tree
Changes from 2 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
41 changes: 41 additions & 0 deletions tests/fixtures/webhooks/private_milestone_accepted_invalid.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
{
"action": "milestoned",
"issue": {
"title": "www.netflix.com - test private issue accepted",
"repository_url": "https://api.github.com/repos/webcompat/webcompat-tests-private",
"number": 600,
"body": "<!-- @browser: Firefox 55.0 -->\n<!-- @ua_header: Mozilla/5.0 (X11; Linux x86_64; rv:55.0) Gecko/20100101 Firefox/55.0 -->\n<!-- @reported_with: web -->\n<!-- @public_url: https://github.com/webcompat/webcompat-tests/issues/1 -->\n\n**URL**: https://www.netflix.com/",
"labels": [
{
"id": 1788251357,
"node_id": "MDU6TGFiZWwxNzg4MjUxMzU3",
"url": "https://api.github.com/repos/webcompat/webcompat-tests/labels/action-needsmoderation",
"name": "action-needsmoderation",
"color": "d36200",
"default": false,
"description": "issue in the process of being moderated"
}
],
"state": "open",
"milestone": {
"url": "https://api.github.com/repos/webcompat/webcompat-tests-private/milestones/2",
"html_url": "https://github.com/webcompat/webcompat-tests-private/milestone/2",
"labels_url": "https://api.github.com/repos/webcompat/webcompat-tests-private/milestones/2/labels",
"id": 5092463,
"node_id": "MDk6TWlsZXN0b25lNTA5MjQ2Mw==",
"number": 2,
"title": "accepted: invalid",
"description": "This is when a bug has been accepted for moderation",
"open_issues": 0,
"closed_issues": 2,
"state": "open",
"created_at": "2020-02-11T00:37:18Z",
"updated_at": "2020-02-11T18:34:04Z",
"due_on": null,
"closed_at": null
}
},
"milestone": {
"title": "accepted: invalid"
}
}
153 changes: 15 additions & 138 deletions tests/unit/test_webhook.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
"""Tests for our webhooks: HTTP handling and helper methods."""

import json
import logging
import os
import unittest
from unittest.mock import patch
Expand All @@ -21,23 +20,13 @@
from webcompat.db import Site
from webcompat.helpers import to_bytes
from webcompat.webhooks import helpers
from webcompat.webhooks.helpers import process_issue_action
from webcompat.webhooks.model import WebHookIssue


# The key is being used for testing and computing the signature.
# The key needs to be a bytes object
key = to_bytes(webcompat.app.config['HOOK_SECRET_KEY'])

# Some expected responses as tuples
accepted = ('Moderated issue accepted', 200, {'Content-Type': 'text/plain'})
rejected = ('Moderated issue rejected', 200, {'Content-Type': 'text/plain'})
boring = ('Not an interesting hook', 403, {'Content-Type': 'text/plain'})
gracias = ('gracias, amigo.', 200, {'Content-Type': 'text/plain'})
wrong_repo = ('Wrong repository', 403, {'Content-Type': 'text/plain'})
oops = ('oops', 400, {'Content-Type': 'text/plain'})
comment_added = ('public url added', 200, {'Content-Type': 'text/plain'})


# Some machinery for opening our test files
def event_data(filename):
Expand Down Expand Up @@ -422,6 +411,21 @@ def test_repo_scope_unknown(self):
actual = helpers.repo_scope(url)
self.assertEqual(expected, actual)

def test_prepare_invalid_issue(self):
"""Test we prepare the right payload for the invalid issue."""
expected = {'body': '<p>Thanks for the report, but this is not a Compatibility\nissue.</p><p>For this project we try to focus our effort on layouts, features\nor content that works as expected in one browser but not in another.\nClosing the issue as Invalid.</p>', # noqa
'milestone': 8,
'state': 'closed',
'title': 'Test title!'}
actual = helpers.prepare_invalid_issue('Test title!')
self.assertEqual(type(actual), dict)
self.assertEqual(actual, expected)

def test_prepare_invalid_issue_no_title(self):
"""Test we raise for a missing title arguement."""
with pytest.raises(ValueError) as excinfo:
helpers.prepare_invalid_issue()

def test_prepare_rejected_issue(self):
"""Test we prepare the right payload for the rejected issue."""
expected = {'body': "<p>The content of this issue doesn't meet our\n"
Expand All @@ -436,132 +440,5 @@ def test_prepare_rejected_issue(self):
self.assertEqual(actual, expected)


@patch('webcompat.webhooks.model.make_request')
def test_process_issue_action_right_repo(mock_mr):
"""Test that repository_url matches the CONFIG for public repo."""
mock_mr.return_value.status_code == 200
json_event, signature = event_data('new_event_valid.json')
payload = json.loads(json_event)
issue = WebHookIssue.from_dict(payload)
with webcompat.app.test_request_context():
rv = process_issue_action(issue)
assert rv == gracias


def test_process_issue_action_wrong_repo():
"""Test when repository_url differs from the CONFIG for public repo."""
json_event, signature = event_data('wrong_repo.json')
payload = json.loads(json_event)
issue = WebHookIssue.from_dict(payload)
with webcompat.app.test_request_context():
rv = process_issue_action(issue)
assert rv == wrong_repo


def test_process_issue_action_wrong_repo():
"""Test for issues in the wrong repo."""
json_event, signature = event_data(
'private_milestone_accepted_wrong_repo.json')
payload = json.loads(json_event)
issue = WebHookIssue.from_dict(payload)
with webcompat.app.test_request_context():
rv = process_issue_action(issue)
assert rv == wrong_repo


@patch('webcompat.webhooks.model.make_request')
def test_process_issue_action_acceptable_issue(mock_mr):
"""Test for acceptable issues from private repo."""
mock_mr.return_value.status_code == 200
json_event, signature = event_data('private_milestone_accepted.json')
payload = json.loads(json_event)
issue = WebHookIssue.from_dict(payload)
with webcompat.app.test_request_context():
rv = process_issue_action(issue)
assert rv == accepted


@patch('webcompat.webhooks.model.make_request')
def test_process_issue_action_private_issue_moderated_ok(mock_mr):
"""Test for private issue successfully moderated."""
mock_mr.return_value.status_code == 200
json_event, signature = event_data('private_milestone_accepted.json')
payload = json.loads(json_event)
issue = WebHookIssue.from_dict(payload)
with webcompat.app.test_request_context():
rv = process_issue_action(issue)
assert rv == accepted


@patch('webcompat.webhooks.model.make_request')
def test_process_issue_action_reject_issue(mock_mr):
"""Test for rejected issues from private repo."""
mock_mr.return_value.status_code == 200
json_event, signature = event_data('private_milestone_closed.json')
payload = json.loads(json_event)
issue = WebHookIssue.from_dict(payload)
with webcompat.app.test_request_context():
rv = process_issue_action(issue)
assert rv == rejected


@patch('webcompat.webhooks.model.make_request')
def test_process_issue_action_acceptable_issue_closed(mock_mr):
"""Test for accepted issues being closed."""
mock_mr.return_value.status_code == 200
json_event, signature = event_data(
'private_milestone_accepted_closed.json')
payload = json.loads(json_event)
issue = WebHookIssue.from_dict(payload)
with webcompat.app.test_request_context():
rv = process_issue_action(issue)
assert rv == boring


@patch('webcompat.webhooks.model.make_request')
def test_process_issue_action_comment_public_uri(mock_mr):
"""Test we are getting the right message on public uri comment."""
mock_mr.return_value.status_code == 200
json_event, signature = event_data('private_issue_opened.json')
payload = json.loads(json_event)
issue = WebHookIssue.from_dict(payload)
with webcompat.app.test_request_context():
rv = process_issue_action(issue)
assert rv == comment_added


@patch('webcompat.webhooks.model.make_request')
def test_process_issue_action_github_api_exception(mock_mr, caplog):
"""Test GitHub API exception handling.

Each of the test scenarios have the following:
issue_payload, expected_log, method
method is unused in the test, but is meant to provide context to
the reader for where the exception is happening.
"""
caplog.set_level(logging.INFO)
mock_mr.side_effect = HTTPError()
mock_mr.status_code = 400
scenarios = [
('private_milestone_accepted.json',
'private:moving to public failed', 'moderate_private_issue'),
('private_issue_no_source.json', 'comment failed',
'comment_public_uri'),
('new_event_valid.json', 'public:opened labels failed',
'tag_as_public'),
('private_milestone_closed.json',
'public rejection failed', 'reject_private_issue')
]
for scenario in scenarios:
issue_payload, expected_log, method = scenario
json_event, signature = event_data(issue_payload)
payload = json.loads(json_event)
issue = WebHookIssue.from_dict(payload)
with webcompat.app.test_request_context():
rv = process_issue_action(issue)
assert rv == oops
assert expected_log in caplog.text


if __name__ == '__main__':
unittest.main()
Loading