1 | # -*- coding: utf-8 -*- |
---|
2 | """ |
---|
3 | Unit tests for running web2py |
---|
4 | """ |
---|
5 | |
---|
6 | from __future__ import print_function |
---|
7 | |
---|
8 | import sys |
---|
9 | import os |
---|
10 | import unittest |
---|
11 | import subprocess |
---|
12 | import time |
---|
13 | import shutil |
---|
14 | |
---|
15 | from gluon.contrib.webclient import WebClient |
---|
16 | from gluon._compat import urllib2, PY2 |
---|
17 | from gluon.fileutils import create_app |
---|
18 | |
---|
19 | test_app_name = '_test_web' |
---|
20 | |
---|
21 | webserverprocess = None |
---|
22 | |
---|
23 | def startwebserver(): |
---|
24 | global webserverprocess |
---|
25 | path = path = os.path.dirname(os.path.abspath(__file__)) |
---|
26 | if not os.path.isfile(os.path.join(path, 'web2py.py')): |
---|
27 | i = 0 |
---|
28 | while i < 10: |
---|
29 | i += 1 |
---|
30 | if os.path.exists(os.path.join(path, 'web2py.py')): |
---|
31 | break |
---|
32 | path = os.path.abspath(os.path.join(path, '..')) |
---|
33 | web2py_exec = os.path.join(path, 'web2py.py') |
---|
34 | webserverprocess = subprocess.Popen([sys.executable, web2py_exec, '-a', 'testpass']) |
---|
35 | print('Sleeping before web2py starts...') |
---|
36 | for a in range(1, 11): |
---|
37 | time.sleep(1) |
---|
38 | print("%d..." % a) |
---|
39 | try: |
---|
40 | c = WebClient('http://127.0.0.1:8000/') |
---|
41 | c.get(test_app_name) |
---|
42 | break |
---|
43 | except: |
---|
44 | continue |
---|
45 | print('') |
---|
46 | |
---|
47 | |
---|
48 | def stopwebserver(): |
---|
49 | global webserverprocess |
---|
50 | print('Killing webserver') |
---|
51 | webserverprocess.terminate() |
---|
52 | |
---|
53 | |
---|
54 | class Cookie(unittest.TestCase): |
---|
55 | def testParseMultipleEquals(self): |
---|
56 | """ Test for issue #1500. |
---|
57 | Ensure that a cookie containing one or more '=' is correctly parsed |
---|
58 | """ |
---|
59 | client = WebClient() |
---|
60 | client.headers['set-cookie'] = "key = value with one =;" |
---|
61 | client._parse_headers_in_cookies() |
---|
62 | self.assertIn("key", client.cookies) |
---|
63 | self.assertEqual(client.cookies['key'], "value with one =") |
---|
64 | |
---|
65 | client.headers['set-cookie'] = "key = value with one = and another one =;" |
---|
66 | client._parse_headers_in_cookies() |
---|
67 | client._parse_headers_in_cookies() |
---|
68 | self.assertIn("key", client.cookies) |
---|
69 | self.assertEqual(client.cookies['key'], "value with one = and another one =") |
---|
70 | |
---|
71 | |
---|
72 | class LiveTest(unittest.TestCase): |
---|
73 | |
---|
74 | @classmethod |
---|
75 | def setUpClass(cls): |
---|
76 | appdir = os.path.join('applications', test_app_name) |
---|
77 | if not os.path.exists(appdir): |
---|
78 | os.mkdir(appdir) |
---|
79 | create_app(appdir) |
---|
80 | startwebserver() |
---|
81 | |
---|
82 | @classmethod |
---|
83 | def tearDownClass(cls): |
---|
84 | stopwebserver() |
---|
85 | appdir = os.path.join('applications', test_app_name) |
---|
86 | if os.path.exists(appdir): |
---|
87 | shutil.rmtree(appdir) |
---|
88 | |
---|
89 | |
---|
90 | @unittest.skipIf("datastore" in os.getenv("DB", ""), "TODO: setup web test for app engine") |
---|
91 | class TestWeb(LiveTest): |
---|
92 | def testRegisterAndLogin(self): |
---|
93 | client = WebClient("http://127.0.0.1:8000/%s/default/" % test_app_name) |
---|
94 | |
---|
95 | client.get('index') |
---|
96 | |
---|
97 | # register |
---|
98 | data = dict(first_name='Homer', |
---|
99 | last_name='Simpson', |
---|
100 | email='homer@web2py.com', |
---|
101 | password='test', |
---|
102 | password_two='test', |
---|
103 | _formname='register') |
---|
104 | client.post('user/register', data=data) |
---|
105 | |
---|
106 | # logout |
---|
107 | client.get('user/logout') |
---|
108 | |
---|
109 | # login again |
---|
110 | data = dict(email='homer@web2py.com', |
---|
111 | password='test', |
---|
112 | _formname='login') |
---|
113 | client.post('user/login', data=data) |
---|
114 | self.assertIn('Homer', client.text) |
---|
115 | |
---|
116 | # check registration and login were successful |
---|
117 | client.get('index') |
---|
118 | |
---|
119 | self.assertIn('Homer', client.text) |
---|
120 | |
---|
121 | client = WebClient('http://127.0.0.1:8000/admin/default/') |
---|
122 | client.post('index', data=dict(password='testpass')) |
---|
123 | client.get('site') |
---|
124 | client.get('design/' + test_app_name) |
---|
125 | |
---|
126 | def testStaticCache(self): |
---|
127 | s = WebClient("http://127.0.0.1:8000/%s/" % test_app_name) |
---|
128 | s.get('static/js/web2py.js') |
---|
129 | self.assertNotIn('expires', s.headers) |
---|
130 | self.assertFalse(s.headers['cache-control'].startswith('max-age')) |
---|
131 | text = s.text |
---|
132 | s.get('static/_1.2.3/js/web2py.js') |
---|
133 | self.assertEqual(text, s.text) |
---|
134 | self.assertIn('expires', s.headers) |
---|
135 | self.assertTrue(s.headers['cache-control'].startswith('max-age')) |
---|
136 | |
---|
137 | @unittest.skipIf(not(PY2), 'skip PY3 testSoap') |
---|
138 | def testSoap(self): |
---|
139 | # test soap server implementation |
---|
140 | from gluon.contrib.pysimplesoap.client import SoapClient, SoapFault |
---|
141 | url = 'http://127.0.0.1:8000/examples/soap_examples/call/soap?WSDL' |
---|
142 | client = SoapClient(wsdl=url) |
---|
143 | ret = client.SubIntegers(a=3, b=2) |
---|
144 | # check that the value returned is ok |
---|
145 | self.assertIn('SubResult', ret) |
---|
146 | self.assertEqual(ret['SubResult'], 1) |
---|
147 | |
---|
148 | try: |
---|
149 | ret = client.Division(a=3, b=0) |
---|
150 | except SoapFault as sf: |
---|
151 | # verify the exception value is ok |
---|
152 | # self.assertEqual(sf.faultstring, "float division by zero") # true only in 2.7 |
---|
153 | self.assertEqual(sf.faultcode, "Server.ZeroDivisionError") |
---|
154 | |
---|
155 | # store sent and received xml for low level test |
---|
156 | xml_request = client.xml_request |
---|
157 | xml_response = client.xml_response |
---|
158 | |
---|
159 | # do a low level raw soap request (using |
---|
160 | s = WebClient('http://127.0.0.1:8000/') |
---|
161 | try: |
---|
162 | s.post('examples/soap_examples/call/soap', data=xml_request, method="POST") |
---|
163 | except urllib2.HTTPError as e: |
---|
164 | self.assertEqual(e.msg, 'INTERNAL SERVER ERROR') |
---|
165 | # check internal server error returned (issue 153) |
---|
166 | self.assertEqual(s.status, 500) |
---|
167 | self.assertEqual(s.text, xml_response) |
---|