source: OpenRLabs-Git/deploy/rlabs-docker/web2py-rlabs/gluon/tests/test_globals.py

main
Last change on this file was 42bd667, checked in by David Fuertes <dfuertes@…>, 4 years ago

Historial Limpio

  • Property mode set to 100755
File size: 10.5 KB
Line 
1#!/usr/bin/env python
2# -*- coding: utf-8 -*-
3
4"""
5    Unit tests for gluon.globals
6"""
7
8
9import re
10import unittest
11
12from gluon.globals import Request, Response, Session
13from gluon.rewrite import regex_url_in
14from gluon import URL
15from gluon._compat import basestring
16
17
18def setup_clean_session():
19    request = Request(env={})
20    request.application = 'a'
21    request.controller = 'c'
22    request.function = 'f'
23    request.folder = 'applications/admin'
24    response = Response()
25    session = Session()
26    session.connect(request, response)
27    from gluon.globals import current
28    current.request = request
29    current.response = response
30    current.session = session
31    return current
32
33
34class testRequest(unittest.TestCase):
35
36    def setUp(self):
37        from gluon.globals import current
38        current.response = Response()
39
40    def test_restful_simple(self):
41        env = {'request_method': 'GET', 'PATH_INFO': '/welcome/default/index/1.pdf'}
42        r = Request(env)
43        regex_url_in(r, env)
44
45        @r.restful()
46        def simple_rest():
47            def GET(*args, **vars):
48                return args[0]
49            return locals()
50
51        self.assertEqual(simple_rest(), '1')
52
53    def test_restful_calls_post(self):
54        env = {'request_method': 'POST', 'PATH_INFO': '/welcome/default/index'}
55        r = Request(env)
56        regex_url_in(r, env)
57
58        @r.restful()
59        def post_rest():
60            def POST(*args, **vars):
61                return 'I posted'
62            return locals()
63
64        self.assertEqual(post_rest(), 'I posted')
65
66    def test_restful_ignore_extension(self):
67        env = {'request_method': 'GET', 'PATH_INFO': '/welcome/default/index/127.0.0.1'}
68        r = Request(env)
69        regex_url_in(r, env)
70
71        @r.restful(ignore_extension=True)
72        def ignore_rest():
73            def GET(*args, **vars):
74                return args[0]
75            return locals()
76
77        self.assertEqual(ignore_rest(), '127.0.0.1')
78
79
80class testResponse(unittest.TestCase):
81
82    # port from python 2.7, needed for 2.5 and 2.6 tests
83    def assertRegexpMatches(self, text, expected_regexp, msg=None):
84        """Fail the test unless the text matches the regular expression."""
85        if isinstance(expected_regexp, basestring):
86            expected_regexp = re.compile(expected_regexp)
87        if not expected_regexp.search(text):
88            msg = msg or "Regexp didn't match"
89            msg = '%s: %r not found in %r' % (
90                msg, expected_regexp.pattern, text)
91            raise self.failureException(msg)
92
93    def test_include_files(self):
94
95        def return_includes(response, extensions=None):
96            response.include_files(extensions)
97            return response.body.getvalue()
98
99        response = Response()
100        response.files.append(URL('a', 'static', 'css/file.css'))
101        content = return_includes(response)
102        self.assertEqual(content, '<link href="/a/static/css/file.css" rel="stylesheet" type="text/css" />')
103
104        response = Response()
105        response.files.append(URL('a', 'static', 'css/file.js'))
106        content = return_includes(response)
107        self.assertEqual(content, '<script src="/a/static/css/file.js" type="text/javascript"></script>')
108
109        response = Response()
110        response.files.append(URL('a', 'static', 'css/file.coffee'))
111        content = return_includes(response)
112        self.assertEqual(content, '<script src="/a/static/css/file.coffee" type="text/coffee"></script>')
113
114        response = Response()
115        response.files.append(URL('a', 'static', 'css/file.ts'))
116        content = return_includes(response)
117        self.assertEqual(content, '<script src="/a/static/css/file.ts" type="text/typescript"></script>')
118
119        response = Response()
120        response.files.append(URL('a', 'static', 'css/file.less'))
121        content = return_includes(response)
122        self.assertEqual(content, '<link href="/a/static/css/file.less" rel="stylesheet/less" type="text/css" />')
123
124        response = Response()
125        response.files.append(('css:inline', 'background-color; white;'))
126        content = return_includes(response)
127        self.assertEqual(content, '<style type="text/css">\nbackground-color; white;\n</style>')
128
129        response = Response()
130        response.files.append(('js:inline', 'alert("hello")'))
131        content = return_includes(response)
132        self.assertEqual(content, '<script type="text/javascript">\nalert("hello")\n</script>')
133
134        response = Response()
135        response.files.append('https://code.jquery.com/jquery-1.11.3.min.js')
136        content = return_includes(response)
137        self.assertEqual(content, '<script src="https://code.jquery.com/jquery-1.11.3.min.js" type="text/javascript"></script>')
138
139        response = Response()
140        response.files.append('https://code.jquery.com/jquery-1.11.3.min.js?var=0')
141        content = return_includes(response)
142        self.assertEqual(content, '<script src="https://code.jquery.com/jquery-1.11.3.min.js?var=0" type="text/javascript"></script>')
143
144        response = Response()
145        response.files.append('https://code.jquery.com/jquery-1.11.3.min.js?var=0')
146        response.files.append('https://code.jquery.com/jquery-1.11.3.min.js?var=0')
147        response.files.append(URL('a', 'static', 'css/file.css'))
148        response.files.append(URL('a', 'static', 'css/file.css'))
149        content = return_includes(response)
150        self.assertEqual(content,
151                         '<script src="https://code.jquery.com/jquery-1.11.3.min.js?var=0" type="text/javascript"></script>' +
152                         '<link href="/a/static/css/file.css" rel="stylesheet" type="text/css" />')
153
154        response = Response()
155        response.files.append(('js', 'http://maps.google.com/maps/api/js?sensor=false'))
156        response.files.append('https://code.jquery.com/jquery-1.11.3.min.js?var=0')
157        response.files.append(URL('a', 'static', 'css/file.css'))
158        response.files.append(URL('a', 'static', 'css/file.ts'))
159        content = return_includes(response)
160        self.assertEqual(content,
161                         '<script src="http://maps.google.com/maps/api/js?sensor=false" type="text/javascript"></script>' +
162                         '<script src="https://code.jquery.com/jquery-1.11.3.min.js?var=0" type="text/javascript"></script>' +
163                         '<link href="/a/static/css/file.css" rel="stylesheet" type="text/css" />' +
164                         '<script src="/a/static/css/file.ts" type="text/typescript"></script>'
165                         )
166
167        response = Response()
168        response.files.append(URL('a', 'static', 'css/file.js'))
169        response.files.append(URL('a', 'static', 'css/file.css'))
170        content = return_includes(response, extensions=['css'])
171        self.assertEqual(content, '<link href="/a/static/css/file.css" rel="stylesheet" type="text/css" />')
172
173        # regr test for #628
174        response = Response()
175        response.files.append('http://maps.google.com/maps/api/js?sensor=false')
176        content = return_includes(response)
177        self.assertEqual(content, '')
178
179        # regr test for #628
180        response = Response()
181        response.files.append(('js', 'http://maps.google.com/maps/api/js?sensor=false'))
182        content = return_includes(response)
183        self.assertEqual(content, '<script src="http://maps.google.com/maps/api/js?sensor=false" type="text/javascript"></script>')
184
185        response = Response()
186        response.files.append(['js', 'http://maps.google.com/maps/api/js?sensor=false'])
187        content = return_includes(response)
188        self.assertEqual(content, '<script src="http://maps.google.com/maps/api/js?sensor=false" type="text/javascript"></script>')
189
190        response = Response()
191        response.files.append(('js1', 'http://maps.google.com/maps/api/js?sensor=false'))
192        content = return_includes(response)
193        self.assertEqual(content, '')
194
195    def test_cookies(self):
196        current = setup_clean_session()
197        cookie = str(current.response.cookies)
198        session_key = '%s=%s' % (current.response.session_id_name, current.response.session_id)
199        self.assertRegexpMatches(cookie, r'^Set-Cookie: ')
200        self.assertTrue(session_key in cookie)
201        self.assertTrue('Path=/' in cookie)
202
203    def test_cookies_secure(self):
204        current = setup_clean_session()
205        current.session._fixup_before_save()
206        cookie = str(current.response.cookies)
207        self.assertTrue('secure' not in cookie.lower())
208
209        current = setup_clean_session()
210        current.session.secure()
211        current.session._fixup_before_save()
212        cookie = str(current.response.cookies)
213        self.assertTrue('secure' in cookie.lower())
214
215    def test_cookies_httponly(self):
216        current = setup_clean_session()
217        current.session._fixup_before_save()
218        cookie = str(current.response.cookies)
219        # cookies in PY3 have capital letters
220        self.assertTrue('httponly' in cookie.lower())
221
222        current = setup_clean_session()
223        current.session.httponly_cookies = True
224        current.session._fixup_before_save()
225        cookie = str(current.response.cookies)
226        self.assertTrue('httponly' in cookie.lower())
227
228        current = setup_clean_session()
229        current.session.httponly_cookies = False
230        current.session._fixup_before_save()
231        cookie = str(current.response.cookies)
232        self.assertTrue('httponly' not in cookie.lower())
233
234    def test_cookies_samesite(self):
235        # Test Lax is the default mode
236        current = setup_clean_session()
237        current.session._fixup_before_save()
238        cookie = str(current.response.cookies)
239        self.assertTrue('samesite=lax' in cookie.lower())
240        # Test you can disable samesite
241        current = setup_clean_session()
242        current.session.samesite(False)
243        current.session._fixup_before_save()
244        cookie = str(current.response.cookies)
245        self.assertTrue('samesite' not in cookie.lower())
246        # Test you can change mode
247        current = setup_clean_session()
248        current.session.samesite('Strict')
249        current.session._fixup_before_save()
250        cookie = str(current.response.cookies)
251        self.assertTrue('samesite=strict' in cookie.lower())
252
253    def test_include_meta(self):
254        response = Response()
255        response.meta['web2py'] = 'web2py'
256        response.include_meta()
257        self.assertEqual(response.body.getvalue(), '\n<meta name="web2py" content="web2py" />\n')
258        response = Response()
259        response.meta['meta_dict'] = {'tag_name':'tag_value'}
260        response.include_meta()
261        self.assertEqual(response.body.getvalue(), '\n<meta tag_name="tag_value" />\n')
Note: See TracBrowser for help on using the repository browser.