Skip to content

Commit

Permalink
refactor tests
Browse files Browse the repository at this point in the history
  • Loading branch information
claire-peters committed Jul 8, 2023
1 parent 43fe2dd commit 3d9e86b
Show file tree
Hide file tree
Showing 5 changed files with 141 additions and 170 deletions.
19 changes: 5 additions & 14 deletions coldfront/core/allocation/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,10 @@
AllocationChangeRequest,
)
from coldfront.core.test_helpers.factories import (
fake,
setup_models,
ResourceFactory,
UserFactory,
ProjectFactory,
ResourceFactory,
AllocationFactory,
AllocationChangeRequestFactory,
)
Expand Down Expand Up @@ -72,27 +71,20 @@ def setUpTestData(cls):
"""Set up users and project for testing"""
super(AllocationListViewTest, cls).setUpTestData()
cls.additional_allocations = [
AllocationFactory(
project=ProjectFactory(
title=fake.unique.project_title(),
pi=UserFactory(username=fake.unique.username()),
)
)
for i in list(range(100))
AllocationFactory() for i in list(range(50))
]
for allocation in cls.additional_allocations:
allocation.resources.add(ResourceFactory(name='holylfs09/tier1', id=2))
cls.nonproj_nonallocation_user = UserFactory(
username='rdrake', is_staff=False, is_superuser=False
)
cls.nonproj_nonallocation_user = UserFactory(username='rdrake')

def test_allocation_list_access_admin(self):
"""Confirm that AllocationList access control works for admin"""
self.allocation_access_tstbase('/allocation/')

# confirm that show_all_allocations=on enables admin to view all allocations
response = self.client.get("/allocation/?show_all_allocations=on")
self.assertEqual(len(response.context['item_list']), 101)

self.assertEqual(len(response.context['item_list']), Allocation.objects.all().count())

def test_allocation_list_access_pi(self):
"""Confirm that AllocationList access control works for pi
Expand Down Expand Up @@ -198,7 +190,6 @@ def setUp(self):

def test_allocationchangeview_access(self):
"""Test get request"""
kwargs = {'pk': 1}
self.allocation_access_tstbase(self.url)
utils.test_user_can_access(self, self.pi_user, self.url) # Manager can access
utils.test_user_cannot_access(self, self.proj_allocation_user, self.url) # user can't access
Expand Down
69 changes: 26 additions & 43 deletions coldfront/core/project/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,16 @@

from coldfront.core.test_helpers import utils
from coldfront.core.test_helpers.factories import (
setup_models,
UserFactory,
ProjectFactory,
ProjectUserFactory,
PAttributeTypeFactory,
ProjectAttributeFactory,
ProjectStatusChoiceFactory,
setup_models,
ProjectAttributeTypeFactory,
ProjectUserRoleChoiceFactory,
fake
)
from coldfront.core.project.models import ProjectUserStatusChoice
from coldfront.core.project.models import ProjectUserStatusChoice, Project

logging.disable(logging.CRITICAL)

Expand All @@ -37,8 +35,6 @@ def setUpTestData(cls):
cls.nonproject_user = cls.nonproj_allocation_user
# add pi_user and project_user to project_user

cls.normal_projuser = ProjectUserFactory(project=cls.project, user=cls.project_user)

attributetype = PAttributeTypeFactory(name='string')
cls.projectattributetype = ProjectAttributeTypeFactory(attribute_type=attributetype)# ProjectAttributeType.objects.get(pk=1)

Expand Down Expand Up @@ -90,13 +86,6 @@ def setUpTestData(cls):
cls.projectattribute = ProjectAttributeFactory(value=36238,
proj_attr_type=cls.projectattributetype, project=cls.project)
cls.url = f'/project/{cls.project.pk}/'
cls.no_allocation_project = ProjectFactory(title=fake.unique.project_title(),
pi=UserFactory(username=fake.unique.user_name()))

def test_projectdetail_render(self):
# test rendering for project with no allocation
no_allocation_proj_url = f'/project/{self.no_allocation_project.pk}/'
utils.test_user_can_access(self, self.admin_user, no_allocation_proj_url)

def test_projectdetail_access(self):
"""Test project detail page access"""
Expand All @@ -108,8 +97,6 @@ def test_projectdetail_access(self):
# user not belonging to project cannot access
utils.test_user_cannot_access(self, self.nonproject_user, self.url)



def test_projectdetail_permissions(self):
"""Test project detail page access permissions"""
# admin has is_allowed_to_update_project set to True
Expand Down Expand Up @@ -219,12 +206,14 @@ def test_project_attribute_create_post_required_values(self):
self.client.force_login(self.admin_user,
backend='django.contrib.auth.backends.ModelBackend')
# missing project
response = self.client.post(self.url, data={'proj_attr_type': self.projectattributetype.pk,
'value': 'test_value'})
response = self.client.post(self.url, data={
'proj_attr_type': self.projectattributetype.pk, 'value': 'test_value'
})
self.assertFormError(response, 'form', 'project', 'This field is required.')
# missing value
response = self.client.post(self.url, data={'proj_attr_type': self.projectattributetype.pk,
'project': self.project.pk})
response = self.client.post(self.url, data={
'proj_attr_type': self.projectattributetype.pk, 'project': self.project.pk
})
self.assertFormError(response, 'form', 'value', 'This field is required.')


Expand All @@ -234,9 +223,11 @@ def test_project_attribute_create_value_type_match(self):
self.client.force_login(self.admin_user,
backend='django.contrib.auth.backends.ModelBackend')
# test that value must be numeric if proj_attr_type is string
response = self.client.post(self.url, data={'proj_attr_type': self.int_projectattributetype.pk,
'value': True,
'project': self.project.pk})
response = self.client.post(self.url, data={
'proj_attr_type': self.int_projectattributetype.pk,
'value': True,
'project': self.project.pk
})
self.assertFormError(response, 'form', '', 'Invalid Value True. Value must be an int.')


Expand All @@ -247,7 +238,9 @@ class ProjectAttributeUpdateTest(ProjectViewTestBase):
def setUpTestData(cls):
"""Set up users and project for testing"""
super(ProjectAttributeUpdateTest, cls).setUpTestData()
cls.projectattribute = ProjectAttributeFactory(value=36238, proj_attr_type=cls.projectattributetype, project=cls.project)
cls.projectattribute = ProjectAttributeFactory(
value=36238, proj_attr_type=cls.projectattributetype, project=cls.project
)
cls.url = f'/project/{cls.project.pk}/project-attribute-update/{cls.projectattribute.pk}'


Expand Down Expand Up @@ -292,11 +285,7 @@ def setUpTestData(cls):
"""Set up users and project for testing"""
super(ProjectListViewTest, cls).setUpTestData()
# add 100 projects to test pagination, permissions, search functionality
cls.additional_projects = [
ProjectFactory(title=fake.unique.project_title(),
pi=UserFactory(username=fake.unique.user_name()))
for i in list(range(100))
]
cls.additional_projects = [ProjectFactory() for i in list(range(100))]
cls.url = '/project/'

### ProjectListView access tests ###
Expand All @@ -316,31 +305,27 @@ def test_project_list_access(self):
def test_project_list_display_members(self):
"""Test that project list displays only projects that user is an active member of."""
# deactivated projectuser won't see project on their page
self.normal_projuser.status, _ = ProjectUserStatusChoice.objects.get_or_create(name='Removed')
self.normal_projuser.save()
self.client.force_login(self.normal_projuser.user, backend='django.contrib.auth.backends.ModelBackend')
response = self.client.get(self.url)
self.npu.status, _ = ProjectUserStatusChoice.objects.get_or_create(name='Removed')
self.npu.save()
response = utils.login_and_get_page(self.client, self.normal_projuser, self.url)
self.assertEqual(len(response.context['object_list']), 0)

def test_project_list_displayall_permission_admin(self):
"""Test that the projectlist displayall option displays all projects to admin"""
url = self.url + '?show_all_projects=on'
self.client.force_login(self.admin_user, backend='django.contrib.auth.backends.ModelBackend')
response = self.client.get(url)
self.assertGreaterEqual(101, len(response.context['object_list']))
response = utils.login_and_get_page(self.client, self.admin_user, url)
self.assertEqual(len(response.context['object_list']), Project.objects.all().count())

def test_project_list_displayall_permission_pi(self):
"""Test that the projectlist displayall option displays only the pi's projects to the pi"""
url = self.url + '?show_all_projects=on'
self.client.force_login(self.pi_user, backend='django.contrib.auth.backends.ModelBackend')
response = self.client.get(url)
response = utils.login_and_get_page(self.client, self.pi_user, url)
self.assertEqual(len(response.context['object_list']), 1)

def test_project_list_displayall_permission_project_user(self):
"""Test that the projectlist displayall option displays only the project user's projects to the project user"""
url = self.url + '?show_all_projects=on'
self.client.force_login(self.project_user, backend='django.contrib.auth.backends.ModelBackend')
response = self.client.get(url)
response = utils.login_and_get_page(self.client, self.project_user, url)
self.assertEqual(len(response.context['object_list']), 1)


Expand All @@ -349,18 +334,16 @@ def test_project_list_displayall_permission_project_user(self):
def test_project_list_search(self):
"""Test that project list search works."""
url_base = self.url + '?show_all_projects=on'
self.client.force_login(self.admin_user, backend='django.contrib.auth.backends.ModelBackend')
# search by project project_title
url = url_base + '&title=' + self.project.title
response = self.client.get(url)
response = utils.login_and_get_page(self.client, self.admin_user, url)
self.assertEqual(len(response.context['object_list']), 1)


def test_project_list_search_pagination(self):
"""confirm that navigation to next page of search works as expected"""
url = self.url + '?show_all_projects=on'
self.client.force_login(self.admin_user, backend='django.contrib.auth.backends.ModelBackend')
response = self.client.get(url)
response = utils.login_and_get_page(self.client, self.admin_user, url)



Expand Down
Loading

0 comments on commit 3d9e86b

Please sign in to comment.