Use correct content type on JSON responses

Currently, in cases where swauth returns a JSON document as its body,
it does not specify a content type, and swob defaults it to text/html.

This change uses a standard content type of 'application/json' in each
of these instances, and adjusts the tests accordingly.

Closes-Bug: #1545430
APIImpact

Change-Id: I96d343a87f462811bcefb7d402887f8a570fe6bd
This commit is contained in:
Brian Cline
2016-02-14 04:55:42 -06:00
parent 669df58783
commit 556aa15697
2 changed files with 49 additions and 11 deletions

View File

@@ -26,6 +26,7 @@ from swauth.authtypes import MAX_TOKEN_LENGTH
from swauth import middleware as auth
CONTENT_TYPE_JSON = 'application/json'
DEFAULT_TOKEN_LIFE = 86400
MAX_TOKEN_LIFE = 100000
@@ -710,6 +711,7 @@ class TestAuth(unittest.TestCase):
headers={'X-Auth-User': 'act:usr',
'X-Auth-Key': 'key'}).get_response(self.test_auth)
self.assertEqual(resp.status_int, 200)
self.assertEqual(resp.content_type, CONTENT_TYPE_JSON)
self.assertTrue(resp.headers.get('x-auth-token',
'').startswith('AUTH_tk'), resp.headers.get('x-auth-token'))
self.assertEqual(resp.headers.get('x-auth-token'),
@@ -742,6 +744,7 @@ class TestAuth(unittest.TestCase):
'X-Auth-Key': 'key',
'X-Auth-Token-Lifetime': 10}).get_response(self.test_auth)
self.assertEqual(resp.status_int, 200)
self.assertEqual(resp.content_type, CONTENT_TYPE_JSON)
left = int(resp.headers['x-auth-token-expires'])
self.assertTrue(left > 0, '%d > 0' % left)
self.assertTrue(left <= 10, '%d <= 10' % left)
@@ -779,6 +782,7 @@ class TestAuth(unittest.TestCase):
'X-Auth-Token-Lifetime': MAX_TOKEN_LIFE * 10})
resp = req.get_response(self.test_auth)
self.assertEqual(resp.status_int, 200)
self.assertEqual(resp.content_type, CONTENT_TYPE_JSON)
left = int(resp.headers['x-auth-token-expires'])
self.assertTrue(left > DEFAULT_TOKEN_LIFE,
'%d > %d' % (left, DEFAULT_TOKEN_LIFE))
@@ -815,6 +819,7 @@ class TestAuth(unittest.TestCase):
headers={'X-Storage-User': 'usr',
'X-Storage-Pass': 'key'}).get_response(self.test_auth)
self.assertEqual(resp.status_int, 200)
self.assertEqual(resp.content_type, CONTENT_TYPE_JSON)
self.assertTrue(resp.headers.get('x-auth-token',
'').startswith('AUTH_tk'), resp.headers.get('x-auth-token'))
self.assertEqual(resp.headers.get('x-auth-token'),
@@ -846,6 +851,7 @@ class TestAuth(unittest.TestCase):
headers={'X-Storage-User': 'act:usr',
'X-Storage-Pass': 'key'}).get_response(self.test_auth)
self.assertEqual(resp.status_int, 200)
self.assertEqual(resp.content_type, CONTENT_TYPE_JSON)
self.assertTrue(resp.headers.get('x-auth-token',
'').startswith('AUTH_tk'), resp.headers.get('x-auth-token'))
self.assertEqual(resp.headers.get('x-auth-token'),
@@ -877,6 +883,7 @@ class TestAuth(unittest.TestCase):
headers={'X-Auth-User': 'act:usr',
'X-Auth-Key': 'key'}).get_response(self.test_auth)
self.assertEqual(resp.status_int, 200)
self.assertEqual(resp.content_type, CONTENT_TYPE_JSON)
self.assertTrue(resp.headers.get('x-auth-token',
'').startswith('AUTH_tk'), resp.headers.get('x-auth-token'))
self.assertEqual(resp.headers.get('x-auth-token'),
@@ -907,6 +914,7 @@ class TestAuth(unittest.TestCase):
headers={'X-Auth-User': 'act:usr',
'X-Auth-Key': 'key'}).get_response(self.test_auth)
self.assertEqual(resp.status_int, 200)
self.assertEqual(resp.content_type, CONTENT_TYPE_JSON)
self.assertEqual(resp.headers.get('x-auth-token'), 'AUTH_tktest')
self.assertEqual(resp.headers.get('x-auth-token'),
resp.headers.get('x-storage-token'))
@@ -940,6 +948,7 @@ class TestAuth(unittest.TestCase):
'X-Auth-Key': 'key',
'X-Auth-New-Token': 'true'}).get_response(self.test_auth)
self.assertEqual(resp.status_int, 200)
self.assertEqual(resp.content_type, CONTENT_TYPE_JSON)
self.assertNotEqual(resp.headers.get('x-auth-token'), 'AUTH_tktest')
self.assertEqual(resp.headers.get('x-auth-token'),
resp.headers.get('x-storage-token'))
@@ -977,6 +986,7 @@ class TestAuth(unittest.TestCase):
headers={'X-Auth-User': 'act:usr',
'X-Auth-Key': 'key'}).get_response(self.test_auth)
self.assertEqual(resp.status_int, 200)
self.assertEqual(resp.content_type, CONTENT_TYPE_JSON)
self.assertNotEqual(resp.headers.get('x-auth-token'), 'AUTH_tktest')
self.assertEqual(resp.headers.get('x-auth-token'),
resp.headers.get('x-storage-token'))
@@ -1014,6 +1024,7 @@ class TestAuth(unittest.TestCase):
headers={'X-Auth-User': 'act:usr',
'X-Auth-Key': 'key'}).get_response(self.test_auth)
self.assertEqual(resp.status_int, 200)
self.assertEqual(resp.content_type, CONTENT_TYPE_JSON)
self.assertNotEqual(resp.headers.get('x-auth-token'), 'AUTH_tktest')
self.assertEqual(resp.headers.get('x-auth-token'),
resp.headers.get('x-storage-token'))
@@ -1144,6 +1155,7 @@ class TestAuth(unittest.TestCase):
'X-Auth-Admin-Key': 'supertest'}
).get_response(self.test_auth)
self.assertEqual(resp.status_int, 200)
self.assertEqual(resp.content_type, CONTENT_TYPE_JSON)
self.assertEqual(json.loads(resp.body),
{"accounts": [{"name": "act"}]})
self.assertEqual(self.test_auth.app.calls, 2)
@@ -1165,6 +1177,7 @@ class TestAuth(unittest.TestCase):
'X-Auth-Admin-Key': 'key'}
).get_response(self.test_auth)
self.assertEqual(resp.status_int, 200)
self.assertEqual(resp.content_type, CONTENT_TYPE_JSON)
self.assertEqual(json.loads(resp.body),
{"accounts": [{"name": "act"}]})
self.assertEqual(self.test_auth.app.calls, 3)
@@ -1253,6 +1266,7 @@ class TestAuth(unittest.TestCase):
'X-Auth-Admin-Key': 'supertest'}
).get_response(self.test_auth)
self.assertEqual(resp.status_int, 200)
self.assertEqual(resp.content_type, CONTENT_TYPE_JSON)
self.assertEqual(json.loads(resp.body),
{'account_id': 'AUTH_cfa',
'services': {'storage':
@@ -1288,6 +1302,7 @@ class TestAuth(unittest.TestCase):
'X-Auth-Admin-Key': 'key'}
).get_response(self.test_auth)
self.assertEqual(resp.status_int, 200)
self.assertEqual(resp.content_type, CONTENT_TYPE_JSON)
self.assertEqual(json.loads(resp.body),
{'account_id': 'AUTH_cfa',
'services': {'storage':
@@ -1429,6 +1444,7 @@ class TestAuth(unittest.TestCase):
body=json.dumps({'new_service': {'new_endpoint': 'new_value'}})
).get_response(self.test_auth)
self.assertEqual(resp.status_int, 200)
self.assertEqual(resp.content_type, CONTENT_TYPE_JSON)
self.assertEqual(json.loads(resp.body),
{'storage': {'default': 'local',
'local': 'http://127.0.0.1:8080/v1/AUTH_cfa'},
@@ -1449,6 +1465,7 @@ class TestAuth(unittest.TestCase):
body=json.dumps({'storage': {'new_endpoint': 'new_value'}})
).get_response(self.test_auth)
self.assertEqual(resp.status_int, 200)
self.assertEqual(resp.content_type, CONTENT_TYPE_JSON)
self.assertEqual(json.loads(resp.body),
{'storage': {'default': 'local',
'local': 'http://127.0.0.1:8080/v1/AUTH_cfa',
@@ -1469,6 +1486,7 @@ class TestAuth(unittest.TestCase):
body=json.dumps({'storage': {'local': 'new_value'}})
).get_response(self.test_auth)
self.assertEqual(resp.status_int, 200)
self.assertEqual(resp.content_type, CONTENT_TYPE_JSON)
self.assertEqual(json.loads(resp.body),
{'storage': {'default': 'local',
'local': 'new_value'}})
@@ -2391,6 +2409,7 @@ class TestAuth(unittest.TestCase):
'X-Auth-Admin-Key': 'supertest'}
).get_response(self.test_auth)
self.assertEqual(resp.status_int, 200)
self.assertEqual(resp.content_type, CONTENT_TYPE_JSON)
self.assertEqual(resp.body, json.dumps(
{"groups": [{"name": "act:usr"}, {"name": "act"},
{"name": ".admin"}],
@@ -2441,6 +2460,7 @@ class TestAuth(unittest.TestCase):
'X-Auth-Admin-Key': 'supertest'}
).get_response(self.test_auth)
self.assertEqual(resp.status_int, 200)
self.assertEqual(resp.content_type, CONTENT_TYPE_JSON)
self.assertEqual(resp.body, json.dumps(
{"groups": [{"name": ".admin"}, {"name": "act"},
{"name": "act:tester"}, {"name": "act:tester3"}]}))
@@ -2479,6 +2499,7 @@ class TestAuth(unittest.TestCase):
'X-Auth-Admin-Key': 'supertest'}
).get_response(self.test_auth)
self.assertEqual(resp.status_int, 200)
self.assertEqual(resp.content_type, CONTENT_TYPE_JSON)
self.assertEqual(resp.body, json.dumps(
{"groups": [{"name": ".admin"}, {"name": "act"},
{"name": "act:tester"}, {"name": "act:tester3"}]}))
@@ -2535,6 +2556,7 @@ class TestAuth(unittest.TestCase):
'X-Auth-Admin-Key': 'key'}
).get_response(self.test_auth)
self.assertEqual(resp.status_int, 200)
self.assertEqual(resp.content_type, CONTENT_TYPE_JSON)
self.assertEqual(resp.body, json.dumps(
{"groups": [{"name": "act:usr"}, {"name": "act"}],
"auth": "plaintext:key"}))
@@ -2610,6 +2632,7 @@ class TestAuth(unittest.TestCase):
'X-Auth-Admin-Key': 'supertest'}
).get_response(self.test_auth)
self.assertEqual(resp.status_int, 200)
self.assertEqual(resp.content_type, CONTENT_TYPE_JSON)
self.assertEqual(resp.body, json.dumps(
{"groups": [{"name": "act:usr"}, {"name": "act"},
{"name": ".reseller_admin"}],
@@ -3662,6 +3685,7 @@ class TestAuth(unittest.TestCase):
headers={'X-Auth-User': sent_user,
'X-Auth-Key': sent_key}).get_response(self.test_auth)
self.assertEqual(resp.status_int, 200)
self.assertEqual(resp.content_type, CONTENT_TYPE_JSON)
self.assertTrue(resp.headers.get('x-auth-token',
'').startswith('AUTH_tk'), resp.headers.get('x-auth-token'))
self.assertEqual(resp.headers.get('x-auth-token'),