Skip to content

Commit

Permalink
Move versioning testcases to its own class
Browse files Browse the repository at this point in the history
  • Loading branch information
jonathan-s committed Feb 25, 2020
1 parent 60be831 commit 0fd3442
Showing 1 changed file with 180 additions and 177 deletions.
357 changes: 180 additions & 177 deletions tests/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -160,112 +160,6 @@ def test_create_alias_view_name(self):
self._publish(alias)
self.assertEqual(alias.name, 'test alias')

@skipUnless(is_versioning_enabled(), 'Test only relevant for versioning')
def test_create_alias_view_name_draft_alias(self):
with self.login_user_context(self.superuser):
response = self.client.post(self.get_create_alias_endpoint(), data={
'plugin': self.plugin.pk,
'category': self.category.pk,
'name': 'test alias',
'language': self.language,
})
self.assertEqual(response.status_code, 200)

alias = Alias.objects.last()
# AliasContent not published
self.assertEqual(alias.name, 'Alias {} (No content)'.format(alias.pk))

@skipUnless(is_versioning_enabled(), 'Test only relevant for versioning')
def test_create_alias_view_creating_version(self):
with self.login_user_context(self.superuser):
response = self.client.post(self.get_create_alias_endpoint(), data={
'plugin': self.plugin.pk,
'category': self.category.pk,
'name': 'test alias',
'language': self.language,
})
self.assertEqual(response.status_code, 200)

alias = Alias.objects.last()
if is_versioning_enabled():
from djangocms_versioning.models import Version
self.assertEqual(Version.objects.filter_by_grouper(alias).count(), 1)

@skipIf(is_versioning_enabled(), 'Test only relevant without versioning enabled')
def test_create_alias_name_unique_per_category_and_language(self):
self._create_alias(
name='test alias',
category=self.category,
)
with self.login_user_context(self.superuser):
response = self.client.post(self.get_create_alias_endpoint(), data={
'plugin': self.plugin.pk,
'category': self.category.pk,
'name': 'test alias',
'language': self.language,
})

self.assertEqual(response.status_code, 200)
self.assertContains(
response,
'Alias with this Name and Category already exists.',
)
self.assertEqual(
AliasContent.objects.filter(
name='test alias',
language=self.language,
alias__category=self.category,
).count(),
1,
)

@skipUnless(is_versioning_enabled(), 'Test only relevant for versioning')
def test_create_alias_name_without_uniqness(self):
alias1 = self._create_alias(
name='test alias',
category=self.category,
published=True,
)
with self.login_user_context(self.superuser):
response = self.client.post(self.get_create_alias_endpoint(), data={
'plugin': self.plugin.pk,
'category': self.category.pk,
'name': 'test alias',
'language': self.language,
})

self.assertEqual(response.status_code, 200)
self.assertContains(
response,
'Alias with this Name and Category already exists.',
)

self._unpublish(alias1)

with self.login_user_context(self.superuser):
response = self.client.post(self.get_create_alias_endpoint(), data={
'plugin': self.plugin.pk,
'category': self.category.pk,
'name': 'test alias',
'language': self.language,
})

self.assertEqual(response.status_code, 200)
self.assertNotContains(
response,
'Alias with this Name and Category already exists.',
)

alias = Alias.objects.last()
self._publish(alias)
qs = AliasContent.objects.filter(
name='test alias',
language=self.language,
alias__category=self.category,
)
self.assertEqual(qs.count(), 1)
self.assertEqual(qs.first(), alias.get_content(self.language))

def test_create_alias_view_post_no_plugin_or_placeholder(self):
with self.login_user_context(self.superuser):
response = self.client.post(self.get_create_alias_endpoint(), data={
Expand Down Expand Up @@ -336,61 +230,6 @@ def test_create_alias_view_post_placeholder(self):
target.get_bound_plugin().body,
)

@skipUnless(is_versioning_enabled(), 'Test only relevant for versioning')
def test_create_alias_view_post_placeholder_from_draft_page(self):
page1 = self._create_page('test alias page')
placeholder = page1.get_placeholders(self.language).get(
slot='content',
)
plugin = add_plugin(
placeholder,
'TextPlugin',
language=self.language,
body='test alias',
)
self._unpublish(page1)
with self.login_user_context(self.superuser):
response = self.client.post(self.get_create_alias_endpoint(), data={
'placeholder': placeholder.pk,
'category': self.category.pk,
'name': 'test alias',
'language': self.language,
})
self.assertEqual(response.status_code, 200)

# Source plugins are kept in original placeholder
plugins = placeholder.get_plugins()
self.assertEqual(plugins.count(), 1)
plugin_in_placeholder = plugins[0].get_bound_plugin()
self.assertEqual(plugin, plugin_in_placeholder)

alias = Alias.objects.first()
self._publish(alias)

source_plugins = placeholder.get_plugins()
alias_plugins = alias.get_placeholder(self.language).get_plugins()

self.assertEqual(alias_plugins.count(), source_plugins.count())
for source, target in zip(source_plugins, alias_plugins):
self.assertEqual(source.plugin_type, target.plugin_type)
self.assertEqual(
source.get_bound_plugin().body,
target.get_bound_plugin().body,
)

@skipUnless(is_versioning_enabled(), 'Test only relevant for versioning')
def test_create_alias_with_replace_plugin_with_versioning_checks(self):
# 403 if you try to edit placeholder of published page
with self.login_user_context(self.superuser):
response = self.client.post(self.get_create_alias_endpoint(), data={
'placeholder': self.placeholder.pk,
'category': self.category.pk,
'name': 'test alias 5',
'language': self.language,
'replace': True,
})
self.assertEqual(response.status_code, 403)

def test_create_alias_view_post_placeholder_replace(self):
placeholder = self.placeholder
if is_versioning_enabled():
Expand Down Expand Up @@ -554,22 +393,6 @@ def test_detach_view(self):
self.assertEqual(plugins[1].get_bound_plugin().body, 'test 2')
self.assertEqual(plugins[2].get_bound_plugin().body, 'test 88')

@skipUnless(is_versioning_enabled(), 'Test only relevant for versioning')
def test_detach_view_with_versioning_checks(self):
# 403 when placeholder from non-draft page
alias = self._create_alias()
plugin = add_plugin(
self.placeholder,
'Alias',
language='en',
alias=alias,
)
with self.login_user_context(self.superuser):
response = self.client.post(
self.get_detach_alias_plugin_endpoint(plugin.pk),
)
self.assertEqual(response.status_code, 403)

def test_list_view(self):
category1 = Category.objects.create(
name='Category 1',
Expand Down Expand Up @@ -844,6 +667,34 @@ def test_view_multilanguage(self):
self.assertNotContains(detail_response, en_plugin.body)
self.assertNotContains(list_response, en_plugin.body)

@skipIf(is_versioning_enabled(), 'Test only relevant without versioning enabled')
def test_create_alias_name_unique_per_category_and_language(self):
self._create_alias(
name='test alias',
category=self.category,
)
with self.login_user_context(self.superuser):
response = self.client.post(self.get_create_alias_endpoint(), data={
'plugin': self.plugin.pk,
'category': self.category.pk,
'name': 'test alias',
'language': self.language,
})

self.assertEqual(response.status_code, 200)
self.assertContains(
response,
'Alias with this Name and Category already exists.',
)
self.assertEqual(
AliasContent.objects.filter(
name='test alias',
language=self.language,
alias__category=self.category,
).count(),
1,
)

def test_set_alias_position_view(self):
alias1 = Alias.objects.create(category=self.category) # 0
alias2 = Alias.objects.create(category=self.category) # 1
Expand Down Expand Up @@ -1566,3 +1417,155 @@ def test_alias_multisite_support(self):
response = self.client.get(site2_page.get_absolute_url())
self.assertContains(response, 'test alias multisite')
self.assertContains(response, 'Another alias plugin')


class AliasViewsUsingVersioningTestCase(BaseAliasPluginTestCase):

@skipUnless(is_versioning_enabled(), 'Test only relevant for versioning')
def test_create_alias_view_name_draft_alias(self):
with self.login_user_context(self.superuser):
response = self.client.post(self.get_create_alias_endpoint(), data={
'plugin': self.plugin.pk,
'category': self.category.pk,
'name': 'test alias',
'language': self.language,
})
self.assertEqual(response.status_code, 200)

alias = Alias.objects.last()
# AliasContent not published
self.assertEqual(alias.name, 'Alias {} (No content)'.format(alias.pk))

@skipUnless(is_versioning_enabled(), 'Test only relevant for versioning')
def test_create_alias_view_creating_version(self):
with self.login_user_context(self.superuser):
response = self.client.post(self.get_create_alias_endpoint(), data={
'plugin': self.plugin.pk,
'category': self.category.pk,
'name': 'test alias',
'language': self.language,
})
self.assertEqual(response.status_code, 200)

alias = Alias.objects.last()
if is_versioning_enabled():
from djangocms_versioning.models import Version
self.assertEqual(Version.objects.filter_by_grouper(alias).count(), 1)

@skipUnless(is_versioning_enabled(), 'Test only relevant for versioning')
def test_create_alias_name_without_uniqness(self):
alias1 = self._create_alias(
name='test alias',
category=self.category,
published=True,
)
with self.login_user_context(self.superuser):
response = self.client.post(self.get_create_alias_endpoint(), data={
'plugin': self.plugin.pk,
'category': self.category.pk,
'name': 'test alias',
'language': self.language,
})

self.assertEqual(response.status_code, 200)
self.assertContains(
response,
'Alias with this Name and Category already exists.',
)

self._unpublish(alias1)

with self.login_user_context(self.superuser):
response = self.client.post(self.get_create_alias_endpoint(), data={
'plugin': self.plugin.pk,
'category': self.category.pk,
'name': 'test alias',
'language': self.language,
})

self.assertEqual(response.status_code, 200)
self.assertNotContains(
response,
'Alias with this Name and Category already exists.',
)

alias = Alias.objects.last()
self._publish(alias)
qs = AliasContent.objects.filter(
name='test alias',
language=self.language,
alias__category=self.category,
)
self.assertEqual(qs.count(), 1)
self.assertEqual(qs.first(), alias.get_content(self.language))

@skipUnless(is_versioning_enabled(), 'Test only relevant for versioning')
def test_create_alias_view_post_placeholder_from_draft_page(self):
page1 = self._create_page('test alias page')
placeholder = page1.get_placeholders(self.language).get(
slot='content',
)
plugin = add_plugin(
placeholder,
'TextPlugin',
language=self.language,
body='test alias',
)
self._unpublish(page1)
with self.login_user_context(self.superuser):
response = self.client.post(self.get_create_alias_endpoint(), data={
'placeholder': placeholder.pk,
'category': self.category.pk,
'name': 'test alias',
'language': self.language,
})
self.assertEqual(response.status_code, 200)

# Source plugins are kept in original placeholder
plugins = placeholder.get_plugins()
self.assertEqual(plugins.count(), 1)
plugin_in_placeholder = plugins[0].get_bound_plugin()
self.assertEqual(plugin, plugin_in_placeholder)

alias = Alias.objects.first()
self._publish(alias)

source_plugins = placeholder.get_plugins()
alias_plugins = alias.get_placeholder(self.language).get_plugins()

self.assertEqual(alias_plugins.count(), source_plugins.count())
for source, target in zip(source_plugins, alias_plugins):
self.assertEqual(source.plugin_type, target.plugin_type)
self.assertEqual(
source.get_bound_plugin().body,
target.get_bound_plugin().body,
)

@skipUnless(is_versioning_enabled(), 'Test only relevant for versioning')
def test_create_alias_with_replace_plugin_with_versioning_checks(self):
# 403 if you try to edit placeholder of published page
with self.login_user_context(self.superuser):
response = self.client.post(self.get_create_alias_endpoint(), data={
'placeholder': self.placeholder.pk,
'category': self.category.pk,
'name': 'test alias 5',
'language': self.language,
'replace': True,
})
self.assertEqual(response.status_code, 403)

@skipUnless(is_versioning_enabled(), 'Test only relevant for versioning')
def test_detach_view_with_versioning_checks(self):
# 403 when placeholder from non-draft page
alias = self._create_alias()
plugin = add_plugin(
self.placeholder,
'Alias',
language='en',
alias=alias,
)
with self.login_user_context(self.superuser):
response = self.client.post(
self.get_detach_alias_plugin_endpoint(plugin.pk),
)
self.assertEqual(response.status_code, 403)

0 comments on commit 0fd3442

Please sign in to comment.