Generic auto-completion mechanism

This commit is contained in:
Martin Pépin 2019-12-11 22:00:10 +01:00
parent b8cd5f1da5
commit b1d8bb04c4
7 changed files with 235 additions and 122 deletions

View file

@ -15,9 +15,9 @@ from django.test import Client, TestCase, override_settings
from django.urls import reverse
from bda.models import Salle, Tirage
from gestioncof.autocomplete import Clipper
from gestioncof.models import CalendarSubscription, Club, Event, Survey, SurveyAnswer
from gestioncof.tests.testcases import ViewTestCaseMixin
from shared.views.autocomplete import Clipper
from .utils import create_member, create_root, create_user
@ -285,21 +285,19 @@ class RegistrationAutocompleteViewTests(ViewTestCaseMixin, TestCase):
self.mockLDAP([])
def _test(self, query, expected_users, expected_members, expected_clippers):
def _test(self, query, expected_others, expected_members, expected_clippers):
r = self.client.get(self.url, {"q": query})
self.assertEqual(r.status_code, 200)
self.assertQuerysetEqual(
r.context["users"], map(repr, expected_users), ordered=False
r.context["others"], map(repr, expected_others), ordered=False
)
self.assertQuerysetEqual(
r.context["members"],
map(lambda u: repr(u.profile), expected_members),
ordered=False,
r.context["members"], map(repr, expected_members), ordered=False,
)
self.assertCountEqual(
map(str, r.context.get("clippers", [])), map(str, expected_clippers)
map(str, r.context["clippers"]), map(str, expected_clippers)
)
def test_username(self):
@ -322,7 +320,7 @@ class RegistrationAutocompleteViewTests(ViewTestCaseMixin, TestCase):
mock_ldap.search.assert_called_once_with(
"dc=spi,dc=ens,dc=fr",
"(&(|(cn=*aa*)(uid=*aa*))(|(cn=*bb*)(uid=*bb*)))",
attributes=["uid", "cn"],
attributes=["cn", "uid"],
)
def test_clipper_escaped(self):
@ -333,14 +331,14 @@ class RegistrationAutocompleteViewTests(ViewTestCaseMixin, TestCase):
mock_ldap.search.assert_not_called()
def test_clipper_no_duplicate(self):
self.mockLDAP([("uid", "uu_u1")])
self.mockLDAP([("uid", "abc")])
self._test("uu u1", [self.u1], [], [Clipper("uid", "uu_u1")])
self._test("abc", [self.u1], [], [Clipper("uid", "abc")])
self.u1.profile.login_clipper = "uid"
self.u1.profile.save()
self.u1.username = "uid"
self.u1.save()
self._test("uu u1", [self.u1], [], [])
self._test("abc", [self.u1], [], [])
class HomeViewTests(ViewTestCaseMixin, TestCase):