diff --git a/gestion/tests.py b/gestion/tests.py index 111c6b0a..ce7fe83b 100644 --- a/gestion/tests.py +++ b/gestion/tests.py @@ -10,10 +10,22 @@ from __future__ import division from __future__ import print_function from __future__ import unicode_literals +from django.db.utils import IntegrityError from django.test import TestCase from gestion.models import Profile, User +def create_profile(username): + """ + In order to create a profile, I need to create a User first. + This is annoying in most situations, as I just want a profile. + """ + u = User.objects.create(username=username, + first_name=username.title(), + last_name=username.title()) + p, = Profile.objects.filter(user__username=username) + return p + class SimpleTest(TestCase): def test_delete_user(self): @@ -31,3 +43,22 @@ class SimpleTest(TestCase): u.save() Profile.objects.get(user__username='foo').delete() self.assertFalse(User.objects.filter(username='foo').exists()) + + def test_create_profiles(self): + from kfet.models import Account + from cof.models import CofProfile + + # test that two accounts can be linked to the same profile. + p = create_profile('foo') + pkfet = Account.objects.create(profile=p, trigramme='PIG') + pcof = CofProfile.objects.create(profile=p) + self.assertEqual(pkfet.profile, pcof.profile) + + # each profile is linked to at most one account of the same type. + p1 = create_profile('bar') + p2 = create_profile('baz') + pkfet1 = Account.objects.create(profile=p1, trigramme='BAR') + pcof2 = CofProfile.objects.create(profile=p2) + pkfet2 = Account.objects.create(profile=p2, trigramme='BAZ') + self.assertRaises(IntegrityError, + CofProfile.objects.create, profile=p2) diff --git a/kfet/models.py b/kfet/models.py index 0a85d076..93486073 100644 --- a/kfet/models.py +++ b/kfet/models.py @@ -150,7 +150,7 @@ class Account(models.Model): perms.add('kfet.perform_negative_operations') return perms, stop_ope - # Surcharge Méthode save() avec gestions de User et CofProfile + # Surcharge Méthode save() avec gestions de User et Profile # Args: # - data : datas pour User et CofProfile # Action: @@ -166,10 +166,10 @@ class Account(models.Model): user.last_name = data.get("last_name", user.last_name) user.email = data.get("email", user.email) user.save() - # Updating CofProfile with data - cof = self.profile - cof.departement = data.get("departement", cof.departement) - cof.save() + # Updating Profile with data + profile = self.profile + profile.departement = data.get("departement", profile.departement) + profile.save() elif data: # New account @@ -192,15 +192,15 @@ class Account(models.Model): if "email" in data: user.email = data['email'] user.save() - # Creating or updating CofProfile instance - (cof, _) = Profile.objects.get_or_create(user=user) + # Creating or updating Profile instance + (profile, _) = Profile.objects.get_or_create(user=user) if "login_clipper" in data: - cof.login_clipper = data['login_clipper'] + profile.login_clipper = data['login_clipper'] if "departement" in data: - cof.departement = data['departement'] - cof.save() + profile.departement = data['departement'] + profile.save() if data: - self.profile = cof + self.profile = profile super(Account, self).save(*args, **kwargs) # Surcharge de delete