|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | +# |
| 3 | +# --- BEGIN_HEADER --- |
| 4 | +# |
| 5 | +# test_mig_server-createuser - unit tests for the migrid createuser CLI |
| 6 | +# Copyright (C) 2003-2024 The MiG Project by the Science HPC Center at UCPH |
| 7 | +# |
| 8 | +# This file is part of MiG. |
| 9 | +# |
| 10 | +# MiG is free software: you can redistribute it and/or modify |
| 11 | +# it under the terms of the GNU General Public License as published by |
| 12 | +# the Free Software Foundation; either version 2 of the License, or |
| 13 | +# (at your option) any later version. |
| 14 | +# |
| 15 | +# MiG is distributed in the hope that it will be useful, |
| 16 | +# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 17 | +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 18 | +# GNU General Public License for more details. |
| 19 | +# |
| 20 | +# You should have received a copy of the GNU General Public License |
| 21 | +# along with this program; if not, write to the Free Software |
| 22 | +# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, |
| 23 | +# USA. |
| 24 | +# |
| 25 | +# --- END_HEADER --- |
| 26 | +# |
| 27 | + |
| 28 | +"""Unit tests for the migrid module pointed to in the filename""" |
| 29 | + |
| 30 | +from __future__ import print_function |
| 31 | +import codecs |
| 32 | +import os |
| 33 | +import shutil |
| 34 | +import sys |
| 35 | +import unittest |
| 36 | + |
| 37 | +from tests.support import MIG_BASE, TEST_OUTPUT_DIR, MigTestCase, testmain |
| 38 | +from tests.support.picklesupp import PickleAssertMixin |
| 39 | + |
| 40 | +from mig.shared.base import keyword_auto |
| 41 | +from mig.shared.useradm import create_user |
| 42 | + |
| 43 | +_USERADM_CONFIG_DIR_KEYS = ('user_db_home', 'user_home', 'user_settings', |
| 44 | + 'user_cache', 'mrsl_files_dir', 'resource_pending') |
| 45 | + |
| 46 | + |
| 47 | +def _as_bytes(value): |
| 48 | + if isinstance(value, str): |
| 49 | + return codecs.encode(value, 'utf8') |
| 50 | + else: |
| 51 | + return value |
| 52 | + |
| 53 | + |
| 54 | +class TestMigSharedUsedadm_create_user(MigTestCase, PickleAssertMixin): |
| 55 | + def before_each(self): |
| 56 | + configuration = self.configuration |
| 57 | + test_state_path = configuration.state_path |
| 58 | + |
| 59 | + for config_key in _USERADM_CONFIG_DIR_KEYS: |
| 60 | + dir_path = getattr(configuration, config_key)[0:-1] |
| 61 | + try: |
| 62 | + shutil.rmtree(dir_path) |
| 63 | + except: |
| 64 | + pass |
| 65 | + |
| 66 | + self.expected_user_db_home = configuration.user_db_home[0:-1] |
| 67 | + self.expected_user_db_file = os.path.join( |
| 68 | + self.expected_user_db_home, 'MiG-users.db') |
| 69 | + |
| 70 | + def _provide_configuration(self): |
| 71 | + return 'testconfig' |
| 72 | + |
| 73 | + def test_user_db_is_created(self): |
| 74 | + user_dict = {} |
| 75 | + user_dict['full_name'] = "Test User" |
| 76 | + user_dict['organization'] = "Test Org" |
| 77 | + user_dict['state'] = "NA" |
| 78 | + user_dict['country'] = "DK" |
| 79 | + user_dict['email'] = "user@example.com" |
| 80 | + user_dict['comment'] = "This is the create comment" |
| 81 | + user_dict['password'] = "password" |
| 82 | + create_user(user_dict, self.configuration, |
| 83 | + keyword_auto, default_renew=True) |
| 84 | + |
| 85 | + # presence of user home |
| 86 | + path_kind = MigTestCase._absolute_path_kind(self.expected_user_db_home) |
| 87 | + self.assertEqual(path_kind, 'dir') |
| 88 | + |
| 89 | + # presence of user db |
| 90 | + path_kind = MigTestCase._absolute_path_kind(self.expected_user_db_file) |
| 91 | + self.assertEqual(path_kind, 'file') |
| 92 | + |
| 93 | + def test_user_entry_is_recorded(self): |
| 94 | + def _generate_salt(): |
| 95 | + return b'CCCC12344321CCCC' |
| 96 | + |
| 97 | + expected_user_id = '/C=DK/ST=NA/L=NA/O=Test Org/OU=NA/CN=Test User/emailAddress=user@example.com' |
| 98 | + expected_user_unique_id = 'OUejmKGMFWPWLyi5chqQalxDgltTuG1SoZUsqGyj32yY3275GjA2GfMo5odeWuKQ' |
| 99 | + expected_user_password_hash = "PBKDF2$sha256$10000$b'CCCC12344321CCCC'$b'bph8p/avUq42IYeOdJoJuUqrJ7Q32eaT'" |
| 100 | + |
| 101 | + user_dict = {} |
| 102 | + user_dict['full_name'] = "Test User" |
| 103 | + user_dict['organization'] = "Test Org" |
| 104 | + user_dict['state'] = "NA" |
| 105 | + user_dict['country'] = "DK" |
| 106 | + user_dict['email'] = "user@example.com" |
| 107 | + user_dict['comment'] = "This is the create comment" |
| 108 | + user_dict['password'] = "password" |
| 109 | + |
| 110 | + create_user(user_dict, self.configuration, |
| 111 | + keyword_auto, default_renew=True) |
| 112 | + |
| 113 | + pickled = self.assertPickledFile(self.expected_user_db_file) |
| 114 | + # FIXME: Python3 pickle appears to be keyed by bytes |
| 115 | + picked_expected_user_id = _as_bytes(expected_user_id) |
| 116 | + self.assertIn(picked_expected_user_id, pickled) |
| 117 | + |
| 118 | + actual_user_object = pickled[picked_expected_user_id] |
| 119 | + |
| 120 | + # TODO: remove resetting the handful of keys here done because changes |
| 121 | + # to make them assertion frienfly values will increase the size |
| 122 | + # of the diff which, at time of commit, are best minimised. |
| 123 | + actual_user_object[b'created'] = 9999999999.9999999 |
| 124 | + actual_user_object[b'unique_id'] = '__UNIQUE_ID__' |
| 125 | + |
| 126 | + self.assertEqual(actual_user_object, { |
| 127 | + b'full_name': b'Test User', |
| 128 | + b'organization': b'Test Org', |
| 129 | + b'state': b'NA', |
| 130 | + b'country': b'DK', |
| 131 | + b'email': b'user@example.com', |
| 132 | + b'comment': b'This is the create comment', |
| 133 | + b'password': b'password', |
| 134 | + b'distinguished_name': picked_expected_user_id, |
| 135 | + b'created': 9999999999.9999999, |
| 136 | + b'unique_id': '__UNIQUE_ID__', |
| 137 | + b'openid_names': [], |
| 138 | + }) |
| 139 | + |
| 140 | + |
| 141 | +if __name__ == '__main__': |
| 142 | + testmain() |
0 commit comments