comparison bio/tests/test_views.py @ 767:a4ae99359286

Added a bio app test for changing your avatar.
author Brian Neal <bgneal@gmail.com>
date Mon, 20 Jan 2014 20:04:25 -0600
parents 22d158ef2217
children 1b1a12abde3b
comparison
equal deleted inserted replaced
766:22d158ef2217 767:a4ae99359286
1 """ 1 """
2 View tests for the bio application. 2 View tests for the bio application.
3 3
4 """ 4 """
5 from io import BytesIO
6 import os
7
5 from django.contrib.auth.models import User 8 from django.contrib.auth.models import User
6 from django.test import TestCase 9 from django.test import TestCase
7 from django.core.urlresolvers import reverse, NoReverseMatch 10 from django.core.urlresolvers import reverse, NoReverseMatch
11
12 from PIL import Image
13
14 from bio.models import UserProfile
8 15
9 16
10 class MemberSearchTest(TestCase): 17 class MemberSearchTest(TestCase):
11 18
12 USERNAME = u'John' 19 USERNAME = u'John'
125 def setUp(self): 132 def setUp(self):
126 u1 = User.objects.create_user('paul', '', 'pw') 133 u1 = User.objects.create_user('paul', '', 'pw')
127 u1.save() 134 u1.save()
128 u2 = User.objects.create_user('eddie', '', 'pw') 135 u2 = User.objects.create_user('eddie', '', 'pw')
129 u2.save() 136 u2.save()
137
138 def tearDown(self):
139 profile = UserProfile.objects.get(user__username='paul')
140 if profile.avatar:
141 os.remove(profile.avatar.path)
130 142
131 def test_anon_my_profile(self): 143 def test_anon_my_profile(self):
132 my_url = reverse('bio-me') 144 my_url = reverse('bio-me')
133 response = self.client.get(my_url) 145 response = self.client.get(my_url)
134 login_url = reverse('accounts-login') + '?next=' + my_url 146 login_url = reverse('accounts-login') + '?next=' + my_url
185 self.assertEqual(u.last_name, 'Johnson') 197 self.assertEqual(u.last_name, 'Johnson')
186 self.assertEqual(u.email, 'pjmoto@example.com') 198 self.assertEqual(u.email, 'pjmoto@example.com')
187 profile = u.get_profile() 199 profile = u.get_profile()
188 self.assertEqual(profile.occupation, 'Surf Guitar Pioneer') 200 self.assertEqual(profile.occupation, 'Surf Guitar Pioneer')
189 self.assertEqual(profile.profile_text, 'Wrote Mr. Moto at age 15.') 201 self.assertEqual(profile.profile_text, 'Wrote Mr. Moto at age 15.')
202
203 def test_avatar_change(self):
204 self.client.login(username='paul', password='pw')
205 url = reverse('bio-change_avatar')
206 response = self.client.get(url)
207 self.assertEqual(response.status_code, 200)
208
209 # Create an in-memory image for a dummy avatar
210 file_obj = BytesIO()
211 image = Image.new('RGBA', size=(100, 100))
212 image.save(file_obj, 'png')
213 file_obj.name = 'avatar.png'
214 file_obj.seek(0)
215
216 response = self.client.post(url, {'avatar_file': file_obj}, follow=True)
217 self.assertRedirects(response, reverse('bio-me'))
218