This repository has been archived by the owner on Oct 20, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
phabricator_fox_test.py
134 lines (114 loc) · 4.73 KB
/
phabricator_fox_test.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# TODO(colin): fix these lint errors (http://pep8.readthedocs.io/en/release-1.7.x/intro.html#error-codes)
# pep8-disable:E128
import unittest
import re
import main
import mock
import webapp2
import json
import phabricator_fox
# These example strings were taken from the logs of
# https://console.cloud.google.com/logs/viewer?project=khan-webhooks
ADDED_REVIEWER = ("amy added a reviewer for D33318: "
"Update hover interaction to match the spec: kimerie.")
REQUESTED_REVIEW = ("dhruv requested review of D41797: Increase "
"email spam time limit for devserver.")
class PhabFoxLogicTest(unittest.TestCase):
def test_added_reviewer(self):
match = re.match(phabricator_fox.MESSAGE_RX, ADDED_REVIEWER)
self.assertIsNone(match)
def test_requested_review(self):
match = re.match(phabricator_fox.MESSAGE_RX, REQUESTED_REVIEW)
self.assertIsNotNone(match)
class TestPhabricatorFoxHandlers(unittest.TestCase):
def setUp(self):
self.mock_send_to_slack = self.mock_function(
'main._build_slack_message', return_value="test message")
self.args = json.dumps({
'object': {
'phid': 'PHID-object'
},
'transactions': [
{
'phid': 'PHID-transaction'
}
]
})
def _activate_patcher(self, patcher):
"""Activate a patcher (returned by mock.patch()) for this test.
Returns the patched thing.
"""
self.addCleanup(patcher.stop)
return patcher.start()
def mock_function(self, function_spec, **kwargs):
"""Mock the given function for the duration of this test.
kwargs are passed to mock.patch. By default, we use autospec=True,
which patches it to a mock with the same methods/signature as the one
being replaced.
Returns the mock.
"""
# If you set spec or autospec, we assume you mean to override us.
if 'spec' not in kwargs and 'autospec' not in kwargs:
kwargs['autospec'] = True
m = self._activate_patcher(mock.patch(function_spec, **kwargs))
return m
def _get_response(self, args):
request = webapp2.Request.blank('/new-phabricator-feed')
request.method = 'POST'
request.body = args
response = request.get_response(main.app)
return response
def test_empty_transactions(self):
self.args = json.dumps({
'object': {
'phid': 'PHID-test'
},
'transactions': []
})
response = self._get_response(self.args)
self.assertEqual(response.status_int, 200)
self.assertEqual(self.mock_send_to_slack.call_count, 0)
def test_no_response(self):
self.mock_function(
'main._transaction_search_from_phids', return_value=None)
response = self._get_response(self.args)
self.assertEqual(response.status_int, 404)
self.assertEqual(self.mock_send_to_slack.call_count, 0)
def test_transaction_create(self):
self.mock_function(
'main._transaction_search_from_phids', return_value={
'data': [{'type': 'create', 'authorPHID': "PHID-user"}]})
self.mock_function('main._phid_query_from_phid', return_value={
"PHID-test": {
"phid": "PHID-test",
"uri": "https://test",
"name": "D123",
"fullName": "D123: test",
"status": "open"}})
self.mock_function(
'main._get_author_username', return_value="test user")
response = self._get_response(self.args)
self.assertEqual(response.status_int, 200)
self.assertEqual(self.mock_send_to_slack.call_count, 1)
self.assertEqual(
self.mock_send_to_slack.call_args_list[0][0][1], 'create')
def test_transaction_abandon(self):
self.mock_function(
'main._transaction_search_from_phids', return_value={
'data': [{'type': 'abandon', 'authorPHID': "PHID-user"}]})
self.mock_function('main._phid_query_from_phid', return_value={
"PHID-test": {
"phid": "PHID-test",
"uri": "https://test",
"name": "D123",
"fullName": "D123: test",
"status": "open"}})
self.mock_function(
'main._get_author_username', return_value="test user")
response = self._get_response(self.args)
self.assertEqual(response.status_int, 200)
self.assertEqual(self.mock_send_to_slack.call_count, 1)
self.assertEqual(
self.mock_send_to_slack.call_args_list[0][0][1], 'abandon')
if __name__ == '__main__':
unittest.main()