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

CONCD-815 tests for user-cached views #2571

Merged
merged 6 commits into from
Oct 25, 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
107 changes: 107 additions & 0 deletions concordia/tests/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
override_settings,
)
from django.urls import reverse
from django.utils.decorators import method_decorator
from django.utils.timezone import now

from concordia.models import (
Expand All @@ -32,8 +33,11 @@
from concordia.views import (
AccountProfileView,
CompletedCampaignListView,
FilteredItemDetailView,
FilteredProjectDetailView,
ratelimit_view,
registration_rate,
user_cache_control,
)

from .utils import (
Expand All @@ -46,6 +50,7 @@
create_item,
create_project,
create_topic,
create_transcription,
)


Expand Down Expand Up @@ -1536,6 +1541,108 @@ def test_find_next_transcribable_hierarchy(self):
)


class UserCacheControlTest(CreateTestUsers, TestCase):
"""
Tests for the user_cache_control decorator
"""

def setUp(self):
self.factory = RequestFactory()
self.user = self.create_user("testuser")

def test_vary_on_cookie(self):
@method_decorator(user_cache_control, name="dispatch")
def a_view(request):
return HttpResponse()

request = self.factory.get("/rand")
request.user = self.user
resp = a_view(None, request)
self.assertEqual(resp.status_code, 200)


class FilteredCampaignDetailViewTests(CreateTestUsers, TestCase):
def test_get_context_data(self):
campaign = create_campaign()
kwargs = {"slug": campaign.slug}
url = reverse("transcriptions:filtered-campaign-detail", kwargs=kwargs)

self.login_user(is_staff=False)
response = self.client.get(url, kwargs)
self.assertFalse(response.context.get("filter_by_reviewable", False))
self.logout_user()

self.user = self.create_staff_user()
self.login_user()
response = self.client.get(url, kwargs)
self.assertTrue(response.context.get("filter_by_reviewable"))


class FilteredProjectDetailViewTests(CreateTestUsers, TestCase):
def setUp(self):
self.project = create_project()
self.kwargs = {
"campaign_slug": self.project.campaign.slug,
"slug": self.project.slug,
}
self.url = reverse("transcriptions:filtered-project-detail", kwargs=self.kwargs)
self.login_user()

def test_get_queryset(self):
item1 = create_item(project=self.project, item_id="testitem.012345679")
asset1 = create_asset(item=item1)
create_transcription(asset=asset1, user=get_anonymous_user(), submitted=now())

item2 = create_item(
project=create_project(slug="project-two", campaign=self.project.campaign)
)
asset2 = create_asset(item=item2)
create_transcription(asset=asset2, user=self.user, submitted=now())

view = FilteredProjectDetailView()
view.kwargs = self.kwargs
view.request = RequestFactory().get(self.url, self.kwargs)
view.request.user = self.user
qs = view.get_queryset()
self.assertIn(item1, qs)
self.assertNotIn(item2, qs)

def test_get_context_data(self):
response = self.client.get(self.url, self.kwargs)
self.assertTrue(response.context.get("filter_by_reviewable"))


class FilteredItemDetailViewTests(CreateTestUsers, TestCase):
def setUp(self):
self.item = create_item()
self.kwargs = {
"campaign_slug": self.item.project.campaign.slug,
"project_slug": self.item.project.slug,
"item_id": self.item.item_id,
}
self.url = reverse("transcriptions:filtered-item-detail", kwargs=self.kwargs)
self.login_user()

def test_get_queryset(self):
asset1 = create_asset(item=self.item)
create_transcription(asset=asset1, user=get_anonymous_user(), submitted=now())

asset2 = create_asset(item=self.item, slug="asset-two")
create_transcription(asset=asset2, user=self.user, submitted=now())

view = FilteredItemDetailView()
view.kwargs = self.kwargs
view.request = RequestFactory().get(self.url, self.kwargs)
view.request.user = self.user
qs = view.get_queryset()
self.assertIn(asset1, qs)
self.assertNotIn(asset2, qs)

def test_get_context_data(self):
response = self.client.get(self.url, self.kwargs)
self.assertTrue(response.context.get("filter_by_reviewable"))


class RateLimitTests(TestCase):
def setUp(self):
self.request_factory = RequestFactory()
Expand Down
1 change: 1 addition & 0 deletions concordia/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -1399,6 +1399,7 @@ def get_queryset(self):

def get_context_data(self, **kwargs):
self.kwargs["filter_by_reviewable"] = True
kwargs["filter_by_reviewable"] = True
return super().get_context_data(**kwargs)


Expand Down