-
Notifications
You must be signed in to change notification settings - Fork 4
Add basic success case unit tests of the shared code dealing with peers. #324
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
File renamed without changes.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
{ | ||
"full_name": "Test Peer User", | ||
"organization": "Test Org", | ||
"state": "NA", | ||
"country": "DK", | ||
"email": "[email protected]", | ||
"comment": "[email protected]", | ||
"locality": "", | ||
"organizational_unit": "", | ||
"expire": 1758970812, | ||
"created": 1727434813.0792377, | ||
"openid_names": [], | ||
"distinguished_name": "/C=DK/ST=NA/L=NA/O=Test Org/OU=NA/CN=Test User/[email protected]" | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,138 @@ | ||
# -*- coding: utf-8 -*- | ||
# | ||
# --- BEGIN_HEADER --- | ||
# | ||
# test_mig_shared_accountreq - unit test of the corresponding mig lib module | ||
# Copyright (C) 2003-2025 The MiG Project by the Science HPC Center at UCPH | ||
# | ||
# This file is part of MiG. | ||
# | ||
# MiG is free software: you can redistribute it and/or modify | ||
# it under the terms of the GNU General Public License as published by | ||
# the Free Software Foundation; either version 2 of the License, or | ||
# (at your option) any later version. | ||
# | ||
# MiG is distributed in the hope that it will be useful, | ||
# but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
# GNU General Public License for more details. | ||
# | ||
# You should have received a copy of the GNU General Public License | ||
# along with this program; if not, write to the Free Software | ||
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, | ||
# USA. | ||
# | ||
# --- END_HEADER --- | ||
# | ||
|
||
"""Unit tests for the migrid module pointed to in the filename""" | ||
|
||
import datetime | ||
import os | ||
import pickle | ||
import sys | ||
import unittest | ||
|
||
from tests.support import MigTestCase, testmain, fixturefile, ensure_dirs_exist | ||
|
||
import mig.shared.accountreq as accountreq | ||
from mig.shared.base import canonical_user, fill_distinguished_name | ||
from mig.shared.defaults import keyword_auto | ||
|
||
|
||
class MigSharedAccountreq__peers(MigTestCase): | ||
"""Unit tests for peers related functions within the accountreq module""" | ||
|
||
TEST_PEER_DN = '/C=DK/ST=NA/L=NA/O=Test Org/OU=NA/CN=Test User/[email protected]' | ||
TEST_USER_DN = '/C=DK/ST=NA/L=NA/O=Test Org/OU=NA/CN=Test User/[email protected]' | ||
|
||
@property | ||
def user_settings_dir(self): | ||
return self.configuration.user_settings | ||
|
||
@property | ||
def user_pending_dir(self): | ||
return self.configuration.user_pending | ||
|
||
def _load_saved_peer(self, absolute_path): | ||
self.assertPathWithin(absolute_path, start=self.user_pending_dir) | ||
with open(absolute_path, 'rb') as pickle_file: | ||
value = pickle.load(pickle_file) | ||
|
||
def _string_if_bytes(value): | ||
if isinstance(value, bytes): | ||
return str(value, 'utf8') | ||
else: | ||
return value | ||
return {_string_if_bytes(x): _string_if_bytes(y) for x, y in value.items()} | ||
|
||
def _peer_dict_from_fixture(self): | ||
fixture_data, _ = fixturefile("peer_user_dict", fixture_format="json") | ||
assert fixture_data["distinguished_name"] == self.TEST_PEER_DN | ||
return fixture_data | ||
|
||
def _record_peer_acceptance(self, test_client_dir_name, peer_distinguished_name): | ||
"""Fabricate a peer acceptance record in a particular user settings dir. | ||
""" | ||
|
||
test_user_accepted_peers_file = os.path.join(self.user_settings_dir, test_client_dir_name, "peers") | ||
expire_tomorrow = datetime.date.today() + datetime.timedelta(days=1) | ||
with open(test_user_accepted_peers_file, "wb") as test_user_accepted_peers: | ||
pickle.dump({peer_distinguished_name:{'expire': str(expire_tomorrow) }}, test_user_accepted_peers) | ||
|
||
def _provide_configuration(self): | ||
return 'testconfig' | ||
|
||
def before_each(self): | ||
ensure_dirs_exist(self.configuration.user_cache) | ||
ensure_dirs_exist(self.configuration.user_pending) | ||
ensure_dirs_exist(self.configuration.user_settings) | ||
ensure_dirs_exist(self.configuration.mrsl_files_dir) | ||
ensure_dirs_exist(self.configuration.resource_pending) | ||
|
||
def test_a_new_peer(self): | ||
# precondition | ||
self.assertDirEmpty(self.configuration.user_pending) | ||
request_dict = self._peer_dict_from_fixture() | ||
|
||
success, _ = accountreq.save_account_request(self.configuration, request_dict) | ||
|
||
# check that we have an output directory now | ||
absolute_files = self.assertDirNotEmpty(self.user_pending_dir) | ||
self.assertEqual(len(absolute_files), 1) | ||
# check the saved peer | ||
peer_user_dict = self._load_saved_peer(absolute_files[0]) | ||
self.assertEqual(peer_user_dict, request_dict) | ||
|
||
def test_listing_peers(self): | ||
# precondition | ||
self.assertDirEmpty(self.user_pending_dir) | ||
request_dict = self._peer_dict_from_fixture() | ||
accountreq.save_account_request(self.configuration, request_dict) | ||
|
||
success, listing = accountreq.list_account_reqs(self.configuration) | ||
|
||
self.assertTrue(success) | ||
self.assertEqual(len(listing), 1) | ||
# check the fabricated peer was listed | ||
peer_temp_file_name = listing[0] # sadly listing returns _relative_ dirs | ||
peer_pickle_file = os.path.join(self.user_pending_dir, peer_temp_file_name) | ||
peer_pickle = self._load_saved_peer(peer_pickle_file) | ||
self.assertEqual(peer_pickle['distinguished_name'], self.TEST_PEER_DN) | ||
|
||
def test_peer_acceptance(self): | ||
test_client_dir = self._provision_test_user(self, self.TEST_USER_DN) | ||
test_client_dir_name = os.path.basename(test_client_dir) | ||
self._record_peer_acceptance(test_client_dir_name, self.TEST_PEER_DN) | ||
self.assertDirEmpty(self.user_pending_dir) | ||
request_dict = self._peer_dict_from_fixture() | ||
success, req_path = accountreq.save_account_request(self.configuration, request_dict) | ||
arranged_req_id = os.path.basename(req_path) | ||
|
||
success, message = accountreq.accept_account_req(arranged_req_id, self.configuration, keyword_auto) | ||
|
||
self.assertTrue(success) | ||
|
||
|
||
if __name__ == '__main__': | ||
testmain() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.