""" Author: Michel Peltriaux Organization: Struktur- und Genehmigungsdirektion Nord, Rhineland-Palatinate, Germany Contact: ksp-servicestelle@sgdnord.rlp.de Created on: 12.09.23 """ from django.test import RequestFactory from django.urls import reverse from django.utils.translation import gettext_lazy as _ from konova.tests.test_views import BaseTestCase from user.forms.modals.team import NewTeamModalForm, EditTeamModalForm from user.models import Team class NewTeamModalFormTestCase(BaseTestCase): def setUp(self) -> None: super().setUp() self.request = RequestFactory().request() self.request.user = self.user def test_init(self): form = NewTeamModalForm( request=self.request ) self.assertEqual(form.form_title, str(_("Create new team"))) self.assertEqual(form.form_caption, str(_("You will become the administrator for this group by default. You do not need to add yourself to the list of members."))) self.assertEqual(form.action_url, reverse("user:team-new")) self.assertEqual(form.cancel_redirect, reverse("user:team-index")) self.assertEqual(form.request, self.request) self.assertEqual(form.user, self.request.user) def test_is_valid(self): invalid_data = { "name": self.team.name, "description": "Test description", "members": [self.superuser.id,], } form = NewTeamModalForm( invalid_data, request=self.request ) self.assertFalse(form.is_valid()) self.assertTrue(form.has_error("name")) valid_data = invalid_data valid_data["name"] = self.team.name + "_OTHER" form = NewTeamModalForm( invalid_data, request=self.request ) self.assertTrue(form.is_valid()) def test_save(self): valid_data = { "name": self.team.name + "_OTHER", "description": "Test description", "members": [self.superuser.id,], } form = NewTeamModalForm( valid_data, request=self.request ) self.assertTrue(form.is_valid()) obj = form.save() self.assertEqual(obj.name, valid_data["name"]) self.assertEqual(obj.description, valid_data["description"]) users = obj.users.all() admins = obj.admins.all() self.assertIn(self.request.user, users) self.assertIn(self.request.user, admins) self.assertIn(self.superuser, users) self.assertNotIn(self.superuser, admins) class EditTeamModalFormTestCase(NewTeamModalFormTestCase): def test_init(self): self.team.admins.add(self.superuser) form = EditTeamModalForm(request=self.request, instance=self.team) self.assertEqual(form.form_title, str(_("Edit team"))) self.assertEqual(form.action_url, reverse("user:team-edit", args=(self.team.id,))) self.assertEqual(form.cancel_redirect, reverse("user:team-index")) self.assertEqual(form.fields["name"].initial, self.team.name) self.assertEqual(form.fields["description"].initial, self.team.description) self.assertEqual(form.fields["members"].initial.count(), 1) self.assertIn(self.superuser, form.fields["members"].initial) self.assertEqual(form.fields["admins"].initial.count(), 1) self.assertIn(self.superuser, form.fields["admins"].initial) def test_is_valid(self): data = { "name": self.team.name, "description": self.team.description, "members": self.team.users.values_list("id", flat=True), "admins": self.team.admins.values_list("id", flat=True), } form = EditTeamModalForm( data, request=self.request, instance=self.team ) # Error 1: Admin not in user list self.team.users.set([self.superuser]) self.team.admins.set([self.user]) self.assertFalse(form.is_valid()) self.assertTrue(form.has_error("admins")) # Error 2: Admin list empty self.team.admins.set([]) self.assertFalse(form.is_valid()) self.assertTrue(form.has_error("admins")) # Error 3: Name taken other_team = Team.objects.create( name=self.team.name ) self.team.admins.set([self.superuser]) self.assertFalse(form.is_valid()) self.assertTrue(form.has_error("name")) def test_save(self): data = { "name": self.team.name + "_EDITED", "description": self.team.description + "_EDITED", "members": [self.user.id, self.superuser.id,], "admins": [self.user.id,], } form = EditTeamModalForm( data, request=self.request, instance=self.team ) self.assertTrue(form.is_valid(), msg=form.errors) obj = form.save() self.assertEqual(obj.name, data["name"]) self.assertEqual(obj.description, data["description"]) self.assertIn(self.user, obj.users.all()) self.assertIn(self.superuser, obj.users.all()) self.assertIn(self.user, obj.admins.all()) self.assertEqual(obj.admins.count(), 1) self.assertEqual(obj.users.count(), 2)