Skip to content

Commit

Permalink
add tests, fix issues, refactor (#817)
Browse files Browse the repository at this point in the history
  • Loading branch information
mikkonie committed Jun 13, 2024
1 parent 274988b commit ab208cf
Show file tree
Hide file tree
Showing 4 changed files with 215 additions and 151 deletions.
13 changes: 6 additions & 7 deletions projectroles/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -361,11 +361,9 @@ def _get_parent_choices(cls, instance, user):
ret += [(c.sodar_uuid, c.full_title) for c in categories]
return sorted(ret, key=lambda x: x[1])

def _init_remote_sites(self, project):
def _init_remote_sites(self):
"""
Initialize remote site fields in the form.
:param project: Project object or None
"""
p_display = get_display_name(PROJECT_TYPE_PROJECT)
for site in RemoteSite.objects.filter(
Expand All @@ -377,9 +375,9 @@ def _init_remote_sites(self, project):
p_display, site.name, site.url
)
f_initial = False
if project:
if self.instance.pk:
rp = RemoteProject.objects.filter(
site=site, project=project
site=site, project=self.instance
).first()
# NOTE: Only "read roles" is supported at the moment
f_initial = rp and rp.level == REMOTE_LEVEL_READ_ROLES
Expand Down Expand Up @@ -641,9 +639,10 @@ def __init__(self, project=None, current_user=None, *args, **kwargs):
parent_project = Project.objects.filter(sodar_uuid=project).first()
# Add remote site fields if on source site
if settings.PROJECTROLES_SITE_MODE == SITE_MODE_SOURCE and (
parent_project or project and project.type == PROJECT_TYPE_PROJECT
parent_project
or (self.instance.pk and self.instance.type == PROJECT_TYPE_PROJECT)
):
self._init_remote_sites(project)
self._init_remote_sites()
# Add settings fields
self._init_app_settings()

Expand Down
2 changes: 2 additions & 0 deletions projectroles/tests/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,7 @@ def make_site(
mode=SODAR_CONSTANTS['SITE_MODE_TARGET'],
description='',
secret=build_secret(),
sodar_uuid=uuid.uuid4(),
):
"""Make and save a RemoteSite"""
values = {
Expand All @@ -219,6 +220,7 @@ def make_site(
'secret': secret,
'user_display': user_display,
'owner_modifiable': owner_modifiable,
'sodar_uuid': sodar_uuid,
}
site = RemoteSite(**values)
site.save()
Expand Down
35 changes: 33 additions & 2 deletions projectroles/tests/test_ui.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import socket
import time
import uuid

from urllib.parse import urlencode

Expand Down Expand Up @@ -74,6 +75,8 @@
'div_id_settings.example_project_app.project_category_bool_setting'
)
PUBLIC_ACCESS_ID = 'id_public_guest_access'
REMOTE_SITE_UUID = uuid.uuid4()
REMOTE_SITE_ID = 'id_remote_site.{}'.format(REMOTE_SITE_UUID)


class LiveUserMixin:
Expand Down Expand Up @@ -1600,11 +1603,20 @@ def test_archive_visibility_archived(self):
)


class TestProjectCreateView(UITestBase):
class TestProjectCreateView(RemoteSiteMixin, UITestBase):
"""Tests for ProjectCreateView UI"""

def setUp(self):
super().setUp()
self.remote_site = self.make_site(
name=REMOTE_SITE_NAME,
url=REMOTE_SITE_URL,
mode=SITE_MODE_TARGET,
description='',
secret=REMOTE_SITE_SECRET,
user_display=True,
sodar_uuid=REMOTE_SITE_UUID,
)
self.url = reverse(
'projectroles:create', kwargs={'project': self.category.sodar_uuid}
)
Expand Down Expand Up @@ -1638,13 +1650,16 @@ def test_fields_top(self):
"""Test rendering of dynamic fields for top level creation view"""
self.login_and_redirect(self.superuser, self.url_top)
self.assert_displayed(By.ID, PUBLIC_ACCESS_ID, False)
with self.assertRaises(NoSuchElementException):
self.selenium.find_element(By.ID, REMOTE_SITE_ID)
self.assert_displayed(By.ID, PROJECT_SETTING_ID, False)
self.assert_displayed(By.ID, CATEGORY_SETTING_ID, True)

def test_fields_project(self):
"""Test rendering of dynamic fields for project creation"""
self.login_and_redirect(self.superuser, self.url)
self.assert_displayed(By.ID, PUBLIC_ACCESS_ID, False)
self.assert_displayed(By.ID, REMOTE_SITE_ID, False)
self.assert_displayed(By.ID, PROJECT_SETTING_ID, False)
self.assert_displayed(By.ID, CATEGORY_SETTING_ID, False)
select = Select(
Expand All @@ -1655,13 +1670,15 @@ def test_fields_project(self):
WebDriverWait(self.selenium, 10).until(
ec.visibility_of_element_located((By.ID, PUBLIC_ACCESS_ID))
)
self.assert_displayed(By.ID, REMOTE_SITE_ID, True)
self.assert_displayed(By.ID, PROJECT_SETTING_ID, True)
self.assert_displayed(By.ID, CATEGORY_SETTING_ID, False)

def test_fields_category(self):
"""Test rendering of dynamic fields for category creation"""
self.login_and_redirect(self.superuser, self.url)
self.assert_displayed(By.ID, PUBLIC_ACCESS_ID, False)
self.assert_displayed(By.ID, REMOTE_SITE_ID, False)
self.assert_displayed(By.ID, PROJECT_SETTING_ID, False)
self.assert_displayed(By.ID, CATEGORY_SETTING_ID, False)
select = Select(
Expand All @@ -1673,7 +1690,9 @@ def test_fields_category(self):
ec.visibility_of_element_located((By.ID, CATEGORY_SETTING_ID))
)
self.assert_displayed(By.ID, PUBLIC_ACCESS_ID, False)
self.assert_displayed(By.ID, REMOTE_SITE_ID, False)
self.assert_displayed(By.ID, PROJECT_SETTING_ID, False)
self.assert_displayed(By.ID, CATEGORY_SETTING_ID, True)

def test_settings_label_icon(self):
"""Test rendering of app settings icon for project creation"""
Expand Down Expand Up @@ -1713,11 +1732,20 @@ def test_submit_button(self):
break


class TestProjectUpdateView(UITestBase):
class TestProjectUpdateView(RemoteSiteMixin, UITestBase):
"""Tests for ProjectUpdateView UI"""

def setUp(self):
super().setUp()
self.remote_site = self.make_site(
name=REMOTE_SITE_NAME,
url=REMOTE_SITE_URL,
mode=SITE_MODE_TARGET,
description='',
secret=REMOTE_SITE_SECRET,
user_display=True,
sodar_uuid=REMOTE_SITE_UUID,
)
self.url = reverse(
'projectroles:update', kwargs={'project': self.project.sodar_uuid}
)
Expand Down Expand Up @@ -1746,13 +1774,16 @@ def test_fields_project(self):
"""Test field visibility for project update"""
self.login_and_redirect(self.superuser, self.url)
self.assert_displayed(By.ID, PUBLIC_ACCESS_ID, True)
self.assert_displayed(By.ID, REMOTE_SITE_ID, True)
self.assert_displayed(By.ID, PROJECT_SETTING_ID, True)
self.assert_displayed(By.ID, CATEGORY_SETTING_ID, False)

def test_fields_category(self):
"""Test field visibility for category update"""
self.login_and_redirect(self.superuser, self.url_cat)
self.assert_displayed(By.ID, PUBLIC_ACCESS_ID, False)
with self.assertRaises(NoSuchElementException):
self.selenium.find_element(By.ID, REMOTE_SITE_ID)
self.assert_displayed(By.ID, PROJECT_SETTING_ID, False)
self.assert_displayed(By.ID, CATEGORY_SETTING_ID, True)

Expand Down
Loading

0 comments on commit ab208cf

Please sign in to comment.