Merge "Add tests for YamlJsonSerializer"

This commit is contained in:
Jenkins 2016-08-04 20:49:37 +00:00 committed by Gerrit Code Review
commit 80ad3bc11f
5 changed files with 63 additions and 7 deletions

View File

@ -16,6 +16,7 @@ import json
import os
import betamax.serializers.base
import six
import yaml
@ -47,10 +48,11 @@ def _unicode_representer(dumper, uni):
def _indent_json(val):
if not val:
return ''
return json.dumps(
json.loads(val), indent=2,
separators=(',', ': '), sort_keys=False,
default=unicode)
default=six.text_type)
def _is_json_body(interaction):
@ -79,14 +81,14 @@ class YamlJsonSerializer(betamax.serializers.base.BaseSerializer):
"""Specialized Dumper which does nice blocks and unicode."""
yaml.representer.BaseRepresenter.represent_scalar = _represent_scalar
MyDumper.add_representer(unicode, _unicode_representer)
MyDumper.add_representer(six.text_type, _unicode_representer)
return yaml.dump(
cassette_data, Dumper=MyDumper, default_flow_style=False)
def deserialize(self, cassette_data):
try:
# There should be only one document
return list(yaml.load_all(cassette_data))[0]
return yaml.safe_load(cassette_data)
except yaml.error.YAMLError:
return {}

View File

@ -26,7 +26,7 @@ http_interactions:
Content-Type:
- application/json
method: POST
uri: http://keystonauth.betamax_test/v2.0/tokens
uri: http://keystoneauth.betamax_test/v2.0/tokens
response:
body:
string: |-
@ -86,7 +86,7 @@ http_interactions:
status:
message: OK
code: 200
url: http://keystonauth.betamax_test/v2.0/tokens
url: http://keystoneauth.betamax_test/v2.0/tokens
recorded_at: '2015-11-27T15:17:19'
recorded_with: betamax/0.5.1

View File

@ -0,0 +1 @@
{"http_interactions": [{"request": {"body": {"string": "{\"auth\": {\"tenantName\": \"test_tenant_name\", \"passwordCredentials\": {\"username\": \"test_user_name\", \"password\": \"test_password\"}}}", "encoding": "utf-8"}, "headers": {"Content-Length": ["128"], "Accept-Encoding": ["gzip, deflate"], "Accept": ["application/json"], "User-Agent": ["keystoneauth1"], "Connection": ["keep-alive"], "Content-Type": ["application/json"]}, "method": "POST", "uri": "http://keystoneauth.betamax_test/v2.0/tokens"}, "response": {"body": {"string": "{\"access\": {\"token\": {\"issued_at\": \"2015-11-27T15:17:19.755470\", \"expires\": \"2015-11-27T16:17:19Z\", \"id\": \"c000c5ee4ba04594a00886028584b50d\", \"tenant\": {\"description\": null, \"enabled\": true, \"id\": \"6932cad596634a61ac9c759fb91beef1\", \"name\": \"test_tenant_name\"}, \"audit_ids\": [\"jY3gYg_YTbmzY2a4ioGuCw\"]}, \"user\": {\"username\": \"test_user_name\", \"roles_links\": [], \"id\": \"96995e6cc15b40fa8e7cd762f6a5d4c0\", \"roles\": [{\"name\": \"_member_\"}], \"name\": \"67eff5f6-9477-4961-88b4-437e6596a795\"}, \"metadata\": {\"is_admin\": 0, \"roles\": [\"9fe2ff9ee4384b1894a90878d3e92bab\"]}}}", "encoding": null}, "headers": {"X-Openstack-Request-Id": ["req-f9e188b4-06fd-4a4c-a952-2315b368218c"], "Content-Length": ["2684"], "Connection": ["keep-alive"], "Date": ["Fri, 27 Nov 2015 15:17:19 GMT"], "Content-Type": ["application/json"], "Vary": ["X-Auth-Token"], "X-Distribution": ["Ubuntu"], "Server": ["Fake"]}, "status": {"message": "OK", "code": 200}, "url": "http://keystoneauth.betamax_test/v2.0/tokens"}, "recorded_at": "2015-11-27T15:17:19"}], "recorded_with": "betamax/0.5.1"}

View File

@ -25,7 +25,7 @@ class TestBetamaxFixture(testtools.TestCase):
TEST_USERNAME = 'test_user_name'
TEST_PASSWORD = 'test_password'
TEST_TENANT_NAME = 'test_tenant_name'
TEST_AUTH_URL = 'http://keystonauth.betamax_test/v2.0/'
TEST_AUTH_URL = 'http://keystoneauth.betamax_test/v2.0/'
V2_TOKEN = v2Fixtures.Token(tenant_name=TEST_TENANT_NAME,
user_name=TEST_USERNAME)

View File

@ -0,0 +1,53 @@
# 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
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# 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
import os
import testtools
import yaml
from keystoneauth1.fixture import serializer
class TestBetamaxSerializer(testtools.TestCase):
TEST_FILE = os.path.join(
os.path.dirname(os.path.abspath(__file__)),
'data', 'ksa_betamax_test_cassette.yaml')
TEST_JSON = os.path.join(
os.path.dirname(os.path.abspath(__file__)),
'data', 'ksa_serializer_data.json')
def setUp(self):
super(TestBetamaxSerializer, self).setUp()
self.serializer = serializer.YamlJsonSerializer()
def test_deserialize(self):
data = self.serializer.deserialize(open(self.TEST_FILE, 'r').read())
request = data['http_interactions'][0]['request']
self.assertEqual(
'http://keystoneauth.betamax_test/v2.0/tokens',
request['uri'])
payload = json.loads(request['body']['string'])
self.assertEqual('test_tenant_name', payload['auth']['tenantName'])
def test_serialize(self):
data = json.loads(open(self.TEST_JSON, 'r').read())
serialized = self.serializer.serialize(data)
data = yaml.load(serialized)
request = data['http_interactions'][0]['request']
self.assertEqual(
'http://keystoneauth.betamax_test/v2.0/tokens',
request['uri'])
payload = json.loads(request['body']['string'])
self.assertEqual('test_tenant_name', payload['auth']['tenantName'])