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) 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 @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}', callback=get_callback) 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