Skip to content
Snippets Groups Projects
Select Git revision
  • f0075ef55b562208077fb969979fac3f9e4d9a63
  • develop default
  • master protected
  • async-provision
  • DBOARD3-1252/inventory-api
  • 0.90
  • 0.89
  • 0.88
  • 0.87
  • 0.86
  • 0.85
  • 0.84
  • 0.83
  • 0.82
  • 0.81
  • 0.80
  • 0.79
  • 0.78
  • 0.77
  • 0.76
  • 0.75
  • 0.74
  • 0.73
  • 0.72
  • 0.71
25 results

test_grafana_dashboard.py

Blame
  • test_grafana_dashboard.py 5.56 KiB
    import pytest
    import json
    import requests
    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(
            method=responses.GET,
            url=request.BASE_URL + f'api/dashboards/uid/{UID+1}',
            json={'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(
            method=responses.GET,
            url=request.BASE_URL + f'api/dashboards/uid/{UID}',
            json=dashboards[0])
    
        responses.add(
            method=responses.GET,
            url=request.BASE_URL + 'api/search',
            json=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, {}, ''))
    
        with pytest.raises(requests.HTTPError):
            data = dashboard._delete_dashboard(request, UID + 1)
    
    
    @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')
    
        responses.add(
            method=responses.DELETE,
            url=request.BASE_URL + f'api/dashboards/uid/{UID}',
            json={'message': 'deleted dashboard'})
    
        responses.add(
            method=responses.GET,
            url=request.BASE_URL + f'api/dashboards/uid/{UID}',
            json={})
    
        responses.add(
            method=responses.GET,
            url=request.BASE_URL + 'api/search',
            json=[dash])
    
        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(
            method=responses.GET,
            url=request.BASE_URL + 'api/search',
            json=dashboards)
    
        responses.add(
            method=responses.GET,
            url=request.BASE_URL + f'api/dashboards/uid/{UID}',
            json=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')
    
        responses.add(
            method=responses.GET,
            url=request.BASE_URL + f'api/dashboards/uid/{UID}',
            json={'dashboard': dashboard})
    
        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