|
| 1 | +from unittest.mock import patch |
| 2 | +from aiounittest import AsyncTestCase, futurized |
| 3 | +from aioresponses import aioresponses |
| 4 | +from asyncopenstackclient import AuthPassword |
| 5 | + |
| 6 | + |
| 7 | +class TestAuth(AsyncTestCase): |
| 8 | + |
| 9 | + def setUp(self): |
| 10 | + self.auth_args = ('auth_url', 'username', 'password', 'mock_project_name', |
| 11 | + 'mock_user_domain_name', 'mock_project_domain_name') |
| 12 | + self.auth = AuthPassword(*self.auth_args) |
| 13 | + |
| 14 | + def tearDown(self): |
| 15 | + patch.stopall() |
| 16 | + |
| 17 | + async def test_create_object(self): |
| 18 | + self.assertTrue(self.auth.auth_url.endswith("/auth/tokens")) |
| 19 | + self.assertTrue('Content-Type' in self.auth.headers) |
| 20 | + |
| 21 | + async def test_get_token(self): |
| 22 | + body = { |
| 23 | + "token": { |
| 24 | + "catalog": { |
| 25 | + "endpoints": [ |
| 26 | + {"name": "mock_endpoint", "endpoints": [{"url": "mock_url", "interface": "public"}]} |
| 27 | + ] |
| 28 | + }, |
| 29 | + "expires_at": "1970-01-01T01:00:00.000000Z" |
| 30 | + } |
| 31 | + } |
| 32 | + headers = { |
| 33 | + "Vary": "X-Auth-Token", |
| 34 | + "x-openstack-request-id": "1234", |
| 35 | + "Content-Type": "application/json", |
| 36 | + "X-Subject-Token": "gAAAAABao" |
| 37 | + |
| 38 | + } |
| 39 | + with aioresponses() as req: |
| 40 | + req.post('auth_url/auth/tokens', payload=body, headers=headers) |
| 41 | + |
| 42 | + res = await self.auth.get_token() |
| 43 | + |
| 44 | + self.assertEqual(res, (headers["X-Subject-Token"], 0, body["token"]["catalog"])) |
| 45 | + |
| 46 | + def test_get_endpoint_url_existing_endpoint(self): |
| 47 | + self.auth.endpoints = [ |
| 48 | + {"name": "mock_endpoint", "endpoints": [{"url": "mock_url", "interface": "public"}]} |
| 49 | + ] |
| 50 | + |
| 51 | + endpoint_url = self.auth.get_endpoint_url("mock_endpoint") |
| 52 | + |
| 53 | + self.assertEqual(endpoint_url, "mock_url") |
| 54 | + |
| 55 | + def test_get_endpoint_url_bad_endpoint_name(self): |
| 56 | + self.auth.endpoints = [ |
| 57 | + {"name": "mock_endpoint", "endpoints": [{"url": "mock_url", "interface": "public"}]} |
| 58 | + ] |
| 59 | + with self.assertRaises(ValueError): |
| 60 | + self.auth.get_endpoint_url("none_existing_endpoint") |
| 61 | + |
| 62 | + def test_get_endpoint_url_bad_interface(self): |
| 63 | + self.auth.endpoints = [ |
| 64 | + {"name": "mock_endpoint", "endpoints": [{"url": "mock_url", "interface": "public"}]} |
| 65 | + ] |
| 66 | + with self.assertRaises(ValueError): |
| 67 | + self.auth.get_endpoint_url("mock_endpoint", prefered_interface="not_existing_interface") |
| 68 | + |
| 69 | + async def test_authenticate_first_time(self): |
| 70 | + |
| 71 | + mock_get_token_results = [ |
| 72 | + futurized(('mock-token1', 1000, 'whatever')), |
| 73 | + futurized(('mock-token2', 1000, 'whatever')), |
| 74 | + ] |
| 75 | + |
| 76 | + # time is gonna be called 2 times becouse of Pythons lazy evaluation |
| 77 | + mock_time_results = [ |
| 78 | + 900, |
| 79 | + 1100 |
| 80 | + ] |
| 81 | + |
| 82 | + patch('asyncopenstackclient.auth.AuthPassword.get_token', side_effect=mock_get_token_results).start() |
| 83 | + patch('asyncopenstackclient.auth.time', side_effect=mock_time_results).start() |
| 84 | + |
| 85 | + # first time token should be None and get_token shall be called |
| 86 | + await self.auth.authenticate() |
| 87 | + self.assertEqual(self.auth.token, 'mock-token1') |
| 88 | + |
| 89 | + # second time, token is not None and current time is before token expiration, no change |
| 90 | + await self.auth.authenticate() |
| 91 | + self.assertEqual(self.auth.token, 'mock-token1') |
| 92 | + |
| 93 | + # third time, token expires and should be renewed |
| 94 | + await self.auth.authenticate() |
| 95 | + self.assertEqual(self.auth.token, 'mock-token2') |
0 commit comments