-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_main.py
138 lines (104 loc) · 4.2 KB
/
test_main.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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
"""Test suite for the whois app."""
import unittest
import mock
from testing.assertions import ResponseAssertions
from testing import helpers
from testing.cptestcase import BaseCherryPyTestCase
import apps.whois.main
class TestWhois(BaseCherryPyTestCase, ResponseAssertions):
"""
Tests for the whois application controller
"""
@classmethod
def setUpClass(cls):
"""Start a faux cherrypy server"""
helpers.start_server(apps.whois.main.Controller)
@classmethod
def tearDownClass(cls):
"""Shut down the faux server"""
helpers.stop_server()
def test_allow(self):
"""Verify the controller's supported HTTP methods"""
response = self.request("/", method="HEAD")
self.assertAllowedMethods(response, ("GET",))
def test_default(self):
"""Make a request with no arguments"""
response = self.request("/")
self.assertEqual(response.code, 200)
@mock.patch("cherrypy.engine.publish")
def test_invalid_address_hostname(self, publish_mock):
"""Request lookup of an invalid hostname"""
def side_effect(*args, **_):
"""Side effects local function"""
if args[0] == "url:internal":
return ["/"]
return True
publish_mock.side_effect = side_effect
response = self.request("/", address="invalid")
self.assertEqual(response.code, 303)
@mock.patch("cherrypy.tools.negotiable.render_html")
@mock.patch("cherrypy.engine.publish")
def test_valid_address_as_hostname(self, publish_mock, render_mock):
"""Request lookup of a valid hostname"""
def side_effect(*args, **_):
"""Side effects local function"""
if args[0] == "ip:facts":
return [{}]
if args[0] in ("cache:get", "urlfetch:get"):
return [None]
return mock.DEFAULT
publish_mock.side_effect = side_effect
self.request("/", address="localhost")
self.assertEqual(
helpers.html_var(render_mock, "ip_address"),
"127.0.0.1"
)
@mock.patch("cherrypy.engine.publish")
def test_invalid_address_as_ip(self, publish_mock):
"""Request lookup of an invalid IP address"""
def side_effect(*args, **_):
"""Overrides to be returned by the mock"""
if args[0] == "url:internal":
return ["/"]
return mock.DEFAULT
publish_mock.side_effect = side_effect
response = self.request("/", address="333.333.333.333")
self.assertEqual(response.code, 303)
@mock.patch("cherrypy.tools.negotiable.render_html")
@mock.patch("cherrypy.engine.publish")
def test_address_as_ip(self, publish_mock, render_mock):
"""Request lookup of a cached IP address"""
cache_fake = {"foo": "bar"}
def side_effect(*args, **_):
"""Overrides to be returned by the mock"""
if args[0] == "cache:get":
return [cache_fake]
if args[0] == "urlfetch:get":
return [None]
return mock.DEFAULT
publish_mock.side_effect = side_effect
self.request("/", address="127.0.0.1")
self.assertEqual(helpers.html_var(render_mock, "whois"), cache_fake)
self.assertEqual(helpers.html_var(render_mock, "ip_facts"), cache_fake)
@mock.patch("cherrypy.tools.negotiable.render_html")
@mock.patch("cherrypy.engine.publish")
def test_address_as_ip_nocache(self, publish_mock, render_mock):
"""Request lookup of an uncached IP address"""
def side_effect(*args, **_):
"""Side effects local function"""
if args[0] == "cache:get":
return [None]
if args[0] == "ip:facts":
return [{"hello": "world"}]
if args[0] == "urlfetch:get":
return [{"foo": "bar"}]
return mock.DEFAULT
publish_mock.side_effect = side_effect
self.request("/", address="127.0.0.1")
print(render_mock.calls)
self.assertEqual(
helpers.html_var(render_mock, "ip_facts"),
{"hello": "world"}
)
if __name__ == "__main__":
unittest.main()