forked from jayfk/statuspage
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtests.py
203 lines (147 loc) · 6.41 KB
/
tests.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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
# -*- coding: utf-8 -*-
from __future__ import absolute_import, print_function, unicode_literals
import unittest
from datetime import datetime
from unittest import TestCase
from mock import patch, Mock
from click.testing import CliRunner
from statuspage import cli, update, create, iter_systems, get_severity, SYSTEM_LABEL_COLOR
from github import UnknownObjectException
class CLITestCase(TestCase):
def setUp(self):
self.patcher = patch('statuspage.Github')
self.gh = self.patcher.start()
# setup mocked label
self.label = Mock()
self.label.color = "171717"
self.label.name = "Website"
self.label1 = Mock()
self.label1.color = "171717"
self.label1.name = "API"
self.gh().get_user().get_repo().get_labels.return_value = [self.label, self.label1]
# set up mocked issue
self.issue = Mock()
self.issue.created_at = datetime.now()
self.issue.state = "open"
self.issue_label = Mock()
self.issue_label.color = "FF4D4D"
self.issue_label.name = "major outage"
self.issue.get_labels.return_value = [self.issue_label, self.label]
self.issue.user.login = "some-dude"
self.comment = Mock()
self.comment.user.login = "some-dude"
self.issue.get_comments.return_value = [self.comment, ]
self.issue1 = Mock()
self.issue1.created_at = datetime.now()
self.issue1.state = "open"
self.issue1.user.login = "some-dude"
self.issue1.get_labels.return_value = [self.issue_label, self.label1]
self.issue1.get_comments.return_value = [self.comment, ]
self.gh().get_user().get_repo().get_issues.return_value = [self.issue, self.issue1]
self.template = Mock()
self.template.decoded_content = b"some foo"
self.gh().get_user().get_repo().get_file_contents.return_value = self.template
self.gh().get_organization().get_repo().get_file_contents.return_value = self.template
self.collaborator = Mock()
self.collaborator.login = "some-dude"
self.gh().get_user().get_repo().get_collaborators.return_value = [self.collaborator,]
self.gh().get_organization().get_repo().get_collaborators.return_value = [self.collaborator,]
def tearDown(self):
self.patcher.stop()
@patch("statuspage.run_update")
def test_create(self, run_update):
label = Mock()
self.gh().get_user().create_repo().get_labels.return_value = [label,]
runner = CliRunner()
result = runner.invoke(
create,
["--name", "testrepo", "--token", "token", "--systems", "sys1,sys2"]
)
self.assertEqual(result.exit_code, 0)
self.gh.assert_called_with("token")
@patch("statuspage.run_update")
def test_create_org(self, run_update):
runner = CliRunner()
result = runner.invoke(
create,
["--name", "testrepo",
"--token", "token",
"--systems", "sys1,sys2",
"--org", "some"]
)
self.assertEqual(result.exit_code, 0)
self.gh.assert_called_with("token")
self.gh().get_organization.assert_called_with("some")
def test_update(self):
runner = CliRunner()
result = runner.invoke(update, ["--name", "testrepo", "--token", "token"])
self.assertEqual(result.exit_code, 0)
self.gh.assert_called_with("token")
self.gh().get_user().get_repo.assert_called_with(name="testrepo")
self.gh().get_user().get_repo().get_labels.assert_called_once_with()
def test_update_org(self):
runner = CliRunner()
result = runner.invoke(update, ["--name", "testrepo", "--token", "token", "--org", "some"])
self.assertEqual(result.exit_code, 0)
self.gh.assert_called_with("token")
self.gh().get_organization().get_repo.assert_called_with(name="testrepo")
self.gh().get_organization().get_repo().get_labels.assert_called_once_with()
def test_update_index_does_not_exist(self):
self.gh().get_user().get_repo().update_file.side_effect = UnknownObjectException(status=404, data="foo")
runner = CliRunner()
result = runner.invoke(update, ["--name", "testrepo", "--token", "token"])
self.assertEqual(result.exit_code, 0)
self.gh.assert_called_with("token")
self.gh().get_user().get_repo.assert_called_with(name="testrepo")
self.gh().get_user().get_repo().get_labels.assert_called_once_with()
self.gh().get_user().get_repo().create_file.assert_called_once_with(
branch='gh-pages',
content='some foo',
message='initial',
path='/index.html'
)
def test_update_non_labeled_issue_not_displayed(self):
self.issue.get_labels.return_value = []
runner = CliRunner()
result = runner.invoke(update, ["--name", "testrepo", "--token", "token"])
self.assertEqual(result.exit_code, 0)
# make sure that get_comments is not called for the first issue but for the second
self.issue.get_comments.assert_not_called()
self.issue1.get_comments.assert_called_once_with()
def test_update_non_colaborator_issue_not_displayed(self):
self.issue.user.login = "some-other-dude"
runner = CliRunner()
result = runner.invoke(update, ["--name", "testrepo", "--token", "token"])
self.assertEqual(result.exit_code, 0)
# make sure that get_comments is not called for the first issue but for the second
self.issue.get_comments.assert_not_called()
self.issue1.get_comments.assert_called_once_with()
class UtilTestCase(TestCase):
def test_iter_systems(self):
label1 = Mock()
label2 = Mock()
label1.name = "website"
label1.color = SYSTEM_LABEL_COLOR
self.assertEqual(
list(iter_systems([label1, label2])),
["website", ]
)
self.assertEqual(
list(iter_systems([label2])),
[]
)
def test_severity(self):
label1 = Mock()
label2 = Mock()
label1.color = "FF4D4D"
self.assertEqual(
get_severity([label1, label2]),
"major outage"
)
label1.color = "000000"
self.assertEqual(
get_severity([label1, label2]),
None
)
if __name__ == '__main__':
unittest.main()