-
Notifications
You must be signed in to change notification settings - Fork 44
Expand file tree
/
Copy pathtest_grant_feed_view.py
More file actions
590 lines (488 loc) · 24 KB
/
test_grant_feed_view.py
File metadata and controls
590 lines (488 loc) · 24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
from datetime import datetime, timedelta
from decimal import Decimal
import pytz
from django.contrib.contenttypes.models import ContentType
from django.core.cache import cache
from rest_framework.test import APITestCase
from purchase.models import Fundraise, Grant, GrantApplication
from reputation.models import Escrow
from researchhub_document.helpers import create_post
from researchhub_document.related_models.constants.document_type import (
GRANT,
PREREGISTRATION,
)
from researchhub_document.related_models.researchhub_post_model import ResearchhubPost
from researchhub_document.related_models.researchhub_unified_document_model import (
ResearchhubUnifiedDocument,
)
from user.tests.helpers import create_random_authenticated_user
class GrantFeedViewTests(APITestCase):
def setUp(self):
# Clear any existing data to avoid test interference
cache.clear() # Clear cache to avoid test interference
GrantApplication.objects.all().delete()
Grant.objects.all().delete()
ResearchhubPost.objects.filter(document_type=GRANT).delete()
# Create users
self.moderator = create_random_authenticated_user(
"grant_feed_moderator", moderator=True
)
self.user = create_random_authenticated_user("grant_feed_user")
# Create grant posts
self.open_post = create_post(
created_by=self.moderator, document_type=GRANT, title="Open Grant"
)
self.closed_post = create_post(
created_by=self.moderator, document_type=GRANT, title="Closed Grant"
)
self.completed_post = create_post(
created_by=self.moderator, document_type=GRANT, title="Completed Grant"
)
# Create grants with different statuses
self.open_grant = Grant.objects.create(
created_by=self.moderator,
unified_document=self.open_post.unified_document,
amount=Decimal("50000.00"),
currency="USD",
organization="NSF",
description="Open research grant",
status=Grant.OPEN,
end_date=datetime.now(pytz.UTC) + timedelta(days=30),
)
self.closed_grant = Grant.objects.create(
created_by=self.moderator,
unified_document=self.closed_post.unified_document,
amount=Decimal("75000.00"),
currency="USD",
organization="NIH",
description="Closed research grant",
status=Grant.CLOSED,
end_date=datetime.now(pytz.UTC) - timedelta(days=10),
)
self.completed_grant = Grant.objects.create(
created_by=self.moderator,
unified_document=self.completed_post.unified_document,
amount=Decimal("100000.00"),
currency="USD",
organization="DOE",
description="Completed research grant",
status=Grant.COMPLETED,
end_date=datetime.now(pytz.UTC) - timedelta(days=5),
)
def tearDown(self):
"""Clean up after each test"""
GrantApplication.objects.all().delete()
Grant.objects.all().delete()
ResearchhubPost.objects.filter(document_type=GRANT).delete()
cache.clear() # Clear cache to avoid test interference
def test_grant_feed_list_authenticated(self):
"""Test that authenticated users can access the grant feed"""
self.client.force_authenticate(self.user)
response = self.client.get("/api/grant_feed/")
self.assertEqual(response.status_code, 200)
self.assertEqual(len(response.data["results"]), 3)
def test_grant_feed_list_unauthenticated(self):
"""Test that unauthenticated users can access the grant feed (public access)"""
response = self.client.get("/api/grant_feed/")
self.assertEqual(response.status_code, 200)
def test_grant_feed_returns_all_grants(self):
"""Test that grant feed returns all grants (no filtering by status/organization)"""
self.client.force_authenticate(self.user)
response = self.client.get("/api/grant_feed/")
self.assertEqual(response.status_code, 200)
# Should return all 3 grants since we removed filtering
self.assertEqual(len(response.data["results"]), 3)
def test_grant_feed_order_open_first(self):
"""Test that grant feed orders OPEN grants first"""
self.client.force_authenticate(self.user)
response = self.client.get("/api/grant_feed/")
self.assertEqual(response.status_code, 200)
results = response.data["results"]
# First result should be the OPEN grant
first_result = results[0]
self.assertEqual(first_result["content_object"]["title"], "Open Grant")
def test_grant_feed_entry_serializer_fields(self):
"""Test that grant feed entries include the expected fields"""
self.client.force_authenticate(self.user)
response = self.client.get("/api/grant_feed/?status=OPEN")
self.assertEqual(response.status_code, 200)
result = response.data["results"][0]
# Check base feed entry fields
self.assertIn("id", result)
self.assertIn("content_type", result)
self.assertIn("content_object", result)
self.assertIn("created_date", result)
self.assertIn("action_date", result)
self.assertIn("action", result)
self.assertIn("author", result)
# Check grant-specific fields are in the content_object.grant
content_object = result["content_object"]
self.assertIn("grant", content_object)
grant_data = content_object["grant"]
self.assertIn("organization", grant_data)
self.assertIn("amount", grant_data)
self.assertIn("is_expired", grant_data)
def test_grant_feed_organization_field(self):
"""Test that the organization field is correctly populated"""
self.client.force_authenticate(self.user)
response = self.client.get("/api/grant_feed/?status=OPEN")
result = response.data["results"][0]
grant_data = result["content_object"]["grant"]
self.assertEqual(grant_data["organization"], "NSF")
def test_grant_feed_grant_amount_field(self):
"""Test that the grant_amount field is correctly populated"""
self.client.force_authenticate(self.user)
response = self.client.get("/api/grant_feed/?status=OPEN")
result = response.data["results"][0]
grant_amount = result["content_object"]["grant"]["amount"]
self.assertEqual(grant_amount["usd"], 50000.0)
self.assertEqual(grant_amount["formatted"], "50,000.00 USD")
def test_grant_feed_is_expired_field(self):
"""Test that the is_expired field is correctly populated"""
self.client.force_authenticate(self.user)
# Get all grants and find the specific ones we want to test
response = self.client.get("/api/grant_feed/")
results = response.data["results"]
# Find open grant (not expired)
open_grant = None
closed_grant = None
for result in results:
grant_data = result["content_object"]["grant"]
if grant_data["status"] == "OPEN":
open_grant = grant_data
elif grant_data["status"] == "CLOSED":
closed_grant = grant_data
# Test open grant (not expired)
self.assertIsNotNone(open_grant, "Open grant not found in results")
self.assertFalse(open_grant["is_expired"])
# Test closed grant (expired)
self.assertIsNotNone(closed_grant, "Closed grant not found in results")
self.assertTrue(closed_grant["is_expired"])
def test_grant_feed_content_object_includes_grant_data(self):
"""Test that the content object includes grant-specific data"""
self.client.force_authenticate(self.user)
response = self.client.get("/api/grant_feed/")
# Should have results since we return all grants
self.assertGreater(len(response.data["results"]), 0)
# Check that grant data is included in the first result
result = response.data["results"][0]
content_object = result["content_object"]
# Check that grant data is included
self.assertIn("grant", content_object)
grant_data = content_object["grant"]
# Check that grant data has the expected fields
self.assertIn("organization", grant_data)
self.assertIn("amount", grant_data)
self.assertIn("status", grant_data)
self.assertIn("is_expired", grant_data)
def test_grant_feed_caching(self):
"""Test that grant feed responses can be cached for early pages"""
self.client.force_authenticate(self.user)
# First request should populate cache
response1 = self.client.get("/api/grant_feed/?page=1")
self.assertEqual(response1.status_code, 200)
# Second request should potentially use cache
response2 = self.client.get("/api/grant_feed/?page=1")
self.assertEqual(response2.status_code, 200)
# Responses should be identical
self.assertEqual(response1.data, response2.data)
def test_grant_feed_includes_applications(self):
"""Test that grant feed includes application data"""
# Create applicant users
applicant1 = create_random_authenticated_user("applicant1")
applicant2 = create_random_authenticated_user("applicant2")
# Create preregistration posts for applications
preregistration1 = create_post(
created_by=applicant1,
document_type=PREREGISTRATION,
title="Preregistration 1",
)
preregistration2 = create_post(
created_by=applicant2,
document_type=PREREGISTRATION,
title="Preregistration 2",
)
# Create applications
GrantApplication.objects.create(
grant=self.open_grant,
preregistration_post=preregistration1,
applicant=applicant1,
)
GrantApplication.objects.create(
grant=self.open_grant,
preregistration_post=preregistration2,
applicant=applicant2,
)
self.client.force_authenticate(self.user)
response = self.client.get("/api/grant_feed/")
self.assertEqual(response.status_code, 200)
results = response.data["results"]
self.assertTrue(len(results) > 0)
# Find the grant entry
grant_entry = None
for entry in results:
if entry["content_object"]["id"] == self.open_post.id:
grant_entry = entry
break
self.assertIsNotNone(grant_entry)
grant_data = grant_entry["content_object"]["grant"]
self.assertIn("applications", grant_data)
applications = grant_data["applications"]
self.assertEqual(len(applications), 2)
# Check application structure
application1 = applications[0]
self.assertIn("id", application1)
self.assertIn("created_date", application1)
self.assertIn("applicant", application1)
self.assertIn("preregistration_post_id", application1)
# Check applicant structure using SimpleAuthorSerializer
applicant_data = application1["applicant"]
self.assertIn("id", applicant_data)
self.assertIn("first_name", applicant_data)
self.assertIn("last_name", applicant_data)
self.assertIn("profile_image", applicant_data)
self.assertIn("headline", applicant_data)
self.assertIn("user", applicant_data)
# Verify applicant IDs are correct
applicant_ids = [app["applicant"]["id"] for app in applications]
self.assertIn(applicant1.author_profile.id, applicant_ids)
self.assertIn(applicant2.author_profile.id, applicant_ids)
def test_grant_feed_empty_applications(self):
"""Test that grant feed handles grants with no applications"""
self.client.force_authenticate(self.user)
response = self.client.get("/api/grant_feed/")
self.assertEqual(response.status_code, 200)
results = response.data["results"]
self.assertTrue(len(results) > 0)
# Find the grant entry
grant_entry = None
for entry in results:
if entry["content_object"]["id"] == self.open_post.id:
grant_entry = entry
break
self.assertIsNotNone(grant_entry)
grant_data = grant_entry["content_object"]["grant"]
self.assertIn("applications", grant_data)
# Should be empty list when no applications
applications = grant_data["applications"]
self.assertEqual(len(applications), 0)
self.assertIsInstance(applications, list)
def test_grant_feed_invalid_status_filter(self):
"""Test grant feed with invalid status filter"""
self.client.force_authenticate(self.user)
response = self.client.get("/api/grant_feed/?status=INVALID")
# Should return 200 but with no results (invalid status filter is ignored)
self.assertEqual(response.status_code, 200)
def test_grant_feed_filter_by_status_open(self):
"""Test grant feed filtering by OPEN status"""
self.client.force_authenticate(self.user)
response = self.client.get("/api/grant_feed/?status=OPEN")
self.assertEqual(response.status_code, 200)
self.assertEqual(len(response.data["results"]), 1)
self.assertEqual(
response.data["results"][0]["content_object"]["title"], "Open Grant"
)
def test_grant_feed_filter_by_status_closed(self):
"""Test grant feed filtering by CLOSED status returns all inactive grants"""
# Arrange
self.client.force_authenticate(self.user)
# Act
response = self.client.get("/api/grant_feed/?status=CLOSED")
# Assert - CLOSED filter returns both CLOSED and COMPLETED grants
self.assertEqual(response.status_code, 200)
self.assertEqual(len(response.data["results"]), 2)
titles = [r["content_object"]["title"] for r in response.data["results"]]
self.assertIn("Closed Grant", titles)
self.assertIn("Completed Grant", titles)
def test_grant_feed_filter_by_status_completed(self):
"""Test grant feed filtering by COMPLETED status returns all inactive grants"""
# Arrange
self.client.force_authenticate(self.user)
# Act
response = self.client.get("/api/grant_feed/?status=COMPLETED")
# Assert - COMPLETED filter also returns both CLOSED and COMPLETED grants
self.assertEqual(response.status_code, 200)
self.assertEqual(len(response.data["results"]), 2)
titles = [r["content_object"]["title"] for r in response.data["results"]]
self.assertIn("Closed Grant", titles)
self.assertIn("Completed Grant", titles)
def test_grant_feed_expired_open_grant_appears_in_closed(self):
"""Test that an OPEN grant with expired end_date appears in CLOSED filter"""
# Arrange
expired_post = create_post(
created_by=self.moderator, document_type=GRANT, title="Expired Open Grant"
)
Grant.objects.create(
created_by=self.moderator,
unified_document=expired_post.unified_document,
amount=Decimal("25000.00"),
currency="USD",
organization="Expired Org",
description="Expired but still OPEN status",
status=Grant.OPEN,
end_date=datetime.now(pytz.UTC) - timedelta(days=1),
)
self.client.force_authenticate(self.user)
# Act
open_response = self.client.get("/api/grant_feed/?status=OPEN")
closed_response = self.client.get("/api/grant_feed/?status=CLOSED")
# Assert - expired OPEN grant should NOT appear in OPEN filter
open_titles = [r["content_object"]["title"] for r in open_response.data["results"]]
self.assertNotIn("Expired Open Grant", open_titles)
# Assert - expired OPEN grant SHOULD appear in CLOSED filter
closed_titles = [r["content_object"]["title"] for r in closed_response.data["results"]]
self.assertIn("Expired Open Grant", closed_titles)
def test_grant_feed_filter_by_organization(self):
"""Test grant feed filtering by organization"""
self.client.force_authenticate(self.user)
response = self.client.get("/api/grant_feed/?organization=NSF")
self.assertEqual(response.status_code, 200)
self.assertEqual(len(response.data["results"]), 1)
self.assertEqual(
response.data["results"][0]["content_object"]["title"], "Open Grant"
)
def test_grant_feed_filter_by_organization_partial_match(self):
"""Test grant feed filtering by organization with partial match"""
self.client.force_authenticate(self.user)
response = self.client.get("/api/grant_feed/?organization=NS")
self.assertEqual(response.status_code, 200)
self.assertEqual(len(response.data["results"]), 1)
self.assertEqual(
response.data["results"][0]["content_object"]["title"], "Open Grant"
)
def test_grant_feed_multiple_filters(self):
"""Test grant feed with multiple filters"""
self.client.force_authenticate(self.user)
response = self.client.get("/api/grant_feed/?status=OPEN&organization=NSF")
self.assertEqual(response.status_code, 200)
self.assertEqual(len(response.data["results"]), 1)
self.assertEqual(
response.data["results"][0]["content_object"]["title"], "Open Grant"
)
def test_grant_feed_no_grants(self):
"""Test grant feed with filters that return no results"""
self.client.force_authenticate(self.user)
response = self.client.get(
"/api/grant_feed/?status=OPEN&organization=NONEXISTENT"
)
self.assertEqual(response.status_code, 200)
self.assertEqual(len(response.data["results"]), 0)
def test_ordering_validation(self):
"""Test that FundOrderingFilter handles different ordering scenarios correctly for grants."""
from unittest.mock import Mock, patch
from rest_framework.request import Request
from rest_framework.test import APIRequestFactory
from feed.filters import FundOrderingFilter
filter_instance = FundOrderingFilter()
factory = APIRequestFactory()
mock_queryset = Mock()
mock_view = Mock()
# Setup view with ordering_fields and is_grant_view
mock_view.ordering_fields = [
"newest",
"upvotes",
"most_applicants",
"amount_raised",
]
mock_view.ordering = "newest"
mock_view.is_grant_view = True
# Test custom sorting (upvotes) - patch the specific sorting method
request = factory.get("/?ordering=upvotes")
drf_request = Request(request)
with (
patch.object(filter_instance, "_apply_upvotes_sorting") as mock_upvotes,
patch.object(filter_instance, "_apply_include_ended_filter") as mock_filter,
):
mock_filter.return_value = mock_queryset
mock_upvotes.return_value = mock_queryset
filter_instance.filter_queryset(drf_request, mock_queryset, mock_view)
mock_upvotes.assert_called_once_with(mock_queryset)
# Test newest sorting (default - no ordering param)
request = factory.get("/")
drf_request = Request(request)
with (
patch.object(filter_instance, "_apply_newest_sorting") as mock_newest,
patch.object(filter_instance, "_apply_include_ended_filter") as mock_filter,
):
mock_filter.return_value = mock_queryset
mock_newest.return_value = mock_queryset
filter_instance.filter_queryset(drf_request, mock_queryset, mock_view)
# Check that it was called with queryset and model_config
assert mock_newest.call_count == 1
args = mock_newest.call_args[0]
assert args[0] == mock_queryset
assert "model_class" in args[1] # model_config has model_class
# Test with '-' prefix - should be stripped and work
request = factory.get("/?ordering=-upvotes")
drf_request = Request(request)
with (
patch.object(filter_instance, "_apply_upvotes_sorting") as mock_upvotes,
patch.object(filter_instance, "_apply_include_ended_filter") as mock_filter,
):
mock_filter.return_value = mock_queryset
mock_upvotes.return_value = mock_queryset
filter_instance.filter_queryset(drf_request, mock_queryset, mock_view)
mock_upvotes.assert_called_once_with(mock_queryset)
def test_ordering_validation_integration(self):
"""Test ordering validation through the actual grant feed API endpoint."""
self.client.force_authenticate(self.user)
# Test valid ordering
response = self.client.get("/api/grant_feed/?ordering=upvotes")
self.assertEqual(response.status_code, 200)
# Test invalid ordering - should fall back to default (newest)
response = self.client.get("/api/grant_feed/?ordering=invalid_field")
self.assertEqual(response.status_code, 200)
# Test with '-' prefix - should work
response = self.client.get("/api/grant_feed/?ordering=-upvotes")
self.assertEqual(response.status_code, 200)
def test_created_by_filter_returns_grants_by_creator(self):
"""Test created_by filter returns only grants by specified user."""
# Arrange
self.client.force_authenticate(self.user)
# Act
response = self.client.get(f"/api/grant_feed/?created_by={self.moderator.id}")
# Assert
self.assertEqual(len(response.data["results"]), 3)
def test_created_by_filter_returns_empty_for_non_creator(self):
"""Test created_by filter returns empty when user created no grants."""
# Arrange
self.client.force_authenticate(self.user)
# Act
response = self.client.get(f"/api/grant_feed/?created_by={self.user.id}")
# Assert
self.assertEqual(len(response.data["results"]), 0)
def test_leaderboard_ordering(self):
"""Top 5 OPEN grants: funded first, then by budget; closed excluded."""
# Arrange — 6 open grants (+ 1 from setUp = 7 open, 2 closed/completed)
grants = []
for i in range(6):
post = create_post(created_by=self.moderator, document_type=GRANT, title=f"Grant {i}")
grants.append(Grant.objects.create(
created_by=self.moderator, unified_document=post.unified_document,
amount=Decimal(str(1000 * (i + 1))), currency="USD",
organization="Org", description="", status=Grant.OPEN,
))
# Fund grants[0] (smallest budget) so it ranks first
applicant = create_random_authenticated_user("applicant")
doc = ResearchhubUnifiedDocument.objects.create(document_type=PREREGISTRATION)
proposal = ResearchhubPost.objects.create(
title="P", created_by=applicant, document_type=PREREGISTRATION, unified_document=doc,
)
GrantApplication.objects.create(grant=grants[0], preregistration_post=proposal, applicant=applicant)
escrow = Escrow.objects.create(
amount_holding=99999, hold_type=Escrow.FUNDRAISE, created_by=applicant,
content_type=ContentType.objects.get_for_model(ResearchhubUnifiedDocument), object_id=doc.id,
)
Fundraise.objects.create(
created_by=applicant, unified_document=doc, escrow=escrow,
status=Fundraise.OPEN, goal_amount=100000,
)
# Act
self.client.force_authenticate(self.user)
response = self.client.get("/api/grant_feed/?ordering=leaderboard")
titles = [r["content_object"]["title"] for r in response.data["results"]]
# Assert — capped at 5, funded grant first, closed/completed excluded
self.assertEqual(len(titles), 5)
self.assertEqual(titles[0], "Grant 0")
self.assertNotIn("Closed Grant", titles)
self.assertNotIn("Completed Grant", titles)