blob: f06ec48890aa37f5d690292fd1ed2a4447b278ae [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
35 from sync_principal import SyncPrincipal
36 self.step_class = SyncPrincipal
37
38 self.service = OpenStackService()
39 self.trust_domain = TrustDomain(owner=self.service, name="test-trust")
40
41 def tearDown(self):
42 sys.path = self.unittest_setup["sys_path_save"]
43
44 def test_sync_record_create_noexist(self):
45 fakeconn = MagicMock()
46 with patch.object(self.step_class, "connect_openstack_admin") as fake_connect_openstack_admin:
47 fake_connect_openstack_admin.return_value = fakeconn
48
49 trust_domain_id = 5678
50
51 xos_principal = Principal(name="test-principal", trust_domain=self.trust_domain)
52
53 step = self.step_class()
54 fakeconn.identity.find_user.return_value = None
55 fakeconn.identity.find_domain.return_value = MagicMock(id=trust_domain_id)
56
57 os_user = MagicMock()
58 os_user.id = "1234"
59 fakeconn.identity.create_user.return_value = os_user
60
61 step.sync_record(xos_principal)
62
63 fakeconn.identity.create_user.assert_called_with(name=xos_principal.name, domain_id=trust_domain_id)
64 self.assertEqual(xos_principal.backend_handle, "1234")
65
66 def test_sync_record_create_exists(self):
67 fakeconn = MagicMock()
68 with patch.object(self.step_class, "connect_openstack_admin") as fake_connect_openstack_admin:
69 fake_connect_openstack_admin.return_value = fakeconn
70
71 xos_principal = Principal(name="test-principal", trust_domain=self.trust_domain)
72
73 os_user = MagicMock()
74 os_user.id = "1234"
75
76 step = self.step_class()
77 fakeconn.identity.find_user.return_value = os_user
78 fakeconn.identity.create_user.return_value = None
79
80 step.sync_record(xos_principal)
81
82 fakeconn.identity.create_user.assert_not_called()
83 self.assertEqual(xos_principal.backend_handle, "1234")
84
85 def test_delete_record(self):
86 fakeconn = MagicMock()
87 with patch.object(self.step_class, "connect_openstack_admin") as fake_connect_openstack_admin:
88 fake_connect_openstack_admin.return_value = fakeconn
89
90 xos_principal = Principal(name="test-principal", trust_domain=self.trust_domain)
91
92 step = self.step_class()
93 os_user = MagicMock()
94 os_user.id = "1234"
95 fakeconn.identity.find_user.return_value = os_user
96 fakeconn.identity.delete_user.return_value = None
97
98 step.delete_record(xos_principal)
99 fakeconn.identity.delete_user.assert_called_with("1234")
100
101if __name__ == '__main__':
102 unittest.main()