OLD | NEW |
1 #!/usr/bin/env python | 1 #!/usr/bin/env python |
2 # Copyright 2013 The Chromium Authors. All rights reserved. | 2 # Copyright 2013 The Chromium Authors. All rights reserved. |
3 # Use of this source code is governed by a BSD-style license that can be | 3 # Use of this source code is governed by a BSD-style license that can be |
4 # found in the LICENSE file. | 4 # found in the LICENSE file. |
5 | 5 |
6 import unittest | 6 import unittest |
7 | 7 |
8 from empty_dir_file_system import EmptyDirFileSystem | 8 from empty_dir_file_system import EmptyDirFileSystem |
9 from fake_fetchers import ConfigureFakeFetchers | 9 from fake_fetchers import ConfigureFakeFetchers |
10 from host_file_system_creator import HostFileSystemCreator | 10 from host_file_system_creator import HostFileSystemCreator |
11 from patch_servlet import PatchServlet | 11 from patch_servlet import PatchServlet |
12 from render_servlet import RenderServlet | 12 from render_servlet import RenderServlet |
13 from server_instance import ServerInstance | 13 from server_instance import ServerInstance |
14 from servlet import Request | 14 from servlet import Request |
15 from test_branch_utility import TestBranchUtility | 15 from test_branch_utility import TestBranchUtility |
16 from test_util import DisableLogging | 16 from test_util import DisableLogging |
17 | 17 |
18 _ALLOWED_HOST = 'https://chrome-apps-doc.appspot.com' | 18 _ALLOWED_HOST = 'https://chrome-apps-doc.appspot.com' |
19 | 19 |
20 class _RenderServletDelegate(RenderServlet.Delegate): | 20 class _RenderServletDelegate(RenderServlet.Delegate): |
21 def CreateServerInstanceForChannel(self, channel): | 21 def CreateServerInstance(self): |
22 return ServerInstance.ForLocal() | 22 return ServerInstance.ForLocal() |
23 | 23 |
24 class _PatchServletDelegate(RenderServlet.Delegate): | 24 class _PatchServletDelegate(RenderServlet.Delegate): |
25 def CreateAppSamplesFileSystem(self, object_store_creator): | 25 def CreateAppSamplesFileSystem(self, object_store_creator): |
26 return EmptyDirFileSystem() | 26 return EmptyDirFileSystem() |
27 | 27 |
28 def CreateBranchUtility(self, object_store_creator): | 28 def CreateBranchUtility(self, object_store_creator): |
29 return TestBranchUtility.CreateWithCannedData() | 29 return TestBranchUtility.CreateWithCannedData() |
30 | 30 |
31 def CreateHostFileSystemCreator(self, object_store_creator): | 31 def CreateHostFileSystemCreator(self, object_store_creator): |
32 return HostFileSystemCreator.ForLocal(object_store_creator) | 32 return HostFileSystemCreator.ForLocal(object_store_creator) |
33 | 33 |
34 class PatchServletTest(unittest.TestCase): | 34 class PatchServletTest(unittest.TestCase): |
35 def setUp(self): | 35 def setUp(self): |
36 ConfigureFakeFetchers() | 36 ConfigureFakeFetchers() |
37 | 37 |
38 def _RenderWithPatch(self, path, issue): | 38 def _RenderWithPatch(self, path, issue): |
39 real_path = '%s/%s' % (issue, path) | 39 path_with_issue = '%s/%s' % (issue, path) |
40 return PatchServlet(Request.ForTest(real_path, host=_ALLOWED_HOST), | 40 return PatchServlet(Request.ForTest(path_with_issue, host=_ALLOWED_HOST), |
41 _PatchServletDelegate()).Get() | 41 _PatchServletDelegate()).Get() |
42 | 42 |
43 def _RenderWithoutPatch(self, path): | 43 def _RenderWithoutPatch(self, path): |
44 return RenderServlet(Request.ForTest(path, host=_ALLOWED_HOST), | 44 return RenderServlet(Request.ForTest(path, host=_ALLOWED_HOST), |
45 _RenderServletDelegate()).Get() | 45 _RenderServletDelegate()).Get() |
46 | 46 |
47 def _RenderAndCheck(self, path, issue, expected_equal): | 47 def _RenderAndCheck(self, path, issue, expected_equal): |
48 patched_response = self._RenderWithPatch(path, issue) | 48 patched_response = self._RenderWithPatch(path, issue) |
49 unpatched_response = self._RenderWithoutPatch(path) | 49 unpatched_response = self._RenderWithoutPatch(path) |
50 patched_response.headers.pop('cache-control', None) | 50 patched_response.headers.pop('cache-control', None) |
(...skipping 10 matching lines...) Expand all Loading... |
61 self.assertNotEqual(patched_content, unpatched_content) | 61 self.assertNotEqual(patched_content, unpatched_content) |
62 | 62 |
63 def _RenderAndAssertEqual(self, path, issue): | 63 def _RenderAndAssertEqual(self, path, issue): |
64 self._RenderAndCheck(path, issue, True) | 64 self._RenderAndCheck(path, issue, True) |
65 | 65 |
66 def _RenderAndAssertNotEqual(self, path, issue): | 66 def _RenderAndAssertNotEqual(self, path, issue): |
67 self._RenderAndCheck(path, issue, False) | 67 self._RenderAndCheck(path, issue, False) |
68 | 68 |
69 @DisableLogging('warning') | 69 @DisableLogging('warning') |
70 def _AssertNotFound(self, path, issue): | 70 def _AssertNotFound(self, path, issue): |
71 self.assertEqual(self._RenderWithPatch(path, issue).status, 404, | 71 response = self._RenderWithPatch(path, issue) |
72 'Path %s with issue %s should have been removed.' % (path, issue)) | 72 self.assertEqual(response.status, 404, |
| 73 'Path %s with issue %s should have been removed for %s.' % ( |
| 74 path, issue, response)) |
73 | 75 |
74 def _AssertOk(self, path, issue): | 76 def _AssertOk(self, path, issue): |
75 response = self._RenderWithPatch(path, issue) | 77 response = self._RenderWithPatch(path, issue) |
76 self.assertEqual(response.status, 200, | 78 self.assertEqual(response.status, 200, |
77 'Failed to render path %s with issue %s.' % (path, issue)) | 79 'Failed to render path %s with issue %s.' % (path, issue)) |
78 self.assertTrue(len(response.content.ToString()) > 0, | 80 self.assertTrue(len(response.content.ToString()) > 0, |
79 'Rendered result for path %s with issue %s should not be empty.' % | 81 'Rendered result for path %s with issue %s should not be empty.' % |
80 (path, issue)) | 82 (path, issue)) |
81 | 83 |
| 84 def _AssertRedirect(self, path, issue, redirect_path): |
| 85 response = self._RenderWithPatch(path, issue) |
| 86 self.assertEqual(302, response.status) |
| 87 self.assertEqual('/_patch/%s/%s' % (issue, redirect_path), |
| 88 response.headers['Location']) |
| 89 |
82 def testRender(self): | 90 def testRender(self): |
83 # '_patch' is not included in paths below because it's stripped by Handler. | 91 # '_patch' is not included in paths below because it's stripped by Handler. |
84 issue = '14096030' | 92 issue = '14096030' |
85 | 93 |
86 # extensions_sidenav.json is modified in the patch. | 94 # extensions_sidenav.json is modified in the patch. |
87 self._RenderAndAssertNotEqual('extensions/index.html', issue) | 95 self._RenderAndAssertNotEqual('extensions/index.html', issue) |
| 96 |
88 # apps_sidenav.json is not patched. | 97 # apps_sidenav.json is not patched. |
89 self._RenderAndAssertEqual('apps/about_apps.html', issue) | 98 self._RenderAndAssertEqual('apps/about_apps.html', issue) |
90 | 99 |
91 # extensions/runtime.html is removed in the patch. | 100 # extensions/runtime.html is removed in the patch, should redirect to the |
92 self._AssertNotFound('extensions/runtime.html', issue) | 101 # apps version. |
| 102 self._AssertRedirect('extensions/runtime.html', issue, |
| 103 'apps/runtime.html') |
| 104 |
93 # apps/runtime.html is not removed. | 105 # apps/runtime.html is not removed. |
94 self._RenderAndAssertEqual('apps/runtime.html', issue) | 106 self._RenderAndAssertEqual('apps/runtime.html', issue) |
95 | 107 |
96 # test_foo.html is added in the patch. | 108 # test_foo.html is added in the patch. |
97 self._AssertOk('extensions/test_foo.html', issue) | 109 self._AssertOk('extensions/test_foo.html', issue) |
98 | 110 |
99 # Invalid issue number results in a 404. | 111 # Invalid issue number results in a 404. |
100 self._AssertNotFound('extensions/index.html', '11111') | 112 self._AssertNotFound('extensions/index.html', '11111') |
101 | 113 |
102 def testXssRedirect(self): | 114 def testXssRedirect(self): |
(...skipping 13 matching lines...) Expand all Loading... |
116 '%s/_patch/12345' % _ALLOWED_HOST)) | 128 '%s/_patch/12345' % _ALLOWED_HOST)) |
117 self.assertTrue(*is_redirect('http://developers.google.com', '12345', | 129 self.assertTrue(*is_redirect('http://developers.google.com', '12345', |
118 '%s/_patch/12345' % _ALLOWED_HOST)) | 130 '%s/_patch/12345' % _ALLOWED_HOST)) |
119 self.assertFalse(*is_redirect('http://chrome-apps-doc.appspot.com', '12345', | 131 self.assertFalse(*is_redirect('http://chrome-apps-doc.appspot.com', '12345', |
120 None)) | 132 None)) |
121 self.assertFalse(*is_redirect('http://some-other-app.appspot.com', '12345', | 133 self.assertFalse(*is_redirect('http://some-other-app.appspot.com', '12345', |
122 None)) | 134 None)) |
123 | 135 |
124 if __name__ == '__main__': | 136 if __name__ == '__main__': |
125 unittest.main() | 137 unittest.main() |
OLD | NEW |