1# Copyright (C) 2013 Google Inc. All rights reserved.
2#
3# Redistribution and use in source and binary forms, with or without
4# modification, are permitted provided that the following conditions are
5# met:
6#
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
12# distribution.
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.
16#
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.
28
29# Usage: PYTHON_PATH=/path/to/appengine_sdk python loghandler_unittest.py.
30
31import dev_appserver
32dev_appserver.fix_sys_path()
33
34import unittest
35import webapp2
36
37from google.appengine.ext import testbed
38
39import main
40
41
42class TestHandlers(unittest.TestCase):
43    def setUp(self):
44        self.testbed = testbed.Testbed()
45        self.testbed.activate()
46        self.testbed.init_datastore_v3_stub()
47        self.testbed.init_memcache_stub()
48
49    def test_update_log(self):
50        request = webapp2.Request.blank('/updatelog')
51        request.method = 'POST'
52        request.POST[main.LOG_PARAM] = 'data to log'
53        request.POST[main.NEW_ENTRY_PARAM] = 'on'
54        request.POST[main.NO_NEEDS_REBASELINE_PARAM] = 'off'
55
56        response = request.get_response(main.app)
57        self.assertEqual(response.status_int, 200)
58        self.assertEqual(response.body, 'Wrote new log entry.')
59
60        response = request.get_response(main.app)
61        self.assertEqual(response.status_int, 200)
62        self.assertEqual(response.body, 'Wrote new log entry.')
63
64        request = webapp2.Request.blank('/updatelog')
65        request.method = 'POST'
66        request.POST[main.LOG_PARAM] = 'data to log'
67        request.POST[main.NEW_ENTRY_PARAM] = 'off'
68        request.POST[main.NO_NEEDS_REBASELINE_PARAM] = 'off'
69
70        response = request.get_response(main.app)
71        self.assertEqual(response.status_int, 200)
72        self.assertEqual(response.body, 'Added to existing log entry.')
73
74        request = webapp2.Request.blank('/updatelog')
75        request.method = 'POST'
76        request.POST[main.LOG_PARAM] = 'x' * 1000000
77        request.POST[main.NEW_ENTRY_PARAM] = 'off'
78        request.POST[main.NO_NEEDS_REBASELINE_PARAM] = 'off'
79
80        response = request.get_response(main.app)
81        self.assertEqual(response.status_int, 200)
82        self.assertEqual(response.body, 'Created new log entry because the previous one exceeded the max length.')
83
84        request = webapp2.Request.blank('/updatelog')
85        request.method = 'POST'
86        request.POST[main.LOG_PARAM] = 'data to log'
87        request.POST[main.NEW_ENTRY_PARAM] = 'off'
88        request.POST[main.NO_NEEDS_REBASELINE_PARAM] = 'on'
89
90        response = request.get_response(main.app)
91        self.assertEqual(response.status_int, 200)
92        self.assertEqual(response.body, 'Wrote new no needs rebaseline log.')
93
94        response = request.get_response(main.app)
95        self.assertEqual(response.status_int, 200)
96        self.assertEqual(response.body, 'Overwrote existing no needs rebaseline log.')
97
98        request = webapp2.Request.blank('/updatelog')
99        request.method = 'POST'
100        request.POST[main.LOG_PARAM] = 'data to log'
101        request.POST[main.NEW_ENTRY_PARAM] = 'off'
102        request.POST[main.NO_NEEDS_REBASELINE_PARAM] = 'off'
103
104        response = request.get_response(main.app)
105        self.assertEqual(response.status_int, 200)
106        self.assertEqual(response.body, 'Previous entry was a no need rebaseline log. Writing a new log.')
107
108    def test_update_log_first_entry_without_new_entry_param(self):
109        request = webapp2.Request.blank('/updatelog')
110        request.method = 'POST'
111        request.POST[main.LOG_PARAM] = 'data to log'
112        request.POST[main.NEW_ENTRY_PARAM] = 'off'
113        request.POST[main.NO_NEEDS_REBASELINE_PARAM] = 'off'
114
115        response = request.get_response(main.app)
116        self.assertEqual(response.status_int, 200)
117        self.assertEqual(response.body, 'Wrote new log entry.')
118
119    def test_update_log_first_entry_no_needs_rebaseline_param(self):
120        request = webapp2.Request.blank('/updatelog')
121        request.method = 'POST'
122        request.POST[main.LOG_PARAM] = 'data to log'
123        request.POST[main.NEW_ENTRY_PARAM] = 'off'
124        request.POST[main.NO_NEEDS_REBASELINE_PARAM] = 'on'
125
126        response = request.get_response(main.app)
127        self.assertEqual(response.status_int, 200)
128        self.assertEqual(response.body, 'Wrote new log entry.')
129
130
131if __name__ == '__main__':
132    unittest.main()
133