summaryrefslogtreecommitdiff
path: root/designateclient/tests/base.py
diff options
context:
space:
mode:
authorEndre Karlson <endre.karlson@hp.com>2014-11-13 09:56:24 +0100
committerEndre Karlson <endre.karlson@hp.com>2015-05-07 17:23:09 +0200
commit2a2c85ce21cd03ba871f0b44195e7ed22e680d62 (patch)
tree7924e0480efff1791cdbf5bb9dcd71fc83567953 /designateclient/tests/base.py
parent39d8b54d785bafd550a94b3d396ead8911557cfc (diff)
downloadpython-designateclient-2a2c85ce21cd03ba871f0b44195e7ed22e680d62.tar.gz
V2 Bindings
This provides bindings for: - zones - recordsets - tlds - blacklists - limits - nameservers With associated unit tests. Change-Id: Ie9b79340bd327b78916fd038633842da3ace881b
Diffstat (limited to 'designateclient/tests/base.py')
-rw-r--r--designateclient/tests/base.py101
1 files changed, 97 insertions, 4 deletions
diff --git a/designateclient/tests/base.py b/designateclient/tests/base.py
index 6e93268..8ebf5aa 100644
--- a/designateclient/tests/base.py
+++ b/designateclient/tests/base.py
@@ -1,7 +1,7 @@
# -*- coding: utf-8 -*-
# Copyright 2010-2011 OpenStack Foundation
-# Copyright (c) 2013 Hewlett-Packard Development Company, L.P.
+# Copyright (c) 2015 Hewlett-Packard Development Company, L.P.
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
@@ -14,16 +14,23 @@
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
-
+import json as json_
import os
import fixtures
-import testtools
+from keystoneclient import adapter
+from keystoneclient import session as keystone_session
+from oslotest import base as test
+from requests_mock.contrib import fixture as req_fixture
+import six
+from six.moves.urllib import parse as urlparse
+
+from designateclient import client
_TRUE_VALUES = ('True', 'true', '1', 'yes')
-class TestCase(testtools.TestCase):
+class TestCase(test.BaseTestCase):
"""Test case base class for all unit tests."""
@@ -51,3 +58,89 @@ class TestCase(testtools.TestCase):
self.useFixture(fixtures.MonkeyPatch('sys.stderr', stderr))
self.log_fixture = self.useFixture(fixtures.FakeLogger())
+
+
+class APITestCase(TestCase):
+ """Test case base class for all unit tests."""
+
+ TEST_URL = "http://127.0.0.1:9001/"
+ VERSION = None
+
+ def setUp(self):
+ """Run before each test method to initialize test environment."""
+ super(TestCase, self).setUp()
+ self.log_fixture = self.useFixture(fixtures.FakeLogger())
+
+ self.requests = self.useFixture(req_fixture.Fixture())
+ self.client = self.get_client()
+
+ def get_base(self, base_url=None):
+ if not base_url:
+ base_url = '%sv%s' % (self.TEST_URL, self.VERSION)
+ return base_url
+
+ def stub_url(self, method, parts=None, base_url=None, json=None, **kwargs):
+ base_url = self.get_base(base_url)
+
+ if json:
+ kwargs['text'] = json_.dumps(json)
+ headers = kwargs.setdefault('headers', {})
+ headers['Content-Type'] = 'application/json'
+
+ if parts:
+ url = '/'.join([p.strip('/') for p in [base_url] + parts])
+ else:
+ url = base_url
+
+ url = url.replace("/?", "?")
+ self.requests.register_uri(method, url, **kwargs)
+
+ def get_client(self, version=None, session=None):
+ version = version or self.VERSION
+ session = session or keystone_session.Session()
+ adapted = adapter.Adapter(
+ session=session, endpoint_override=self.get_base())
+ return client.Client(version, session=adapted)
+
+ def assertRequestBodyIs(self, body=None, json=None):
+ last_request_body = self.requests.last_request.body
+ if json:
+ val = json_.loads(last_request_body)
+ self.assertEqual(json, val)
+ elif body:
+ self.assertEqual(body, last_request_body)
+
+ def assertQueryStringIs(self, qs=''):
+ """Verify the QueryString matches what is expected.
+
+ The qs parameter should be of the format \'foo=bar&abc=xyz\'
+ """
+ expected = urlparse.parse_qs(qs, keep_blank_values=True)
+ parts = urlparse.urlparse(self.requests.last_request.url)
+ querystring = urlparse.parse_qs(parts.query, keep_blank_values=True)
+ self.assertEqual(expected, querystring)
+
+ def assertQueryStringContains(self, **kwargs):
+ """Verify the query string contains the expected parameters.
+
+ This method is used to verify that the query string for the most recent
+ request made contains all the parameters provided as ``kwargs``, and
+ that the value of each parameter contains the value for the kwarg. If
+ the value for the kwarg is an empty string (''), then all that's
+ verified is that the parameter is present.
+
+ """
+ parts = urlparse.urlparse(self.requests.last_request.url)
+ qs = urlparse.parse_qs(parts.query, keep_blank_values=True)
+
+ for k, v in six.iteritems(kwargs):
+ self.assertIn(k, qs)
+ self.assertIn(v, qs[k])
+
+ def assertRequestHeaderEqual(self, name, val):
+ """Verify that the last request made contains a header and its value
+
+ The request must have already been made.
+ """
+ headers = self.requests.last_request.headers
+ self.assertEqual(headers.get(name), val)