Skip to content

Commit

Permalink
Unit Test and named urls
Browse files Browse the repository at this point in the history
  • Loading branch information
silviolleite committed Aug 14, 2018
1 parent 447bf17 commit 376f6e4
Show file tree
Hide file tree
Showing 7 changed files with 158 additions and 2 deletions.
4 changes: 2 additions & 2 deletions pwa/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@

# Serve up serviceworker.js and manifest.json at the root
urlpatterns = [
url('^serviceworker.js$', service_worker),
url('^manifest.json$', manifest)
url('^serviceworker.js$', service_worker, name='serviceworker'),
url('^manifest.json$', manifest, name='manifest')
]
14 changes: 14 additions & 0 deletions runtests.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import os
import sys

import django
from django.conf import settings
from django.test.utils import get_runner

if __name__ == "__main__":
os.environ['DJANGO_SETTINGS_MODULE'] = 'tests.settings'
django.setup()
TestRunner = get_runner(settings)
test_runner = TestRunner()
failures = test_runner.run_tests(["tests"])
sys.exit(bool(failures))
Empty file added tests/__init__.py
Empty file.
36 changes: 36 additions & 0 deletions tests/settings.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@

SECRET_KEY = 'fake-key'
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
"pwa",
]

ROOT_URLCONF = 'pwa.urls'

TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [],
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
'django.template.context_processors.debug',
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
],
},
},
]

DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': 'mydatabase',
}
}
21 changes: 21 additions & 0 deletions tests/test_settings_attr.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
from django.test import TestCase
from pwa import app_settings


class AppSettingsTest(TestCase):
def test_has_defined(self):
"""Must have the attributes defined in app_settings.py"""
attributes = [
'PWA_SERVICE_WORKER_PATH',
'PWA_APP_NAME',
'PWA_APP_DESCRIPTION',
'PWA_APP_ROOT_URL',
'PWA_APP_THEME_COLOR',
'PWA_APP_BACKGROUND_COLOR',
'PWA_APP_DISPLAY',
'PWA_APP_START_URL',
'PWA_APP_ICONS'
]
for attr in attributes:
with self.subTest():
self.assertTrue(hasattr(app_settings, attr))
45 changes: 45 additions & 0 deletions tests/test_template_tag_meta.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
from django.test import TestCase
from django.template import Context, Template


class CreateMetaTemplateTagTest(TestCase):
def setUp(self):
context = Context({})
template_to_render = Template(
'{% load pwa %}'
'{% progressive_web_app_meta %}'
)
self.rendered_template = template_to_render.render(context)

def test_has_tags(self):
"""Must contains the tags in HTML"""
tags = [
'<link rel="apple-touch-icon" href="/" sizes="160x160">',
'<link rel="manifest" href="/manifest.json">',
'<meta name="theme-color" content="#000">',
'<meta name="apple-mobile-web-app-capable" content="yes">',
'<meta name="apple-mobile-web-app-title" content="MyApp">',
'<meta name="apple-mobile-web-app-status-bar-style" content="default">'
]
for text in tags:
with self.subTest():
self.assertInHTML(text, self.rendered_template)

def test_has_serviceworker(self):
"""Must have the script tag with serviceworker registration"""
contents = [
'<script type="text/javascript">',
"if ('serviceWorker' in navigator) {",
"navigator.serviceWorker.register('/serviceworker.js', {",
"scope: '.'",
"}).then(function (registration) {",
"console.log('django-progressive-web-app: ServiceWorker registration successful with scope: ', registration.scope);",
"}, function (err) {",
"console.log('django-progressive-web-app: ServiceWorker registration failed: ', err);",
"});",
"</script>"
]

for expected in contents:
with self.subTest():
self.assertIn(expected, self.rendered_template)
40 changes: 40 additions & 0 deletions tests/test_view.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
from django.test import TestCase
from django.shortcuts import resolve_url as r


class ServiceWorkerTest(TestCase):
def setUp(self):
self.response = self.client.get(r('serviceworker'))

def test_get(self):
"""GET /serviceworker.js Should return status code 200"""
self.assertEqual(200, self.response.status_code)


class ManifestTest(TestCase):
def setUp(self):
self.response = self.client.get(r('manifest'), format='json')

def test_get(self):
"""GET /manifest.json Should return status code 200"""
self.assertEqual(self.response.status_code, 200)

def test_template(self):
"""Must have the template manifest.json"""
self.assertTemplateUsed(self.response, 'manifest.json')

def test_manifest_contains(self):
"""Must be the attributes to manitesf.json"""
contents = [
'"name":',
'"short_name":',
'"description":',
'"start_url":',
'"display":',
'"background_color":',
'"theme_color":',
'"icons":',
]
for expected in contents:
with self.subTest():
self.assertContains(self.response, expected)

0 comments on commit 376f6e4

Please sign in to comment.