forked from benjefferies/branch-protection-bot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_run.py
183 lines (150 loc) · 5.14 KB
/
test_run.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
import unittest
from unittest.mock import patch
from run import toggle_enforce_admin
class TestRun(unittest.TestCase):
@patch('run.login')
@patch('run.enable')
def test_should_always_enable_force_admins(self, enable, login):
# Given
options = DotDict({
'retries': 1,
'enforce_admins': 'true',
'owner': 'benjefferies',
'repo': 'branch-bot-protection',
})
# When
toggle_enforce_admin(options)
# Then
enable.assert_called_once()
login.return_value.repository.assert_called_once_with('benjefferies', 'branch-bot-protection')
@patch('run.enable')
def test_should_always_enable_force_admins_when_enabled(self, enable):
# Given
options = DotDict({
'retries': 1,
'enforce_admins': 'true',
'github_repository': 'benjefferies/branch-bot-protection'
})
# When
with patch('run.login') as mock:
mock.return_value\
.repository.return_value\
.branch.return_value\
.protection.return_value\
.enforce_admins.enabled = True
toggle_enforce_admin(options)
# Then
enable.assert_called_once()
@patch('run.login')
@patch('run.disable')
def test_should_always_disable_force_admins(self, disable, login):
# Given
options = DotDict({
'retries': 1,
'enforce_admins': 'false',
'github_repository': 'benjefferies/branch-bot-protection'
})
# When
toggle_enforce_admin(options)
# Then
disable.assert_called_once()
@patch('run.disable')
def test_should_always_disable_force_admins_when_disabled(self, disable):
# Given
options = DotDict({
'retries': 1,
'enforce_admins': 'false',
'github_repository': 'benjefferies/branch-bot-protection'
})
# When
with patch('run.login') as mock:
mock.return_value\
.repository.return_value\
.branch.return_value\
.protection.return_value\
.enforce_admins.enabled = False
toggle_enforce_admin(options)
# Then
disable.assert_called_once()
@patch('run.disable')
def test_should_disable_force_admins(self, disable):
# Given
options = DotDict({
'retries': 1,
'github_repository': 'benjefferies/branch-bot-protection'
})
# When
with patch('run.login') as mock:
mock.return_value\
.repository.return_value\
.branch.return_value\
.protection.return_value\
.enforce_admins.enabled = True
toggle_enforce_admin(options)
# Then
disable.assert_called_once()
@patch('run.enable')
def test_should_enable_force_admins(self, enable):
# Given
options = DotDict({
'retries': 1,
'github_repository': 'benjefferies/branch-bot-protection'
})
# When
with patch('run.login') as mock:
mock.return_value\
.repository.return_value\
.branch.return_value\
.protection.return_value\
.enforce_admins.enabled = False
toggle_enforce_admin(options)
# Then
enable.assert_called_once()
@patch('run.login')
@patch('run.enable')
def test_should_enable_force_admins_using_github_repository_environment_variables(self, enable, login):
# Given
options = DotDict({
'retries': 1,
'enforce_admins': 'true',
'github_repository': 'benjefferies/branch-bot-protection'
})
# When
toggle_enforce_admin(options)
# Then
enable.assert_called_once()
login.return_value.repository.assert_called_once_with('benjefferies', 'branch-bot-protection')
def test_should_error_when_no_github_repository_or_owner_and_repo(self):
# Given
options = DotDict({
'retries': 1,
'enforce_admins': 'true',
})
# When
to_error = lambda: toggle_enforce_admin(options)
# Then
self.assertRaises(RuntimeError, to_error)
@patch('run.login')
@patch('run.enable')
def test_should_use_owner_repo_over_github_repository(self, enable, login):
# Given
options = DotDict({
'retries': 1,
'enforce_admins': 'true',
'owner': 'benjefferies',
'repo': 'branch-protection-bot',
'github_repository': 'other/repo'
})
# When
toggle_enforce_admin(options)
# Then
enable.assert_called_once()
login.return_value.repository.assert_called_once_with('benjefferies', 'branch-protection-bot')
class DotDict(dict):
def __getattr__(self, key):
return self[key] if key in self else ''
def __setattr__(self, key, val):
if key in self.__dict__:
self.__dict__[key] = val
else:
self[key] = val