Skip to content

Commit 3d33d65

Browse files
albu-dikujonasbardino
authored andcommitted
Add basic success case unit tests of the shared code dealing with peers.
1 parent 0fa32e7 commit 3d33d65

File tree

6 files changed

+211
-48
lines changed

6 files changed

+211
-48
lines changed

tests/fixture/peer_user_dict.json

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
{
2+
"full_name": "Test Peer User",
3+
"organization": "Test Org",
4+
"state": "NA",
5+
"country": "DK",
6+
"email": "peer@example.com",
7+
"comment": "test@example.com",
8+
"locality": "",
9+
"organizational_unit": "",
10+
"expire": 1758970812,
11+
"created": 1727434813.0792377,
12+
"openid_names": [],
13+
"distinguished_name": "/C=DK/ST=NA/L=NA/O=Test Org/OU=NA/CN=Test User/emailAddress=peer@example.com"
14+
}

tests/support/__init__.py

Lines changed: 57 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
import json
3636
import logging
3737
import os
38+
import pickle
3839
import shutil
3940
import stat
4041
import sys
@@ -257,6 +258,25 @@ def temppath(self, relative_path, **kwargs):
257258

258259
# custom assertions available for common use
259260

261+
def assertDirEmpty(self, relative_path):
262+
"""Make sure the supplied path is an empty directory"""
263+
path_kind = self.assertPathExists(relative_path)
264+
assert path_kind == "dir", "expected a directory but found %s" % (
265+
path_kind, )
266+
absolute_path = os.path.join(TEST_OUTPUT_DIR, relative_path)
267+
entries = os.listdir(absolute_path)
268+
assert not entries, "directory is not empty"
269+
270+
def assertDirNotEmpty(self, relative_path):
271+
"""Make sure the supplied path is a non-empty directory"""
272+
path_kind = self.assertPathExists(relative_path)
273+
assert path_kind == "dir", "expected a directory but found %s" % (
274+
path_kind, )
275+
absolute_path = os.path.join(TEST_OUTPUT_DIR, relative_path)
276+
entries = os.listdir(absolute_path)
277+
assert entries, "directory is empty"
278+
return [os.path.join(absolute_path, entry) for entry in entries]
279+
260280
def assertFileContentIdentical(self, file_actual, file_expected):
261281
"""Make sure file_actual and file_expected are identical"""
262282
with io.open(file_actual) as f_actual, io.open(file_expected) as f_expected:
@@ -285,9 +305,11 @@ def assertFileExists(self, relative_path):
285305

286306
def assertPathExists(self, relative_path):
287307
"""Make sure file in relative_path exists"""
288-
assert not os.path.isabs(
289-
relative_path), "expected relative path within output folder"
290-
absolute_path = os.path.join(TEST_OUTPUT_DIR, relative_path)
308+
if os.path.isabs(relative_path):
309+
self.assertPathWithin(relative_path, start=TEST_OUTPUT_DIR)
310+
absolute_path = relative_path
311+
else:
312+
absolute_path = os.path.join(TEST_OUTPUT_DIR, relative_path)
291313
return MigTestCase._absolute_path_kind(absolute_path)
292314

293315
@staticmethod
@@ -368,6 +390,34 @@ def _fixture_copy_as_temp(testcase, fixture_format, fixture_data, fixture_path,
368390
shutil.copyfile(fixture_path, copied_fixture_file)
369391
return copied_fixture_file
370392

393+
@staticmethod
394+
def _provision_test_user(self, distinguished_name):
395+
"""Provide a means to fabricate a useable test user on demand.
396+
"""
397+
398+
# ensure a user home directory for our test user
399+
conf_user_home = os.path.normpath(self.configuration.user_home)
400+
from mig.shared.base import client_id_dir
401+
test_client_dir_name = client_id_dir(distinguished_name)
402+
test_user_dir = os.path.join(conf_user_home, test_client_dir_name)
403+
404+
# ensure a user db that includes our test user
405+
conf_user_db_home = ensure_dirs_exist(self.configuration.user_db_home)
406+
prepared_fixture = self.prepareFixtureAssert(
407+
'MiG-users.db--example',
408+
fixture_format='pickle',
409+
)
410+
411+
test_db_file = prepared_fixture.copy_as_temp(prefix=conf_user_db_home)
412+
413+
# create the test user home directory
414+
ensure_dirs_exist(test_user_dir)
415+
# create the test user settings directory
416+
user_settings_dir = os.path.join(self.configuration.user_settings, test_client_dir_name)
417+
ensure_dirs_exist(user_settings_dir)
418+
419+
return test_user_dir
420+
371421

372422
def _to_display_path(value):
373423
"""Convert a relative path to one to be shown as part of test output."""
@@ -419,7 +469,7 @@ def fixturefile(relative_path, fixture_format=None):
419469

420470
data = None
421471

422-
if fixture_format == 'binary':
472+
if fixture_format == 'binary' or fixture_format == 'pickle':
423473
with open(tmp_path, 'rb') as binfile:
424474
data = binfile.read()
425475
elif fixture_format == 'json':
@@ -428,6 +478,9 @@ def fixturefile(relative_path, fixture_format=None):
428478
raise AssertionError(
429479
"unsupported fixture format: %s" % (fixture_format,))
430480

481+
if fixture_format == 'pickle':
482+
data = pickle.loads(data)
483+
431484
return data, tmp_path
432485

433486

Lines changed: 138 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
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+
"""Unit tests for peers related functions within the accountreq module"""
45+
46+
TEST_PEER_DN = '/C=DK/ST=NA/L=NA/O=Test Org/OU=NA/CN=Test User/emailAddress=peer@example.com'
47+
TEST_USER_DN = '/C=DK/ST=NA/L=NA/O=Test Org/OU=NA/CN=Test User/emailAddress=test@example.com'
48+
49+
@property
50+
def user_settings_dir(self):
51+
return self.configuration.user_settings
52+
53+
@property
54+
def user_pending_dir(self):
55+
return self.configuration.user_pending
56+
57+
def _load_saved_peer(self, absolute_path):
58+
self.assertPathWithin(absolute_path, start=self.user_pending_dir)
59+
with open(absolute_path, 'rb') as pickle_file:
60+
value = pickle.load(pickle_file)
61+
62+
def _string_if_bytes(value):
63+
if isinstance(value, bytes):
64+
return str(value, 'utf8')
65+
else:
66+
return value
67+
return {_string_if_bytes(x): _string_if_bytes(y) for x, y in value.items()}
68+
69+
def _peer_dict_from_fixture(self):
70+
fixture_data, _ = fixturefile("peer_user_dict", fixture_format="json")
71+
assert fixture_data["distinguished_name"] == self.TEST_PEER_DN
72+
return fixture_data
73+
74+
def _record_peer_acceptance(self, test_client_dir_name, peer_distinguished_name):
75+
"""Fabricate a peer acceptance record in a particular user settings dir.
76+
"""
77+
78+
test_user_accepted_peers_file = os.path.join(self.user_settings_dir, test_client_dir_name, "peers")
79+
expire_tomorrow = datetime.date.today() + datetime.timedelta(days=1)
80+
with open(test_user_accepted_peers_file, "wb") as test_user_accepted_peers:
81+
pickle.dump({peer_distinguished_name:{'expire': str(expire_tomorrow) }}, test_user_accepted_peers)
82+
83+
def _provide_configuration(self):
84+
return 'testconfig'
85+
86+
def before_each(self):
87+
ensure_dirs_exist(self.configuration.user_cache)
88+
ensure_dirs_exist(self.configuration.user_pending)
89+
ensure_dirs_exist(self.configuration.user_settings)
90+
ensure_dirs_exist(self.configuration.mrsl_files_dir)
91+
ensure_dirs_exist(self.configuration.resource_pending)
92+
93+
def test_a_new_peer(self):
94+
# precondition
95+
self.assertDirEmpty(self.configuration.user_pending)
96+
request_dict = self._peer_dict_from_fixture()
97+
98+
success, _ = accountreq.save_account_request(self.configuration, request_dict)
99+
100+
# check that we have an output directory now
101+
absolute_files = self.assertDirNotEmpty(self.user_pending_dir)
102+
self.assertEqual(len(absolute_files), 1)
103+
# check the saved peer
104+
peer_user_dict = self._load_saved_peer(absolute_files[0])
105+
self.assertEqual(peer_user_dict, request_dict)
106+
107+
def test_listing_peers(self):
108+
# precondition
109+
self.assertDirEmpty(self.user_pending_dir)
110+
request_dict = self._peer_dict_from_fixture()
111+
accountreq.save_account_request(self.configuration, request_dict)
112+
113+
success, listing = accountreq.list_account_reqs(self.configuration)
114+
115+
self.assertTrue(success)
116+
self.assertEqual(len(listing), 1)
117+
# check the fabricated peer was listed
118+
peer_temp_file_name = listing[0] # sadly listing returns _relative_ dirs
119+
peer_pickle_file = os.path.join(self.user_pending_dir, peer_temp_file_name)
120+
peer_pickle = self._load_saved_peer(peer_pickle_file)
121+
self.assertEqual(peer_pickle['distinguished_name'], self.TEST_PEER_DN)
122+
123+
def test_peer_acceptance(self):
124+
test_client_dir = self._provision_test_user(self, self.TEST_USER_DN)
125+
test_client_dir_name = os.path.basename(test_client_dir)
126+
self._record_peer_acceptance(test_client_dir_name, self.TEST_PEER_DN)
127+
self.assertDirEmpty(self.user_pending_dir)
128+
request_dict = self._peer_dict_from_fixture()
129+
success, req_path = accountreq.save_account_request(self.configuration, request_dict)
130+
arranged_req_id = os.path.basename(req_path)
131+
132+
success, message = accountreq.accept_account_req(arranged_req_id, self.configuration, keyword_auto)
133+
134+
self.assertTrue(success)
135+
136+
137+
if __name__ == '__main__':
138+
testmain()

tests/test_mig_shared_functionality_cat.py

Lines changed: 1 addition & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -69,25 +69,7 @@ def _provide_configuration(self):
6969
return 'testconfig'
7070

7171
def before_each(self):
72-
# ensure a user home directory for our test user
73-
conf_user_home = self.configuration.user_home[:-1]
74-
test_client_dir = client_id_dir(self.TEST_CLIENT_ID)
75-
test_user_dir = os.path.join(conf_user_home, test_client_dir)
76-
77-
# ensure a user db that includes our test user
78-
79-
conf_user_db_home = ensure_dirs_exist(self.configuration.user_db_home)
80-
temppath(conf_user_db_home, self)
81-
prepared_fixture = self.prepareFixtureAssert(
82-
'MiG-users.db--example',
83-
fixture_format='binary',
84-
)
85-
86-
test_db_file = prepared_fixture.copy_as_temp(prefix=conf_user_db_home)
87-
88-
# create the test user home directory
89-
self.test_user_dir = ensure_dirs_exist(test_user_dir)
90-
temppath(self.test_user_dir, self)
72+
self.test_user_dir = self._provision_test_user(self, self.TEST_CLIENT_ID)
9173
self.test_environ = create_http_environ(self.configuration)
9274

9375
def assertSingleOutputObject(self, output_objects, with_object_type=None):

tests/test_mig_shared_functionality_datatransfer.py

Lines changed: 1 addition & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -74,31 +74,7 @@ def _provide_configuration(self):
7474
return "testconfig"
7575

7676
def before_each(self):
77-
# ensure a user home directory for our test user
78-
conf_user_home = self.configuration.user_home[:-1]
79-
test_client_dir = client_id_dir(self.TEST_CLIENT_ID)
80-
test_user_dir = os.path.join(conf_user_home, test_client_dir)
81-
82-
# ensure a user db that includes our test user
83-
conf_user_db_home = ensure_dirs_exist(self.configuration.user_db_home)
84-
temppath(conf_user_db_home, self)
85-
prepared_fixture = self.prepareFixtureAssert(
86-
"MiG-users.db--example",
87-
fixture_format="binary",
88-
)
89-
90-
prepared_fixture.copy_as_temp(prefix=conf_user_db_home)
91-
92-
# create the test user home directory
93-
self.test_user_dir = ensure_dirs_exist(test_user_dir)
94-
temppath(self.test_user_dir, self)
95-
96-
# ensure the user_settings home directory for our test user
97-
conf_user_settings_home = ensure_dirs_exist(self.configuration.user_settings)
98-
temppath(conf_user_settings_home, self)
99-
test_user_settings_dir = os.path.join(conf_user_settings_home, test_client_dir)
100-
ensure_dirs_exist(test_user_settings_dir)
101-
77+
self.test_user_dir = self._provision_test_user(self, self.TEST_CLIENT_ID)
10278
self.test_environ = create_http_environ(self.configuration)
10379

10480
def test_default_disabled_site_transfer(self):

0 commit comments

Comments
 (0)