From 3d5d98ffb816e3b07fa383e77cc288a76d74fa72 Mon Sep 17 00:00:00 2001 From: zhengxie Date: Fri, 18 Mar 2016 15:49:31 +0800 Subject: [PATCH] Update nickname cache invalidation --- seahub/api2/endpoints/account.py | 1 - seahub/profile/models.py | 12 +++++-- seahub/profile/views.py | 4 +-- seahub/test_settings.py | 10 +++--- seahub/test_utils.py | 6 ++++ tests/api/test_public_repo.py | 2 ++ tests/api/test_shared_repo.py | 1 + .../models/test_update_nickname_cache.py | 35 +++++++++++++++++++ .../seahub/profile/views/test_edit_profile.py | 33 +++++++++++++++++ tests/seahub/views/init/test_libraries.py | 2 ++ 10 files changed, 95 insertions(+), 11 deletions(-) create mode 100644 tests/seahub/profile/models/test_update_nickname_cache.py create mode 100644 tests/seahub/profile/views/test_edit_profile.py diff --git a/seahub/api2/endpoints/account.py b/seahub/api2/endpoints/account.py index e55c7709a8..7ba116544a 100644 --- a/seahub/api2/endpoints/account.py +++ b/seahub/api2/endpoints/account.py @@ -75,7 +75,6 @@ class Account(APIView): profile.intro = note profile.save() - refresh_profile_cache(email) def _update_account_quota(self, request, email): storage = request.DATA.get("storage", None) diff --git a/seahub/profile/models.py b/seahub/profile/models.py index 1d343b44d2..1c8b6ed11d 100644 --- a/seahub/profile/models.py +++ b/seahub/profile/models.py @@ -129,11 +129,19 @@ class DetailedProfile(models.Model): telephone = models.CharField(max_length=100) objects = DetailedProfileManager() -########## signal handler + +########## signal handlers +from django.db.models.signals import post_save +from .utils import refresh_cache + @receiver(user_registered) def clean_email_id_cache(sender, **kwargs): from seahub.utils import normalize_cache_key - + user = kwargs['user'] key = normalize_cache_key(user.email, EMAIL_ID_CACHE_PREFIX) cache.set(key, user.id, EMAIL_ID_CACHE_TIMEOUT) + +@receiver(post_save, sender=Profile, dispatch_uid="update_nickname_cache") +def update_nickname_cache(sender, instance, **kwargs): + refresh_cache(instance.user) diff --git a/seahub/profile/views.py b/seahub/profile/views.py index 747c33b147..04658f77b4 100644 --- a/seahub/profile/views.py +++ b/seahub/profile/views.py @@ -36,9 +36,7 @@ def edit_profile(request): if form.is_valid(): form.save(username=username) messages.success(request, _(u'Successfully edited profile.')) - # refresh nickname cache - refresh_cache(request.user.username) - + return HttpResponseRedirect(reverse('edit_profile')) else: messages.error(request, _(u'Failed to edit profile')) diff --git a/seahub/test_settings.py b/seahub/test_settings.py index fe69d987ba..22cc9eaa88 100644 --- a/seahub/test_settings.py +++ b/seahub/test_settings.py @@ -1,11 +1,11 @@ from .settings import * # no cache for testing -CACHES = { - 'default': { - 'BACKEND': 'django.core.cache.backends.dummy.DummyCache', - } -} +# CACHES = { +# 'default': { +# 'BACKEND': 'django.core.cache.backends.dummy.DummyCache', +# } +# } # enlarge api throttle REST_FRAMEWORK = { diff --git a/seahub/test_utils.py b/seahub/test_utils.py index 2f53ad1da8..e082a119ca 100644 --- a/seahub/test_utils.py +++ b/seahub/test_utils.py @@ -1,6 +1,7 @@ import os from uuid import uuid4 +from django.core.cache import cache from django.core.urlresolvers import reverse from django.test import TestCase from exam.decorators import fixture @@ -108,3 +109,8 @@ class BaseTestCase(TestCase, Fixtures): reverse('auth_login'), {'login': user.username, 'password': 'secret'} ) + + def clear_cache(self): + # clear cache between every test case to avoid config option cache + # issue which cause test failed + cache.clear() diff --git a/tests/api/test_public_repo.py b/tests/api/test_public_repo.py index a1732718ec..02852ee195 100644 --- a/tests/api/test_public_repo.py +++ b/tests/api/test_public_repo.py @@ -18,6 +18,8 @@ class RepoPublicTest(BaseTestCase): def tearDown(self): self.remove_repo(self.repo_id) + # clear cache between every test case to avoid config option cache issue + self.clear_cache() def test_admin_can_set_pub_repo(self): self.login_as(self.admin) diff --git a/tests/api/test_shared_repo.py b/tests/api/test_shared_repo.py index ce2645745f..f4c023a14c 100644 --- a/tests/api/test_shared_repo.py +++ b/tests/api/test_shared_repo.py @@ -16,6 +16,7 @@ class SharedRepoTest(BaseTestCase): def tearDown(self): self.remove_repo(self.repo_id) + self.clear_cache() def test_admin_can_share_repo_to_public(self): self.login_as(self.admin) diff --git a/tests/seahub/profile/models/test_update_nickname_cache.py b/tests/seahub/profile/models/test_update_nickname_cache.py new file mode 100644 index 0000000000..6707387608 --- /dev/null +++ b/tests/seahub/profile/models/test_update_nickname_cache.py @@ -0,0 +1,35 @@ +from seahub.base.templatetags.seahub_tags import email2nickname +from seahub.profile.models import Profile +from seahub.test_utils import BaseTestCase + +from tests.common.utils import randstring + + +class UpdateNicknameCacheTest(BaseTestCase): + def setUp(self): + self.tmp_user = self.create_user('user_%s@test.com' % randstring(4), + is_staff=False) + assert len(Profile.objects.all()) == 0 + + def tearDown(self): + self.remove_user(self.tmp_user.username) + + def test_update_when_call_object_method(self): + username = self.tmp_user.username + assert email2nickname(username) == username.split('@')[0] + + Profile.objects.add_or_update(username, 'nickname') + assert email2nickname(username) == 'nickname' + + def test_updated_when_call_save(self): + username = self.tmp_user.username + assert email2nickname(username) == username.split('@')[0] + + p = Profile.objects.get_profile_by_user(username) + if p is None: + p = Profile(user=username) + + p.nickname = 'nickname' + p.save() + + assert email2nickname(username) == 'nickname' diff --git a/tests/seahub/profile/views/test_edit_profile.py b/tests/seahub/profile/views/test_edit_profile.py new file mode 100644 index 0000000000..a454c1193f --- /dev/null +++ b/tests/seahub/profile/views/test_edit_profile.py @@ -0,0 +1,33 @@ +from django.core.urlresolvers import reverse + +from seahub.base.templatetags.seahub_tags import email2nickname +from seahub.profile.models import Profile +from seahub.test_utils import BaseTestCase +from tests.common.utils import randstring + +class EditProfileTest(BaseTestCase): + def setUp(self): + self.tmp_user = self.create_user('user_%s@test.com' % randstring(4), + is_staff=False) + assert len(Profile.objects.all()) == 0 + + self.url = reverse('edit_profile') + self.login_as(self.tmp_user) + + def tearDown(self): + self.remove_user(self.tmp_user.username) + + def test_can_render_edit_page(self): + resp = self.client.get(self.url) + self.assertEqual(200, resp.status_code) + self.assertTemplateUsed(resp, 'profile/set_profile.html') + + def test_can_edit(self): + assert email2nickname(self.tmp_user.username) == self.tmp_user.username.split('@')[0] + + resp = self.client.post(self.url, { + 'nickname': 'new nickname' + }) + self.assertEqual(302, resp.status_code) + self.assertRegexpMatches(resp['Location'], r'http://testserver/profile/') + assert email2nickname(self.tmp_user.username) == 'new nickname' diff --git a/tests/seahub/views/init/test_libraries.py b/tests/seahub/views/init/test_libraries.py index 3a67cb966f..0bce62451e 100644 --- a/tests/seahub/views/init/test_libraries.py +++ b/tests/seahub/views/init/test_libraries.py @@ -27,6 +27,8 @@ class LibrariesTest(BaseTestCase): assert UserOptions.objects.is_user_guide_enabled(username) is False def test_pub_repo_creation_config(self): + self.clear_cache() + # user self.login_as(self.user)