Skip to content

Commit

Permalink
refactor tests
Browse files Browse the repository at this point in the history
  • Loading branch information
mikkonie committed Nov 1, 2023
1 parent 48ad9fe commit ac7cec3
Showing 1 changed file with 79 additions and 95 deletions.
174 changes: 79 additions & 95 deletions projectroles/tests/test_remote_projects_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -129,21 +129,41 @@
SET_STAR_UUID = str(uuid.uuid4())


class RemoteProjectsAPITestBase(RoleMixin, TestCase):
"""Base class for remote project API tests"""

def assert_app_setting(self, uuid, expected):
"""
Assert app setting model data. Model id and sodar_uuid fields can be
left out of the expected dict, they will be populated automatically.
:param uuid: AppSetting UUID
:param expected: Dict of expected data as model_to_dict output
"""
set_obj = AppSetting.objects.get(sodar_uuid=uuid)
expected['id'] = set_obj.id
expected['sodar_uuid'] = set_obj.sodar_uuid
set_dict = model_to_dict(set_obj)
self.assertEqual(set_dict, expected)

def setUp(self):
# Init roles
self.init_roles()


class TestGetSourceData(
ProjectMixin,
RoleMixin,
RoleAssignmentMixin,
RemoteSiteMixin,
RemoteProjectMixin,
SODARUserMixin,
AppSettingMixin,
TestCase,
RemoteProjectsAPITestBase,
):
"""Tests for get_source_data()"""

def setUp(self):
# Init roles
self.init_roles()
super().setUp()
# Init an LDAP user on the source site
self.user_source = self.make_sodar_user(
username=SOURCE_USER_USERNAME,
Expand Down Expand Up @@ -573,24 +593,22 @@ def test_get_no_access(self):
@override_settings(PROJECTROLES_SITE_MODE=SITE_MODE_TARGET)
class SyncRemoteDataTestBase(
ProjectMixin,
RoleMixin,
RoleAssignmentMixin,
RemoteSiteMixin,
RemoteProjectMixin,
SODARUserMixin,
AppSettingMixin,
TestCase,
RemoteProjectsAPITestBase,
):
"""Base class for tests for sync_remote_data()"""

def setUp(self):
super().setUp()
# Init users
self.admin_user = self.make_user(settings.PROJECTROLES_DEFAULT_ADMIN)
self.admin_user.is_staff = True
self.admin_user.is_superuser = True
self.maxDiff = None
# Init roles
self.init_roles()
# Init source site
self.source_site = self.make_site(
name=SOURCE_SITE_NAME,
Expand Down Expand Up @@ -764,7 +782,7 @@ def test_create(self):
},
}
)
original_data = deepcopy(self.default_data)
og_data = deepcopy(self.default_data)
# Do sync
self.remote_api.sync_remote_data(self.source_site, self.default_data)

Expand Down Expand Up @@ -964,10 +982,6 @@ def test_create(self):
self.assertEqual(peer_project_dict, expected)

# Assert app settings
set_dict = model_to_dict(
AppSetting.objects.get(sodar_uuid=SET_IP_RESTRICT_UUID)
)
set_dict.pop('id')
expected = {
'name': 'ip_restrict',
'type': 'BOOLEAN',
Expand All @@ -977,13 +991,8 @@ def test_create(self):
'app_plugin': None,
'user': None,
'user_modifiable': True,
'sodar_uuid': uuid.UUID(SET_IP_RESTRICT_UUID),
}
self.assertEqual(set_dict, expected)
set_dict = model_to_dict(
AppSetting.objects.get(sodar_uuid=SET_IP_ALLOWLIST_UUID)
)
set_dict.pop('id')
self.assert_app_setting(SET_IP_RESTRICT_UUID, expected)
expected = {
'name': 'ip_allowlist',
'type': 'JSON',
Expand All @@ -993,13 +1002,8 @@ def test_create(self):
'app_plugin': None,
'user': None,
'user_modifiable': True,
'sodar_uuid': uuid.UUID(SET_IP_ALLOWLIST_UUID),
}
self.assertEqual(set_dict, expected)
set_dict = model_to_dict(
AppSetting.objects.get(sodar_uuid=SET_STAR_UUID)
)
set_dict.pop('id')
self.assert_app_setting(SET_IP_ALLOWLIST_UUID, expected)
expected = {
'name': 'project_star',
'type': 'BOOLEAN',
Expand All @@ -1009,12 +1013,11 @@ def test_create(self):
'project': project_obj.id,
'user': new_user.id,
'user_modifiable': True,
'sodar_uuid': uuid.UUID(SET_STAR_UUID),
}
self.assertEqual(set_dict, expected)
self.assert_app_setting(SET_STAR_UUID, expected)

# Assert remote_data changes
expected = original_data
expected = og_data
expected['users'][SOURCE_USER_UUID]['status'] = 'created'
expected['users'][SOURCE_USER2_UUID]['status'] = 'created'
expected['users'][SOURCE_USER3_UUID]['status'] = 'created'
Expand Down Expand Up @@ -1050,6 +1053,7 @@ def test_create(self):
expected['app_settings'][SET_STAR_UUID]['status'] = 'created'
self.assertEqual(self.default_data, expected)

# TODO: Update once local settings updating is fixed
def test_create_app_setting_local(self):
"""Test sync with local app setting"""
remote_data = self.default_data
Expand All @@ -1071,7 +1075,6 @@ def test_create_app_setting_local(self):
expected['app_settings'][SET_IP_ALLOWLIST_UUID]['status'] = 'created'
expected['app_settings'][SET_STAR_UUID]['status'] = 'created'
self.assertEqual(remote_data, expected)
# TODO: Assert app setting data (should be unchanged because local)

def test_create_multiple(self):
"""Test sync with multiple non-existing projects"""
Expand Down Expand Up @@ -1099,7 +1102,7 @@ def test_create_multiple(self):
}
},
}
original_data = deepcopy(remote_data)
og_data = deepcopy(remote_data)
self.remote_api.sync_remote_data(self.source_site, remote_data)

self.assertEqual(Project.objects.all().count(), 3)
Expand Down Expand Up @@ -1137,7 +1140,7 @@ def test_create_multiple(self):
}
self.assertEqual(model_to_dict(p_new_owner_obj), expected)

expected = original_data
expected = og_data
expected['users'][SOURCE_USER_UUID]['status'] = 'created'
expected['projects'][SOURCE_CATEGORY_UUID]['status'] = 'created'
expected['projects'][SOURCE_CATEGORY_UUID]['roles'][
Expand Down Expand Up @@ -1247,7 +1250,7 @@ def test_create_no_access(self):
remote_data['projects'][SOURCE_PROJECT_UUID][
'level'
] = REMOTE_LEVEL_READ_INFO
original_data = deepcopy(remote_data)
og_data = deepcopy(remote_data)
self.remote_api.sync_remote_data(self.source_site, remote_data)

self.assertEqual(Project.objects.all().count(), 0)
Expand All @@ -1256,7 +1259,7 @@ def test_create_no_access(self):
self.assertEqual(RemoteProject.objects.all().count(), 0)
self.assertEqual(RemoteSite.objects.all().count(), 1)
# Assert no changes between update_data and remote_data
self.assertEqual(original_data, remote_data)
self.assertEqual(og_data, remote_data)

def test_create_local_user(self):
"""Test sync with a local non-owner user"""
Expand Down Expand Up @@ -1421,7 +1424,6 @@ class TestSyncRemoteDataUpdate(SyncRemoteDataTestBase):

def setUp(self):
super().setUp()

# Set up target category and project
self.category_obj = self.make_project(
title='NewCategoryTitle',
Expand Down Expand Up @@ -1556,7 +1558,7 @@ def test_update(self):
remote_data['peer_sites'][PEER_SITE_UUID][
'user_display'
] = NEW_PEER_USER_DISPLAY
original_data = deepcopy(remote_data)
og_data = deepcopy(remote_data)
# Change projectroles app settings
remote_data['app_settings'][SET_IP_RESTRICT_UUID]['value'] = True
remote_data['app_settings'][SET_IP_ALLOWLIST_UUID]['value_json'] = [
Expand Down Expand Up @@ -1696,49 +1698,43 @@ def test_update(self):
peer_project_dict.pop('date_access')
self.assertEqual(peer_project_dict, expected)

# TODO: Simplify
app_setting_ip_restrict_obj = AppSetting.objects.get(
sodar_uuid=SET_IP_RESTRICT_UUID,
)
app_setting_ip_allowlist_obj = AppSetting.objects.get(
sodar_uuid=SET_IP_ALLOWLIST_UUID,
)
expected_ip_restrict = {
# Assert app settings
expected = {
'name': 'ip_restrict',
'type': 'BOOLEAN',
'value': '1',
'value_json': {},
'sodar_uuid': uuid.UUID(SET_IP_RESTRICT_UUID),
'project': self.project_obj.id,
'app_plugin': None,
'user': None,
'user_modifiable': True,
}
expected_ip_allowlist = {
self.assert_app_setting(SET_IP_RESTRICT_UUID, expected)
expected = {
'name': 'ip_allowlist',
'type': 'JSON',
'value': '',
'value_json': ['192.168.1.1'],
'sodar_uuid': uuid.UUID(SET_IP_ALLOWLIST_UUID),
'project': self.project_obj.id,
'app_plugin': None,
'user': None,
'user_modifiable': True,
}
app_setting_ip_restrict_dict = model_to_dict(
app_setting_ip_restrict_obj
)
app_setting_ip_allowlist_dict = model_to_dict(
app_setting_ip_allowlist_obj
)
app_setting_ip_restrict_dict.pop('id')
app_setting_ip_allowlist_dict.pop('id')

self.assertEqual(app_setting_ip_allowlist_dict, expected_ip_allowlist)
self.assertEqual(app_setting_ip_restrict_dict, expected_ip_restrict)
self.assert_app_setting(SET_IP_ALLOWLIST_UUID, expected)
expected = {
'name': 'project_star',
'type': 'BOOLEAN',
'value': '1',
'value_json': {},
'app_plugin': None,
'project': self.project_obj.id,
'user': self.user_target.id,
'user_modifiable': True,
}
self.assert_app_setting(SET_STAR_UUID, expected)

# Assert update_data changes
expected = original_data
expected = og_data
expected['users'][SOURCE_USER_UUID]['status'] = 'updated'
expected['users'][new_user_uuid]['status'] = 'created'
expected['projects'][SOURCE_CATEGORY_UUID]['status'] = 'updated'
Expand Down Expand Up @@ -1785,7 +1781,7 @@ def test_update_inherited(self):
self.role_contributor,
)

# TODO: Update/fix
# TODO: Update once local settings updating is fixed
def test_update_app_setting_local(self):
"""Test update with local app settings"""
remote_data = self.default_data
Expand All @@ -1796,51 +1792,45 @@ def test_update_app_setting_local(self):
remote_data['app_settings'][SET_IP_ALLOWLIST_UUID]['value_json'] = [
'192.168.1.1'
]
original_data = deepcopy(remote_data)
og_data = deepcopy(remote_data)
self.remote_api.sync_remote_data(self.source_site, remote_data)

# TODO: Simplify
app_setting_ip_restrict_obj = AppSetting.objects.get(
sodar_uuid=SET_IP_RESTRICT_UUID,
)
app_setting_ip_allowlist_obj = AppSetting.objects.get(
sodar_uuid=SET_IP_ALLOWLIST_UUID,
)
expected_ip_restrict = {
expected = {
'name': 'ip_restrict',
'type': 'BOOLEAN',
'value': '0',
'value_json': {},
'sodar_uuid': uuid.UUID(SET_IP_RESTRICT_UUID),
'project': self.project_obj.id,
'app_plugin': None,
'user': None,
'user_modifiable': True,
}
expected_ip_allowlist = {
self.assert_app_setting(SET_IP_RESTRICT_UUID, expected)
expected = {
'name': 'ip_allowlist',
'type': 'JSON',
'value': None,
'value_json': [],
'sodar_uuid': uuid.UUID(SET_IP_ALLOWLIST_UUID),
'project': self.project_obj.id,
'app_plugin': None,
'user': None,
'user_modifiable': True,
}
app_setting_ip_restrict_dict = model_to_dict(
app_setting_ip_restrict_obj
)
app_setting_ip_allowlist_dict = model_to_dict(
app_setting_ip_allowlist_obj
)
app_setting_ip_restrict_dict.pop('id')
app_setting_ip_allowlist_dict.pop('id')
self.assertEqual(app_setting_ip_allowlist_dict, expected_ip_allowlist)
self.assertEqual(app_setting_ip_restrict_dict, expected_ip_restrict)
self.assert_app_setting(SET_IP_ALLOWLIST_UUID, expected)
expected = {
'name': 'project_star',
'type': 'BOOLEAN',
'value': '1',
'value_json': {},
'app_plugin': None,
'project': self.project_obj.id,
'user': self.user_target.id,
'user_modifiable': True,
}
self.assert_app_setting(SET_STAR_UUID, expected)

# Assert update_data changes
expected = original_data
expected = og_data
expected['users'][SOURCE_USER_UUID]['status'] = 'updated'
expected['projects'][SOURCE_CATEGORY_UUID]['status'] = 'updated'
expected['projects'][SOURCE_PROJECT_UUID]['status'] = 'updated'
Expand Down Expand Up @@ -1933,7 +1923,7 @@ def test_delete_role(self):
self.assertEqual(RemoteSite.objects.all().count(), 2)

remote_data = self.default_data
original_data = deepcopy(remote_data)
og_data = deepcopy(remote_data)
self.remote_api.sync_remote_data(self.source_site, remote_data)

self.assertEqual(Project.objects.all().count(), 2)
Expand All @@ -1947,7 +1937,7 @@ def test_delete_role(self):
role__name=PROJECT_ROLE_CONTRIBUTOR,
)

expected = original_data
expected = og_data
expected['projects'][SOURCE_PROJECT_UUID]['roles'][new_role_uuid] = {
'user': new_user_username,
'role': PROJECT_ROLE_CONTRIBUTOR,
Expand All @@ -1967,7 +1957,7 @@ def test_update_no_changes(self):
self.assertEqual(RemoteSite.objects.all().count(), 2)

remote_data = self.default_data
original_data = deepcopy(remote_data)
og_data = deepcopy(remote_data)
self.remote_api.sync_remote_data(self.source_site, remote_data)

self.assertEqual(Project.objects.all().count(), 2)
Expand Down Expand Up @@ -2089,13 +2079,7 @@ def test_update_no_changes(self):

# Assert no changes between update_data and remote_data
# Except global app settings, they are always updated.
original_data['app_settings'][SET_IP_RESTRICT_UUID][
'status'
] = 'updated'
original_data['app_settings'][SET_IP_ALLOWLIST_UUID][
'status'
] = 'updated'
original_data['app_settings'][SET_STAR_UUID]['status'] = 'updated'
self.assertEqual(original_data, remote_data)

# TODO: Test syncing of PROJECT_USER settings with differing user UUIDs
og_data['app_settings'][SET_IP_RESTRICT_UUID]['status'] = 'updated'
og_data['app_settings'][SET_IP_ALLOWLIST_UUID]['status'] = 'updated'
og_data['app_settings'][SET_STAR_UUID]['status'] = 'updated'
self.assertEqual(og_data, remote_data)

0 comments on commit ac7cec3

Please sign in to comment.