-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathtests.py
77 lines (65 loc) · 2.93 KB
/
tests.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# -*- coding: utf-8 -*-
from allowedsites import AllowedSites
from django.contrib.sites.models import Site
from django.http.request import validate_host
from django.test import TestCase as TestCaseUsingDB
class AllowedSitesTestCase(TestCaseUsingDB):
def setUp(self):
Site.objects.all().delete() # remove default
Site.objects.create(domain='example.com:8080', name='test')
Site.objects.create(domain='example.org:8181', name='test 2')
def test_get_raw_sites_unevaluated(self):
allowed_cls = AllowedSites(defaults=['yay.com'])
with self.assertNumQueries(0):
allowed_cls.get_raw_sites()
def test_get_raw_sites_evaluated(self):
allowed_cls = AllowedSites(defaults=['yay.com'])
with self.assertNumQueries(1):
data = tuple(allowed_cls.get_raw_sites())
self.assertEqual(len(data), 2)
# do it again to demonstrate it uses iterator()
with self.assertNumQueries(1):
data = tuple(allowed_cls.get_raw_sites())
self.assertEqual(len(data), 2)
def test_get_domains(self):
allowed_cls = AllowedSites(defaults=['yay.com'])
with self.assertNumQueries(1):
data = allowed_cls.get_domains()
self.assertEqual(data, frozenset(['example.com', 'example.org']))
def test_iterable(self):
"""
this is what Django does internally in django.http.request
allowed_cls is synonymous with settings.ALLOWED_HOSTS
"""
allowed_cls = AllowedSites(defaults=['yay.com'])
with self.assertNumQueries(1):
self.assertTrue(validate_host('example.com', allowed_cls))
with self.assertNumQueries(1):
self.assertTrue(validate_host('example.org', allowed_cls))
with self.assertNumQueries(1):
self.assertFalse(validate_host('djangoproject.com', allowed_cls))
# ideally this should be 0 queries, because it's a default ...
with self.assertNumQueries(1):
self.assertTrue(validate_host('yay.com', allowed_cls))
def test_length(self):
allowed_cls = AllowedSites(defaults=['yay.com'])
self.assertEqual(len(allowed_cls), 3)
def test_containment(self):
allowed_cls = AllowedSites(defaults=['yay.com'])
self.assertIn('example.org', allowed_cls)
self.assertNotIn('djangoproject.org', allowed_cls)
def test_bool_true(self):
allowed_cls = AllowedSites()
self.assertTrue(allowed_cls)
def test_bool_false(self):
allowed_cls = AllowedSites()
Site.objects.all().delete()
self.assertFalse(allowed_cls)
def test_equality(self):
allowed_cls = AllowedSites()
allowed_cls2 = AllowedSites()
self.assertEqual(allowed_cls, allowed_cls2)
def test_inequality(self):
allowed_cls = AllowedSites()
allowed_cls2 = AllowedSites(defaults='test.com')
self.assertNotEqual(allowed_cls, allowed_cls2)