-
Notifications
You must be signed in to change notification settings - Fork 8
/
test_service_account.py
310 lines (235 loc) · 11.4 KB
/
test_service_account.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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
from unittest import TestCase
from unittest.mock import MagicMock, patch
from grafana_api.model import APIModel
from grafana_api.service_account import ServiceAccount
class ServiceAccountTestCase(TestCase):
model: APIModel = APIModel(
host=MagicMock(), username=MagicMock(), password=MagicMock()
)
service_account: ServiceAccount = ServiceAccount(grafana_api_model=model)
@patch("grafana_api.api.Api.call_the_api")
def test_search_service_account(self, call_the_api_mock):
call_the_api_mock.return_value = dict({"totalCount": 2})
self.assertEqual(
dict({"totalCount": 2}),
self.service_account.search_service_account(),
)
@patch("grafana_api.api.Api.call_the_api")
def test_search_service_account_specify_query(self, call_the_api_mock):
call_the_api_mock.return_value = dict({"totalCount": 2})
self.assertEqual(
dict({"totalCount": 2}),
self.service_account.search_service_account(query="test"),
)
@patch("grafana_api.api.Api.call_the_api")
def test_search_service_account_no_valid_result(self, call_the_api_mock):
call_the_api_mock.return_value = dict()
with self.assertRaises(Exception):
self.service_account.search_service_account()
@patch("grafana_api.api.Api.call_the_api")
def test_create_service_account(self, call_the_api_mock):
call_the_api_mock.return_value = dict({"id": 2})
self.assertEqual(
dict({"id": 2}),
self.service_account.create_service_account("test", "test"),
)
@patch("grafana_api.api.Api.call_the_api")
def test_create_service_account_no_name(self, call_the_api_mock):
call_the_api_mock.return_value = dict({"id": 2})
with self.assertRaises(ValueError):
self.service_account.create_service_account("", ""),
@patch("grafana_api.api.Api.call_the_api")
def test_create_service_account_no_valid_result(self, call_the_api_mock):
call_the_api_mock.return_value = dict()
with self.assertRaises(Exception):
self.service_account.create_service_account("test", "test")
@patch("grafana_api.api.Api.call_the_api")
def test_get_service_account_by_id(self, call_the_api_mock):
call_the_api_mock.return_value = dict({"id": 2})
self.assertEqual(
dict({"id": 2}),
self.service_account.get_service_account_by_id(1),
)
@patch("grafana_api.api.Api.call_the_api")
def test_get_service_account_by_id_no_id(self, call_the_api_mock):
call_the_api_mock.return_value = dict({"id": 2})
with self.assertRaises(ValueError):
self.service_account.get_service_account_by_id(0),
@patch("grafana_api.api.Api.call_the_api")
def test_get_service_account_by_id_no_valid_result(self, call_the_api_mock):
call_the_api_mock.return_value = dict()
with self.assertRaises(Exception):
self.service_account.get_service_account_by_id(1)
@patch("grafana_api.api.Api.call_the_api")
def test_update_service_account(self, call_the_api_mock):
call_the_api_mock.return_value = dict({"id": 2})
self.assertEqual(
dict({"id": 2}),
self.service_account.update_service_account(1, "test", "test"),
)
@patch("grafana_api.api.Api.call_the_api")
def test_update_service_account_no_id(self, call_the_api_mock):
call_the_api_mock.return_value = dict({"id": 2})
with self.assertRaises(ValueError):
self.service_account.update_service_account(0, "", ""),
@patch("grafana_api.api.Api.call_the_api")
def test_update_service_account_no_valid_result(self, call_the_api_mock):
call_the_api_mock.return_value = dict()
with self.assertRaises(Exception):
self.service_account.update_service_account(1, "test", "test")
@patch("grafana_api.api.Api.call_the_api")
def test_delete_service_account_no_id(self, call_the_api_mock):
call_the_api_mock.return_value = dict({"status": 400})
with self.assertRaises(ValueError):
self.service_account.delete_service_account(0),
@patch("grafana_api.api.Api.call_the_api")
def test_delete_service_account_no_valid_result(self, call_the_api_mock):
call_the_api_mock.return_value = dict({"status": 400})
with self.assertRaises(Exception):
self.service_account.delete_service_account(1)
@patch("grafana_api.api.Api.call_the_api")
def test_delete_service_account(self, call_the_api_mock):
call_the_api_mock.return_value = dict({"message": "Service account deleted"})
self.assertEqual(
None,
self.service_account.delete_service_account(1),
)
@patch("grafana_api.api.Api.call_the_api")
def test_get_service_account_tokens_by_id(self, call_the_api_mock):
call_the_api_mock.return_value = list([{"id": 2}])
self.assertEqual(
list([{"id": 2}]),
self.service_account.get_service_account_tokens_by_id(1),
)
@patch("grafana_api.api.Api.call_the_api")
def test_get_service_account_tokens_by_id_no_id(self, call_the_api_mock):
call_the_api_mock.return_value = list([{"id": 2}])
with self.assertRaises(ValueError):
self.service_account.get_service_account_tokens_by_id(0),
@patch("grafana_api.api.Api.call_the_api")
def test_get_service_account_tokens_by_id_no_valid_result(self, call_the_api_mock):
call_the_api_mock.return_value = list([{"id": None}])
with self.assertRaises(Exception):
self.service_account.get_service_account_tokens_by_id(1)
@patch("grafana_api.api.Api.call_the_api")
def test_create_service_account_token_by_id(self, call_the_api_mock):
call_the_api_mock.return_value = dict({"id": 2})
self.assertEqual(
dict({"id": 2}),
self.service_account.create_service_account_token_by_id(1, "test", "test"),
)
@patch("grafana_api.api.Api.call_the_api")
def test_create_service_account_token_by_id_no_id(self, call_the_api_mock):
call_the_api_mock.return_value = dict({"id": 2})
with self.assertRaises(ValueError):
self.service_account.create_service_account_token_by_id(0, "", ""),
@patch("grafana_api.api.Api.call_the_api")
def test_create_service_account_token_by_id_no_valid_result(
self, call_the_api_mock
):
call_the_api_mock.return_value = dict()
with self.assertRaises(Exception):
self.service_account.create_service_account_token_by_id(1, "test", "test")
@patch("grafana_api.api.Api.call_the_api")
def test_delete_service_account_token_by_id(self, call_the_api_mock):
call_the_api_mock.return_value = dict(
{"message": "Service account token deleted"}
)
self.assertEqual(
None,
self.service_account.delete_service_account_token_by_id(1, 1),
)
@patch("grafana_api.api.Api.call_the_api")
def test_delete_service_account_token_by_id_no_id(self, call_the_api_mock):
call_the_api_mock.return_value = dict({"id": 2})
with self.assertRaises(ValueError):
self.service_account.delete_service_account_token_by_id(0, 0)
@patch("grafana_api.api.Api.call_the_api")
def test_delete_service_account_token_by_id_no_valid_result(
self, call_the_api_mock
):
call_the_api_mock.return_value = dict({"message": "Test"})
with self.assertRaises(Exception):
self.service_account.delete_service_account_token_by_id(1, 1)
@patch("grafana_api.api.Api.call_the_api")
def test_migrate_api_keys_to_service_accounts(self, call_the_api_mock):
call_the_api_mock.return_value = dict(
{"message": "API keys migrated to service accounts"}
)
self.assertEqual(
None,
self.service_account.migrate_api_keys_to_service_accounts(),
)
@patch("grafana_api.api.Api.call_the_api")
def test_migrate_api_keys_to_service_accounts_no_valid_result(
self, call_the_api_mock
):
call_the_api_mock.return_value = dict({"message": "Test"})
with self.assertRaises(Exception):
self.service_account.migrate_api_keys_to_service_accounts()
@patch("grafana_api.api.Api.call_the_api")
def test_migrate_api_key_to_service_account(self, call_the_api_mock):
call_the_api_mock.return_value = dict({"message": "Service accounts migrated"})
self.assertEqual(
None,
self.service_account.migrate_api_key_to_service_account(1),
)
@patch("grafana_api.api.Api.call_the_api")
def test_migrate_api_key_to_service_account_no_id(self, call_the_api_mock):
call_the_api_mock.return_value = dict({"message": "Test"})
with self.assertRaises(ValueError):
self.service_account.migrate_api_key_to_service_account(0)
@patch("grafana_api.api.Api.call_the_api")
def test_migrate_api_key_to_service_account_no_valid_result(
self, call_the_api_mock
):
call_the_api_mock.return_value = dict({"message": "Test"})
with self.assertRaises(Exception):
self.service_account.migrate_api_key_to_service_account(1)
@patch("grafana_api.api.Api.call_the_api")
def test_get_service_account_migration_status(self, call_the_api_mock):
call_the_api_mock.return_value = dict({"migrated": True})
self.assertEqual(
True,
self.service_account.get_service_account_migration_status(),
)
@patch("grafana_api.api.Api.call_the_api")
def test_get_service_account_migration_status_no_valid_result(
self, call_the_api_mock
):
call_the_api_mock.return_value = dict({"migrated": None})
with self.assertRaises(Exception):
self.service_account.get_service_account_migration_status()
@patch("grafana_api.api.Api.call_the_api")
def test_hide_the_api_keys_tab(self, call_the_api_mock):
call_the_api_mock.return_value = dict({"message": "API keys hidden"})
self.assertEqual(
None,
self.service_account.hide_the_api_keys_tab(),
)
@patch("grafana_api.api.Api.call_the_api")
def test_hide_the_api_keys_tab_internal_error(self, call_the_api_mock):
call_the_api_mock.return_value = dict({"message": "Test"})
with self.assertRaises(Exception):
self.service_account.hide_the_api_keys_tab()
@patch("grafana_api.api.Api.call_the_api")
def test_revert_service_account_token_api_key(self, call_the_api_mock):
call_the_api_mock.return_value = dict(
{"message": "reverted service account to API key"}
)
self.assertEqual(
None,
self.service_account.revert_service_account_token_to_api_key(1, 1),
)
@patch("grafana_api.api.Api.call_the_api")
def test_revert_service_account_token_api_key_no_id(self, call_the_api_mock):
call_the_api_mock.return_value = dict({"id": 2})
with self.assertRaises(ValueError):
self.service_account.revert_service_account_token_to_api_key(0, 0)
@patch("grafana_api.api.Api.call_the_api")
def test_revert_service_account_token_api_key_no_valid_result(
self, call_the_api_mock
):
call_the_api_mock.return_value = dict({"message": "Test"})
with self.assertRaises(Exception):
self.service_account.revert_service_account_token_to_api_key(1, 1)