For 2 of my models, Users and Groups, I have a view to delete objects. The code is almost the same for each of them, it works in the application but unit test have different results: it works for users, not for groups.
Thanks in advance for your advises.
Here are the unit tests:
class TestAdmUsers(TestCase):
def setUp(self):
self.company = create_dummy_company("Société de test")
self.user_staff = create_dummy_user(self.company, "staff", admin=True)
self.usr11 = create_dummy_user(self.company, "user11")
self.usr12 = create_dummy_user(self.company, "user12", admin=True)
self.usr13 = create_dummy_user(self.company, "user13")
self.client.force_login(self.user_staff.user)
def test_adm_delete_user(self):
test_usercomp_id = self.usr13.id
usrcomp = UserComp.objects.get(user__username="user13")
self.assertEqual(usrcomp.id, test_usercomp_id)
url = reverse("polls:adm_delete_user", args=[self.company.comp_slug, test_usercomp_id])
response = self.client.get(url)
self.assertEqual(response.status_code, 302)
with self.assertRaises(User.DoesNotExist):
User.objects.get(id=test_usercomp_id)
with self.assertRaises(UserComp.DoesNotExist):
UserComp.objects.get(id=test_usercomp_id)
class TestAdmGroups(TestCase):
def setUp(self):
self.company = create_dummy_company("Société de test")
self.user_staff = create_dummy_user(self.company, "staff", admin=True)
self.usr11 = create_dummy_user(self.company, "user11")
self.usr12 = create_dummy_user(self.company, "user12", admin=True)
self.usr13 = create_dummy_user(self.company, "user13")
self.usr14 = create_dummy_user(self.company, "user14")
user_list = [self.usr11.id, self.usr12.id, self.usr13.id, self.usr14.id]
users = UserComp.objects.filter(id__in=user_list)
self.group1 = UserGroup.create_group({
"company": self.company,
"group_name": "Groupe 1",
"weight": 40,
},
user_list=users)
def test_adm_delete_group(self):
test_group_id = self.group1.id
grp = UserGroup.objects.get(group_name="Groupe 1")
self.assertEqual(grp.id, test_group_id)
url = reverse("polls:adm_delete_group", args=[self.company.comp_slug, test_group_id])
response = self.client.get(url)
self.assertEqual(response.status_code, 302)
with self.assertRaises(UserGroup.DoesNotExist):
UserGroup.objects.get(id=test_group_id)
The test runs fine for users, but I have this fali for groups:
Traceback (most recent call last): File "D:\Mes documents\Informatique\Developpement\Votes AG\projet_votes\polls\tests_admin.py", line 354, in test_adm_delete_group UserGroup.objects.get(id=test_group_id) AssertionError: DoesNotExist not raised
Here are related models:
class UserComp(models.Model):
user = models.OneToOneField(User, on_delete=models.CASCADE, verbose_name="Utilisateur")
company = models.ForeignKey(Company, on_delete=models.CASCADE, verbose_name="Société")
phone_regex = RegexValidator(regex=r'^0[0-9]([ .-]?[0-9]{2}){4}$', message=("Format de numéro de téléphone invalide"))
phone_num = models.CharField("numéro de téléphone", validators=[phone_regex], max_length=14, null=True, blank=True)
is_admin = models.BooleanField("administrateur", default=False)
class UserGroup(models.Model):
company = models.ForeignKey(
Company, on_delete=models.CASCADE, verbose_name="société"
)
users = models.ManyToManyField(UserComp, verbose_name="utilisateurs", blank=True)
group_name = models.CharField("nom", max_length=100)
weight = models.IntegerField("poids", default=0)
hidden = models.BooleanField(default=False)
And the views:
@user_passes_test(lambda u: u.is_superuser or (u.id is not None and u.usercomp.is_admin))
def adm_delete_user(request, comp_slug, usr_id):
del_usr = User.objects.get(pk=usr_id)
msg = "Utilisateur {0} {1} supprimé.".\
format(del_usr.last_name, del_usr.first_name)
User.objects.get(pk=usr_id).delete()
messages.success(request, msg)
return redirect("polls:adm_users", comp_slug=comp_slug)
@user_passes_test(lambda u: u.is_superuser or (u.id is not None and u.usercomp.is_admin))
def adm_delete_group(request, comp_slug, grp_id):
del_grp = UserGroup.objects.get(pk=grp_id)
msg = "Groupe {0} supprimé.".format(del_grp.group_name)
UserGroup.objects.get(pk=grp_id).delete()
messages.success(request, msg)
return redirect("polls:adm_groups", comp_slug=comp_slug)
CodePudding user response:
There is no logged in staff on your TestAdmGroups
test class so it most likely does not run the delete, but fails on user_passes_test
(which returns a 302).
So to fix, just log in the staff user in TestAdmGroups
:
class TestAdmGroups(TestCase):
...
self.client.force_login(self.user_staff.user)