1 # Copyright (C) 2013 Google Inc. All rights reserved.
3 # Redistribution and use in source and binary forms, with or without
4 # modification, are permitted provided that the following conditions are
7 # * Redistributions of source code must retain the above copyright
8 # notice, this list of conditions and the following disclaimer.
9 # * Redistributions in binary form must reproduce the above
10 # copyright notice, this list of conditions and the following disclaimer
11 # in the documentation and/or other materials provided with the
13 # * Neither the name of Google Inc. nor the names of its
14 # contributors may be used to endorse or promote products derived from
15 # this software without specific prior written permission.
17 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18 # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19 # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
20 # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
21 # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22 # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23 # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27 # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 # Usage: PYTHONPATH=/path/to/appengine_sdk python main_unittest.py.
32 dev_appserver
.fix_sys_path()
38 from google
.appengine
.ext
import testbed
43 class TestHandlers(unittest
.TestCase
):
45 self
.testbed
= testbed
.Testbed()
46 self
.testbed
.activate()
47 self
.testbed
.init_datastore_v3_stub()
48 self
.testbed
.init_memcache_stub()
49 self
.testbed
.init_mail_stub()
51 def _new_request(self
, data
, command
=None):
52 request
= webapp2
.Request
.blank('/updatelog')
53 request
.method
= 'POST'
54 request
.POST
[main
.LOG_PARAM
] = data
55 request
.POST
[main
.NEW_ENTRY_PARAM
] = 'off'
56 if command
is not None:
57 request
.POST
[main
.COMMAND_PARAM
] = json
.dumps(command
)
60 def _assert_response(self
, response
, body
):
61 self
.assertEqual(response
.status_int
, 200)
62 self
.assertEqual(response
.body
, body
)
64 def test_update_log(self
):
65 request
= self
._new
_request
('data to log')
66 request
.POST
[main
.NEW_ENTRY_PARAM
] = 'on'
67 self
._assert
_response
(request
.get_response(main
.app
), 'Wrote new log entry.')
69 self
._assert
_response
(request
.get_response(main
.app
), 'Wrote new log entry.')
71 request
= self
._new
_request
('data to log')
72 self
._assert
_response
(request
.get_response(main
.app
), 'Added to existing log entry.')
74 request
= self
._new
_request
('x' * 1000000)
75 self
._assert
_response
(request
.get_response(main
.app
), 'Created new log entry because the previous one exceeded the max length.')
77 request
= self
._new
_request
('data to log')
78 self
._assert
_response
(request
.get_response(main
.app
), 'Created new log entry because the previous one exceeded the max length.')
80 request
= self
._new
_request
('data to log')
81 self
._assert
_response
(request
.get_response(main
.app
), 'Added to existing log entry.')
83 def test_command_execution(self
):
84 request
= self
._new
_request
('data to log', {})
85 self
._assert
_response
(request
.get_response(main
.app
), 'Wrote new log entry.\nERROR: No command data')
87 request
= self
._new
_request
('data to log', {"foo": "bar"})
88 self
._assert
_response
(request
.get_response(main
.app
), 'Added to existing log entry.\nCommand is missing a name: {"foo": "bar"}')
90 request
= self
._new
_request
('data to log', {
92 'to': 'foo@example.com',
95 self
._assert
_response
(request
.get_response(main
.app
), 'Added to existing log entry.\nMalformed command JSON')
97 request
= self
._new
_request
('data to log', {
99 'to': 'foo@example.com',
100 'subject': 'Subject',
103 self
._assert
_response
(request
.get_response(main
.app
), 'Added to existing log entry.\nSent mail to foo@example.com (Subject)')
105 def test_update_log_first_entry_without_new_entry_param(self
):
106 request
= webapp2
.Request
.blank('/updatelog')
107 request
.method
= 'POST'
108 request
.POST
[main
.LOG_PARAM
] = 'data to log'
109 request
.POST
[main
.NEW_ENTRY_PARAM
] = 'off'
111 response
= request
.get_response(main
.app
)
112 self
.assertEqual(response
.status_int
, 200)
113 self
.assertEqual(response
.body
, 'Wrote new log entry.')
115 def test_update_log_first_entry_no_needs_rebaseline_param(self
):
116 request
= webapp2
.Request
.blank('/updatelog')
117 request
.method
= 'POST'
118 request
.POST
[main
.LOG_PARAM
] = 'data to log'
119 request
.POST
[main
.NEW_ENTRY_PARAM
] = 'off'
121 response
= request
.get_response(main
.app
)
122 self
.assertEqual(response
.status_int
, 200)
123 self
.assertEqual(response
.body
, 'Wrote new log entry.')
126 if __name__
== '__main__':