diff --git a/.coveragerc b/.coveragerc index 701fac8..48875c8 100644 --- a/.coveragerc +++ b/.coveragerc @@ -1,2 +1,3 @@ [run] -source = base +source = apps.base +omit = apps/base/tests/utils.py diff --git a/apps/base/tests/test_urls.py b/apps/base/tests/test_urls.py new file mode 100644 index 0000000..a8839ae --- /dev/null +++ b/apps/base/tests/test_urls.py @@ -0,0 +1,10 @@ +# -*- coding: utf-8 -*- +from __future__ import unicode_literals +from django.test import SimpleTestCase +from django.urls import reverse + + +class UrlsTestCase(SimpleTestCase): + def test_root_reverse(self): + response = self.client.get(reverse('root')) + self.assertEqual(response.status_code, 200) diff --git a/apps/base/tests/test_views.py b/apps/base/tests/test_views.py index 6d42e77..7178768 100644 --- a/apps/base/tests/test_views.py +++ b/apps/base/tests/test_views.py @@ -1,31 +1,84 @@ # -*- coding: utf-8 -*- from __future__ import unicode_literals +import datetime +import re import socket +import unittest from django.test import SimpleTestCase +from unittest import mock + +from ..views import RootView -class DjangoAdminTestCase(SimpleTestCase): +class RootUnitTestCase(unittest.TestCase): + def test_get_context_data(self): + default_color_hex = '47825b' + + test_data_sets = [ + {}, + {'hostname': 'localhost'}, + {'hostname': 'abcde'}, + {'hostname': 'frodo.abcdef.example.com'}, + {'hostname': 'aBc-dEf', 'expected_color_hex': 'abcdef'}, + {'hostname': 'ab34EF12cd56ZZ', 'expected_color_hex': 'ab34ef'}, + {'hostname': 'abcde.example.com', 'expected_color_hex': 'abcdee'}, + {'hostname': 'abcdef', 'kwargs': {'color_hex': '123456'}, 'expected_color_hex': '123456'}, + ] + + real_hostname = socket.gethostname() + + for test_data in test_data_sets: + if 'hostname' in test_data: + hostname = test_data['hostname'] + else: + hostname = real_hostname + + if 'expected_color_hex' in test_data: + expected_color_hex = test_data['expected_color_hex'] + else: + expected_color_hex = default_color_hex + + with mock.patch('socket.gethostname', return_value=hostname): + v = RootView() + if 'kwargs' in test_data: + c = v.get_context_data(**test_data['kwargs']) + else: + c = v.get_context_data() + + self.assertIsInstance(c, dict) + self.assertIn('hostname', c) + self.assertEqual(c['hostname'], hostname) + self.assertIn('color_hex', c) + self.assertTrue(re.match('[0-9a-f]{6}', c['color_hex'])) + self.assertEqual(c['color_hex'], expected_color_hex) + self.assertIn('time', c) + self.assertIsInstance(c['time'], datetime.datetime) + + +class DjangoAdminDjangoTestCase(SimpleTestCase): def test_djangoadmin(self): response = self.client.get('/djangoadmin', follow=True) self.assertContains(response, 'Django administration') -class RootTestCase(SimpleTestCase): - def setUp(self): - super(RootTestCase, self).setUp() - self.response = self.client.get('/') - +class RootDjangoTestCase(SimpleTestCase): def test_root_template(self): - response = self.response + response = self.client.get('/') self.assertTemplateUsed(response, 'base/root.html') def test_root_context(self): - response = self.response + response = self.client.get('/') self.assertIn('hostname', response.context) hostname = socket.gethostname() self.assertEqual(response.context['hostname'], hostname) + self.assertIn('color_hex', response.context) + self.assertTrue(re.match('[0-9a-f]{6}', response.context['color_hex'])) + self.assertIn('time', response.context) + self.assertIsInstance(response.context['time'], datetime.datetime) def test_root_content(self): - response = self.response + response = self.client.get('/') hostname = socket.gethostname().capitalize() self.assertContains(response, hostname) + + diff --git a/apps/base/views.py b/apps/base/views.py index 3b0c7a3..ae9f4cc 100644 --- a/apps/base/views.py +++ b/apps/base/views.py @@ -8,16 +8,23 @@ from django.views import generic class RootView(generic.TemplateView): template_name = 'base/root.html' + _default_color_hex = '47825b' + + def _get_color_hex(self, hostname=None): + color_hex = self._default_color_hex + if hostname: + buf = hostname + buf = re.sub('[-.]', '', buf) + buf = buf[:6].lower() + if re.match('[0-9a-f]{6}', buf): + color_hex = buf + return color_hex def get_context_data(self, **kwargs): if 'hostname' not in kwargs: kwargs['hostname'] = socket.gethostname() if 'color_hex' not in kwargs: - buf = kwargs['hostname'] - buf = re.sub('[-.]', '', buf) - buf = buf[:6].lower() - if re.match('[0-9a-f]{6}', buf): - kwargs['color_hex'] = buf + kwargs['color_hex'] = self._get_color_hex(kwargs['hostname']) if 'time' not in kwargs: kwargs['time'] = timezone.now() return super(RootView, self).get_context_data(**kwargs)