Theme unit tests

pull/318/head
Piero Toffanin 2017-11-10 11:48:17 -05:00
rodzic ad46350efb
commit b53b48216e
4 zmienionych plików z 121 dodań i 28 usunięć

Wyświetl plik

@ -1,19 +1,16 @@
import logging
import os
from shutil import rmtree
from django.core.exceptions import ValidationError
from django.db.models import signals
from django.db import models
from django.db.models import signals
from django.dispatch import receiver
from imagekit.models import ImageSpecField
from imagekit.processors import ResizeToFit
from pathlib import Path
from shutil import rmtree
from webodm import settings
from .theme import Theme
from .theme import Theme, update_theme_css
logger = logging.getLogger('app.logger')
@ -82,14 +79,6 @@ def setting_pre_save(sender, instance, **kwargs):
@receiver(signals.post_save, sender=Setting, dispatch_uid="setting_post_save")
def setting_post_save(sender, instance, created, **kwargs):
"""
Touch theme.scss to invalidate its cache and force
compressor to regenerate it
"""
update_theme_css()
theme_file = os.path.join('app', 'static', 'app', 'css', 'theme.scss')
try:
Path(theme_file).touch()
logger.info("Touched {}".format(theme_file))
except:
logger.warning("Failed to touch {}".format(theme_file))

Wyświetl plik

@ -1,8 +1,13 @@
import logging
import os
from pathlib import Path
from django.db.models import signals
from django.db import models
from colorfield.fields import ColorField
from django.dispatch import receiver
from webodm import settings
logger = logging.getLogger('app.logger')
@ -37,3 +42,22 @@ class Theme(models.Model):
def __str__(self):
return self.name
@receiver(signals.post_save, sender=Theme, dispatch_uid="theme_post_save")
def theme_post_save(sender, instance, created, **kwargs):
update_theme_css()
def update_theme_css():
"""
Touch theme.scss to invalidate its cache and force
compressor to regenerate it
"""
theme_file = os.path.join('app', 'static', 'app', 'css', 'theme.scss')
try:
Path(theme_file).touch()
logger.info("Regenerate cache for {}".format(theme_file))
except:
logger.warning("Failed to touch {}".format(theme_file))

Wyświetl plik

@ -1,17 +1,14 @@
import os
import time
from django.contrib.auth.models import User, Group
from django.core.exceptions import ValidationError
from django.core.files import File
from django.test import Client
from shutil import rmtree
from app.models import Setting
from app.models import Theme
from .classes import BootTestCase
from app.contexts.settings import load as load_settings
from app.models import Setting
from app.models import Theme
from webodm import settings as webodm_settings
from .classes import BootTestCase
class TestSettings(BootTestCase):
@ -49,6 +46,7 @@ class TestSettings(BootTestCase):
# Access smaller logo (should generate a cached copy),
# and check that's been created
print("Access: " + settings.app_logo_favicon.url)
time.sleep(0.5)
favicon_path = os.path.join(webodm_settings.MEDIA_ROOT, settings.app_logo_favicon.name)
self.assertTrue(os.path.exists(favicon_path), "Favicon logo exists")
@ -70,13 +68,14 @@ class TestSettings(BootTestCase):
self.assertFalse(os.path.exists(favicon_path), "Favicon logo has been removed")
# Resized images have not been created yet
self.assertFalse(os.path.exists(os.path.join(webodm_settings.MEDIA_ROOT, settings.app_logo_36.name)), "Resized logo does not exist")
logo_36_path = os.path.join(webodm_settings.MEDIA_ROOT, settings.app_logo_36.name)
self.assertFalse(os.path.exists(logo_36_path), "Resized logo does not exist")
# When we access its URL, it gets created (lazy)
print("Access: " + settings.app_logo_36.url)
self.assertTrue(os.path.exists(os.path.join(webodm_settings.MEDIA_ROOT, settings.app_logo_36.name)), "Resized logo does not exist")
c = Client()
time.sleep(0.5)
self.assertTrue(os.path.exists(logo_36_path), "Resized logo exists")

Wyświetl plik

@ -0,0 +1,81 @@
import os
import re
from django.contrib.staticfiles import finders
from django.test import Client
from .classes import BootTestCase
from app.contexts.settings import load as load_settings
class TestSettings(BootTestCase):
def setUp(self):
pass
def tearDown(self):
pass
def test_settings(self):
c = Client()
c.login(username="testuser", password="test1234")
# Get a page
res = c.get('/dashboard/', follow=True)
body = res.content.decode("utf-8")
# There shouldn't be a footer by default
self.assertFalse("<footer>" in body)
# Find the theme.scss file
matches = re.search(r'/static/(CACHE/css/theme\.[\w\d]+\.css)', body)
self.assertTrue(matches is not None, "Found theme.css")
# We can find it in the file system
css_file = finders.find(matches[1])
self.assertTrue(os.path.exists(css_file), "theme.css exists in file system")
css_content = ""
with open(css_file, "r") as f:
css_content = f.read()
# A strong purple color is not part of the default theme
purple = "8400ff"
self.assertFalse(purple in css_content)
# But colors from the theme are
theme = load_settings()["SETTINGS"].theme
self.assertTrue(theme.primary in css_content)
# Let's change the theme
theme.primary = purple # add color
theme.html_footer = "<p>hello</p>"
theme.save()
# A new cache file should have been created for the CSS
# Get a page
res = c.get('/dashboard/', follow=True)
body = res.content.decode("utf-8")
# We now have a footer
self.assertTrue("<footer><p>hello</p></footer>" in body)
# Find the theme.scss file
matches = re.search(r'/static/(CACHE/css/theme\.[\w\d]+\.css)', body)
self.assertTrue(matches is not None, "Found theme.css")
new_css_file = finders.find(matches[1])
self.assertTrue(os.path.exists(new_css_file), "new theme.css exists in file system")
# It's not the same file
self.assertTrue(new_css_file != css_file, "It's a new file")
# Purple color is in there
css_content = ""
with open(new_css_file, "r") as f:
css_content = f.read()
self.assertTrue(purple in css_content)