• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

zopefoundation / Zope / 3956162881

pending completion
3956162881

push

github

Michael Howitz
Update to deprecation warning free releases.

4401 of 7036 branches covered (62.55%)

Branch coverage included in aggregate %.

27161 of 31488 relevant lines covered (86.26%)

0.86 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

99.15
/src/Testing/tests/test_testbrowser.py
1
##############################################################################
2
#
3
# Copyright (c) 2004, 2005 Zope Foundation and Contributors.
4
# All Rights Reserved.
5
#
6
# This software is subject to the provisions of the Zope Public License,
7
# Version 2.1 (ZPL).  A copy of the ZPL should accompany this distribution.
8
# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
9
# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
10
# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
11
# FOR A PARTICULAR PURPOSE.
12
#
13
##############################################################################
14
"""Tests for the testbrowser module.
1✔
15
"""
16

17
import unittest
1✔
18
from urllib.error import HTTPError
1✔
19

20
import transaction
1✔
21
from AccessControl.Permissions import view
1✔
22
from OFS.SimpleItem import Item
1✔
23
from Testing.testbrowser import Browser
1✔
24
from Testing.ZopeTestCase import FunctionalTestCase
1✔
25
from Testing.ZopeTestCase import user_name
1✔
26
from Testing.ZopeTestCase import user_password
1✔
27
from zExceptions import NotFound
1✔
28
from ZPublisher.httpexceptions import HTTPExceptionHandler
1✔
29
from ZPublisher.WSGIPublisher import publish_module
1✔
30

31

32
class CookieStub(Item):
1✔
33
    """This is a cookie stub."""
34

35
    def __call__(self, REQUEST):
1✔
36
        REQUEST.RESPONSE.setCookie('evil', 'cookie')
1✔
37
        return 'Stub'
1✔
38

39

40
class ExceptionStub(Item):
1✔
41
    """This is a stub, raising an exception."""
42

43
    def __call__(self, REQUEST):
1✔
44
        raise ValueError('dummy')
1✔
45

46

47
class RedirectStub(Item):
1✔
48
    """This is a stub, causing a redirect."""
49

50
    def __call__(self, REQUEST):
1✔
51
        return REQUEST.RESPONSE.redirect('/redirected')
1✔
52

53

54
class TestTestbrowser(FunctionalTestCase):
1✔
55

56
    def test_auth(self):
1✔
57
        # Based on Testing.ZopeTestCase.testFunctional
58
        basic_auth = f'{user_name}:{user_password}'
1✔
59
        self.folder.addDTMLDocument('secret_html', file='secret')
1✔
60
        self.folder.secret_html.manage_permission(view, ['Owner'])
1✔
61
        path = '/' + self.folder.absolute_url(1) + '/secret_html'
1✔
62

63
        # Test direct publishing
64
        response = self.publish(path + '/secret_html')
1✔
65
        self.assertEqual(response.getStatus(), 401)
1✔
66
        response = self.publish(path + '/secret_html', basic_auth)
1✔
67
        self.assertEqual(response.getStatus(), 200)
1✔
68
        self.assertEqual(response.getBody(), b'secret')
1✔
69

70
        # Test browser
71
        url = 'http://localhost' + path
1✔
72
        browser = Browser()
1✔
73
        browser.raiseHttpErrors = False
1✔
74
        browser.open(url)
1✔
75
        self.assertTrue(browser.headers['status'].startswith('401'))
1✔
76

77
        browser.login(user_name, user_password)
1✔
78
        browser.open(url)
1✔
79
        self.assertTrue(browser.headers['status'].startswith('200'))
1✔
80
        self.assertEqual(browser.contents, 'secret')
1✔
81

82
    def test_cookies(self):
1✔
83
        # We want to make sure that our testbrowser correctly
84
        # understands cookies.
85
        self.folder._setObject('stub', CookieStub())
1✔
86

87
        # Test direct publishing
88
        response = self.publish('/test_folder_1_/stub')
1✔
89
        self.assertEqual(response.getCookie('evil')['value'], 'cookie')
1✔
90

91
        browser = Browser()
1✔
92
        browser.open('http://localhost/test_folder_1_/stub')
1✔
93
        self.assertEqual(browser.cookies.get('evil'), 'cookie')
1✔
94

95
    def test_handle_errors_true(self):
1✔
96
        self.folder._setObject('stub', ExceptionStub())
1✔
97
        browser = Browser()
1✔
98

99
        # An error which cannot be handled by Zope is propagated to the client:
100
        with self.assertRaises(ValueError):
1✔
101
            browser.open('http://localhost/test_folder_1_/stub')
1✔
102
        self.assertIsNone(browser.contents)
1✔
103

104
        # Handled errors become an instance of `HTTPError`:
105
        with self.assertRaises(HTTPError):
1✔
106
            browser.open('http://localhost/nothing-is-here')
1✔
107
        self.assertTrue(browser.headers['status'].startswith('404'))
1✔
108

109
    def test_handle_errors_true_redirect(self):
1✔
110
        self.folder._setObject('redirect', RedirectStub())
1✔
111
        browser = Browser()
1✔
112

113
        with self.assertRaises(HTTPError):
1✔
114
            browser.open('http://localhost/test_folder_1_/redirect')
1✔
115
        self.assertTrue(browser.headers['status'].startswith('404'))
1✔
116
        self.assertEqual(browser.url, 'http://localhost/redirected')
1✔
117

118
    def test_handle_errors_false(self):
1✔
119
        self.folder._setObject('stub', ExceptionStub())
1✔
120
        browser = Browser()
1✔
121
        browser.handleErrors = False
1✔
122

123
        # Even errors which can be handled by Zope go to the client:
124
        with self.assertRaises(NotFound):
1✔
125
            browser.open('http://localhost/nothing-is-here')
1✔
126
        self.assertTrue(browser.contents is None)
1✔
127

128
    def test_handle_errors_false_redirect(self):
1✔
129
        self.folder._setObject('redirect', RedirectStub())
1✔
130
        browser = Browser()
1✔
131
        browser.handleErrors = False
1✔
132

133
        with self.assertRaises(NotFound):
1✔
134
            browser.open('http://localhost/test_folder_1_/redirect')
1✔
135
        self.assertTrue(browser.contents is None)
1✔
136

137
    def test_handle_errors_false_HTTPExceptionHandler_in_app(self):
1✔
138
        """HTTPExceptionHandler does not handle errors if requested via WSGI.
139

140
        This is needed when HTTPExceptionHandler is part of the WSGI pipeline.
141
        """
142
        class WSGITestAppWithHTTPExceptionHandler:
1✔
143
            """Minimized testbrowser.WSGITestApp with HTTPExceptionHandler."""
144

145
            def __call__(self, environ, start_response):
1✔
146
                publish = HTTPExceptionHandler(publish_module)
1✔
147
                wsgi_result = publish(environ, start_response)
1✔
148

149
                return wsgi_result
×
150

151
        self.folder._setObject('stub', ExceptionStub())
1✔
152
        transaction.commit()
1✔
153
        browser = Browser(wsgi_app=WSGITestAppWithHTTPExceptionHandler())
1✔
154
        browser.handleErrors = False
1✔
155

156
        with self.assertRaises(ValueError):
1✔
157
            browser.open('http://localhost/test_folder_1_/stub')
1✔
158
        self.assertIsNone(browser.contents)
1✔
159

160
    def test_raise_http_errors_false(self):
1✔
161
        self.folder._setObject('stub', ExceptionStub())
1✔
162
        browser = Browser()
1✔
163
        browser.raiseHttpErrors = False
1✔
164

165
        # Internal server errors are still raised:
166
        with self.assertRaises(ValueError):
1✔
167
            browser.open('http://localhost/test_folder_1_/stub')
1✔
168
        self.assertIsNone(browser.contents)
1✔
169

170
        # But errors handled by Zope do not create an exception:
171
        browser.open('http://localhost/nothing-is-here')
1✔
172
        self.assertTrue(browser.headers['status'].startswith('404'))
1✔
173

174
    def test_raise_http_errors_false_redirect(self):
1✔
175
        self.folder._setObject('redirect', RedirectStub())
1✔
176
        browser = Browser()
1✔
177
        browser.raiseHttpErrors = False
1✔
178

179
        browser.open('http://localhost/test_folder_1_/redirect')
1✔
180
        self.assertTrue(browser.headers['status'].startswith('404'))
1✔
181
        self.assertEqual(browser.url, 'http://localhost/redirected')
1✔
182

183
    def test_headers_camel_case(self):
1✔
184
        # The Zope2 response mungs headers so they come out
185
        # in camel case. We should do the same.
186
        self.folder._setObject('stub', CookieStub())
1✔
187

188
        browser = Browser()
1✔
189
        browser.open('http://localhost/test_folder_1_/stub')
1✔
190
        header_text = str(browser.headers)
1✔
191
        self.assertTrue('Content-Length: ' in header_text)
1✔
192
        self.assertTrue('Content-Type: ' in header_text)
1✔
193

194

195
def test_suite():
1✔
196
    return unittest.defaultTestLoader.loadTestsFromTestCase(TestTestbrowser)
1✔
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc