Skip to content
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
16 changes: 8 additions & 8 deletions coldfront/core/allocation/tests/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ def test_status_is_expired_and_no_end_date_has_validation_error(self):

def test_status_is_expired_and_end_date_not_past_has_validation_error(self):
"""Test that an allocation with status 'expired' and end date in the future raises a validation error."""
end_date_in_the_future: datetime.date = (timezone.now() + datetime.timedelta(days=1)).date()
end_date_in_the_future: datetime.date = datetime.date.today() + datetime.timedelta(days=1)
actual_allocation: Allocation = AllocationFactory.build(
status=self.expired_status, end_date=end_date_in_the_future, project=self.project
)
Expand All @@ -70,7 +70,7 @@ def test_status_is_expired_and_end_date_not_past_has_validation_error(self):

def test_status_is_expired_and_start_date_after_end_date_has_validation_error(self):
"""Test that an allocation with status 'expired' and start date after end date raises a validation error."""
end_date: datetime.date = (timezone.now() + datetime.timedelta(days=1)).date()
end_date: datetime.date = datetime.date.today() + datetime.timedelta(days=1)
start_date_after_end_date: datetime.date = end_date + datetime.timedelta(days=1)

actual_allocation: Allocation = AllocationFactory.build(
Expand Down Expand Up @@ -116,7 +116,7 @@ def test_status_is_active_and_no_end_date_has_validation_error(self):

def test_status_is_active_and_start_date_after_end_date_has_validation_error(self):
"""Test that an allocation with status 'active' and start date after end date raises a validation error."""
end_date: datetime.date = (timezone.now() + datetime.timedelta(days=1)).date()
end_date: datetime.date = datetime.date.today() + datetime.timedelta(days=1)
start_date_after_end_date: datetime.date = end_date + datetime.timedelta(days=1)

actual_allocation: Allocation = AllocationFactory.build(
Expand Down Expand Up @@ -237,32 +237,32 @@ class AllocationModelExpiresInTests(TestCase):

def test_end_date_is_today_returns_zero(self):
"""Test that the expires_in method returns 0 when the end date is today."""
allocation: Allocation = AllocationFactory(end_date=timezone.now().date())
allocation: Allocation = AllocationFactory(end_date=datetime.date.today())
self.assertEqual(allocation.expires_in, 0)

def test_end_date_tomorrow_returns_one(self):
"""Test that the expires_in method returns 1 when the end date is tomorrow."""
tomorrow: datetime.date = (timezone.now() + datetime.timedelta(days=1)).date()
tomorrow: datetime.date = datetime.date.today() + datetime.timedelta(days=1)
allocation: Allocation = AllocationFactory(end_date=tomorrow)
self.assertEqual(allocation.expires_in, 1)

def test_end_date_yesterday_returns_negative_one(self):
"""Test that the expires_in method returns -1 when the end date is yesterday."""
yesterday: datetime.date = (timezone.now() - datetime.timedelta(days=1)).date()
yesterday: datetime.date = datetime.date.today() - datetime.timedelta(days=1)
allocation: Allocation = AllocationFactory(end_date=yesterday)
self.assertEqual(allocation.expires_in, -1)

def test_end_date_one_week_ago_returns_negative_seven(self):
"""Test that the expires_in method returns -7 when the end date is one week ago."""
days_in_a_week: int = 7
one_week_ago: datetime.date = (timezone.now() - datetime.timedelta(days=days_in_a_week)).date()
one_week_ago: datetime.date = datetime.date.today() - datetime.timedelta(days=days_in_a_week)
allocation: Allocation = AllocationFactory(end_date=one_week_ago)
self.assertEqual(allocation.expires_in, -days_in_a_week)

def test_end_date_in_one_week_returns_seven(self):
"""Test that the expires_in method returns 7 when the end date is in one week."""
days_in_a_week: int = 7
one_week_from_now: datetime.date = (timezone.now() + datetime.timedelta(days=days_in_a_week)).date()
one_week_from_now: datetime.date = datetime.date.today() + datetime.timedelta(days=days_in_a_week)
allocation: Allocation = AllocationFactory(end_date=one_week_from_now)
self.assertEqual(allocation.expires_in, days_in_a_week)

Expand Down
6 changes: 3 additions & 3 deletions coldfront/core/allocation/tests/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ def setUp(self):
"attributeform-TOTAL_FORMS": "1",
"end_date_extension": 0,
}
self.url = "/allocation/1/change-request"
self.url = f"/allocation/{self.allocation.pk}/change-request"

def test_allocationchangeview_access(self):
"""Test get request"""
Expand All @@ -178,7 +178,7 @@ def test_allocationchangeview_post_extension(self):

self.post_data["end_date_extension"] = 90
self.assertEqual(len(AllocationChangeRequest.objects.all()), 0)
response = self.client.post("/allocation/1/change-request", data=self.post_data, follow=True)
response = self.client.post(self.url, data=self.post_data, follow=True)
self.assertEqual(response.status_code, 200)
self.assertContains(response, "Allocation change request successfully submitted.")
self.assertEqual(len(AllocationChangeRequest.objects.all()), 1)
Expand All @@ -188,7 +188,7 @@ def test_allocationchangeview_post_no_change(self):

self.assertEqual(len(AllocationChangeRequest.objects.all()), 0)

response = self.client.post("/allocation/1/change-request", data=self.post_data, follow=True)
response = self.client.post(self.url, data=self.post_data, follow=True)
self.assertEqual(response.status_code, 200)
self.assertContains(response, "You must request a change")
self.assertEqual(len(AllocationChangeRequest.objects.all()), 0)
Expand Down
2 changes: 2 additions & 0 deletions coldfront/core/project/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,12 @@
class ProjectSearchForm(forms.Form):
"""Search form for the Project list page."""

TITLE = "Title"
LAST_NAME = "Last Name"
USERNAME = "Username"
FIELD_OF_SCIENCE = "Field of Science"

title = forms.CharField(label=TITLE, max_length=255, required=False)
last_name = forms.CharField(label=LAST_NAME, max_length=100, required=False)
username = forms.CharField(label=USERNAME, max_length=100, required=False)
field_of_science = forms.CharField(label=FIELD_OF_SCIENCE, max_length=100, required=False)
Expand Down
66 changes: 31 additions & 35 deletions coldfront/core/project/tests/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,10 @@ def setUpTestData(cls):
cls.project = ProjectFactory(status=ProjectStatusChoiceFactory(name="Active"))

user_role = ProjectUserRoleChoiceFactory(name="User")
project_user = ProjectUserFactory(project=cls.project, role=user_role)
cls.project_user = project_user.user
cls.project_user = ProjectUserFactory(project=cls.project, role=user_role)

manager_role = ProjectUserRoleChoiceFactory(name="Manager")
pi_user = ProjectUserFactory(project=cls.project, role=manager_role, user=cls.project.pi)
cls.pi_user = pi_user.user
cls.pi_user = ProjectUserFactory(project=cls.project, role=manager_role, user=cls.project.pi)
cls.admin_user = UserFactory(is_staff=True, is_superuser=True)
cls.nonproject_user = UserFactory(is_staff=False, is_superuser=False)

Expand Down Expand Up @@ -69,8 +67,8 @@ def test_projectdetail_access(self):
# logged-out user gets redirected, admin can access create page
self.project_access_tstbase(self.url)
# pi and projectuser can access
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_can_access(self, self.pi_user.user, self.url)
utils.test_user_can_access(self, self.project_user.user, self.url)
# user not belonging to project cannot access
utils.test_user_cannot_access(self, self.nonproject_user, self.url)

Expand All @@ -80,10 +78,10 @@ def test_projectdetail_permissions(self):
response = utils.login_and_get_page(self.client, self.admin_user, self.url)
self.assertEqual(response.context["is_allowed_to_update_project"], True)
# pi has is_allowed_to_update_project set to True
response = utils.login_and_get_page(self.client, self.pi_user, self.url)
response = utils.login_and_get_page(self.client, self.pi_user.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)
response = utils.login_and_get_page(self.client, self.project_user.user, self.url)
self.assertEqual(response.context["is_allowed_to_update_project"], False)

def test_projectdetail_request_allocation_button_visibility(self):
Expand All @@ -92,27 +90,27 @@ def test_projectdetail_request_allocation_button_visibility(self):
# admin can see request allocation button
utils.page_contains_for_user(self, self.admin_user, self.url, button_text)
# pi can see request allocation button
utils.page_contains_for_user(self, self.pi_user, self.url, button_text)
utils.page_contains_for_user(self, self.pi_user.user, self.url, button_text)
# non-manager user cannot see request allocation button
utils.page_does_not_contain_for_user(self, self.project_user, self.url, button_text)
utils.page_does_not_contain_for_user(self, self.project_user.user, self.url, button_text)

def test_projectdetail_edituser_button_visibility(self):
"""Test visibility of projectdetail edit button across user levels"""
# admin can see edit button
utils.page_contains_for_user(self, self.admin_user, self.url, "fa-user-edit")
# pi can see edit button
utils.page_contains_for_user(self, self.pi_user, self.url, "fa-user-edit")
utils.page_contains_for_user(self, self.pi_user.user, self.url, "fa-user-edit")
# non-manager user cannot see edit button
utils.page_does_not_contain_for_user(self, self.project_user, self.url, "fa-user-edit")
utils.page_does_not_contain_for_user(self, self.project_user.user, self.url, "fa-user-edit")

def test_projectdetail_addnotification_button_visibility(self):
"""Test visibility of projectdetail add notification button across user levels"""
# admin can see add notification button
utils.page_contains_for_user(self, self.admin_user, self.url, "Add Notification")
# pi cannot see add notification button
utils.page_does_not_contain_for_user(self, self.pi_user, self.url, "Add Notification")
utils.page_does_not_contain_for_user(self, self.pi_user.user, self.url, "Add Notification")
# non-manager user cannot see add notification button
utils.page_does_not_contain_for_user(self, self.project_user, self.url, "Add Notification")
utils.page_does_not_contain_for_user(self, self.project_user.user, self.url, "Add Notification")


class ProjectCreateTest(ProjectViewTestBase):
Expand All @@ -129,8 +127,8 @@ def test_project_access(self):
# logged-out user gets redirected, admin can access create page
self.project_access_tstbase(self.url)
# pi, projectuser and nonproject user cannot access create page
utils.test_user_cannot_access(self, self.pi_user, self.url)
utils.test_user_cannot_access(self, self.project_user, self.url)
utils.test_user_cannot_access(self, self.pi_user.user, self.url)
utils.test_user_cannot_access(self, self.project_user.user, self.url)
utils.test_user_cannot_access(self, self.nonproject_user, self.url)


Expand All @@ -150,9 +148,9 @@ def test_project_access(self):
# logged-out user gets redirected, admin can access create page
self.project_access_tstbase(self.url)
# pi can access create page
utils.test_user_can_access(self, self.pi_user, self.url)
utils.test_user_can_access(self, self.pi_user.user, self.url)
# project user and nonproject user cannot access create page
utils.test_user_cannot_access(self, self.project_user, self.url)
utils.test_user_cannot_access(self, self.project_user.user, self.url)
utils.test_user_cannot_access(self, self.nonproject_user, self.url)

def test_project_attribute_create_post(self):
Expand Down Expand Up @@ -207,9 +205,9 @@ def setUpTestData(cls):
def test_project_attribute_update_access(self):
"""Test access to project attribute update 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.pi_user.user, self.url)
# project user, pi, and nonproject user cannot access update page
utils.test_user_cannot_access(self, self.project_user, self.url)
utils.test_user_cannot_access(self, self.project_user.user, self.url)
utils.test_user_cannot_access(self, self.nonproject_user, self.url)


Expand All @@ -230,9 +228,9 @@ def test_project_attribute_delete_access(self):
# logged-out user gets redirected, admin can access delete page
self.project_access_tstbase(self.url)
# pi can access delete page
utils.test_user_can_access(self, self.pi_user, self.url)
utils.test_user_can_access(self, self.pi_user.user, self.url)
# project user and nonproject user cannot access delete page
utils.test_user_cannot_access(self, self.project_user, self.url)
utils.test_user_cannot_access(self, self.project_user.user, self.url)
utils.test_user_cannot_access(self, self.nonproject_user, self.url)


Expand All @@ -245,7 +243,8 @@ def setUpTestData(cls):
super(ProjectListViewTest, cls).setUpTestData()
# add 100 projects to test pagination, permissions, search functionality
additional_projects = [ProjectFactory() for i in list(range(100))]
cls.additional_projects = [p for p in additional_projects if p.pi.last_name != cls.project.pi.last_name]
# cls.additional_projects = [p for p in additional_projects if p.pi.last_name != cls.project.pi.last_name]
cls.additional_projects = additional_projects
cls.url = "/project/"

### ProjectListView access tests ###
Expand All @@ -255,21 +254,21 @@ def test_project_list_access(self):
# logged-out user gets redirected, admin can access list page
self.project_access_tstbase(self.url)
# all other users can access list page
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_can_access(self, self.pi_user.user, self.url)
utils.test_user_can_access(self, self.project_user.user, self.url)
utils.test_user_can_access(self, self.nonproject_user, self.url)

### ProjectListView display tests ###

def test_project_list_display_members(self):
"""Project list displays only projects that user is an active member of"""
# deactivated projectuser won't see project on their page
response = utils.login_and_get_page(self.client, self.project_user, self.url)
response = utils.login_and_get_page(self.client, self.project_user.user, self.url)
self.assertEqual(len(response.context["object_list"]), 1)
proj_user = self.project.projectuser_set.get(user=self.project_user)
proj_user = self.project.projectuser_set.get(user=self.project_user.user)
proj_user.status, _ = ProjectUserStatusChoice.objects.get_or_create(name="Removed")
proj_user.save()
response = utils.login_and_get_page(self.client, self.project_user, self.url)
response = utils.login_and_get_page(self.client, self.project_user.user, self.url)
self.assertEqual(len(response.context["object_list"]), 0)

def test_project_list_displayall_permission_admin(self):
Expand All @@ -281,27 +280,24 @@ def test_project_list_displayall_permission_admin(self):
def test_project_list_displayall_permission_pi(self):
"""Projectlist displayall option displays only the pi's projects to the pi"""
url = self.url + "?show_all_projects=on"
response = utils.login_and_get_page(self.client, self.pi_user, url)
response = utils.login_and_get_page(self.client, self.pi_user.user, url)
self.assertEqual(len(response.context["object_list"]), 1)

def test_project_list_displayall_permission_project_user(self):
"""Projectlist displayall displays only projects projectuser belongs to"""
url = self.url + "?show_all_projects=on"
response = utils.login_and_get_page(self.client, self.project_user, url)
response = utils.login_and_get_page(self.client, self.project_user.user, url)
self.assertEqual(len(response.context["object_list"]), 1)

### ProjectListView search tests ###

def test_project_list_search(self):
"""Test that project list search works."""
url_base = self.url + "?show_all_projects=on"
url = (
f"{url_base}&last_name={self.project.pi.last_name}"
+ f"&field_of_science={self.project.field_of_science.description}"
)
url = f"{url_base}&title={self.project.title}"
# search by project project_title
response = utils.login_and_get_page(self.client, self.admin_user, url)
self.assertEqual(len(response.context["object_list"]), 1)
self.assertIn(self.project, response.context["object_list"])


class ProjectRemoveUsersViewTest(ProjectViewTestBase):
Expand Down
Loading