1 # All rights reserved. This program and the accompanying materials
2 # are made available under the terms of the Apache License, Version 2.0
3 # which accompanies this distribution, and is available at
4 # http://www.apache.org/licenses/LICENSE-2.0
9 from tornado import web
11 from opnfv_testapi.common import message
12 from opnfv_testapi.resources import project_models
13 from opnfv_testapi.tests.unit import executor
14 from opnfv_testapi.tests.unit import fake_pymongo
15 from opnfv_testapi.tests.unit.resources import test_base as base
18 class TestToken(base.TestBase):
20 from opnfv_testapi.router import url_mappings
21 return web.Application(
22 url_mappings.mappings,
29 class TestTokenCreateProject(TestToken):
31 super(TestTokenCreateProject, self).setUp()
32 self.req_d = project_models.ProjectCreateRequest('vping')
33 fake_pymongo.tokens.insert({"access_token": "12345"})
34 self.basePath = '/api/v1/projects'
36 @executor.create(httplib.FORBIDDEN, message.invalid_token())
37 def test_projectCreateTokenInvalid(self):
38 self.headers['X-Auth-Token'] = '1234'
41 @executor.create(httplib.UNAUTHORIZED, message.unauthorized())
42 def test_projectCreateTokenUnauthorized(self):
43 if 'X-Auth-Token' in self.headers:
44 self.headers.pop('X-Auth-Token')
47 @executor.create(httplib.OK, '_create_success')
48 def test_projectCreateTokenSuccess(self):
49 self.headers['X-Auth-Token'] = '12345'
52 def _create_success(self, body):
53 self.assertIn('CreateResponse', str(type(body)))
56 class TestTokenDeleteProject(TestToken):
58 super(TestTokenDeleteProject, self).setUp()
59 self.req_d = project_models.ProjectCreateRequest('vping')
60 fake_pymongo.tokens.insert({"access_token": "12345"})
61 self.basePath = '/api/v1/projects'
62 self.headers['X-Auth-Token'] = '12345'
65 @executor.delete(httplib.FORBIDDEN, message.invalid_token())
66 def test_projectDeleteTokenIvalid(self):
67 self.headers['X-Auth-Token'] = '1234'
68 return self.req_d.name
70 @executor.delete(httplib.UNAUTHORIZED, message.unauthorized())
71 def test_projectDeleteTokenUnauthorized(self):
72 self.headers.pop('X-Auth-Token')
73 return self.req_d.name
75 @executor.delete(httplib.OK, '_delete_success')
76 def test_projectDeleteTokenSuccess(self):
77 return self.req_d.name
79 def _delete_success(self, body):
80 self.assertEqual('', body)
83 class TestTokenUpdateProject(TestToken):
85 super(TestTokenUpdateProject, self).setUp()
86 self.req_d = project_models.ProjectCreateRequest('vping')
87 fake_pymongo.tokens.insert({"access_token": "12345"})
88 self.basePath = '/api/v1/projects'
89 self.headers['X-Auth-Token'] = '12345'
92 @executor.update(httplib.FORBIDDEN, message.invalid_token())
93 def test_projectUpdateTokenIvalid(self):
94 self.headers['X-Auth-Token'] = '1234'
95 req = project_models.ProjectUpdateRequest('newName', 'new description')
96 return req, self.req_d.name
98 @executor.update(httplib.UNAUTHORIZED, message.unauthorized())
99 def test_projectUpdateTokenUnauthorized(self):
100 self.headers.pop('X-Auth-Token')
101 req = project_models.ProjectUpdateRequest('newName', 'new description')
102 return req, self.req_d.name
104 @executor.update(httplib.OK, '_update_success')
105 def test_projectUpdateTokenSuccess(self):
106 req = project_models.ProjectUpdateRequest('newName', 'new description')
107 return req, self.req_d.name
109 def _update_success(self, request, body):
110 self.assertIn(request.name, body)
113 if __name__ == '__main__':