Fix SnippetsCmsForm

+ Prevent querying the database from tests too soon.

=> tests pass.
This commit is contained in:
Aurélien Delobelle 2017-10-25 20:27:03 +02:00
parent 94ab754f82
commit a7dbc64e2b
3 changed files with 14 additions and 10 deletions

View file

@ -19,7 +19,6 @@ from .fields import GroupsField, CorePermissionsField
from .middleware import TemporaryAuthMiddleware from .middleware import TemporaryAuthMiddleware
from .models import GenericTeamToken, Group, Permission from .models import GenericTeamToken, Group, Permission
from .utils import get_kfet_generic_user from .utils import get_kfet_generic_user
from .views import GenericLoginView
## ##
@ -261,6 +260,10 @@ class GenericLoginViewTests(TestCase):
patcher_messages.start() patcher_messages.start()
self.addCleanup(patcher_messages.stop) self.addCleanup(patcher_messages.stop)
# Prevent querying the database too soon.
from .views import GenericLoginView
self.view_cls = GenericLoginView
user_acc = Account(trigramme='000') user_acc = Account(trigramme='000')
user_acc.save({'username': 'user'}) user_acc.save({'username': 'user'})
self.user = user_acc.user self.user = user_acc.user
@ -352,14 +355,14 @@ class GenericLoginViewTests(TestCase):
""" """
token = GenericTeamToken.objects.create(token='valid') token = GenericTeamToken.objects.create(token='valid')
self._set_signed_cookie( self._set_signed_cookie(
self.client, GenericLoginView.TOKEN_COOKIE_NAME, 'valid') self.client, self.view_cls.TOKEN_COOKIE_NAME, 'valid')
r = self.client.get(self.url) r = self.client.get(self.url)
self.assertRedirects(r, reverse('kfet.kpsul')) self.assertRedirects(r, reverse('kfet.kpsul'))
self.assertEqual(r.wsgi_request.user, self.generic_user) self.assertEqual(r.wsgi_request.user, self.generic_user)
self._is_cookie_deleted( self._is_cookie_deleted(
self.client, GenericLoginView.TOKEN_COOKIE_NAME) self.client, self.view_cls.TOKEN_COOKIE_NAME)
with self.assertRaises(GenericTeamToken.DoesNotExist): with self.assertRaises(GenericTeamToken.DoesNotExist):
token.refresh_from_db() token.refresh_from_db()
@ -368,14 +371,14 @@ class GenericLoginViewTests(TestCase):
If token is invalid, delete it and try again. If token is invalid, delete it and try again.
""" """
self._set_signed_cookie( self._set_signed_cookie(
self.client, GenericLoginView.TOKEN_COOKIE_NAME, 'invalid') self.client, self.view_cls.TOKEN_COOKIE_NAME, 'invalid')
r = self.client.get(self.url) r = self.client.get(self.url)
self.assertRedirects(r, self.url, fetch_redirect_response=False) self.assertRedirects(r, self.url, fetch_redirect_response=False)
self.assertEqual(r.wsgi_request.user, AnonymousUser()) self.assertEqual(r.wsgi_request.user, AnonymousUser())
self._is_cookie_deleted( self._is_cookie_deleted(
self.client, GenericLoginView.TOKEN_COOKIE_NAME) self.client, self.view_cls.TOKEN_COOKIE_NAME)
def test_flow_ok(self): def test_flow_ok(self):
""" """

View file

@ -58,7 +58,7 @@ class CmsGroupForm(KeepUnselectableModelFormMixin, forms.ModelForm):
class SnippetsCmsGroupForm(KeepUnselectableModelFormMixin, forms.ModelForm): class SnippetsCmsGroupForm(KeepUnselectableModelFormMixin, forms.ModelForm):
permissions = BasePermissionsField( permissions = BasePermissionsField(
label='', label='', required=False,
queryset=Permission.kfetcms.all(), queryset=Permission.kfetcms.all(),
) )

View file

@ -6,14 +6,15 @@ from wagtail.wagtailadmin.forms import (
from wagtail.wagtailcore import hooks from wagtail.wagtailcore import hooks
from wagtail.wagtailcore.models import Collection, Page from wagtail.wagtailcore.models import Collection, Page
from .forms import (
CmsGroupForm, SnippetsCmsGroupForm, prepare_page_permissions_formset,
prepare_collection_member_permissions_formset,
)
from .utils import get_kfet_root_collection, get_kfet_root_page from .utils import get_kfet_root_collection, get_kfet_root_page
def get_kfetcms_group_formview_extra(): def get_kfetcms_group_formview_extra():
# Prevents querying the database too soon.
from .forms import (
CmsGroupForm, SnippetsCmsGroupForm, prepare_page_permissions_formset,
prepare_collection_member_permissions_formset,
)
forms = [] forms = []
# Misc cms-related permissions. # Misc cms-related permissions.