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

livedata: fix cache_unless_admin_or_observer #387

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
56 changes: 55 additions & 1 deletion oioioi/livedata/tests.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,22 @@
from django.contrib.auth.models import User
from django.test import override_settings
from django.core.cache import cache

from oioioi.base.tests import TestCase
from oioioi.contests.current_contest import reverse
from oioioi.contests.models import Contest, Round
from oioioi.livedata.utils import get_display_name

# TODO


class TestLivedata(TestCase):
fixtures = ['test_users', 'test_users_nonames']
fixtures = ['test_users', 'test_users_nonames', 'test_contest']

def setUp(self):
contest = Contest.objects.get()
contest.controller_name = 'oioioi.pa.controllers.PAFinalsContestController'
contest.save()

def test_get_user_name(self):
cases = [
Expand All @@ -19,3 +28,48 @@ def test_get_user_name(self):
for username, display in cases:
user = User.objects.get(username=username)
self.assertEqual(get_display_name(user), display or username)

@override_settings(CACHES={
'default': {
'BACKEND': 'django.core.cache.backends.locmem.LocMemCache',
'LOCATION': 'test-cache',
}
})
def test_cache_unless_admin_or_observer(self):
contest = Contest.objects.get()
round = Round.objects.filter(contest_id=contest.id).get()
view_name = 'livedata_teams_view'
cache_key = '%s/%s/%s' % (view_name, contest.id, round.id)
url = reverse(view_name, kwargs={
'contest_id': contest.id, 'round_id': round.id})
# For users that aren't admins or observers, the second request will
# come from the cache, this verifies if the cached result's content is
# the same as the uncached result
self.assertIsNone(cache.get(cache_key))
request_first = self.client.get(url)
self.assertIsNotNone(cache.get(cache_key))
request_cached = self.client.get(url)
self.assertEqual(
request_first.content,
request_cached.content,
"Cached response differs"
)
# Test if admin gets content from cache, by forcefully setting a set
# "magic string" on the cache key used by the view
magic_string = 'random'
cached_data = cache.get(cache_key)
cached_data['content'] = magic_string
cache.set(cache_key, cached_data)
request_cached = self.client.get(url)
self.assertEqual(
request_cached.content.decode(request_cached.charset),
magic_string,
"Response from cache does not contain expected cached data"
)
self.assertTrue(self.client.login(username='test_admin'))
request_admin = self.client.get(url)
self.assertNotEqual(
request_admin.content.decode(request_admin.charset),
magic_string,
"Admin should not be receiving a response from cache"
)
2 changes: 1 addition & 1 deletion oioioi/livedata/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ def inner(request, round_id):
cache.set(
cache_key,
{
'content': str(result.content),
'content': result.content.decode(result.charset),
'content_type': result['Content-Type'],
},
settings.LIVEDATA_CACHE_TIMEOUT,
Expand Down
Loading