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
8 from tornado import web
11 from opnfv_testapi.common import constants
12 from opnfv_testapi.resources import project_models
13 from opnfv_testapi.router import url_mappings
14 import test_base as base
17 class TestToken(base.TestBase):
19 return web.Application(
20 url_mappings.mappings,
27 class TestTokenCreateProject(TestToken):
29 super(TestTokenCreateProject, self).setUp()
30 self.req_d = project_models.ProjectCreateRequest('vping')
31 fake_pymongo.tokens.insert({"access_token": "12345"})
32 self.basePath = '/api/v1/projects'
34 def test_projectCreateTokenInvalid(self):
35 self.headers['X-Auth-Token'] = '1234'
36 code, body = self.create_d()
37 self.assertEqual(code, constants.HTTP_FORBIDDEN)
38 self.assertIn('Invalid Token.', body)
40 def test_projectCreateTokenUnauthorized(self):
41 self.headers.pop('X-Auth-Token')
42 code, body = self.create_d()
43 self.assertEqual(code, constants.HTTP_UNAUTHORIZED)
44 self.assertIn('No Authentication Header.', body)
46 def test_projectCreateTokenSuccess(self):
47 self.headers['X-Auth-Token'] = '12345'
48 code, body = self.create_d()
49 self.assertEqual(code, constants.HTTP_OK)
52 class TestTokenDeleteProject(TestToken):
54 super(TestTokenDeleteProject, self).setUp()
55 self.req_d = project_models.ProjectCreateRequest('vping')
56 fake_pymongo.tokens.insert({"access_token": "12345"})
57 self.basePath = '/api/v1/projects'
59 def test_projectDeleteTokenIvalid(self):
60 self.headers['X-Auth-Token'] = '12345'
62 self.headers['X-Auth-Token'] = '1234'
63 code, body = self.delete(self.req_d.name)
64 self.assertEqual(code, constants.HTTP_FORBIDDEN)
65 self.assertIn('Invalid Token.', body)
67 def test_projectDeleteTokenUnauthorized(self):
68 self.headers['X-Auth-Token'] = '12345'
70 self.headers.pop('X-Auth-Token')
71 code, body = self.delete(self.req_d.name)
72 self.assertEqual(code, constants.HTTP_UNAUTHORIZED)
73 self.assertIn('No Authentication Header.', body)
75 def test_projectDeleteTokenSuccess(self):
76 self.headers['X-Auth-Token'] = '12345'
78 code, body = self.delete(self.req_d.name)
79 self.assertEqual(code, constants.HTTP_OK)
82 class TestTokenUpdateProject(TestToken):
84 super(TestTokenUpdateProject, self).setUp()
85 self.req_d = project_models.ProjectCreateRequest('vping')
86 fake_pymongo.tokens.insert({"access_token": "12345"})
87 self.basePath = '/api/v1/projects'
89 def test_projectUpdateTokenIvalid(self):
90 self.headers['X-Auth-Token'] = '12345'
92 code, body = self.get(self.req_d.name)
93 self.headers['X-Auth-Token'] = '1234'
94 req = project_models.ProjectUpdateRequest('newName', 'new description')
95 code, body = self.update(req, self.req_d.name)
96 self.assertEqual(code, constants.HTTP_FORBIDDEN)
97 self.assertIn('Invalid Token.', body)
99 def test_projectUpdateTokenUnauthorized(self):
100 self.headers['X-Auth-Token'] = '12345'
102 code, body = self.get(self.req_d.name)
103 self.headers.pop('X-Auth-Token')
104 req = project_models.ProjectUpdateRequest('newName', 'new description')
105 code, body = self.update(req, self.req_d.name)
106 self.assertEqual(code, constants.HTTP_UNAUTHORIZED)
107 self.assertIn('No Authentication Header.', body)
109 def test_projectUpdateTokenSuccess(self):
110 self.headers['X-Auth-Token'] = '12345'
112 code, body = self.get(self.req_d.name)
113 req = project_models.ProjectUpdateRequest('newName', 'new description')
114 code, body = self.update(req, self.req_d.name)
115 self.assertEqual(code, constants.HTTP_OK)
117 if __name__ == '__main__':