2 # -------------------------------------------------------------------------
3 # Copyright (c) 2018 Intel Corporation Intellectual Property
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
9 # http://www.apache.org/licenses/LICENSE-2.0
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.
17 # -------------------------------------------------------------------------
20 from uuid import uuid4
24 import osdf.adapters.aaf.sms as SMS
25 from osdf.config.base import osdf_config
28 class TestSMS(unittest.TestCase):
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'
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"]
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']
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
60 # Part 2: Retrieve Secret Test
61 # Mock requests for retrieve_secrets
63 d_url = self.domain_url.format(sms_url, domain_name)
64 s_url = self.secret_url.format(sms_url, domain_name)
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')
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())
82 if __name__ == "__main__":