|
@@ -0,0 +1,44 @@
|
|
|
+from django.contrib.auth.models import User
|
|
|
+from django.core.urlresolvers import reverse
|
|
|
+from django.test import TestCase
|
|
|
+
|
|
|
+from .models import Profile
|
|
|
+from .forms import UserForm, ProfileForm
|
|
|
+
|
|
|
+
|
|
|
+class AccountsTests(TestCase):
|
|
|
+ def setUp(self):
|
|
|
+ user = User.objects.create_user('user', email='user@example.net', password='user')
|
|
|
+
|
|
|
+ def test_auth(self):
|
|
|
+ self.assertEquals(self.client.get(reverse('login')).status_code, 200)
|
|
|
+ self.assertEquals(self.client.get(reverse('password_reset')).status_code, 200)
|
|
|
+ self.assertEquals(self.client.get(reverse('password_reset_done')).status_code, 200)
|
|
|
+
|
|
|
+ def test_login_logout(self):
|
|
|
+ self.assertEquals(self.client.get(reverse('login')).status_code, 200)
|
|
|
+ self.client.login(username='user', password='user')
|
|
|
+ self.assertEquals(self.client.get(reverse('home')).status_code, 200)
|
|
|
+ self.assertRedirects(self.client.get(reverse('logout')), reverse('home'), target_status_code=302) # home is redirected to login
|
|
|
+ self.assertRedirects(self.client.get(reverse('home')), reverse('login') + '?next=' + reverse('home'))
|
|
|
+
|
|
|
+ def test_profile(self):
|
|
|
+ response = self.client.get(reverse('profile'))
|
|
|
+ self.assertRedirects(response, reverse('login') + '?next=' + reverse('profile'))
|
|
|
+ self.client.login(username='user', password='user')
|
|
|
+ response = self.client.get(reverse('profile'))
|
|
|
+ self.assertEqual(response.status_code, 200)
|
|
|
+ user = User.objects.get(username='user')
|
|
|
+ user_form = UserForm(None, instance=user)
|
|
|
+ data = {key: getattr(user_form.instance, key) for key in user_form.fields}
|
|
|
+ profile_form = ProfileForm(instance=user.profile)
|
|
|
+ data.update({key: getattr(profile_form.instance, key) for key in profile_form.fields})
|
|
|
+ data['username'] = 'user2' # try to tamper username
|
|
|
+ data['email'] = 'user@example.org'
|
|
|
+ data['address'] = '221B Baker Street'
|
|
|
+ response = self.client.post(reverse('profile'), data)
|
|
|
+ self.assertRedirects(response, reverse('profile'))
|
|
|
+ user = User.objects.get(pk=user.pk) # refresh user
|
|
|
+ self.assertEquals(user.username, 'user') # should not be modified
|
|
|
+ self.assertEquals(user.email, 'user@example.org')
|
|
|
+ self.assertEquals(user.profile.address, '221B Baker Street')
|