charm-keystone-saml-mellon/unit_tests/test_keystone_saml_mellon_handlers.py
Hervé Beraud 0225c61e76 Use unittest.mock instead of mock
The mock third party library was needed for mock support in py2
runtimes. Since we now only support py36 and later, we can use the
standard lib unittest.mock module instead.

Note that https://github.com/openstack/charms.openstack is used during tests
and he need `mock`, unfortunatelly it doesn't declare `mock` in its
requirements so it retrieve mock from other charm project (cross dependency).
So we depend on charms.openstack first and when
Ib1ed5b598a52375e29e247db9ab4786df5b6d142 will be merged then CI
will pass without errors.

Depends-On: Ib1ed5b598a52375e29e247db9ab4786df5b6d142
Change-Id: Ib7e61c0293e398b831fbf8a6ade02bf833b78948
2021-12-15 14:04:23 +00:00

124 lines
4.6 KiB
Python

# Copyright 2019 Canonical Ltd
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from unittest import mock
import charm.openstack.keystone_saml_mellon as keystone_saml_mellon
import reactive.keystone_saml_mellon_handlers as handlers
import charms_openstack.test_utils as test_utils
class TestRegisteredHooks(test_utils.TestRegisteredHooks):
def test_hooks(self):
defaults = [
'charm.installed',
'update-status']
hook_set = {
'hook': {
'default_upgrade_charm': ('upgrade-charm',),
},
'when': {
'publish_sp_fid': (
'keystone-fid-service-provider.connected',),
'render_config': (
'keystone-fid-service-provider.available',),
'configure_websso': (
'websso-fid-service-provider.connected',),
},
'when_not': {
'keystone_departed': (
'keystone-fid-service-provider.connected',),
'assess_status': ('always.run',),
},
}
# test that the hooks were registered via the
# reactive.keystone_saml_mellon_handlers
self.registered_hooks_test_helper(handlers, hook_set, defaults)
class TestKeystoneSAMLMellonHandlers(test_utils.PatchHelper):
def setUp(self):
super().setUp()
self.patch_release(
keystone_saml_mellon.KeystoneSAMLMellonCharm.release)
self.keystone_saml_mellon_charm = mock.MagicMock()
self.patch_object(handlers.charm, 'provide_charm_instance',
new=mock.MagicMock())
self.provide_charm_instance().__enter__.return_value = (
self.keystone_saml_mellon_charm)
self.provide_charm_instance().__exit__.return_value = None
self.patch_object(handlers.reactive, 'any_file_changed',
new=mock.MagicMock())
self.endpoint = mock.MagicMock()
self.protocol_name = "mapped"
self.remote_id_attribute = "https://samltest.id"
self.idp_name = "samltest"
self.user_facing_name = "samltest.id"
self.keystone_saml_mellon_charm.options.protocol_name = (
self.protocol_name)
self.keystone_saml_mellon_charm.options.remote_id_attribute = (
self.remote_id_attribute)
self.keystone_saml_mellon_charm.options.idp_name = self.idp_name
self.keystone_saml_mellon_charm.options.user_facing_name = (
self.user_facing_name)
self.all_joined_units = []
for i in range(0, 2):
unit = mock.MagicMock()
unit.name = "keystone-{}".format(i)
unit.recieved = {"hostname": unit.name,
"port": "5000",
"tls-enabled": True}
self.all_joined_units.append(unit)
def test_keystone_departed(self):
handlers.keystone_departed()
self.keystone_saml_mellon_charm.remove_config.assert_called_once_with()
def test_publish_sp_fid(self):
handlers.publish_sp_fid(self.endpoint)
self.endpoint.publish.assert_called_once_with(
self.protocol_name, self.remote_id_attribute)
def test_render_config(self):
# No restart
self.any_file_changed.return_value = False
(self.keystone_saml_mellon_charm
.configuration_complete.return_value) = True
handlers.render_config(self.endpoint)
self.keystone_saml_mellon_charm.render_config.assert_called_once_with(
self.endpoint)
self.endpoint.request_restart.assert_not_called()
# Restart
self.any_file_changed.return_value = True
handlers.render_config(self.endpoint)
self.endpoint.request_restart.assert_called_once_with()
def test_configure_websso(self):
handlers.configure_websso(self.endpoint)
self.endpoint.publish.assert_called_once_with(
self.protocol_name, self.idp_name, self.user_facing_name)
def test_assess_status(self):
handlers.assess_status()
self.keystone_saml_mellon_charm.assess_status.assert_called_once_with()