1
Fork 0

unittest piwik integration

This commit is contained in:
Jake Howard 2016-10-08 22:44:45 +01:00
parent 3941e1a5cd
commit fba5843050
Signed by: jake
GPG key ID: 57AFB45680EDD477
3 changed files with 20 additions and 9 deletions

View file

@ -6,12 +6,13 @@ from bs4 import BeautifulSoup
class TestClient: class TestClient:
output_path = os.path.realpath('./output') output_path = os.path.realpath('./output')
def get(self, path): def get(self, path, JS=True):
file_path = self.build_path(path) file_path = self.build_path(path)
content = "".join(open(file_path).readlines()) content = "".join(open(file_path).readlines())
if path.endswith('html'): if path.endswith('html'):
content = BeautifulSoup(content, 'html.parser') content = BeautifulSoup(content, 'html.parser')
for script in content(["noscript"]): # Remove extra tags if JS:
for script in content(["noscript"]): # Remove noscript tags
script.extract() script.extract()
return content return content
@ -21,11 +22,7 @@ class TestClient:
return os.path.join(self.output_path, path) return os.path.join(self.output_path, path)
def exists(self, path): def exists(self, path):
try: return os.path.exists(self.build_path(path))
with open(self.build_path(path)):
return True
except FileNotFoundError:
return False
class TestCase(unittest.TestCase): class TestCase(unittest.TestCase):

View file

@ -1,6 +1,8 @@
from tests import TestCase from tests import TestCase
from config import settings, DotDictionary from config import settings, DotDictionary
from bs4 import BeautifulSoup from bs4 import BeautifulSoup
from unittest import skipIf
from os import environ
class CorePagesTestCase(TestCase): class CorePagesTestCase(TestCase):
@ -50,6 +52,18 @@ class CorePagesTestCase(TestCase):
self.assertIn(link.attrs['alt'], settings.footer_accounts) self.assertIn(link.attrs['alt'], settings.footer_accounts)
self.assertIn("fa fa-", str(list(link.children)[0])) self.assertIn("fa fa-", str(list(link.children)[0]))
@skipIf(not environ.get('BUILD_PRODUCTION', False), 'Not building production')
def test_has_analytics(self):
content = self.client.get('index.html', False)
piwik_script_tag = content.find('script', id='piwik')
self.assertNotEqual(piwik_script_tag, None)
piwik_script = self.get_children(piwik_script_tag)
self.assertIn('piwik.js', piwik_script)
self.assertIn(str(settings.piwik.site_id), piwik_script)
piwik_img = content.find('noscript', id='piwik').find('img')
self.assertIn(settings.piwik.url, piwik_img.attrs['src'])
self.assertIn(str(settings.piwik.site_id), piwik_img.attrs['src'])
class DotDictionaryTestCase(TestCase): class DotDictionaryTestCase(TestCase):
def setUp(self): def setUp(self):

View file

@ -13,6 +13,6 @@
g.type='text/javascript'; g.async=true; g.defer=true; g.src=u+'piwik.js'; s.parentNode.insertBefore(g,s); g.type='text/javascript'; g.async=true; g.defer=true; g.src=u+'piwik.js'; s.parentNode.insertBefore(g,s);
})(); })();
</script> </script>
<noscript><p><img src="//{{ PIWIK.url }}/piwik.php?idsite={{ PIWIK.site_id }}" style="border:0;" alt="" /></p></noscript> <noscript id="piwik"><p><img src="//{{ PIWIK.url }}/piwik.php?idsite={{ PIWIK.site_id }}" style="border:0;" alt="" /></p></noscript>
<!-- End Piwik Code --> <!-- End Piwik Code -->
{% endif %} {% endif %}