2016-06-11 15:28:39 +00:00
|
|
|
from django.contrib.auth.models import User
|
2016-06-12 19:09:52 +00:00
|
|
|
from django.contrib.sites.models import Site
|
2016-06-11 15:28:39 +00:00
|
|
|
from django.core.urlresolvers import reverse
|
2016-06-12 14:40:23 +00:00
|
|
|
from django.test import TestCase
|
2016-06-07 20:59:13 +00:00
|
|
|
|
2016-06-19 23:00:08 +00:00
|
|
|
from .models import Participation, Profile
|
2016-06-11 15:28:39 +00:00
|
|
|
|
|
|
|
ROOT_URL = 'accounts'
|
|
|
|
|
|
|
|
|
|
|
|
class AccountTests(TestCase):
|
|
|
|
def setUp(self):
|
2016-06-27 23:04:45 +00:00
|
|
|
a, b, c = (User.objects.create_user(guy, email='%s@example.org' % guy, password=guy) for guy in 'abc')
|
|
|
|
Participation.objects.create(user=a, site=Site.objects.first())
|
2016-07-09 22:23:01 +00:00
|
|
|
Participation.objects.create(user=b, site=Site.objects.first())
|
2016-06-27 23:04:45 +00:00
|
|
|
Participation.objects.create(user=c, site=Site.objects.first(), orga=True)
|
2016-06-11 15:28:39 +00:00
|
|
|
|
2016-06-12 19:09:52 +00:00
|
|
|
def test_models(self):
|
2016-06-27 23:04:45 +00:00
|
|
|
self.assertEqual(Profile.objects.count(), 3)
|
|
|
|
self.client.login(username='c', password='c')
|
2016-06-14 19:39:04 +00:00
|
|
|
for model in [Profile, Participation]:
|
2016-06-12 19:09:52 +00:00
|
|
|
item = model.objects.first()
|
2016-06-25 11:39:28 +00:00
|
|
|
self.assertEqual(self.client.get(item.full_link()).status_code, 200)
|
2016-06-12 19:09:52 +00:00
|
|
|
self.assertTrue(str(item))
|
2016-06-11 15:28:39 +00:00
|
|
|
|
2016-06-12 19:09:52 +00:00
|
|
|
def test_views(self):
|
2016-06-11 15:28:39 +00:00
|
|
|
# User b wants to update its username, email and biography
|
|
|
|
user = User.objects.get(username='b')
|
|
|
|
self.assertEqual(user.email, 'b@example.org')
|
2016-06-12 15:11:11 +00:00
|
|
|
self.assertEqual(user.profile.biography, '')
|
2016-06-11 15:28:39 +00:00
|
|
|
|
|
|
|
self.client.login(username='b', password='b')
|
|
|
|
|
|
|
|
# He tries with an invalid address
|
2016-07-11 19:33:34 +00:00
|
|
|
self.client.post(reverse('profile'), {'email': 'bnewdomain.com', 'username': 'z', 'biography': 'tester',
|
|
|
|
'video_licence': 1})
|
2016-06-11 15:28:39 +00:00
|
|
|
self.assertEqual(User.objects.filter(username='z').count(), 0)
|
|
|
|
|
2016-07-11 19:33:34 +00:00
|
|
|
self.client.post(reverse('profile'), {'email': 'b@newdomain.com', 'username': 'z', 'biography': 'tester',
|
|
|
|
'video_licence': 1})
|
2016-06-11 15:28:39 +00:00
|
|
|
|
|
|
|
user = User.objects.get(username='z')
|
|
|
|
self.assertEqual(user.email, 'b@newdomain.com')
|
2016-06-12 15:11:11 +00:00
|
|
|
self.assertEqual(user.profile.biography, 'tester')
|
2016-06-12 20:19:01 +00:00
|
|
|
self.client.logout()
|
2016-06-19 23:00:08 +00:00
|
|
|
|
|
|
|
def test_participant_views(self):
|
2016-06-29 23:02:00 +00:00
|
|
|
self.assertEqual(self.client.get(reverse('register')).status_code, 200)
|
2016-06-19 23:00:08 +00:00
|
|
|
self.client.login(username='b', password='b')
|
2016-07-09 22:23:01 +00:00
|
|
|
self.assertEqual(self.client.get(reverse('list-participant')).status_code, 403)
|
|
|
|
self.assertEqual(self.client.post(reverse('edit-participant', kwargs={'username': 'a'}),
|
|
|
|
{'biography': 'foo', 'notes': 'bar'}).status_code, 403)
|
2016-06-19 23:00:08 +00:00
|
|
|
b = User.objects.get(username='b')
|
|
|
|
b.is_superuser = True
|
|
|
|
b.save()
|
2016-06-27 23:04:45 +00:00
|
|
|
self.assertEqual(self.client.get(reverse('list-participant')).status_code, 200)
|
2016-07-09 19:30:49 +00:00
|
|
|
b = Participation.objects.get(user=b)
|
2016-07-09 22:23:01 +00:00
|
|
|
b.is_superuser = False
|
2016-06-29 23:02:00 +00:00
|
|
|
b.orga = True
|
|
|
|
b.save()
|
|
|
|
self.assertEqual(self.client.post(reverse('edit-participant', kwargs={'username': 'a'}),
|
|
|
|
{'biography': 'foo', 'nootes': 'bar'}).status_code, 200)
|
|
|
|
self.assertEqual(User.objects.get(username='a').profile.biography, '')
|
|
|
|
self.assertEqual(self.client.post(reverse('edit-participant', kwargs={'username': 'a'}),
|
2016-07-04 23:16:33 +00:00
|
|
|
{'biography': 'foo', 'notes': 'bar', 'first_name': 'Jules', 'username': 'a',
|
|
|
|
'last_name': 'César', 'email': 'a@example.org', 'transport': 1,
|
2016-07-09 22:42:40 +00:00
|
|
|
'connector': 1, 'video_licence': 2, 'constraints': 'nope', 'orga': 0,
|
2016-07-04 23:16:33 +00:00
|
|
|
}).status_code, 200)
|
2016-06-29 23:02:00 +00:00
|
|
|
self.assertEqual(User.objects.get(username='a').profile.biography, 'foo')
|
2016-07-09 22:42:40 +00:00
|
|
|
self.assertEqual(Participation.objects.get(user=User.objects.get(username='a')).video_licence, 2)
|