decouple the mutual-dependence of config.py and server.py
[releng.git] / utils / test / testapi / opnfv_testapi / tests / unit / resources / 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 httplib
7 import unittest
8
9 from tornado import web
10
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
16
17
18 class TestToken(base.TestBase):
19     def get_app(self):
20         from opnfv_testapi.router import url_mappings
21         return web.Application(
22             url_mappings.mappings,
23             db=fake_pymongo,
24             debug=True,
25             auth=True
26         )
27
28
29 class TestTokenCreateProject(TestToken):
30     def setUp(self):
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'
35
36     @executor.create(httplib.FORBIDDEN, message.invalid_token())
37     def test_projectCreateTokenInvalid(self):
38         self.headers['X-Auth-Token'] = '1234'
39         return self.req_d
40
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')
45         return self.req_d
46
47     @executor.create(httplib.OK, '_create_success')
48     def test_projectCreateTokenSuccess(self):
49         self.headers['X-Auth-Token'] = '12345'
50         return self.req_d
51
52     def _create_success(self, body):
53         self.assertIn('CreateResponse', str(type(body)))
54
55
56 class TestTokenDeleteProject(TestToken):
57     def setUp(self):
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'
63         self.create_d()
64
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
69
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
74
75     @executor.delete(httplib.OK, '_delete_success')
76     def test_projectDeleteTokenSuccess(self):
77         return self.req_d.name
78
79     def _delete_success(self, body):
80         self.assertEqual('', body)
81
82
83 class TestTokenUpdateProject(TestToken):
84     def setUp(self):
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'
90         self.create_d()
91
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
97
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
103
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
108
109     def _update_success(self, request, body):
110         self.assertIn(request.name, body)
111
112
113 if __name__ == '__main__':
114     unittest.main()