From 9710d3b4799f0964b4d66bf26c5320e54ffa8aa3 Mon Sep 17 00:00:00 2001
From: Paul J Stevens
Date: Sat, 26 May 2018 11:45:28 +0200
Subject: [PATCH] post-merge cleanups
---
src/wagtail_personalisation/adapters.py | 4 +-
src/wagtail_personalisation/forms.py | 2 +-
tests/unit/test_utils.py | 67 +++++++------------------
tests/unit/test_views.py | 7 ++-
4 files changed, 23 insertions(+), 57 deletions(-)
diff --git a/src/wagtail_personalisation/adapters.py b/src/wagtail_personalisation/adapters.py
index 7c84f42..f4253cc 100644
--- a/src/wagtail_personalisation/adapters.py
+++ b/src/wagtail_personalisation/adapters.py
@@ -199,11 +199,11 @@ class SessionSegmentsAdapter(BaseSegmentsAdapter):
if result and segment.randomise_into_segment():
if segment.is_static and not segment.is_full:
- if self.request.user.is_authenticated():
+ if self.request.user.is_authenticated:
segment.static_users.add(self.request.user)
additional_segments.append(segment)
elif result:
- if segment.is_static and self.request.user.is_authenticated():
+ if segment.is_static and self.request.user.is_authenticated:
segment.excluded_users.add(self.request.user)
else:
excluded_segments += [segment]
diff --git a/src/wagtail_personalisation/forms.py b/src/wagtail_personalisation/forms.py
index 02555eb..c0fbc08 100644
--- a/src/wagtail_personalisation/forms.py
+++ b/src/wagtail_personalisation/forms.py
@@ -10,7 +10,7 @@ from django.contrib.staticfiles.templatetags.staticfiles import static
from django.test.client import RequestFactory
from django.utils.lru_cache import lru_cache
from django.utils.translation import ugettext_lazy as _
-from wagtail.wagtailadmin.forms import WagtailAdminModelForm
+from wagtail.admin.forms import WagtailAdminModelForm
SessionStore = import_module(settings.SESSION_ENGINE).SessionStore
diff --git a/tests/unit/test_utils.py b/tests/unit/test_utils.py
index f5cee8a..ca1f1d6 100644
--- a/tests/unit/test_utils.py
+++ b/tests/unit/test_utils.py
@@ -1,59 +1,26 @@
+import pytest
+from tests.factories.page import ContentPageFactory
from wagtail_personalisation.utils import (
- exclude_variants, impersonate_other_page)
+ impersonate_other_page)
-class Page(object):
- def __init__(self, path, depth, url_path, title):
- self.path = path
- self.depth = depth
- self.url_path = url_path
- self.title = title
-
- def __eq__(self, other):
- return self.__dict__ == other.__dict__
+@pytest.fixture
+def rootpage():
+ return ContentPageFactory(parent=None, path='/', depth=0, title='root')
-def test_impersonate_other_page():
- page = Page(path="/", depth=0, url_path="/", title="Hoi")
- other_page = Page(path="/other", depth=1, url_path="/other", title="Doei")
-
- impersonate_other_page(page, other_page)
-
- assert page == other_page
+@pytest.fixture
+def page(rootpage):
+ return ContentPageFactory(parent=rootpage, path='/hi', title='Hi')
-class Metadata(object):
- def __init__(self, is_canonical=True):
- self.is_canonical = is_canonical
+@pytest.fixture
+def otherpage(rootpage):
+ return ContentPageFactory(parent=rootpage, path='/bye', title='Bye')
-class PersonalisationMetadataPage(object):
- def __init__(self):
- self.personalisation_metadata = Metadata()
-
-
-def test_exclude_variants_includes_pages_with_no_metadata_property():
- page = PersonalisationMetadataPage()
- del page.personalisation_metadata
- result = exclude_variants([page])
- assert result == [page]
-
-
-def test_exclude_variants_includes_pages_with_metadata_none():
- page = PersonalisationMetadataPage()
- page.personalisation_metadata = None
- result = exclude_variants([page])
- assert result == [page]
-
-
-def test_exclude_variants_includes_pages_with_metadata_canonical():
- page = PersonalisationMetadataPage()
- result = exclude_variants([page])
- assert result == [page]
-
-
-def test_exclude_variants_excludes_pages_with_metadata_not_canonical():
- page = PersonalisationMetadataPage()
- page.personalisation_metadata.is_canonical = False
- result = exclude_variants([page])
- assert result == []
+@pytest.mark.django_db
+def test_impersonate_other_page(page, otherpage):
+ impersonate_other_page(page, otherpage)
+ assert page.title == otherpage.title == 'Bye'
+ assert page.path == otherpage.path
diff --git a/tests/unit/test_views.py b/tests/unit/test_views.py
index b2f3cc2..79d2afc 100644
--- a/tests/unit/test_views.py
+++ b/tests/unit/test_views.py
@@ -1,7 +1,7 @@
import pytest
from django.contrib.auth.models import Permission
-from django.core.urlresolvers import reverse
+from django.urls import reverse
from wagtail_personalisation.models import Segment
from wagtail_personalisation.rules import VisitCountRule
@@ -25,9 +25,8 @@ def test_segment_user_data_view_requires_admin_access(site, client, django_user_
def test_segment_user_data_view(site, client, mocker, django_user_model):
user1 = django_user_model.objects.create(username='first')
user2 = django_user_model.objects.create(username='second')
- admin_user = django_user_model.objects.create(username='admin')
- permission = Permission.objects.get(codename='access_admin')
- admin_user.user_permissions.add(permission)
+ admin_user = django_user_model.objects.create(
+ username='admin', is_superuser=True)
segment = Segment(type=Segment.TYPE_STATIC, count=1)
segment.save()