-
Notifications
You must be signed in to change notification settings - Fork 224
Expand file tree
/
Copy pathtest_validation.py
More file actions
211 lines (175 loc) · 7.61 KB
/
test_validation.py
File metadata and controls
211 lines (175 loc) · 7.61 KB
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
204
205
206
207
208
209
210
211
from pathlib import Path
from bugwarrior.config import validation
from bugwarrior.config.load import format_config, parse_file
from ..base import ConfigTest
class TestValidation(ConfigTest):
def setUp(self):
super().setUp()
self.config = {
'general': {'targets': ['my_service', 'my_kan', 'my_gitlab']},
'my_service': {
'service': 'github',
'login': 'ralph',
'username': 'ralph',
'token': 'abc123',
},
'my_kan': {
'service': 'kanboard',
'url': 'https://kanboard.example.org',
'username': 'ralph',
'password': 'abc123',
},
'my_gitlab': {
'service': 'gitlab',
'host': 'my-git.org',
'login': 'arbitrary_login',
'token': 'arbitrary_token',
'owned': 'false',
},
}
def validate_and_get_service(self, target):
conf = self.validate()
return next(
service_config
for service_config in conf.service_configs
if service_config.target == target
)
def test_valid(self):
self.validate()
def test_main_section_required(self):
del self.config['general']
with self.assertRaises(SystemExit):
formatted = format_config(self.config)
validation.validate_config(formatted, 'general', 'configpath')
self.assertEqual(len(self.caplog.records), 1)
self.assertIn("No section: 'general'", self.caplog.records[0].message)
def test_main_section_missing_targets_option(self):
del self.config['general']['targets']
self.assertValidationError("[general]\ntargets <- Field required")
def test_target_section_missing(self):
del self.config['my_service']
self.assertValidationError(
"[general]\ntargets = "
"['my_service', 'my_kan', 'my_gitlab']"
" <- No [my_service] section found"
)
def test_service_missing(self):
del self.config['my_service']['service']
self.assertValidationError("No option 'service' in section: 'my_service'")
def test_extra_field(self):
"""Undeclared fields are forbidden."""
self.config['my_service']['undeclared_field'] = 'extra'
self.assertValidationError(
'[my_service]\nundeclared_field = extra <- unrecognized option'
)
def test_root_validator(self):
del self.config['my_service']['username']
self.assertValidationError(
'[my_service] <- Value error, section requires one of:\n username\n query'
)
def test_no_scheme_url_validator_default(self):
service_config = self.validate_and_get_service("my_service")
self.assertEqual(service_config.host, 'github.com')
def test_no_scheme_url_validator_set(self):
self.config['my_service']['host'] = 'github.com'
service_config = self.validate_and_get_service("my_service")
self.assertEqual(service_config.host, 'github.com')
def test_no_scheme_url_validator_scheme(self):
self.config['my_service']['host'] = 'https://github.com'
self.assertValidationError(
"host = https://github.com <- URL should not include scheme ('https')"
)
def test_stripped_trailing_slash_url(self):
self.config['my_kan']['url'] = 'https://kanboard.example.org/'
service_config = self.validate_and_get_service("my_kan")
self.assertEqual(service_config.url, 'https://kanboard.example.org')
def test_deprecated_filter_merge_requests(self):
service_config = self.validate_and_get_service("my_gitlab")
self.assertEqual(service_config.include_merge_requests, True)
self.config['my_gitlab']['filter_merge_requests'] = 'true'
service_config = self.validate_and_get_service("my_gitlab")
self.assertEqual(service_config.include_merge_requests, False)
def test_deprecated_filter_merge_requests_and_include_merge_requests(self):
self.config['my_gitlab']['filter_merge_requests'] = 'true'
self.config['my_gitlab']['include_merge_requests'] = 'true'
self.assertValidationError(
'filter_merge_requests and include_merge_requests are incompatible.'
)
def test_deprecated_project_name(self):
"""We're just testing that deprecation doesn't break validation."""
self.config['general']['targets'] = [
'my_service',
'my_kan',
'my_gitlab',
'my_redmine',
]
self.config['my_redmine'] = {
'service': 'redmine',
'url': 'https://example.com',
'key': 'mykey',
}
self.validate()
self.config['my_redmine']['project_name'] = 'myproject'
self.validate()
def test_flavors(self):
self.config['flavor'] = {
'myflavor': {'targets': ['my_service', 'my_gitlab'], 'interactive': False}
}
self.validate()
def test_quoted_flavor_key_error(self):
"""Using ["flavor.myflavor"] instead of [flavor.myflavor] raises an error."""
self.config['flavor.myflavor'] = {'targets': ['my_service']}
self.assertValidationError(
'["flavor.myflavor"] <- Did you mean [flavor.myflavor]?'
)
def test_load_and_validate_example_files(self):
example_dir = Path(__file__).parent
config_files = [
example_dir / 'example-bugwarrior.toml',
example_dir / 'example-bugwarriorrc',
]
expected_by_flavor = {
'general': {
'GithubConfig',
'GitlabConfig',
'GmailConfig',
'JiraConfig',
'KanboardConfig',
'PhabricatorConfig',
'PivotalTrackerConfig',
'RedMineConfig',
'TracConfig',
},
'myflavor': {'GitlabConfig', 'JiraConfig', 'GithubConfig'},
}
for config_path in config_files:
for main_section, expected_configs in expected_by_flavor.items():
with self.subTest(config=config_path.name, main_section=main_section):
formatted_config = parse_file(str(config_path))
for flavor in formatted_config['flavor'].values():
flavor['interactive'] = False
config = validation.validate_config(
formatted_config, main_section, str(config_path)
)
self.assertEqual(
{conf.__class__.__name__ for conf in config.service_configs},
expected_configs,
)
def test_hooks_invalid_option(self):
self.config['hooks'] = {'invalid_option': 'value'}
self.assertValidationError(
'[hooks]\ninvalid_option = value <- unrecognized option'
)
def test_notifications_invalid_backend(self):
self.config['notifications'] = {'backend': 'invalid_backend'}
self.assertValidationError(
"[notifications]\nbackend = invalid_backend <- Input should be "
"'gobject', 'growlnotify' or 'applescript'"
)
def test_service_and_hooks_errors_reported_together(self):
del self.config['my_service']['service']
self.config['hooks'] = {'invalid_option': 'value'}
self.assertValidationError("No option 'service' in section: 'my_service'")
self.assertValidationError(
'[hooks]\ninvalid_option = value <- unrecognized option'
)