Merge "bugfix: E722 do not use bare except"
[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.tests.unit import executor
13 from opnfv_testapi.tests.unit import fake_pymongo
14 from opnfv_testapi.tests.unit.resources import test_result
15
16
17 class TestTokenCreateResult(test_result.TestResultBase):
18     def get_app(self):
19         from opnfv_testapi.router import url_mappings
20         return web.Application(
21             url_mappings.mappings,
22             db=fake_pymongo,
23             debug=True,
24             auth=True
25         )
26
27     def setUp(self):
28         super(TestTokenCreateResult, self).setUp()
29         fake_pymongo.tokens.insert({"access_token": "12345"})
30
31     @executor.create(httplib.FORBIDDEN, message.invalid_token())
32     def test_resultCreateTokenInvalid(self):
33         self.headers['X-Auth-Token'] = '1234'
34         return self.req_d
35
36     @executor.create(httplib.UNAUTHORIZED, message.unauthorized())
37     def test_resultCreateTokenUnauthorized(self):
38         if 'X-Auth-Token' in self.headers:
39             self.headers.pop('X-Auth-Token')
40         return self.req_d
41
42     @executor.create(httplib.OK, '_create_success')
43     def test_resultCreateTokenSuccess(self):
44         self.headers['X-Auth-Token'] = '12345'
45         return self.req_d
46
47     def _create_success(self, body):
48         self.assertIn('CreateResponse', str(type(body)))
49
50
51 if __name__ == '__main__':
52     unittest.main()