Brian Waldon | f20e4ed | 2011-10-27 22:04:15 -0400 | [diff] [blame] | 1 | # vim: tabstop=4 shiftwidth=4 softtabstop=4 |
| 2 | |
| 3 | # Copyright 2011 OpenStack, LLC |
| 4 | # All Rights Reserved. |
| 5 | # |
| 6 | # Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 7 | # not use this file except in compliance with the License. You may obtain |
| 8 | # a copy of the License at |
| 9 | # |
| 10 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | # |
| 12 | # Unless required by applicable law or agreed to in writing, software |
| 13 | # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 14 | # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 15 | # License for the specific language governing permissions and limitations |
| 16 | # under the License. |
| 17 | |
| 18 | """Functional test case against the OpenStack Nova API server""" |
| 19 | |
| 20 | import httplib2 |
| 21 | import json |
Brian Waldon | f20e4ed | 2011-10-27 22:04:15 -0400 | [diff] [blame] | 22 | import uuid |
| 23 | |
| 24 | from kong import keystone |
| 25 | from kong import tests |
| 26 | |
| 27 | |
| 28 | class TestKeystoneAuth(tests.FunctionalTest): |
| 29 | |
| 30 | def setUp(self): |
| 31 | super(TestKeystoneAuth, self).setUp() |
| 32 | |
| 33 | api_version = self.keystone['apiver'] |
| 34 | if api_version != 'v2.0': |
| 35 | raise ValueError("Must use Identity API v2.0") |
| 36 | |
| 37 | args = (self.keystone['service_host'], |
| 38 | self.keystone['service_port'], |
| 39 | api_version) |
| 40 | |
| 41 | self.base_url = "http://%s:%s/%s/tokens" % args |
| 42 | |
| 43 | self.user = self.keystone['user'] |
| 44 | self.password = self.keystone['password'] |
| 45 | self.tenant_id = self.keystone['tenant_id'] |
| 46 | |
| 47 | def test_can_get_token(self): |
| 48 | headers = {'content-type': 'application/json'} |
| 49 | |
| 50 | body = { |
| 51 | "auth": { |
| 52 | "passwordCredentials":{ |
| 53 | "username": self.user, |
| 54 | "password": self.password, |
| 55 | }, |
| 56 | "tenantId": self.tenant_id, |
| 57 | }, |
| 58 | } |
| 59 | |
| 60 | http = httplib2.Http() |
| 61 | response, content = http.request(self.base_url, 'POST', |
| 62 | headers=headers, |
| 63 | body=json.dumps(body)) |
| 64 | |
| 65 | self.assertEqual(response.status, 200) |
| 66 | res_body = json.loads(content) |
| 67 | self.assertTrue(res_body['access']['token']['id']) |
Dolph Mathews | 36b7255 | 2011-11-15 07:48:48 -0800 | [diff] [blame] | 68 | self.assertTrue(res_body['access']['token']['tenant']['id'], |
| 69 | self.tenant_id) |
| 70 | self.assertTrue(res_body['access']['user']['name'], |
| 71 | self.user) |
Brian Waldon | f20e4ed | 2011-10-27 22:04:15 -0400 | [diff] [blame] | 72 | test_can_get_token.tags = ['auth'] |
| 73 | |
| 74 | def test_bad_user(self): |
| 75 | headers = {'content-type': 'application/json'} |
| 76 | |
| 77 | body = { |
| 78 | "auth": { |
| 79 | "passwordCredentials": { |
| 80 | "username": str(uuid.uuid4()), |
| 81 | "password": self.password, |
| 82 | }, |
| 83 | "tenantId": self.tenant_id, |
| 84 | }, |
| 85 | } |
| 86 | |
| 87 | http = httplib2.Http() |
| 88 | response, content = http.request(self.base_url, 'POST', |
| 89 | headers=headers, |
| 90 | body=json.dumps(body)) |
| 91 | |
| 92 | self.assertEqual(response.status, 401) |
| 93 | test_bad_user.tags = ['auth'] |
| 94 | |
| 95 | def test_bad_password(self): |
| 96 | headers = {'content-type': 'application/json'} |
| 97 | |
| 98 | body = { |
| 99 | "auth": { |
| 100 | "passwordCredentials": { |
| 101 | "username": self.user, |
| 102 | "password": str(uuid.uuid4()), |
| 103 | }, |
| 104 | "tenantId": self.tenant_id, |
| 105 | }, |
| 106 | } |
| 107 | |
| 108 | http = httplib2.Http() |
| 109 | response, content = http.request(self.base_url, 'POST', |
| 110 | headers=headers, |
| 111 | body=json.dumps(body)) |
| 112 | |
| 113 | self.assertEqual(response.status, 401) |
| 114 | test_bad_password.tags = ['auth'] |
| 115 | |
| 116 | def test_bad_tenant_id(self): |
| 117 | headers = {'content-type': 'application/json'} |
| 118 | |
| 119 | body = { |
| 120 | "auth": { |
| 121 | "passwordCredentials": { |
| 122 | "username": self.user, |
| 123 | "password": self.password, |
| 124 | }, |
| 125 | "tenantId": str(uuid.uuid4()), |
| 126 | }, |
| 127 | } |
| 128 | |
| 129 | http = httplib2.Http() |
| 130 | response, content = http.request(self.base_url, 'POST', |
| 131 | headers=headers, |
| 132 | body=json.dumps(body)) |
| 133 | |
| 134 | self.assertEqual(response.status, 401) |
| 135 | test_bad_tenant_id.tags = ['auth'] |
| 136 | |
| 137 | |
| 138 | |
| 139 | class TestKeystoneAuthWithNova(tests.FunctionalTest): |
| 140 | |
| 141 | def setUp(self): |
| 142 | super(TestKeystoneAuthWithNova, self).setUp() |
| 143 | args = (self.nova['host'], self.nova['port'], |
| 144 | self.nova['ver'], self.nova['project']) |
| 145 | self.base_url = "http://%s:%s/%s/%s" % args |
| 146 | |
| 147 | self.keystone_api = keystone.API(self.keystone['service_host'], |
| 148 | self.keystone['service_port']) |
| 149 | |
| 150 | def _get_token(self): |
| 151 | user = self.keystone['user'] |
| 152 | password = self.keystone['password'] |
| 153 | tenant_id = self.keystone['tenant_id'] |
| 154 | return self.keystone_api.get_token(user, password, tenant_id) |
| 155 | |
| 156 | def test_good_token(self): |
| 157 | http = httplib2.Http() |
| 158 | url = '%s/flavors' % self.base_url |
| 159 | headers = {'x-auth-token': self._get_token()} |
| 160 | response, content = http.request(url, 'GET', headers=headers) |
| 161 | self.assertEqual(response.status, 200) |
| 162 | test_good_token.tags = ['nova', 'auth'] |
| 163 | |
| 164 | def test_bad_token(self): |
| 165 | http = httplib2.Http() |
| 166 | url = '%s/flavors' % self.base_url |
| 167 | headers = {'x-auth-token': str(uuid.uuid4())} |
| 168 | response, content = http.request(url, 'GET', headers=headers) |
| 169 | self.assertEqual(response.status, 401) |
| 170 | test_bad_token.tags = ['nova', 'auth'] |
| 171 | |
| 172 | def test_no_token(self): |
| 173 | http = httplib2.Http() |
| 174 | url = '%s/flavors' % self.base_url |
| 175 | headers = {'x-auth-token': str(uuid.uuid4())} |
| 176 | response, content = http.request(url, 'GET', headers=headers) |
| 177 | self.assertEqual(response.status, 401) |
| 178 | test_no_token.tags = ['nova', 'auth'] |
| 179 | |
| 180 | def test_no_header(self): |
| 181 | http = httplib2.Http() |
| 182 | url = '%s/flavors' % self.base_url |
| 183 | response, content = http.request(url, 'GET') |
| 184 | self.assertEqual(response.status, 401) |
| 185 | test_no_header.tags = ['nova', 'auth'] |