Debian Codegraph @

config_tempest/tests/test_accounts.py

raw
1 # Copyright 2018 Red Hat, Inc.
2 # All Rights Reserved.
3 #
4 # Licensed under the Apache License, Version 2.0 (the "License"); you may
5 # not use this file except in compliance with the License. You may obtain
6 # a copy of the License at
7 #
8 # http://www.apache.org/licenses/LICENSE-2.0
9 #
10 # Unless required by applicable law or agreed to in writing, software
11 # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12 # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13 # License for the specific language governing permissions and limitations
14 # under the License.
15
16 import os
17 from unittest import mock
18
19 from config_tempest import accounts
20 from config_tempest import main
21 from config_tempest.tests.base import BaseConfigTempestTest
22
23
24 class TestAccounts(BaseConfigTempestTest):
25 """Accounts test class
26
27 Tests for create_accounts_file and write_accounts_file methods.
28 """
29
30 def setUp(self):
31 super(TestAccounts, self).setUp()
32 self.conf = self._get_conf("v2.0", "v3")
33
34 @mock.patch('config_tempest.accounts.write_accounts_file')
35 def test_create_accounts_file(self, mock_write):
36 path = "./etc/accounts.yaml"
37 main.set_options(self.conf, None, False, "", accounts_path=path)
38 # credentials under auth section
39 accounts.create_accounts_file(True, path, self.conf)
40 mock_write.assert_called_with(path, "admin", "adminPass",
41 "adminProject")
42 self.assertEqual(self.conf.get("auth", "test_accounts_file"),
43 os.path.abspath(path))
44
45 # credentials under identity section
46 accounts.create_accounts_file(False, path, self.conf)
47 mock_write.assert_called_with(path, "demo", "secret", "demo")
48 self.assertEqual(self.conf.get("auth", "test_accounts_file"),
49 os.path.abspath(path))
50
51 @mock.patch('yaml.safe_dump')
52 def test_write_accounts_file_admin(self, mock_safe_dump):
53 path = "./etc/accounts.yaml"
54 main.set_options(self.conf, None, False, "", accounts_path=path)
55
56 # write accounts file with admin user
57 expected_accounts = [{
58 "username": "admin",
59 "project_name": "adminProject",
60 "password": "adminPass"
61 }]
62
63 m = mock.mock_open()
64 with mock.patch("builtins.open", m):
65 accounts.write_accounts_file(path, "admin", "adminPass",
66 "adminProject")
67
68 mock_safe_dump.assert_called_once()
69
70 args = mock_safe_dump.call_args[0]
71 result_accounts = args[0]
72 self.assertEqual(result_accounts, expected_accounts)
73
74 @mock.patch('yaml.safe_dump')
75 def test_write_accounts_file_demo(self, mock_safe_dump):
76 path = "./etc/accounts.yaml"
77 main.set_options(self.conf, None, False, "", accounts_path=path)
78
79 # write accounts file with non-reader user
80 expected_accounts = [{
81 "username": "demo",
82 "project_name": "demoProject",
83 "password": "demoPass"
84 }, {
85 "username": "demo_reader",
86 "project_name": "demoProject",
87 "password": "demoPass",
88 "roles": ["reader"]
89 }]
90
91 m = mock.mock_open()
92 with mock.patch("builtins.open", m):
93 accounts.write_accounts_file(path, "demo", "demoPass",
94 "demoProject")
95
96 mock_safe_dump.assert_called_once()
97
98 args = mock_safe_dump.call_args[0]
99 result_accounts = args[0]
100 self.assertEqual(result_accounts, expected_accounts)