update link to upper-constraints.txt
[optf/osdf.git] / test / adapters / test_sms.py
1 #
2 # -------------------------------------------------------------------------
3 #   Copyright (c) 2018 Intel Corporation Intellectual Property
4 #
5 #   Licensed under the Apache License, Version 2.0 (the "License");
6 #   you may not use this file except in compliance with the License.
7 #   You may obtain a copy of the License at
8 #
9 #       http://www.apache.org/licenses/LICENSE-2.0
10 #
11 #   Unless required by applicable law or agreed to in writing, software
12 #   distributed under the License is distributed on an "AS IS" BASIS,
13 #   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 #   See the License for the specific language governing permissions and
15 #   limitations under the License.
16 #
17 # -------------------------------------------------------------------------
18 #
19 import unittest
20 from uuid import uuid4
21
22 import requests_mock
23
24 import osdf.adapters.aaf.sms as SMS
25 from osdf.config.base import osdf_config
26
27
28 class TestSMS(unittest.TestCase):
29
30     def setUp(self):
31         self.config = osdf_config.deployment
32         self.base_domain_url = '{}/v1/sms/domain'
33         self.domain_url = '{}/v1/sms/domain/{}'
34         self.secret_url = self.domain_url + '/secret'
35
36     @requests_mock.mock()
37     def test_sms(self, mock_sms):
38         ''' NOTE: preload_secret during the deployment using a preload script.
39                   For test purposes we need to do preload ourselves'''
40         sms_url = self.config["aaf_sms_url"]
41
42         # JSON Data responses
43         secretnames = {'secretnames': ['s1', 's2', 's3', 's4']}
44         secretvalues = {'values': {'Password': '', 'UserName': ''}}
45         expecect_secret_dict = dict()
46         for secret in secretnames['secretnames']:
47             expecect_secret_dict[secret] = secretvalues['values']
48
49         # Part 1 : Preload Secrets ONLY FOR TEST
50         # Mock requests for preload_secret
51         cd_url = self.base_domain_url.format(sms_url)
52         domain_uuid1 = str(uuid4())
53         domain_name = self.config['secret_domain']
54         s_url = self.secret_url.format(sms_url, domain_name)
55         mock_sms.post(cd_url, status_code=200, json={'uuid': domain_uuid1})
56         mock_sms.post(s_url, status_code=200)
57         # Initialize Secrets from SMS
58         SMS.preload_secrets()
59
60         # Part 2: Retrieve Secret Test
61         # Mock requests for retrieve_secrets
62
63         d_url = self.domain_url.format(sms_url, domain_name)
64         s_url = self.secret_url.format(sms_url, domain_name)
65
66         # Retrieve Secrets from SMS and load to secret cache
67         # Use the secret_cache instead of config files
68         mock_sms.get(s_url, status_code=200, json=secretnames)
69         for secret in secretnames['secretnames']:
70             mock_sms.get('{}/{}'.format(s_url, secret),
71                          status_code=200, json=secretvalues)
72         secret_cache = SMS.retrieve_secrets()
73         self.assertDictEqual(expecect_secret_dict, secret_cache,
74                              'Failed to retrieve secrets')
75
76         # Part 3: Clean up Delete secrets and domain
77         # Mock requests for delete_secrets
78         mock_sms.delete(d_url, status_code=200)
79         self.assertTrue(SMS.delete_secrets())
80
81
82 if __name__ == "__main__":
83     unittest.main()