From f42f357fdf20d76bc5468ec227dac66cd0ca3ca8 Mon Sep 17 00:00:00 2001 From: jelle van der Waa Date: Mon, 22 Jan 2018 18:41:39 +0100 Subject: Mirror tests (#78) * mirrors: Move tests to mirrors/tests Move the tests to separate files in mirrors/tests and expand the model tests with tests for the Mirror class. * Add CheckLocation test * mirrors: Add tests for template filters Include tests for the filters used in the mirrors views. * devel: Add tests for template filter in_group Include a test for a simple case of the in_group filter. --- devel/tests/test_templatetags.py | 12 ++++ mirrors/tests.py | 105 ---------------------------------- mirrors/tests/__init__.py | 12 ++++ mirrors/tests/test_mirrorlist.py | 44 ++++++++++++++ mirrors/tests/test_mirrorlocations.py | 20 +++++++ mirrors/tests/test_mirrorstatus.py | 36 ++++++++++++ mirrors/tests/test_mirrorurl.py | 31 ++++++++++ mirrors/tests/test_models.py | 70 +++++++++++++++++++++++ mirrors/tests/test_templatetags.py | 29 ++++++++++ 9 files changed, 254 insertions(+), 105 deletions(-) create mode 100644 devel/tests/test_templatetags.py delete mode 100644 mirrors/tests.py create mode 100644 mirrors/tests/__init__.py create mode 100644 mirrors/tests/test_mirrorlist.py create mode 100644 mirrors/tests/test_mirrorlocations.py create mode 100644 mirrors/tests/test_mirrorstatus.py create mode 100644 mirrors/tests/test_mirrorurl.py create mode 100644 mirrors/tests/test_models.py create mode 100644 mirrors/tests/test_templatetags.py diff --git a/devel/tests/test_templatetags.py b/devel/tests/test_templatetags.py new file mode 100644 index 00000000..3a0f32a2 --- /dev/null +++ b/devel/tests/test_templatetags.py @@ -0,0 +1,12 @@ +from django.contrib.auth.models import User +from django.test import SimpleTestCase + +from devel.templatetags.group import in_group + + +class DevelTemplatetagsTest(SimpleTestCase): + def test_in_group(self): + user = User.objects.create(username="joeuser", first_name="Joe", + last_name="User", email="user1@example.com") + self.assertEqual(in_group(user, 'none'), False) + diff --git a/mirrors/tests.py b/mirrors/tests.py deleted file mode 100644 index 318811ef..00000000 --- a/mirrors/tests.py +++ /dev/null @@ -1,105 +0,0 @@ -import json - -from django.test import TestCase - -from mirrors.models import MirrorUrl, MirrorProtocol, Mirror - -def create_mirror_url(): - mirror = Mirror.objects.create(name='mirror1', admin_email='admin@archlinux.org') - mirror_protocol = MirrorProtocol.objects.create(protocol='http') - mirror_url = MirrorUrl.objects.create(url='https://archlinux.org', protocol=mirror_protocol, - mirror=mirror, country='US') - return mirror_url - -class MirrorUrlTest(TestCase): - def setUp(self): - self.mirror_url = create_mirror_url() - - def testAddressFamilies(self): - self.assertIsNotNone(self.mirror_url.address_families()) - - def testHostname(self): - self.assertEqual(self.mirror_url.hostname, 'archlinux.org') - - def testGetAbsoluteUrl(self): - absolute_url = self.mirror_url.get_absolute_url() - expected = '/mirrors/%s/%d/' % (self.mirror_url.mirror.name, self.mirror_url.pk) - self.assertEqual(absolute_url, expected) - - def test_mirror_overview(self): - response = self.client.get('/mirrors/') - self.assertEqual(response.status_code, 200) - self.assertIn(self.mirror_url.mirror.name, response.content) - - def testClean(self): - # TODO: add test for self.mirror_url.clean() - pass - - def tearDown(self): - self.mirror_url.delete() - -class MirrorStatusTest(TestCase): - def test_status(self): - response = self.client.get('/mirrors/status/') - self.assertEqual(response.status_code, 200) - - def test_json_endpoint(self): - response = self.client.get('/mirrors/status/json/') - self.assertEqual(response.status_code, 200) - data = json.loads(response.content) - self.assertEqual(data['urls'], []) - - mirror_url = create_mirror_url() - - # Verify that the cache works - response = self.client.get('/mirrors/status/json/') - self.assertEqual(response.status_code, 200) - data = json.loads(response.content) - - # Disables the cache_function's cache - with self.settings(CACHES={'default': {'BACKEND': 'django.core.cache.backends.dummy.DummyCache'}}): - response = self.client.get('/mirrors/status/json/') - self.assertEqual(response.status_code, 200) - data = json.loads(response.content) - - self.assertEqual(len(data['urls']), 1) - mirror = data['urls'][0] - self.assertEqual(mirror['url'], mirror_url.url) - - -class MirrorListTest(TestCase): - def setUp(self): - self.mirror_url = create_mirror_url() - - def test_mirrorlist(self): - response = self.client.get('/mirrorlist/') - self.assertEqual(response.status_code, 200) - - def test_mirrorlist(self): - response = self.client.get('/mirrorlist/') - self.assertEqual(response.status_code, 200) - - def test_mirrorlist_all(self): - response = self.client.get('/mirrorlist/all/') - self.assertEqual(response.status_code, 200) - self.assertIn(self.mirror_url.hostname, response.content) - - def test_mirrorlist_all_http(self): - response = self.client.get('/mirrorlist/all/http/') - self.assertEqual(response.status_code, 200) - self.assertIn(self.mirror_url.hostname, response.content) - - def test_mirrorlist_all_https(self): - response = self.client.get('/mirrorlist/all/https/') - self.assertEqual(response.status_code, 404) - # TODO: test 200 case - - def test_mirrorlist_filter(self): - response = self.client.get('/mirrorlist/?country=all&protocol=http&ip_version=4') - self.assertEqual(response.status_code, 200) - self.assertIn(self.mirror_url.hostname, response.content) - - def test_generate(self): - response = self.client.get('/mirrorlist/?country=all&protocol=http&ip_version=4') - self.assertEqual(response.status_code, 200) - self.assertIn(self.mirror_url.hostname, response.content) diff --git a/mirrors/tests/__init__.py b/mirrors/tests/__init__.py new file mode 100644 index 00000000..fb6c10df --- /dev/null +++ b/mirrors/tests/__init__.py @@ -0,0 +1,12 @@ +from mirrors.models import MirrorUrl, MirrorProtocol, Mirror + + +def create_mirror_url(): + mirror = Mirror.objects.create(name='mirror1', + admin_email='admin@archlinux.org') + mirror_protocol = MirrorProtocol.objects.create(protocol='http') + mirror_url = MirrorUrl.objects.create(url='https://archlinux.org', + protocol=mirror_protocol, + mirror=mirror, + country='US') + return mirror_url diff --git a/mirrors/tests/test_mirrorlist.py b/mirrors/tests/test_mirrorlist.py new file mode 100644 index 00000000..b382b834 --- /dev/null +++ b/mirrors/tests/test_mirrorlist.py @@ -0,0 +1,44 @@ +from django.test import TestCase + +from mirrors.tests import create_mirror_url + + +class MirrorListTest(TestCase): + def setUp(self): + self.mirror_url = create_mirror_url() + + def tearDown(self): + self.mirror_url.delete() + + def test_mirrorlist(self): + response = self.client.get('/mirrorlist/') + self.assertEqual(response.status_code, 200) + + def test_mirrorlist(self): + response = self.client.get('/mirrorlist/') + self.assertEqual(response.status_code, 200) + + def test_mirrorlist_all(self): + response = self.client.get('/mirrorlist/all/') + self.assertEqual(response.status_code, 200) + self.assertIn(self.mirror_url.hostname, response.content) + + def test_mirrorlist_all_http(self): + response = self.client.get('/mirrorlist/all/http/') + self.assertEqual(response.status_code, 200) + self.assertIn(self.mirror_url.hostname, response.content) + + def test_mirrorlist_all_https(self): + response = self.client.get('/mirrorlist/all/https/') + self.assertEqual(response.status_code, 404) + # TODO: test 200 case + + def test_mirrorlist_filter(self): + response = self.client.get('/mirrorlist/?country=all&protocol=http&ip_version=4') + self.assertEqual(response.status_code, 200) + self.assertIn(self.mirror_url.hostname, response.content) + + def test_generate(self): + response = self.client.get('/mirrorlist/?country=all&protocol=http&ip_version=4') + self.assertEqual(response.status_code, 200) + self.assertIn(self.mirror_url.hostname, response.content) diff --git a/mirrors/tests/test_mirrorlocations.py b/mirrors/tests/test_mirrorlocations.py new file mode 100644 index 00000000..92751669 --- /dev/null +++ b/mirrors/tests/test_mirrorlocations.py @@ -0,0 +1,20 @@ +import json + +from django.test import TestCase + +from mirrors.models import CheckLocation + + +class MirrorLocationsTest(TestCase): + def setUp(self): + self.checklocation = CheckLocation.objects.create(hostname='arch.org', + source_ip='8.8.8.8', + country='US') + + def test_mirrorlocations_json(self): + response = self.client.get('/mirrors/locations/json/') + self.assertEqual(response.status_code, 200) + data = json.loads(response.content) + self.assertEqual(1, data['version']) + location = data['locations'][0]['country_code'] + self.assertEqual('US', location) diff --git a/mirrors/tests/test_mirrorstatus.py b/mirrors/tests/test_mirrorstatus.py new file mode 100644 index 00000000..ff4d820e --- /dev/null +++ b/mirrors/tests/test_mirrorstatus.py @@ -0,0 +1,36 @@ +import json + +from django.test import TestCase + +from mirrors.tests import create_mirror_url + + +class MirrorStatusTest(TestCase): + def test_status(self): + response = self.client.get('/mirrors/status/') + self.assertEqual(response.status_code, 200) + + def test_json_endpoint(self): + response = self.client.get('/mirrors/status/json/') + self.assertEqual(response.status_code, 200) + data = json.loads(response.content) + self.assertEqual(data['urls'], []) + + mirror_url = create_mirror_url() + + # Verify that the cache works + response = self.client.get('/mirrors/status/json/') + self.assertEqual(response.status_code, 200) + data = json.loads(response.content) + + # Disables the cache_function's cache + with self.settings(CACHES={'default': {'BACKEND': 'django.core.cache.backends.dummy.DummyCache'}}): + response = self.client.get('/mirrors/status/json/') + self.assertEqual(response.status_code, 200) + data = json.loads(response.content) + + self.assertEqual(len(data['urls']), 1) + mirror = data['urls'][0] + self.assertEqual(mirror['url'], mirror_url.url) + + mirror_url.delete() diff --git a/mirrors/tests/test_mirrorurl.py b/mirrors/tests/test_mirrorurl.py new file mode 100644 index 00000000..cd6eb763 --- /dev/null +++ b/mirrors/tests/test_mirrorurl.py @@ -0,0 +1,31 @@ +from django.test import TestCase + +from mirrors.tests import create_mirror_url + + +class MirrorUrlTest(TestCase): + def setUp(self): + self.mirror_url = create_mirror_url() + + def testAddressFamilies(self): + self.assertIsNotNone(self.mirror_url.address_families()) + + def testHostname(self): + self.assertEqual(self.mirror_url.hostname, 'archlinux.org') + + def testGetAbsoluteUrl(self): + absolute_url = self.mirror_url.get_absolute_url() + expected = '/mirrors/%s/%d/' % (self.mirror_url.mirror.name, self.mirror_url.pk) + self.assertEqual(absolute_url, expected) + + def test_mirror_overview(self): + response = self.client.get('/mirrors/') + self.assertEqual(response.status_code, 200) + self.assertIn(self.mirror_url.mirror.name, response.content) + + def testClean(self): + # TODO: add test for self.mirror_url.clean() + pass + + def tearDown(self): + self.mirror_url.delete() diff --git a/mirrors/tests/test_models.py b/mirrors/tests/test_models.py new file mode 100644 index 00000000..1e1eafec --- /dev/null +++ b/mirrors/tests/test_models.py @@ -0,0 +1,70 @@ +from django.test import TestCase + +from mirrors.models import Mirror, CheckLocation +from mirrors.tests import create_mirror_url + + +class MirrorUrlTest(TestCase): + def setUp(self): + self.mirror_url = create_mirror_url() + + def testAddressFamilies(self): + self.assertIsNotNone(self.mirror_url.address_families()) + + def testHostname(self): + self.assertEqual(self.mirror_url.hostname, 'archlinux.org') + + def testGetAbsoluteUrl(self): + absolute_url = self.mirror_url.get_absolute_url() + expected = '/mirrors/%s/%d/' % (self.mirror_url.mirror.name, self.mirror_url.pk) + self.assertEqual(absolute_url, expected) + + def test_mirror_overview(self): + response = self.client.get('/mirrors/') + self.assertEqual(response.status_code, 200) + self.assertIn(self.mirror_url.mirror.name, response.content) + + def testClean(self): + # TODO: add test for self.mirror_url.clean() + pass + + def tearDown(self): + self.mirror_url.delete() + + +class MirrorTest(TestCase): + def setUp(self): + self.mirror = Mirror.objects.create(name='mirror1', + admin_email='admin@archlinux.org') + + def tearDown(self): + self.mirror.delete() + + def test_downstream(self): + self.assertEqual(list(self.mirror.downstream()), []) + + def test_get_absolute_url(self): + absolute_url = self.mirror.get_absolute_url() + expected = '/mirrors/{}/'.format(self.mirror.name) + self.assertEqual(absolute_url, expected) + + def test_get_full_url(self): + self.assertIn(self.mirror.get_absolute_url(), self.mirror.get_full_url()) + self.assertIn('http', self.mirror.get_full_url('http')) + + +class CheckLocationTest(TestCase): + def setUp(self): + self.checkloc = CheckLocation.objects.create(hostname='arch.org', + source_ip='127.0.0.1', + country='US') + + def tearDown(self): + self.checkloc.delete() + + def test_family(self): + # TODO: mock socket.getaddrinfo in CheckLocation.family + self.assertIsInstance(self.checkloc.family, int) + + def test_ip_version(self): + self.assertIsInstance(self.checkloc.ip_version, int) diff --git a/mirrors/tests/test_templatetags.py b/mirrors/tests/test_templatetags.py new file mode 100644 index 00000000..0ee2a34f --- /dev/null +++ b/mirrors/tests/test_templatetags.py @@ -0,0 +1,29 @@ +from datetime import timedelta + +from django.test import SimpleTestCase + +from mirrors.templatetags.mirror_status import duration, hours, floatvalue + + +class MirrorTemplateTagTest(SimpleTestCase): + def test_duration(self): + self.assertEqual(duration(None), u'') + + self.assertEqual(duration(timedelta(hours=5)), '5:00') + self.assertEqual(duration(timedelta(hours=5, seconds=61)), '5:01') + # Microseconds are skipped + self.assertEqual(duration(timedelta(microseconds=9999), ), '0:00') + + def test_hours(self): + self.assertEqual(hours(None), u'') + + self.assertEqual(hours(timedelta(hours=5)), '5 hours') + self.assertEqual(hours(timedelta(hours=1)), '1 hour') + self.assertEqual(hours(timedelta(seconds=60*60)), '1 hour') + + def test_floatvalue(self): + self.assertEqual(floatvalue(None), u'') + + self.assertEqual(floatvalue(123), '123.00') + self.assertEqual(floatvalue(123.1), '123.10') + self.assertEqual(floatvalue(123.1, 1), '123.1') -- cgit v1.2.3-55-g3dc8