blob: 3fd0102c4160d5c0750290a63dc143060607fb90 [file] [log] [blame]
Scott Baker62c7eaf2018-05-22 15:59:26 -07001
2# Copyright 2017-present Open Networking Foundation
3#
4# Licensed under the Apache License, Version 2.0 (the "License");
5# you may not use this file except in compliance with the License.
6# You may obtain 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,
12# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13# See the License for the specific language governing permissions and
14# limitations under the License.
15
16import json
17import os
18import sys
19import unittest
20from mock import patch, PropertyMock, ANY, MagicMock
21from unit_test_common import setup_sync_unit_test
22
23def fake_connect_openstack_admin(self, service, required_version=None):
24 return MagicMock()
25
26class TestSyncPrincipal(unittest.TestCase):
27
28 def setUp(self):
29 self.unittest_setup = setup_sync_unit_test(os.path.abspath(os.path.dirname(os.path.realpath(__file__))),
30 globals(),
31 [("openstack", "openstack.xproto")] )
32
33 sys.path.append(os.path.join(os.path.abspath(os.path.dirname(os.path.realpath(__file__))), "../steps"))
34
Scott Bakerc808c672019-02-04 11:38:20 -080035 self.model_accessor = self.unittest_setup["model_accessor"]
36
Scott Baker62c7eaf2018-05-22 15:59:26 -070037 from sync_principal import SyncPrincipal
38 self.step_class = SyncPrincipal
39
40 self.service = OpenStackService()
41 self.trust_domain = TrustDomain(owner=self.service, name="test-trust")
42
43 def tearDown(self):
44 sys.path = self.unittest_setup["sys_path_save"]
45
46 def test_sync_record_create_noexist(self):
47 fakeconn = MagicMock()
48 with patch.object(self.step_class, "connect_openstack_admin") as fake_connect_openstack_admin:
49 fake_connect_openstack_admin.return_value = fakeconn
50
51 trust_domain_id = 5678
52
53 xos_principal = Principal(name="test-principal", trust_domain=self.trust_domain)
54
Scott Bakerc808c672019-02-04 11:38:20 -080055 step = self.step_class(model_accessor=self.model_accessor)
Scott Baker62c7eaf2018-05-22 15:59:26 -070056 fakeconn.identity.find_user.return_value = None
57 fakeconn.identity.find_domain.return_value = MagicMock(id=trust_domain_id)
58
59 os_user = MagicMock()
60 os_user.id = "1234"
61 fakeconn.identity.create_user.return_value = os_user
62
63 step.sync_record(xos_principal)
64
65 fakeconn.identity.create_user.assert_called_with(name=xos_principal.name, domain_id=trust_domain_id)
66 self.assertEqual(xos_principal.backend_handle, "1234")
67
68 def test_sync_record_create_exists(self):
69 fakeconn = MagicMock()
70 with patch.object(self.step_class, "connect_openstack_admin") as fake_connect_openstack_admin:
71 fake_connect_openstack_admin.return_value = fakeconn
72
73 xos_principal = Principal(name="test-principal", trust_domain=self.trust_domain)
74
75 os_user = MagicMock()
76 os_user.id = "1234"
77
Scott Bakerc808c672019-02-04 11:38:20 -080078 step = self.step_class(model_accessor=self.model_accessor)
Scott Baker62c7eaf2018-05-22 15:59:26 -070079 fakeconn.identity.find_user.return_value = os_user
80 fakeconn.identity.create_user.return_value = None
81
82 step.sync_record(xos_principal)
83
84 fakeconn.identity.create_user.assert_not_called()
85 self.assertEqual(xos_principal.backend_handle, "1234")
86
87 def test_delete_record(self):
88 fakeconn = MagicMock()
89 with patch.object(self.step_class, "connect_openstack_admin") as fake_connect_openstack_admin:
90 fake_connect_openstack_admin.return_value = fakeconn
91
92 xos_principal = Principal(name="test-principal", trust_domain=self.trust_domain)
93
Scott Bakerc808c672019-02-04 11:38:20 -080094 step = self.step_class(model_accessor=self.model_accessor)
Scott Baker62c7eaf2018-05-22 15:59:26 -070095 os_user = MagicMock()
96 os_user.id = "1234"
97 fakeconn.identity.find_user.return_value = os_user
98 fakeconn.identity.delete_user.return_value = None
99
100 step.delete_record(xos_principal)
101 fakeconn.identity.delete_user.assert_called_with("1234")
102
103if __name__ == '__main__':
104 unittest.main()