From b5ecab6d05aaed8c266eef794bc94b0f39039b2d Mon Sep 17 00:00:00 2001 From: Paul Belanger Date: Thu, 1 Oct 2015 14:09:54 -0400 Subject: [PATCH] Refactor grafana.py test coverage Add 2 new tests for the __init__ function. Testing with / without the apikey. Also rework our requests_mock logic. Change-Id: Idea77b59b1ea2d6a2566e48810aa19c0455479ac Signed-off-by: Paul Belanger --- tests/test_grafana.py | 31 ++++++++++++++++++------------- 1 file changed, 18 insertions(+), 13 deletions(-) diff --git a/tests/test_grafana.py b/tests/test_grafana.py index bb613a2..beb0f7c 100644 --- a/tests/test_grafana.py +++ b/tests/test_grafana.py @@ -12,35 +12,40 @@ # License for the specific language governing permissions and limitations # under the License. -from requests_mock.contrib import fixture as rm_fixture +import requests_mock from testtools import TestCase -from grafana_dashboards import grafana +from grafana_dashboards.grafana import Grafana class TestCaseGrafana(TestCase): def setUp(self): super(TestCaseGrafana, self).setUp() - self.apikey = 'eyJrIjoiT0tTcG1pUlY2RnVKZTFVaDFsNFZXdE9ZWmNrMkZYbk' self.url = 'http://localhost' - self.grafana = grafana.Grafana(self.url, self.apikey) - def test_create_dashboard(self): - mock_requests = self.useFixture(rm_fixture.Fixture()) - mock_requests.post('/api/dashboards/db') + def test_init(self): + grafana = Grafana(self.url) + self.assertNotIn('Authorization', grafana.session.headers) + + def test_init_apikey(self): + apikey = 'eyJrIjoiT0tTcG1pUlY2RnVKZTFVaDFsNFZXdE9ZWmNrMkZYbk' + grafana = Grafana(self.url, apikey) + headers = grafana.session.headers + self.assertIn('Authorization', headers) + self.assertEqual(headers['Authorization'], 'Bearer %s' % apikey) + + @requests_mock.Mocker() + def test_create_dashboard_apikey(self, mock_requests): + grafana = Grafana(self.url) + mock_requests.register_uri('POST', '/api/dashboards/db') data = { "dashboard": { "title": "New dashboard", } } - self.grafana.create_dashboard(data) + grafana.create_dashboard(data) self.assertEqual(mock_requests.call_count, 1) headers = mock_requests.last_request.headers - self._test_headers(headers) - - def _test_headers(self, headers): - self.assertIn('Authorization', headers) - self.assertEqual(headers['Authorization'], 'Bearer %s' % self.apikey) self.assertIn('Content-Type', headers) self.assertEqual(headers['Content-Type'], 'application/json')