Update tests, invalidate cache after changing avatar

This commit is contained in:
Grant McConnaughey 2016-01-19 10:00:34 -06:00
parent f6f8952510
commit 16abb5d50b
6 changed files with 49 additions and 28 deletions

View file

@ -22,4 +22,4 @@ show_missing = True
precision = 2
[html]
directory = output/html/
directory = htmlcov/

3
.gitignore vendored
View file

@ -8,4 +8,5 @@ dist/
*.egg-info/
avatars
.coverage
docs/_build
docs/_build
htmlcov/

View file

@ -13,7 +13,7 @@ django-avatar
:target: https://travis-ci.org/grantmcconnaughey/django-avatar
.. image:: https://coveralls.io/repos/grantmcconnaughey/django-avatar/badge.svg?branch=master&service=github
:target: https://coveralls.io/github/grantmcconnaughey/django-avatar?branch=master
:target: https://coveralls.io/github/grantmcconnaughey/django-avatar?branch=master
Django-avatar is a reusable application for handling user avatars. It has the
ability to default to Gravatar if no avatar is found for a certain user.

View file

@ -1,8 +1,4 @@
try:
from django.conf.urls import url
except ImportError:
# Django < 1.4
from django.conf.urls.defaults import url
from django.conf.urls import url
from avatar import views

View file

@ -11,7 +11,7 @@ from avatar.forms import PrimaryAvatarForm, DeleteAvatarForm, UploadAvatarForm
from avatar.models import Avatar
from avatar.signals import avatar_updated
from avatar.util import (get_primary_avatar, get_default_avatar_url,
get_user_model, get_user)
get_user_model, get_user, invalidate_cache)
def _get_next(request):
@ -107,6 +107,7 @@ def change(request, extra_context=None, next_override=None,
avatar.primary = True
avatar.save()
updated = True
invalidate_cache(request.user)
messages.success(request, _("Successfully updated your avatar."))
if updated:
avatar_updated.send(sender=Avatar, user=request.user, avatar=avatar)

View file

@ -30,9 +30,9 @@ class AvatarTests(TestCase):
self.site = AdminSite()
Image.init()
def testAdminGetAvatarReturnsDifferentImageTags(self):
self.testNormalImageUpload()
self.testNormalImageUpload()
def test_admin_get_avatar_returns_different_image_tags(self):
self.test_normal_image_upload()
self.test_normal_image_upload()
primary = Avatar.objects.get(primary=True)
old = Avatar.objects.get(primary=False)
@ -42,41 +42,43 @@ class AvatarTests(TestCase):
self.assertNotEqual(primary_link, old_link)
def testNonImageUpload(self):
def test_non_image_upload(self):
response = upload_helper(self, "nonimagefile")
self.assertEqual(response.status_code, 200)
self.assertNotEqual(response.context['upload_avatar_form'].errors, {})
def testNormalImageUpload(self):
def test_normal_image_upload(self):
response = upload_helper(self, "test.png")
self.assertEqual(response.status_code, 200)
self.assertEqual(len(response.redirect_chain), 1)
self.assertEqual(response.context['upload_avatar_form'].errors, {})
avatar = get_primary_avatar(self.user)
self.assertNotEqual(avatar, None)
self.assertIsNotNone(avatar)
self.assertEqual(avatar.user, self.user)
self.assertTrue(avatar.primary)
def testImageWithoutExtension(self):
def test_image_without_wrong_extension(self):
# use with AVATAR_ALLOWED_FILE_EXTS = ('.jpg', '.png')
response = upload_helper(self, "imagefilewithoutext")
self.assertEqual(response.status_code, 200)
self.assertEqual(len(response.redirect_chain), 0) # Redirect only if it worked
self.assertNotEqual(response.context['upload_avatar_form'].errors, {})
def testImageWithWrongExtension(self):
def test_image_with_wrong_extension(self):
# use with AVATAR_ALLOWED_FILE_EXTS = ('.jpg', '.png')
response = upload_helper(self, "imagefilewithwrongext.ogg")
self.assertEqual(response.status_code, 200)
self.assertEqual(len(response.redirect_chain), 0) # Redirect only if it worked
self.assertNotEqual(response.context['upload_avatar_form'].errors, {})
def testImageTooBig(self):
def test_image_too_big(self):
# use with AVATAR_MAX_SIZE = 1024 * 1024
response = upload_helper(self, "testbig.png")
self.assertEqual(response.status_code, 200)
self.assertEqual(len(response.redirect_chain), 0) # Redirect only if it worked
self.assertNotEqual(response.context['upload_avatar_form'].errors, {})
def testDefaultUrl(self):
def test_default_url(self):
response = self.client.get(reverse('avatar_render_primary', kwargs={
'user': self.user.username,
'size': 80,
@ -88,18 +90,18 @@ class AvatarTests(TestCase):
self.assertTrue(base_url in loc)
self.assertTrue(loc.endswith(settings.AVATAR_DEFAULT_URL))
def testNonExistingUser(self):
def test_non_existing_user(self):
a = get_primary_avatar("nonexistinguser")
self.assertEqual(a, None)
def testThereCanBeOnlyOnePrimaryAvatar(self):
def test_there_can_be_only_one_primary_avatar(self):
for i in range(1, 10):
self.testNormalImageUpload()
self.test_normal_image_upload()
count = Avatar.objects.filter(user=self.user, primary=True).count()
self.assertEqual(count, 1)
def testDeleteAvatar(self):
self.testNormalImageUpload()
def test_delete_avatar(self):
self.test_normal_image_upload()
avatar = Avatar.objects.filter(user=self.user)
self.assertEqual(len(avatar), 1)
response = self.client.post(reverse('avatar_delete'), {
@ -110,8 +112,8 @@ class AvatarTests(TestCase):
count = Avatar.objects.filter(user=self.user).count()
self.assertEqual(count, 0)
def testDeletePrimaryAvatarAndNewPrimary(self):
self.testThereCanBeOnlyOnePrimaryAvatar()
def test_delete_primary_avatar_and_new_primary(self):
self.test_there_can_be_only_one_primary_avatar()
primary = get_primary_avatar(self.user)
oid = primary.id
self.client.post(reverse('avatar_delete'), {
@ -123,9 +125,30 @@ class AvatarTests(TestCase):
avatars = Avatar.objects.filter(user=self.user)
self.assertEqual(avatars[0].id, primaries[0].id)
def testTooManyAvatars(self):
def test_change_avatar_get(self):
self.test_normal_image_upload()
response = self.client.get(reverse('avatar_change'))
self.assertEqual(response.status_code, 200)
self.assertIsNotNone(response.context['avatar'])
def test_change_avatar_post_updates_primary_avatar(self):
self.test_there_can_be_only_one_primary_avatar()
old_primary = Avatar.objects.get(user=self.user, primary=True)
choice = Avatar.objects.filter(user=self.user, primary=False)[0]
response = self.client.post(reverse('avatar_change'), {
'choice': choice.pk,
})
self.assertEqual(response.status_code, 302)
new_primary = Avatar.objects.get(user=self.user, primary=True)
self.assertEqual(new_primary.pk, choice.pk)
# Avatar with old primary pk exists but it is not primary anymore
self.assertTrue(Avatar.objects.filter(user=self.user, pk=old_primary.pk, primary=False).exists())
def test_too_many_avatars(self):
for i in range(0, settings.AVATAR_MAX_AVATARS_PER_USER):
self.testNormalImageUpload()
self.test_normal_image_upload()
count_before = Avatar.objects.filter(user=self.user).count()
response = upload_helper(self, "test.png")
count_after = Avatar.objects.filter(user=self.user).count()