19b9e3e07ab411d911793423d65fbecab7eb5f0b
[releng.git] / utils / test / testapi / opnfv_testapi / tests / unit / test_token.py
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
5
6 import unittest
7
8 from tornado import web
9
10 import fake_pymongo
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
15
16
17 class TestToken(base.TestBase):
18     def get_app(self):
19         return web.Application(
20             url_mappings.mappings,
21             db=fake_pymongo,
22             debug=True,
23             auth=True
24         )
25
26
27 class TestTokenCreateProject(TestToken):
28     def setUp(self):
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'
33
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)
39
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)
45
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)
50
51
52 class TestTokenDeleteProject(TestToken):
53     def setUp(self):
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'
58
59     def test_projectDeleteTokenIvalid(self):
60         self.headers['X-Auth-Token'] = '12345'
61         self.create_d()
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)
66
67     def test_projectDeleteTokenUnauthorized(self):
68         self.headers['X-Auth-Token'] = '12345'
69         self.create_d()
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)
74
75     def test_projectDeleteTokenSuccess(self):
76         self.headers['X-Auth-Token'] = '12345'
77         self.create_d()
78         code, body = self.delete(self.req_d.name)
79         self.assertEqual(code, constants.HTTP_OK)
80
81
82 class TestTokenUpdateProject(TestToken):
83     def setUp(self):
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'
88
89     def test_projectUpdateTokenIvalid(self):
90         self.headers['X-Auth-Token'] = '12345'
91         self.create_d()
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)
98
99     def test_projectUpdateTokenUnauthorized(self):
100         self.headers['X-Auth-Token'] = '12345'
101         self.create_d()
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)
108
109     def test_projectUpdateTokenSuccess(self):
110         self.headers['X-Auth-Token'] = '12345'
111         self.create_d()
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)
116
117 if __name__ == '__main__':
118     unittest.main()