Skip to content
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

Testing improvements #332

Merged
merged 5 commits into from
Aug 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 18 additions & 15 deletions coldfront/core/allocation/tests/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ def test_allocation_list_access_pi(self):
# confirm that show_all_allocations=on enables admin to view all allocations
self.client.force_login(self.pi_user, backend=BACKEND)
response = self.client.get("/allocation/?show_all_allocations=on")
self.assertEqual(len(response.context['item_list']), 1)
self.assertEqual(len(response.context['item_list']), 2)

def test_allocation_list_access_manager(self):
"""Confirm that AllocationList access control works for managers
Expand All @@ -102,11 +102,11 @@ def test_allocation_list_access_manager(self):
# confirm that show_all_allocations=on enables admin to view all allocations
self.client.force_login(self.proj_datamanager, backend=BACKEND)
response = self.client.get("/allocation/?show_all_allocations=on")
self.assertEqual(len(response.context['item_list']), 1)
self.assertEqual(len(response.context['item_list']), 2)
# confirm that show_all_allocations=on enables admin to view all allocations
self.client.force_login(self.proj_accessmanager, backend=BACKEND)
response = self.client.get("/allocation/?show_all_allocations=on")
self.assertEqual(len(response.context['item_list']), 1)
self.assertEqual(len(response.context['item_list']), 2)

def test_allocation_list_access_user(self):
"""Confirm that AllocationList access control works for non-pi users
Expand All @@ -117,21 +117,21 @@ def test_allocation_list_access_user(self):
# contains only the user's allocations
self.client.force_login(self.proj_allocation_user, backend=BACKEND)
response = self.client.get("/allocation/")
self.assertEqual(len(response.context['item_list']), 1)
self.assertEqual(len(response.context['item_list']), 2)
response = self.client.get("/allocation/?show_all_allocations=on")
self.assertEqual(len(response.context['item_list']), 1)
self.assertEqual(len(response.context['item_list']), 2)

# allocation user not belonging to project can see allocation
self.client.force_login(self.nonproj_allocation_user, backend=BACKEND)
response = self.client.get("/allocation/")
self.assertEqual(len(response.context['item_list']), 1)
self.assertEqual(len(response.context['item_list']), 2)
response = self.client.get("/allocation/?show_all_allocations=on")
self.assertEqual(len(response.context['item_list']), 1)
self.assertEqual(len(response.context['item_list']), 2)

# nonallocation user belonging to project can see allocation
self.client.force_login(self.proj_nonallocation_user, backend=BACKEND)
response = self.client.get("/allocation/?show_all_allocations=on")
self.assertEqual(len(response.context['item_list']), 1)
self.assertEqual(len(response.context['item_list']), 2)

# nonallocation user not belonging to project can't see allocation
self.client.force_login(self.nonproj_nonallocation_user, backend=BACKEND)
Expand Down Expand Up @@ -280,8 +280,10 @@ def setUp(self):

def test_allocation_detail_access(self):
self.allocation_access_tstbase(self.url)
utils.test_user_can_access(self, self.pi_user, self.url) # PI can access
# pi, project nonallocation user, nonproj_allocation_user can access
utils.test_user_can_access(self, self.pi_user, self.url)
utils.test_user_can_access(self, self.proj_nonallocation_user, self.url)
utils.test_user_can_access(self, self.nonproj_allocation_user, self.url)
# check access for allocation user with "Removed" status

def test_allocation_detail_template_value_render(self):
Expand Down Expand Up @@ -328,18 +330,19 @@ def test_allocationattribute_button_visibility(self):
def test_allocationuser_button_visibility(self):
"""Test visibility of "Add/Remove Users" buttons for different user types"""
# we're removing these buttons for everybody, to avoid confusion re: procedure for user addition/removal
# admin can't see add/remove users buttons
utils.page_does_not_contain_for_user(
self, self.admin_user, self.url, 'Add Users'
)
utils.page_does_not_contain_for_user(
self, self.admin_user, self.url, 'Remove Users'
)
# pi
# pi can't see add/remove users buttons
utils.page_does_not_contain_for_user(self, self.pi_user, self.url, 'Add Users')
utils.page_does_not_contain_for_user(
self, self.pi_user, self.url, 'Remove Users'
)
# allocation user
# allocation user can't see add/remove users buttons
utils.page_does_not_contain_for_user(
self, self.proj_allocation_user, self.url, 'Add Users'
)
Expand Down Expand Up @@ -373,20 +376,20 @@ def test_allocationcreateview_access(self):

def test_allocationcreateview_post(self):
"""Test POST to the AllocationCreateView"""
self.assertEqual(len(self.project.allocation_set.all()), 1)
self.assertEqual(len(self.project.allocation_set.all()), 2)
response = self.client.post(self.url, data=self.post_data, follow=True)
self.assertContains(response, "Allocation requested.")
self.assertEqual(len(self.project.allocation_set.all()), 2)
self.assertEqual(len(self.project.allocation_set.all()), 3)

def test_allocationcreateview_post_zeroquantity(self):
"""Test POST to the AllocationCreateView with default post_data:
No expense_code, dua, heavy_io, mounted, external_sharing, high_security
"""
self.post_data['quantity'] = '0'
self.assertEqual(len(self.project.allocation_set.all()), 1)
self.assertEqual(len(self.project.allocation_set.all()), 2)
response = self.client.post(self.url, data=self.post_data, follow=True)
self.assertContains(response, "Allocation requested.")
self.assertEqual(len(self.project.allocation_set.all()), 2)
self.assertEqual(len(self.project.allocation_set.all()), 3)

def test_allocationcreateview_post_offerlettercode_valid(self):
"""ensure 33-digit codes go through and get formatted"""
Expand Down
43 changes: 36 additions & 7 deletions coldfront/core/portal/tests.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from django.test import TestCase, tag
from coldfront.core.test_helpers import utils
from coldfront.core.test_helpers.factories import setup_models
from coldfront.core.allocation.models import AllocationChangeRequest, AllocationChangeStatusChoice

Expand Down Expand Up @@ -31,17 +32,15 @@ class HomePageTest(PortalViewTest):
def test_pi_home_page(self):
"""check that the pi home page displays properly with the existing database
"""
self.client.force_login(self.pi_user)
response = self.client.get('')
response = utils.login_and_get_page(self.client, self.pi_user, '')
self.assertEqual(response.status_code, 200) # page renders
self.assertContains(response, 'Projects') # page contains the title
self.assertEqual(response.context['project_list'].count(), 1) # page contains the correct number of Projects
self.assertEqual(response.context['allocation_list'].count(), 1) # page contains the correct number of Allocations
self.assertEqual(response.context['allocation_list'].count(), 2) # page contains the correct number of Allocations

def test_home_page_requests_view(self):
# when PI has no requests, the requests table is not visible
self.client.force_login(self.pi_user)
response = self.client.get('')
response = utils.login_and_get_page(self.client, self.pi_user, '')
self.assertNotContains(response, 'Requests')
# When PI has requests, the requests table is visible
AllocationChangeRequest.objects.create(
Expand All @@ -51,7 +50,37 @@ def test_home_page_requests_view(self):
response = self.client.get('')
self.assertContains(response, 'Requests')
# normal user sees no requests even when one exists
response = utils.login_and_get_page(self.client, self.proj_allocation_user, '')
self.assertNotContains(response, 'Requests')

def test_home_page_allocations_display(self):
"""check that project allocations display properly on the home page
"""
# PI sees allocation
response = utils.login_and_get_page(self.client, self.pi_user, '')
self.assertEqual(response.context['allocation_list'].count(), 2)
# Storage Manager sees allocation
response = utils.login_and_get_page(self.client, self.proj_datamanager, '')
self.assertEqual(response.context['allocation_list'].count(), 2)
# project user not belonging to allocation cannot see allocation
response = utils.login_and_get_page(self.client, self.proj_nonallocation_user, '')
self.assertEqual(response.context['allocation_list'].count(), 0)
# allocation user not belonging to project cannot see allocation
response = utils.login_and_get_page(self.client, self.nonproj_allocation_user, '')
self.assertEqual(response.context['allocation_list'].count(), 0)

def home_create_project_button_visible(self):
"""check that the create project button is visible on the home page
def test_home_page_projects_display(self):
"""check that projects display properly on the home page
"""
# PI sees project
response = utils.login_and_get_page(self.client, self.pi_user, '')
self.assertEqual(response.context['project_list'].count(), 1)
# Storage Manager sees project
response = utils.login_and_get_page(self.client, self.proj_datamanager, '')
self.assertEqual(response.context['project_list'].count(), 1)
# project user can see project
response = utils.login_and_get_page(self.client, self.proj_allocation_user, '')
self.assertEqual(response.context['project_list'].count(), 1)
# allocation user not belonging to project cannot see project
response = utils.login_and_get_page(self.client, self.nonproj_allocation_user, '')
self.assertEqual(response.context['project_list'].count(), 0)
Original file line number Diff line number Diff line change
Expand Up @@ -323,7 +323,7 @@ <h3 class="d-inline" id="users"><i class="fas fa-users" aria-hidden="true"></i>
<th scope="col">Name</th>
<th scope="col">Email</th>
<th scope="col" class="text-nowrap">Role
<a class="info-button" title="User Role" data-toggle="popover" data-trigger="click" data-content="Manager role grants user access to add/remove users and add/remove/edit allocations for the project.">
<a class="info-button" title="User Role" data-toggle="popover" data-trigger="click" data-content="For details about User Roles, visit FASRC's <a href='https://docs.rc.fas.harvard.edu/kb/coldfront-allocation-management/' title='ColdFront User Guide'>ColdFront User Guide.">
<i class="fas fa-info-circle" aria-hidden="true"></i>
</a>
</th>
Expand Down
Empty file.
120 changes: 120 additions & 0 deletions coldfront/core/project/tests/test_view_projectdetail.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
from coldfront.core.project.tests.test_views import ProjectViewTestBase
from coldfront.core.test_helpers import utils
from coldfront.core.test_helpers.factories import ProjectAttributeFactory


class ProjectDetailViewTest(ProjectViewTestBase):
"""tests for ProjectDetailView"""

@classmethod
def setUpTestData(cls):
"""Set up users and project for testing"""
super(ProjectDetailViewTest, cls).setUpTestData()
cls.projectattribute = ProjectAttributeFactory(value=36238,
proj_attr_type=cls.projectattributetype, project=cls.project)
cls.url = f'/project/{cls.project.pk}/'

### Page access and permissions tests ###
def test_projectdetail_access(self):
"""Test project detail page access.
pi and projectuser can access, nonproject user cannot access.
"""
# logged-out user gets redirected, admin can access create page
self.project_access_tstbase(self.url)
utils.test_user_can_access(self, self.pi_user, self.url)
utils.test_user_can_access(self, self.project_user, self.url)
utils.test_user_cannot_access(self, self.nonproject_user, self.url)

def test_projectdetail_permissions(self):
"""Test ProjectDetail page access permissions"""
# admin and pi have is_allowed_to_update_project set to True
response = utils.login_and_get_page(self.client, self.admin_user, self.url)
self.assertEqual(response.context['is_allowed_to_update_project'], True)

response = utils.login_and_get_page(self.client, self.pi_user, self.url)
self.assertEqual(response.context['is_allowed_to_update_project'], True)

# non-manager user has is_allowed_to_update_project set to False
response = utils.login_and_get_page(self.client, self.project_user, self.url)
self.assertEqual(response.context['is_allowed_to_update_project'], False)

### Permissions-related UI visibility tests ###
def test_projectdetail_request_allocation_button_visibility(self):
"""Test ProjectDetail request allocation button visibility to different projectuser roles"""
button_text = 'Request New Storage Allocation'
# admin, pi, data manager can see request allocation button
utils.page_contains_for_user(self, self.admin_user, self.url, button_text)
utils.page_contains_for_user(self, self.pi_user, self.url, button_text)
utils.page_contains_for_user(self, self.proj_datamanager, self.url, button_text)
# access manager, non-manager user cannot see request allocation button
utils.page_does_not_contain_for_user(self, self.proj_accessmanager, self.url, button_text)
utils.page_does_not_contain_for_user(self, self.project_user, self.url, button_text)

def test_projectdetail_adduser_button_visibility(self):
"""Test ProjectDetail add user button visibility to different projectuser roles"""
button_text = 'Add User'
# admin, pi, accessmanager can see add user button
utils.page_contains_for_user(self, self.admin_user, self.url, button_text)
utils.page_contains_for_user(self, self.pi_user, self.url, button_text)
utils.page_contains_for_user(self, self.proj_accessmanager, self.url, button_text)
# storage manager, non-manager user cannot see add user button
utils.page_does_not_contain_for_user(self, self.proj_datamanager, self.url, button_text)
utils.page_does_not_contain_for_user(self, self.project_user, self.url, button_text)

def test_projectdetail_edituser_button_visibility(self):
"""Test ProjectDetail edit button visibility to different projectuser roles"""
utils.page_contains_for_user(self, self.admin_user, self.url, 'fa-user-edit') # admin can see edit button
utils.page_contains_for_user(self, self.pi_user, self.url, 'fa-user-edit') # pi can see edit button
# access manager, data manager, non-manager user cannot see edit button
utils.page_does_not_contain_for_user(self, self.proj_accessmanager, self.url, 'fa-user-edit')
utils.page_does_not_contain_for_user(self, self.proj_datamanager, self.url, 'fa-user-edit')
utils.page_does_not_contain_for_user(self, self.project_user, self.url, 'fa-user-edit')

def test_projectdetail_addattribute_button_visibility(self):
"""Test ProjectDetail add attribute button visibility to different projectuser roles"""
search_text = 'Add Attribute'
utils.page_contains_for_user(self, self.admin_user, self.url, search_text) # admin can see add attribute button
# pi and non-manager cannot see add attribute button
utils.page_does_not_contain_for_user(self, self.pi_user, self.url, search_text)
utils.page_does_not_contain_for_user(self, self.project_user, self.url, search_text)

def test_projectdetail_addnotification_button_visibility(self):
"""Test ProjectDetail add notification button visibility to different projectuser roles"""
search_text = 'Add Notification'
utils.page_contains_for_user(self, self.admin_user, self.url, search_text) # admin can see add notification button
# pi, access manager, data manager, project user cannot see add notification button
utils.page_does_not_contain_for_user(self, self.pi_user, self.url, search_text)
utils.page_does_not_contain_for_user(self, self.proj_accessmanager, self.url, search_text)
utils.page_does_not_contain_for_user(self, self.proj_datamanager, self.url, search_text)
utils.page_does_not_contain_for_user(self, self.project_user, self.url, search_text)

### Data display tests ###
def test_projectdetail_allocation_table(self):
"""Test ProjectDetail page storage allocation table"""
# pi can see allocation in Allocations table
soup = utils.login_and_get_soup(self.client, self.pi_user, self.url)
allocations_table = soup.find('table', {'id': 'invoice_table'})
self.assertIn("holylfs10/tier1", allocations_table.get_text())
# project user belonging to allocation can see allocation
soup = utils.login_and_get_soup(self.client, self.project_user, self.url)
allocations_table = soup.find('table', {'id': 'invoice_table'})
self.assertIn("holylfs10/tier1", allocations_table.get_text())
# project user not belonging to allocation can see allocation
soup = utils.login_and_get_soup(self.client, self.proj_nonallocation_user, self.url)
allocations_table = soup.find('table', {'id': 'invoice_table'})
self.assertIn("holylfs10/tier1", allocations_table.get_text())

def test_projectdetail_allocation_history_table(self):
"""Test ProjectDetail page storage allocation history table"""
# pi can see allocation in Allocations table
soup = utils.login_and_get_soup(self.client, self.pi_user, self.url)
allocations_table = soup.find('table', {'id': 'allocation_history'})
self.assertIn("holylfs10/tier1", allocations_table.get_text())
# project user belonging to allocation can see allocation
soup = utils.login_and_get_soup(self.client, self.project_user, self.url)
allocations_table = soup.find('table', {'id': 'allocation_history'})
self.assertIn("holylfs10/tier1", allocations_table.get_text())
# project user not belonging to allocation can see allocation
soup = utils.login_and_get_soup(self.client, self.proj_nonallocation_user, self.url)
allocations_table = soup.find('table', {'id': 'allocation_history'})
self.assertIn("holylfs10/tier1", allocations_table.get_text())
Loading
Loading