Skip to content

Commit

Permalink
fix external_link url validator - rebased (django-cms#173)
Browse files Browse the repository at this point in the history
* fixes django-cms#130

* adapt changelog

* added tests and consistency

* isort
  • Loading branch information
FinalAngel authored Jun 21, 2019
1 parent cf7e49b commit 9a81046
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 15 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ Changelog
* Added file link support
* Allow link requirement to be changed when another
CMS plugin inherits from AbstractLink
* Fixed a bug preventing ``HOSTNAME_PATTERN`` to work


2.4.0 (2019-04-16)
Expand Down
19 changes: 19 additions & 0 deletions djangocms_link/migrations/0015_auto_20190621_0407.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# Generated by Django 2.2.2 on 2019-06-21 04:07
from django.db import migrations, models

import djangocms_link.validators


class Migration(migrations.Migration):

dependencies = [
('djangocms_link', '0014_link_file_link'),
]

operations = [
migrations.AlterField(
model_name='link',
name='external_link',
field=models.CharField(blank=True, help_text='Provide a link to an external source.', max_length=2040, validators=[djangocms_link.validators.IntranetURLValidator(intranet_host_re=None)], verbose_name='External link'),
),
]
4 changes: 2 additions & 2 deletions djangocms_link/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,12 +73,12 @@ class AbstractLink(CMSPlugin):
max_length=255,
)
# re: max_length, see: http://stackoverflow.com/questions/417142/
external_link = models.URLField(
external_link = models.CharField(
verbose_name=_('External link'),
blank=True,
max_length=2040,
validators=url_validators,
help_text=_('Provide a valid URL to an external website.'),
help_text=_('Provide a link to an external source.'),
)
internal_link = models.ForeignKey(
Page,
Expand Down
42 changes: 29 additions & 13 deletions tests/test_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

from cms import __version__
from cms.api import add_plugin, create_page
from cms.models import StaticPlaceholder, Placeholder
from cms.models import Placeholder, StaticPlaceholder

from djangocms_helper.base_test import BaseTestCase

Expand Down Expand Up @@ -44,6 +44,22 @@ def test_link(self):
)
self.assertEqual(plugin.get_link(), 'http://example.com')

plugin = add_plugin(
self.page.placeholders.get(slot='content'),
'LinkPlugin',
'en',
external_link='/en/home/'
)
self.assertEqual(plugin.get_link(), '/en/home/')

plugin = add_plugin(
self.page.placeholders.get(slot='content'),
'LinkPlugin',
'en',
external_link='//SEARCHHOST/?q=some+search+string'
)
self.assertEqual(plugin.get_link(), '//SEARCHHOST/?q=some+search+string')

plugin = add_plugin(
self.page.placeholders.get(slot='content'),
'LinkPlugin',
Expand Down Expand Up @@ -127,48 +143,48 @@ def test_link_for_cms_34(self):
def test_optional_link(self):
AbstractLink.link_is_optional = True

plugin1 = add_plugin(
plugin = add_plugin(
self.page.placeholders.get(slot='content'),
'LinkPlugin',
'en',
)
self.assertIsNone(plugin1.clean())
self.assertIsNone(plugin.clean())

AbstractLink.link_is_optional = False

self.assertEqual(AbstractLink.link_is_optional, False)

plugin2 = add_plugin(
plugin = add_plugin(
self.page.placeholders.get(slot='content'),
'LinkPlugin',
'en',
)

# should throw "Please provide a link." error
with self.assertRaises(ValidationError):
plugin2.clean()
plugin.clean()

@skipUnless(CMS_35, "Test relevant only for CMS>=3.5")
def test_in_placeholders(self):
plugin1 = add_plugin(
plugin = add_plugin(
self.page.placeholders.get(slot='content'),
'LinkPlugin',
'en',
internal_link=self.page,
)
self.assertEqual(plugin1.get_link(), '/en/help/')
self.assertEqual(plugin.get_link(), '/en/help/')

placeholder = Placeholder(slot="generated_placeholder")
placeholder.save()

plugin2 = add_plugin(
plugin = add_plugin(
placeholder,
'LinkPlugin',
'en',
internal_link=self.static_page,
)
# the generated placeholder has no page attached to it, thus:
self.assertEqual(plugin2.get_link(), '//example.com/en/static-help/')
self.assertEqual(plugin.get_link(), '//example.com/en/static-help/')

static_placeholder = StaticPlaceholder.objects.create(
name='content_static',
Expand All @@ -177,19 +193,19 @@ def test_in_placeholders(self):
)
static_placeholder.save()

plugin3 = add_plugin(
plugin_a = add_plugin(
static_placeholder.draft,
'LinkPlugin',
'en',
internal_link=self.page,
)

plugin4 = add_plugin(
plugin_b = add_plugin(
static_placeholder.public,
'LinkPlugin',
'en',
internal_link=self.static_page,
)
# static placeholders will always return the full path
self.assertEqual(plugin3.get_link(), '//example.com/en/help/')
self.assertEqual(plugin4.get_link(), '//example.com/en/static-help/')
self.assertEqual(plugin_a.get_link(), '//example.com/en/help/')
self.assertEqual(plugin_b.get_link(), '//example.com/en/static-help/')

0 comments on commit 9a81046

Please sign in to comment.