Newer
Older
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
import json
import responses
from brian_dashboard_manager.grafana import dashboard, provision
from brian_dashboard_manager.grafana.utils.request import TokenRequest
@responses.activate
def test_get_dashboard(data_config):
UID = 1
request = TokenRequest(**data_config, token='test')
responses.add_callback(
method=responses.GET,
url=request.BASE_URL +
f'api/dashboards/uid/{UID}',
callback=lambda f: (
404,
{},
''))
data = dashboard._get_dashboard(request, UID)
assert data is None
responses.add_callback(method=responses.GET,
url=request.BASE_URL +
f'api/dashboards/uid/{UID+1}',
callback=lambda f: (200,
{},
json.dumps({"uid": 1})))
data = dashboard._get_dashboard(request, UID + 1)
assert data['uid'] == 1
@responses.activate
def test_delete_dashboards(data_config):
UID = 1
dashboards = [{'uid': UID}]
request = TokenRequest(**data_config, token='test')
responses.add_callback(
method=responses.GET,
url=request.BASE_URL +
f'api/dashboards/uid/{UID}',
callback=lambda f: (
200,
{},
json.dumps(
dashboards[0])))
responses.add_callback(
method=responses.GET,
url=request.BASE_URL +
'api/search',
callback=lambda f: (
200,
{},
json.dumps(dashboards)))
def delete_callback(request):
uid = request.path_url.split('/')[-1]
assert int(uid) == UID
return 200, {}, json.dumps({'message': 'Dashboard has been deleted.'})
responses.add_callback(
method=responses.DELETE,
url=request.BASE_URL +
f'api/dashboards/uid/{UID}',
callback=delete_callback)
data = dashboard.delete_dashboards(request)
assert data is True
responses.add_callback(
method=responses.DELETE,
url=request.BASE_URL +
f'api/dashboards/uid/{UID+1}',
callback=lambda f: (
400,
{},
''))
data = dashboard._delete_dashboard(request, UID + 1)
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
assert data is False
@responses.activate
def test_delete_dashboard(data_config):
UID = 1
ID = 1
VERSION = 1
FOLDER_ID = 1
TITLE = 'testdashboard'
dash = {'id': ID, 'uid': UID, 'title': TITLE, 'version': VERSION}
request = TokenRequest(**data_config, token='test')
def delete_callback(request):
return 200, {}, json.dumps({'message': 'deleted dashboard'})
responses.add_callback(method=responses.DELETE,
url=request.BASE_URL + f'api/dashboards/uid/{UID}',
callback=delete_callback)
def search_callback(request):
return 200, {}, json.dumps(dash)
responses.add_callback(method=responses.GET,
url=request.BASE_URL + 'api/search',
callback=search_callback)
deleted = dashboard.delete_dashboard(request, dash)
assert deleted
del dash['uid']
deleted = dashboard.delete_dashboard(request, dash, FOLDER_ID)
assert deleted
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
@responses.activate
def test_search_dashboard(data_config):
UID = 1
TITLE = 'testdashboard'
dashboards = [{'uid': UID, 'title': TITLE}]
request = TokenRequest(**data_config, token='test')
responses.add_callback(
method=responses.GET,
url=request.BASE_URL +
'api/search',
callback=lambda f: (
200,
{},
json.dumps(dashboards)))
responses.add_callback(
method=responses.GET,
url=request.BASE_URL +
f'api/dashboards/uid/{UID}',
callback=lambda f: (
200,
{},
json.dumps(
dashboards[0])))
data = dashboard._search_dashboard(
request, {'title': dashboards[0]['title']})
assert data['uid'] == UID
data = dashboard._search_dashboard(request, {'title': 'DoesNotExist'})
assert data is None
@responses.activate
def test_search_dashboard_error(data_config):
request = TokenRequest(**data_config, token='test')
responses.add_callback(
method=responses.GET,
url=request.BASE_URL + 'api/search', callback=lambda f: (400, {}, ''))
data = dashboard._search_dashboard(request, {'title': 'DoesNotExist'})
assert data is None
@responses.activate
def test_create_dashboard(data_config):
UID = 1
ID = 1
VERSION = 1
TITLE = 'testdashboard'
dashboard = {'id': ID, 'uid': UID, 'title': TITLE, 'version': VERSION}
request = TokenRequest(**data_config, token='test')
def get_callback(request):
return 200, {}, json.dumps({'dashboard': dashboard})
responses.add_callback(method=responses.GET,
url=request.BASE_URL + f'api/dashboards/uid/{UID}',
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
responses.add_callback(
method=responses.GET,
url=request.BASE_URL + 'api/search', callback=lambda f: (400, {}, ''))
def post_callback(request):
body = json.loads(request.body)
return 200, {}, json.dumps(body['dashboard'])
responses.add_callback(
method=responses.POST,
url=request.BASE_URL + 'api/dashboards/db', callback=post_callback)
data = provision.create_dashboard(request, dashboard)
assert data == dashboard
@responses.activate
def test_create_dashboard_no_uid_error(data_config):
ID = 1
VERSION = 1
TITLE = 'testdashboard'
dashboard = {'id': ID, 'title': TITLE, 'version': VERSION}
request = TokenRequest(**data_config, token='test')
responses.add_callback(
method=responses.GET,
url=request.BASE_URL + 'api/search', callback=lambda f: (400, {}, ''))
def post_callback(request):
body = json.loads(request.body)
# if a dashboard doesn't have an UID, the ID should not be sent to
# grafana.
assert 'id' not in body['dashboard']
# have already tested a successful response, respond with error here.
return 400, {}, ''
responses.add_callback(
method=responses.POST,
url=request.BASE_URL + 'api/dashboards/db', callback=post_callback)
data = provision.create_dashboard(request, dashboard)
assert data is None