-
Notifications
You must be signed in to change notification settings - Fork 0
/
main_test.py
82 lines (61 loc) · 2.04 KB
/
main_test.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
"""
Test suite for the speak app
"""
import unittest
import mock
from testing.assertions import ResponseAssertions
from testing import helpers
from testing.cptestcase import BaseCherryPyTestCase
import apps.speak.main
class TestSpeak(BaseCherryPyTestCase, ResponseAssertions):
"""
Tests for the speak application controller
"""
@classmethod
def setUpClass(cls):
"""Start a faux cherrypy server"""
helpers.start_server(apps.speak.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", "HEAD", "POST"))
@mock.patch("cherrypy.engine.publish")
def test_muted(self, publish_mock):
"""If the application is muted, responses are returned with 202"""
def side_effect(*args, **_):
"""Side effects local function"""
if args[0] == "speak:can_speak":
return [False]
return mock.DEFAULT
publish_mock.side_effect = side_effect
response = self.request(
"/",
method="POST",
statement="hello"
)
self.assertEqual(response.code, 202)
@mock.patch("cherrypy.engine.publish")
def test_not_muted(self, publish_mock):
"""Valid notifications trigger a speak event"""
def side_effect(*args, **_):
"""Side effects local function"""
if args[0] == "speak:can_speak":
return [True]
return mock.DEFAULT
publish_mock.side_effect = side_effect
response = self.request(
"/",
method="POST",
statement="hello not muted"
)
self.assertEqual(response.code, 204)
self.assertEqual(
publish_mock.call_args[0][1],
"hello not muted"
)
if __name__ == "__main__":
unittest.main()