Skip to content

Commit 55364cb

Browse files
Fix Tests
Squashed commits: - fix project view tests - fix allocation change view tests - fix ProjectListViewTest.test_project_list_search and add title to project search - fix allocation expiry tests - replaced `timezone.now()` calls in test_models.py with `datetime.date.now()`, since that's what the model uses Signed-off-by: Cecilia Lau <[email protected]>
1 parent 146c9e6 commit 55364cb

File tree

5 files changed

+69
-107
lines changed

5 files changed

+69
-107
lines changed

coldfront/core/allocation/tests/test_models.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ def test_status_is_expired_and_no_end_date_has_validation_error(self):
6161

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

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

7676
actual_allocation: Allocation = AllocationFactory.build(
@@ -116,7 +116,7 @@ def test_status_is_active_and_no_end_date_has_validation_error(self):
116116

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

122122
actual_allocation: Allocation = AllocationFactory.build(
@@ -237,32 +237,32 @@ class AllocationModelExpiresInTests(TestCase):
237237

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

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

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

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

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

coldfront/core/allocation/tests/test_views.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,7 @@ def setUp(self):
165165
"attributeform-TOTAL_FORMS": "1",
166166
"end_date_extension": 0,
167167
}
168-
self.url = "/allocation/1/change-request"
168+
self.url = f"/allocation/{self.allocation.pk}/change-request"
169169

170170
def test_allocationchangeview_access(self):
171171
"""Test get request"""
@@ -178,7 +178,7 @@ def test_allocationchangeview_post_extension(self):
178178

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

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

191-
response = self.client.post("/allocation/1/change-request", data=self.post_data, follow=True)
191+
response = self.client.post(self.url, data=self.post_data, follow=True)
192192
self.assertEqual(response.status_code, 200)
193193
self.assertContains(response, "You must request a change")
194194
self.assertEqual(len(AllocationChangeRequest.objects.all()), 0)

coldfront/core/project/forms.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,12 @@
1919
class ProjectSearchForm(forms.Form):
2020
"""Search form for the Project list page."""
2121

22+
TITLE = "Title"
2223
LAST_NAME = "Last Name"
2324
USERNAME = "Username"
2425
FIELD_OF_SCIENCE = "Field of Science"
2526

27+
title = forms.CharField(label=TITLE, max_length=255, required=False)
2628
last_name = forms.CharField(label=LAST_NAME, max_length=100, required=False)
2729
username = forms.CharField(label=USERNAME, max_length=100, required=False)
2830
field_of_science = forms.CharField(label=FIELD_OF_SCIENCE, max_length=100, required=False)

coldfront/core/project/tests/test_views.py

Lines changed: 31 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -32,12 +32,10 @@ def setUpTestData(cls):
3232
cls.project = ProjectFactory(status=ProjectStatusChoiceFactory(name="Active"))
3333

3434
user_role = ProjectUserRoleChoiceFactory(name="User")
35-
project_user = ProjectUserFactory(project=cls.project, role=user_role)
36-
cls.project_user = project_user.user
35+
cls.project_user = ProjectUserFactory(project=cls.project, role=user_role)
3736

3837
manager_role = ProjectUserRoleChoiceFactory(name="Manager")
39-
pi_user = ProjectUserFactory(project=cls.project, role=manager_role, user=cls.project.pi)
40-
cls.pi_user = pi_user.user
38+
cls.pi_user = ProjectUserFactory(project=cls.project, role=manager_role, user=cls.project.pi)
4139
cls.admin_user = UserFactory(is_staff=True, is_superuser=True)
4240
cls.nonproject_user = UserFactory(is_staff=False, is_superuser=False)
4341

@@ -69,8 +67,8 @@ def test_projectdetail_access(self):
6967
# logged-out user gets redirected, admin can access create page
7068
self.project_access_tstbase(self.url)
7169
# pi and projectuser can access
72-
utils.test_user_can_access(self, self.pi_user, self.url)
73-
utils.test_user_can_access(self, self.project_user, self.url)
70+
utils.test_user_can_access(self, self.pi_user.user, self.url)
71+
utils.test_user_can_access(self, self.project_user.user, self.url)
7472
# user not belonging to project cannot access
7573
utils.test_user_cannot_access(self, self.nonproject_user, self.url)
7674

@@ -80,10 +78,10 @@ def test_projectdetail_permissions(self):
8078
response = utils.login_and_get_page(self.client, self.admin_user, self.url)
8179
self.assertEqual(response.context["is_allowed_to_update_project"], True)
8280
# pi has is_allowed_to_update_project set to True
83-
response = utils.login_and_get_page(self.client, self.pi_user, self.url)
81+
response = utils.login_and_get_page(self.client, self.pi_user.user, self.url)
8482
self.assertEqual(response.context["is_allowed_to_update_project"], True)
8583
# non-manager user has is_allowed_to_update_project set to False
86-
response = utils.login_and_get_page(self.client, self.project_user, self.url)
84+
response = utils.login_and_get_page(self.client, self.project_user.user, self.url)
8785
self.assertEqual(response.context["is_allowed_to_update_project"], False)
8886

8987
def test_projectdetail_request_allocation_button_visibility(self):
@@ -92,27 +90,27 @@ def test_projectdetail_request_allocation_button_visibility(self):
9290
# admin can see request allocation button
9391
utils.page_contains_for_user(self, self.admin_user, self.url, button_text)
9492
# pi can see request allocation button
95-
utils.page_contains_for_user(self, self.pi_user, self.url, button_text)
93+
utils.page_contains_for_user(self, self.pi_user.user, self.url, button_text)
9694
# non-manager user cannot see request allocation button
97-
utils.page_does_not_contain_for_user(self, self.project_user, self.url, button_text)
95+
utils.page_does_not_contain_for_user(self, self.project_user.user, self.url, button_text)
9896

9997
def test_projectdetail_edituser_button_visibility(self):
10098
"""Test visibility of projectdetail edit button across user levels"""
10199
# admin can see edit button
102100
utils.page_contains_for_user(self, self.admin_user, self.url, "fa-user-edit")
103101
# pi can see edit button
104-
utils.page_contains_for_user(self, self.pi_user, self.url, "fa-user-edit")
102+
utils.page_contains_for_user(self, self.pi_user.user, self.url, "fa-user-edit")
105103
# non-manager user cannot see edit button
106-
utils.page_does_not_contain_for_user(self, self.project_user, self.url, "fa-user-edit")
104+
utils.page_does_not_contain_for_user(self, self.project_user.user, self.url, "fa-user-edit")
107105

108106
def test_projectdetail_addnotification_button_visibility(self):
109107
"""Test visibility of projectdetail add notification button across user levels"""
110108
# admin can see add notification button
111109
utils.page_contains_for_user(self, self.admin_user, self.url, "Add Notification")
112110
# pi cannot see add notification button
113-
utils.page_does_not_contain_for_user(self, self.pi_user, self.url, "Add Notification")
111+
utils.page_does_not_contain_for_user(self, self.pi_user.user, self.url, "Add Notification")
114112
# non-manager user cannot see add notification button
115-
utils.page_does_not_contain_for_user(self, self.project_user, self.url, "Add Notification")
113+
utils.page_does_not_contain_for_user(self, self.project_user.user, self.url, "Add Notification")
116114

117115

118116
class ProjectCreateTest(ProjectViewTestBase):
@@ -129,8 +127,8 @@ def test_project_access(self):
129127
# logged-out user gets redirected, admin can access create page
130128
self.project_access_tstbase(self.url)
131129
# pi, projectuser and nonproject user cannot access create page
132-
utils.test_user_cannot_access(self, self.pi_user, self.url)
133-
utils.test_user_cannot_access(self, self.project_user, self.url)
130+
utils.test_user_cannot_access(self, self.pi_user.user, self.url)
131+
utils.test_user_cannot_access(self, self.project_user.user, self.url)
134132
utils.test_user_cannot_access(self, self.nonproject_user, self.url)
135133

136134

@@ -150,9 +148,9 @@ def test_project_access(self):
150148
# logged-out user gets redirected, admin can access create page
151149
self.project_access_tstbase(self.url)
152150
# pi can access create page
153-
utils.test_user_can_access(self, self.pi_user, self.url)
151+
utils.test_user_can_access(self, self.pi_user.user, self.url)
154152
# project user and nonproject user cannot access create page
155-
utils.test_user_cannot_access(self, self.project_user, self.url)
153+
utils.test_user_cannot_access(self, self.project_user.user, self.url)
156154
utils.test_user_cannot_access(self, self.nonproject_user, self.url)
157155

158156
def test_project_attribute_create_post(self):
@@ -207,9 +205,9 @@ def setUpTestData(cls):
207205
def test_project_attribute_update_access(self):
208206
"""Test access to project attribute update page"""
209207
self.project_access_tstbase(self.url)
210-
utils.test_user_can_access(self, self.pi_user, self.url)
208+
utils.test_user_can_access(self, self.pi_user.user, self.url)
211209
# project user, pi, and nonproject user cannot access update page
212-
utils.test_user_cannot_access(self, self.project_user, self.url)
210+
utils.test_user_cannot_access(self, self.project_user.user, self.url)
213211
utils.test_user_cannot_access(self, self.nonproject_user, self.url)
214212

215213

@@ -230,9 +228,9 @@ def test_project_attribute_delete_access(self):
230228
# logged-out user gets redirected, admin can access delete page
231229
self.project_access_tstbase(self.url)
232230
# pi can access delete page
233-
utils.test_user_can_access(self, self.pi_user, self.url)
231+
utils.test_user_can_access(self, self.pi_user.user, self.url)
234232
# project user and nonproject user cannot access delete page
235-
utils.test_user_cannot_access(self, self.project_user, self.url)
233+
utils.test_user_cannot_access(self, self.project_user.user, self.url)
236234
utils.test_user_cannot_access(self, self.nonproject_user, self.url)
237235

238236

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

251250
### ProjectListView access tests ###
@@ -255,21 +254,21 @@ def test_project_list_access(self):
255254
# logged-out user gets redirected, admin can access list page
256255
self.project_access_tstbase(self.url)
257256
# all other users can access list page
258-
utils.test_user_can_access(self, self.pi_user, self.url)
259-
utils.test_user_can_access(self, self.project_user, self.url)
257+
utils.test_user_can_access(self, self.pi_user.user, self.url)
258+
utils.test_user_can_access(self, self.project_user.user, self.url)
260259
utils.test_user_can_access(self, self.nonproject_user, self.url)
261260

262261
### ProjectListView display tests ###
263262

264263
def test_project_list_display_members(self):
265264
"""Project list displays only projects that user is an active member of"""
266265
# deactivated projectuser won't see project on their page
267-
response = utils.login_and_get_page(self.client, self.project_user, self.url)
266+
response = utils.login_and_get_page(self.client, self.project_user.user, self.url)
268267
self.assertEqual(len(response.context["object_list"]), 1)
269-
proj_user = self.project.projectuser_set.get(user=self.project_user)
268+
proj_user = self.project.projectuser_set.get(user=self.project_user.user)
270269
proj_user.status, _ = ProjectUserStatusChoice.objects.get_or_create(name="Removed")
271270
proj_user.save()
272-
response = utils.login_and_get_page(self.client, self.project_user, self.url)
271+
response = utils.login_and_get_page(self.client, self.project_user.user, self.url)
273272
self.assertEqual(len(response.context["object_list"]), 0)
274273

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

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

293292
### ProjectListView search tests ###
294293

295294
def test_project_list_search(self):
296295
"""Test that project list search works."""
297296
url_base = self.url + "?show_all_projects=on"
298-
url = (
299-
f"{url_base}&last_name={self.project.pi.last_name}"
300-
+ f"&field_of_science={self.project.field_of_science.description}"
301-
)
297+
url = f"{url_base}&title={self.project.title}"
302298
# search by project project_title
303299
response = utils.login_and_get_page(self.client, self.admin_user, url)
304-
self.assertEqual(len(response.context["object_list"]), 1)
300+
self.assertIn(self.project, response.context["object_list"])
305301

306302

307303
class ProjectRemoveUsersViewTest(ProjectViewTestBase):

0 commit comments

Comments
 (0)