gestioCOF/kfet/tests/test_forms.py
Aurélien Delobelle 4ac7b30bdd Fix UserGroupForm + tests for this form.
- Non-K-Fêt group membership is no longer erased by the account edit
  form.
- Add some tests to ensure proposed choices in this form corresponds to
  K-Fêt groups + test case for #161.

Fixes #161
2017-05-12 16:55:18 +02:00

48 lines
1.4 KiB
Python

# -*- coding: utf-8 -*-
from django.test import TestCase
from django.contrib.auth.models import User, Group
from kfet.forms import UserGroupForm
class UserGroupFormTests(TestCase):
"""Test suite for UserGroupForm."""
def setUp(self):
# create user
self.user = User.objects.create(username="foo", password="foo")
# create some K-Fêt groups
prefix_name = "K-Fêt "
names = ["Group 1", "Group 2", "Group 3"]
self.kfet_groups = [
Group.objects.create(name=prefix_name+name)
for name in names
]
# create a non-K-Fêt group
self.other_group = Group.objects.create(name="Other group")
def test_choices(self):
"""Only K-Fêt groups are selectable."""
form = UserGroupForm(instance=self.user)
groups_field = form.fields['groups']
self.assertEqual(len(groups_field.choices), len(self.kfet_groups))
def test_keep_others(self):
"""User stays in its non-K-Fêt groups."""
user = self.user
# add user to a non-K-Fêt group
user.groups.add(self.other_group)
# add user to some K-Fêt groups through UserGroupForm
data = {
'groups': [group.pk for group in self.kfet_groups],
}
form = UserGroupForm(data, instance=user)
form.is_valid()
form.save()
self.assertEqual(len(user.groups.all()), 1+len(self.kfet_groups))