Browse Source

Merge "Add tests for YamlJsonSerializer"

Jenkins 2 years ago
parent
commit
80ad3bc11f

+ 6
- 4
keystoneauth1/fixture/serializer.py View File

@@ -16,6 +16,7 @@ import json
16 16
 import os
17 17
 
18 18
 import betamax.serializers.base
19
+import six
19 20
 import yaml
20 21
 
21 22
 
@@ -47,10 +48,11 @@ def _unicode_representer(dumper, uni):
47 48
 def _indent_json(val):
48 49
     if not val:
49 50
         return ''
51
+
50 52
     return json.dumps(
51 53
         json.loads(val), indent=2,
52 54
         separators=(',', ': '), sort_keys=False,
53
-        default=unicode)
55
+        default=six.text_type)
54 56
 
55 57
 
56 58
 def _is_json_body(interaction):
@@ -79,14 +81,14 @@ class YamlJsonSerializer(betamax.serializers.base.BaseSerializer):
79 81
             """Specialized Dumper which does nice blocks and unicode."""
80 82
 
81 83
         yaml.representer.BaseRepresenter.represent_scalar = _represent_scalar
82
-        MyDumper.add_representer(unicode, _unicode_representer)
84
+
85
+        MyDumper.add_representer(six.text_type, _unicode_representer)
83 86
 
84 87
         return yaml.dump(
85 88
             cassette_data, Dumper=MyDumper, default_flow_style=False)
86 89
 
87 90
     def deserialize(self, cassette_data):
88 91
         try:
89
-            # There should be only one document
90
-            return list(yaml.load_all(cassette_data))[0]
92
+            return yaml.safe_load(cassette_data)
91 93
         except yaml.error.YAMLError:
92 94
             return {}

+ 2
- 2
keystoneauth1/tests/unit/data/ksa_betamax_test_cassette.yaml View File

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

+ 1
- 0
keystoneauth1/tests/unit/data/ksa_serializer_data.json View File

@@ -0,0 +1 @@
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"}

+ 1
- 1
keystoneauth1/tests/unit/test_betamax_fixture.py View File

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

+ 53
- 0
keystoneauth1/tests/unit/test_betamax_serializer.py View File

@@ -0,0 +1,53 @@
1
+# Licensed under the Apache License, Version 2.0 (the "License"); you may
2
+# not use this file except in compliance with the License. You may obtain
3
+# a copy of the License at
4
+#
5
+#      http://www.apache.org/licenses/LICENSE-2.0
6
+#
7
+# Unless required by applicable law or agreed to in writing, software
8
+# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
9
+# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
10
+# License for the specific language governing permissions and limitations
11
+# under the License.
12
+
13
+import json
14
+import os
15
+
16
+import testtools
17
+import yaml
18
+
19
+from keystoneauth1.fixture import serializer
20
+
21
+
22
+class TestBetamaxSerializer(testtools.TestCase):
23
+
24
+    TEST_FILE = os.path.join(
25
+        os.path.dirname(os.path.abspath(__file__)),
26
+        'data', 'ksa_betamax_test_cassette.yaml')
27
+    TEST_JSON = os.path.join(
28
+        os.path.dirname(os.path.abspath(__file__)),
29
+        'data', 'ksa_serializer_data.json')
30
+
31
+    def setUp(self):
32
+        super(TestBetamaxSerializer, self).setUp()
33
+        self.serializer = serializer.YamlJsonSerializer()
34
+
35
+    def test_deserialize(self):
36
+        data = self.serializer.deserialize(open(self.TEST_FILE, 'r').read())
37
+        request = data['http_interactions'][0]['request']
38
+        self.assertEqual(
39
+            'http://keystoneauth.betamax_test/v2.0/tokens',
40
+            request['uri'])
41
+        payload = json.loads(request['body']['string'])
42
+        self.assertEqual('test_tenant_name', payload['auth']['tenantName'])
43
+
44
+    def test_serialize(self):
45
+        data = json.loads(open(self.TEST_JSON, 'r').read())
46
+        serialized = self.serializer.serialize(data)
47
+        data = yaml.load(serialized)
48
+        request = data['http_interactions'][0]['request']
49
+        self.assertEqual(
50
+            'http://keystoneauth.betamax_test/v2.0/tokens',
51
+            request['uri'])
52
+        payload = json.loads(request['body']['string'])
53
+        self.assertEqual('test_tenant_name', payload['auth']['tenantName'])

Loading…
Cancel
Save