|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | +# |
| 3 | +# --- BEGIN_HEADER --- |
| 4 | +# |
| 5 | +# test_mig_shared_accountreq - unit test of the corresponding mig lib module |
| 6 | +# Copyright (C) 2003-2025 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 | +import datetime |
| 31 | +import os |
| 32 | +import pickle |
| 33 | +import sys |
| 34 | +import unittest |
| 35 | + |
| 36 | +from tests.support import MigTestCase, testmain, fixturefile, ensure_dirs_exist |
| 37 | + |
| 38 | +import mig.shared.accountreq as accountreq |
| 39 | +from mig.shared.base import canonical_user, fill_distinguished_name |
| 40 | +from mig.shared.defaults import keyword_auto |
| 41 | + |
| 42 | + |
| 43 | +class MigSharedAccountreq__peers(MigTestCase): |
| 44 | + |
| 45 | + TEST_PEER_DN = '/C=DK/ST=NA/L=NA/O=Test Org/OU=NA/CN=Test User/[email protected]' |
| 46 | + TEST_USER_DN = '/C=DK/ST=NA/L=NA/O=Test Org/OU=NA/CN=Test User/[email protected]' |
| 47 | + |
| 48 | + @property |
| 49 | + def user_pending_dir(self): |
| 50 | + return self.configuration.user_pending |
| 51 | + |
| 52 | + def _load_pickled_peer(self, absolute_path): |
| 53 | + self.assertPathWithin(absolute_path, start=self.user_pending_dir) |
| 54 | + with open(absolute_path, 'rb') as pickle_file: |
| 55 | + value = pickle.load(pickle_file) |
| 56 | + |
| 57 | + def _string_if_bytes(value): |
| 58 | + if isinstance(value, bytes): |
| 59 | + return str(value, 'utf8') |
| 60 | + else: |
| 61 | + return value |
| 62 | + return {_string_if_bytes(x): _string_if_bytes(y) for x, y in value.items()} |
| 63 | + |
| 64 | + def _peer_dict_from_fixture(self): |
| 65 | + fixture_data, fixture_path = fixturefile( |
| 66 | + "MiG-users.db--example", |
| 67 | + fixture_format="pickle" |
| 68 | + ) |
| 69 | + raw_user = dict(fixture_data[self.TEST_USER_DN]) |
| 70 | + del raw_user["distinguished_name"] |
| 71 | + del raw_user["unique_id"] |
| 72 | + del raw_user["password"] |
| 73 | + del raw_user["password_hash"] |
| 74 | + raw_user[ "email"] = "[email protected]" |
| 75 | + |
| 76 | + # avoid the need for the 'site_peers_explicit_fields' config option.. |
| 77 | + raw_user['comment'] = fixture_data[self.TEST_USER_DN]["email"] |
| 78 | + |
| 79 | + user_dict = canonical_user(self.configuration, raw_user, raw_user.keys()) |
| 80 | + fill_distinguished_name(user_dict) |
| 81 | + return user_dict |
| 82 | + |
| 83 | + def _record_peer_acceptance(self, test_client_dir_name, peer_distinguished_name): |
| 84 | + test_user_accepted_peers_file = os.path.join(self.configuration.user_settings, test_client_dir_name, "peers") |
| 85 | + expire_tomorrow = datetime.date.today() + datetime.timedelta(days=1) |
| 86 | + with open(test_user_accepted_peers_file, "wb") as test_user_accepted_peers: |
| 87 | + pickle.dump({peer_distinguished_name:{'expire': str(expire_tomorrow) }}, test_user_accepted_peers) |
| 88 | + |
| 89 | + def _settings_dir_for_user(self, distinguished_name): |
| 90 | + return os.path.join(self.configuration.user_settings, distinguished_name) |
| 91 | + |
| 92 | + def _provide_configuration(self): |
| 93 | + return 'testconfig' |
| 94 | + |
| 95 | + def before_each(self): |
| 96 | + ensure_dirs_exist(self.configuration.user_cache) |
| 97 | + ensure_dirs_exist(self.configuration.user_pending) |
| 98 | + ensure_dirs_exist(self.configuration.user_settings) |
| 99 | + ensure_dirs_exist(self.configuration.mrsl_files_dir) |
| 100 | + ensure_dirs_exist(self.configuration.resource_pending) |
| 101 | + |
| 102 | + def test_a_new_peer(self): |
| 103 | + # precondition |
| 104 | + self.assertDirEmpty(self.configuration.user_pending) |
| 105 | + request_dict = self._peer_dict_from_fixture() |
| 106 | + |
| 107 | + success, _ = accountreq.save_account_request(self.configuration, request_dict) |
| 108 | + |
| 109 | + # check that we have an output directory now |
| 110 | + absolute_files = self.assertDirNotEmpty(self.user_pending_dir) |
| 111 | + self.assertEqual(len(absolute_files), 1) |
| 112 | + # check the saved peer |
| 113 | + peer_user_dict = self._load_pickled_peer(absolute_files[0]) |
| 114 | + self.assertEqual(peer_user_dict, request_dict) |
| 115 | + |
| 116 | + def test_listing_peers(self): |
| 117 | + # precondition |
| 118 | + self.assertDirEmpty(self.configuration.user_pending) |
| 119 | + request_dict = self._peer_dict_from_fixture() |
| 120 | + success, _ = accountreq.save_account_request(self.configuration, request_dict) |
| 121 | + |
| 122 | + success, listing = accountreq.list_account_reqs(self.configuration) |
| 123 | + |
| 124 | + self.assertTrue(success) |
| 125 | + self.assertEqual(len(listing), 1) |
| 126 | + peer_pickle_file = os.path.join(self.user_pending_dir, listing[0]) |
| 127 | + peer_pickle = self._load_pickled_peer(peer_pickle_file) |
| 128 | + self.assertEqual(peer_pickle['distinguished_name'], self.TEST_PEER_DN) |
| 129 | + |
| 130 | + def test_peer_acceptance(self): |
| 131 | + test_client_dir = self._provision_test_user(self, self.TEST_USER_DN) |
| 132 | + test_client_dir_name = os.path.basename(test_client_dir) |
| 133 | + self._record_peer_acceptance(test_client_dir_name, self.TEST_PEER_DN) |
| 134 | + self.assertDirEmpty(self.configuration.user_pending) |
| 135 | + request_dict = self._peer_dict_from_fixture() |
| 136 | + success, req_path = accountreq.save_account_request(self.configuration, request_dict) |
| 137 | + arranged_req_id = os.path.basename(req_path) |
| 138 | + |
| 139 | + success, message = accountreq.accept_account_req(arranged_req_id, self.configuration, keyword_auto) |
| 140 | + |
| 141 | + self.assertTrue(success) |
| 142 | + |
| 143 | + |
| 144 | +if __name__ == '__main__': |
| 145 | + testmain() |
0 commit comments