import os.path

from django.test import TestCase
from django.test.client import Client
from django.core.urlresolvers import reverse
from django.conf import settings

from django.contrib.auth.models import User

from avatar import AVATAR_DEFAULT_URL

try:
    from PIL import Image
    dir(Image) # Placate PyFlakes
except ImportError:
    import Image


class AvatarUploadTests(TestCase):
    
    def setUp(self):
        self.client = Client()
        self.testdatapath = os.path.join(os.path.dirname(__file__), "testdata")
        self.user = User.objects.create_user('test', 'lennon@thebeatles.com', 'testpassword')
        self.user.save()
        self.client.login(username='test', password='testpassword')
        Image.init()

    def testNonImageUpload(self):
        f = open(os.path.join(self.testdatapath, "nonimagefile"), "rb")
        response = self.client.post(reverse('avatar_add'), {
            'avatar': f,
        })
        f.close()
        self.failUnlessEqual(response.status_code, 200)
        self.failIfEqual(response.context['upload_avatar_form'].errors, {})
        
    def testNormalImageUpload(self):
        f = open(os.path.join(self.testdatapath,"test.png"), "rb")
        response = self.client.post(reverse('avatar_add'), {
            'avatar': f,
        })
        f.close()
        self.failUnlessEqual(response.status_code, 200)
        self.failUnlessEqual(response.context['upload_avatar_form'].errors, {})
        
    def testImageWithoutExtension(self):
        f = open(os.path.join(self.testdatapath,"imagefilewithoutext"), "rb")
        response = self.client.post(reverse('avatar_add'), {
            'avatar': f,
        })
        f.close()
        self.failUnlessEqual(response.status_code, 200)
        self.failUnlessEqual(response.context['upload_avatar_form'].errors, {})
        
    def testImageWithWrongExtension(self):
        f = open(os.path.join(self.testdatapath,"imagefilewithwrongext.ogg"), "rb")
        response = self.client.post(reverse('avatar_add'), {
            'avatar': f,
        })
        f.close()
        self.failUnlessEqual(response.status_code, 200)
        self.failUnlessEqual(response.context['upload_avatar_form'].errors, {})
        
    def testImageTooBig(self):
        f = open(os.path.join(self.testdatapath,"testbig.png"), "rb")
        response = self.client.post(reverse('avatar_add'), {
            'avatar': f,
        })
        f.close()
        self.failUnlessEqual(response.status_code, 200)
        self.failIfEqual(response.context['upload_avatar_form'].errors, {})
    
    def testDefaultUrl(self):
        response = self.client.get(reverse('avatar_render_primary', kwargs={
            'user': self.user.username,
            'size': 80,
        }))
        loc = response['Location']
        base_url = getattr(settings, 'STATIC_URL', None)
        if not base_url:
            base_url = settings.MEDIA_URL
        self.assertTrue(base_url in loc)
        self.assertTrue(loc.endswith(AVATAR_DEFAULT_URL))
        
    # def testTooManyAvatars
    # def testReplaceAvatarWhenMaxIsOne
    # def testHashFileName
    # def testHashUserName
    # def testDeleteFile
    # def testChangePrimaryAvatar
    # def testAutomaticThumbnailCreation

@@ -241,16 +241,59 @@
 AULT_URL
+%0Afrom avatar.util import get_primary_avatar
 %0A%0Atry:%0A 
@@ -1480,32 +1480,118 @@
 m'%5D.errors, %7B%7D)%0A
+        avatar = get_primary_avatar(self.user)%0A        self.failIfEqual(avatar, None)%0A
         %0A    def
@@ -3142,16 +3142,140 @@
 LT_URL))
+%0A%0A    def testNonExistingUser(self):%0A        a = get_primary_avatar(%22nonexistinguser%22)%0A        self.failUnlessEqual(a, None)
 %0A       

Test get_primary_avatar behaviour