Skip to content

Commit

Permalink
tests
Browse files Browse the repository at this point in the history
  • Loading branch information
olivierdalang committed Oct 16, 2018
1 parent 1f3d766 commit c56422e
Showing 1 changed file with 53 additions and 86 deletions.
139 changes: 53 additions & 86 deletions geonode/themes/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,94 +18,61 @@
#
#########################################################################

import os
import re

from geonode.tests.base import GeoNodeBaseTestSupport

from django.contrib.staticfiles import finders
from django.contrib.auth import get_user_model
from django.template.defaultfilters import slugify

from .models import GeoNodeThemeCustomization
from .utils import (find_all_templates,
activate_theme,
deactivate_theme,
theme_css_template,
theme_css_template_regexp,
theme_html_template,
theme_html_template_regexp,)


class ClientLibraryTest(GeoNodeBaseTestSupport):

def setUp(self):
super(GeoNodeBaseTestSupport, self).setUp()
self.test_user = get_user_model().objects.create_user(
"serviceowner", "usermail@fake.mail", "somepassword")
self.geonode_base_css = finders.find('geonode/css/base.css')
self.geonode_base_html = find_all_templates(pattern="geonode_base.html")

def test_theme_customization_uuid(self):
theme = GeoNodeThemeCustomization()
try:
theme.save()
espected_uuid = slugify("theme id %s %s" % (theme.id, theme.date))
self.assertEqual(espected_uuid, theme.theme_uuid)
self.assertEqual(espected_uuid, theme.identifier)
finally:
theme.delete()

def test_theme_activation_deactivation(self):

self.assertTrue(os.path.isfile(self.geonode_base_css))
self.assertTrue(os.path.isfile(self.geonode_base_html[0]))

theme = GeoNodeThemeCustomization()
theme_css = None
try:
theme.save()

activate_theme(theme)
self.assertTrue(theme.is_enabled)

theme_css = os.path.join(os.path.dirname(self.geonode_base_css), "%s.css" % theme.theme_uuid)
self.assertTrue(os.path.isfile(theme_css))

with open(self.geonode_base_css, 'r') as base_css:
value = base_css.read()
theme_regexp = re.compile(theme_css_template_regexp.format(theme.theme_uuid))
self.assertIsNotNone(theme_regexp.search(value))
self.assertTrue(value.startswith(theme_css_template.format(theme.theme_uuid)))

base_css.close()

with open(self.geonode_base_html[0], 'r') as base_html:
value = base_html.read()
theme_regexp = re.compile(theme_html_template_regexp.format(theme.theme_uuid))
self.assertIsNotNone(theme_regexp.search(value))
self.assertTrue(value.startswith(theme_html_template.format(theme.theme_uuid)))

base_html.close()

deactivate_theme(theme)
self.assertFalse(theme.is_enabled)
self.assertIsNotNone(theme_css)
self.assertFalse(os.path.isfile(theme_css))

with open(self.geonode_base_css, 'r') as base_css:
value = base_css.read()
theme_regexp = re.compile(theme_css_template_regexp.format(theme.theme_uuid))
self.assertIsNone(theme_regexp.search(value))
base_css.close()

with open(self.geonode_base_html[0], 'r') as base_html:
value = base_html.read()
theme_regexp = re.compile(theme_html_template_regexp.format(theme.theme_uuid))
self.assertIsNone(theme_regexp.search(value))
base_html.close()
finally:
theme.delete()

self.assertIsNotNone(theme_css)
self.assertFalse(os.path.isfile(theme_css))
class ThemeLibraryTest(GeoNodeBaseTestSupport):

def test_theme_customization(self):
# By default, the homepage should use default welcome text
response = self.client.get(reverse('home'))
self.assertContains(response, "GeoNode is an open source platform for sharing geospatial data and maps.")

# Creating a theme should change the welcome text
theme_1 = GeoNodeThemeCustomization.objects.create(
name='theme_1',
jumbotron_welcome_content='welcome_1',
is_enabled=True,
)
response = self.client.get(reverse('home'))
self.assertNotContains(response, "GeoNode is an open source platform for sharing geospatial data and maps.")
self.assertContains(response, "welcome_1")

# Creating another theme should replace the welcome text
theme_2 = GeoNodeThemeCustomization.objects.create(
name='theme_2',
jumbotron_welcome_content='welcome_2',
is_enabled=True,
)
response = self.client.get(reverse('home'))
self.assertNotContains(response, "welcome_1")
self.assertContains(response, "welcome_2")

# Creating a disabled theme should not replace the welcome text
theme_3 = GeoNodeThemeCustomization.objects.create(
name='theme_3',
jumbotron_welcome_content='welcome_3',
is_enabled=False,
)
response = self.client.get(reverse('home'))
self.assertNotContains(response, "welcome_3")
self.assertContains(response, "welcome_2")

# Enabling that theme afterwards should replace the welcome text
theme_3.is_enabled = True
theme_3.save()
response = self.client.get(reverse('home'))
self.assertNotContains(response, "welcome_2")
self.assertContains(response, "welcome_3")

# We should have only one active theme
active_themes = GeoNodeThemeCustomization.objects.filter(is_enabled=True)
self.assertEqual(active_themes.count(), 1)

# Deleting that theme should revert to default
theme_3.delete()
response = self.client.get(reverse('home'))
self.assertNotContains(response, "welcome_3")
self.assertContains(response, "GeoNode is an open source platform for sharing geospatial data and maps.")

0 comments on commit c56422e

Please sign in to comment.