1 | #!/bin/python |
---|
2 | # -*- coding: utf-8 -*- |
---|
3 | |
---|
4 | """ |
---|
5 | Unit tests for gluon.sqlhtml |
---|
6 | """ |
---|
7 | |
---|
8 | import os |
---|
9 | import sys |
---|
10 | import unittest |
---|
11 | |
---|
12 | |
---|
13 | from gluon.compileapp import run_controller_in, run_view_in, compile_application, remove_compiled_application |
---|
14 | from gluon.languages import TranslatorFactory |
---|
15 | from gluon.storage import Storage, List |
---|
16 | from gluon import fileutils |
---|
17 | from gluon.dal import DAL, Field, Table |
---|
18 | from gluon.http import HTTP |
---|
19 | from gluon.fileutils import open_file |
---|
20 | from gluon.cache import CacheInRam |
---|
21 | |
---|
22 | DEFAULT_URI = os.getenv('DB', 'sqlite:memory') |
---|
23 | |
---|
24 | |
---|
25 | |
---|
26 | def fake_check_credentials(foo): |
---|
27 | return True |
---|
28 | |
---|
29 | |
---|
30 | class TestAppAdmin(unittest.TestCase): |
---|
31 | |
---|
32 | def setUp(self): |
---|
33 | from gluon.globals import Request, Response, Session, current |
---|
34 | from gluon.html import A, DIV, FORM, MENU, TABLE, TR, INPUT, URL, XML |
---|
35 | from gluon.html import ASSIGNJS |
---|
36 | from gluon.validators import IS_NOT_EMPTY |
---|
37 | from gluon.compileapp import LOAD |
---|
38 | from gluon.http import HTTP, redirect |
---|
39 | from gluon.tools import Auth |
---|
40 | from gluon.sql import SQLDB |
---|
41 | from gluon.sqlhtml import SQLTABLE, SQLFORM |
---|
42 | self.original_check_credentials = fileutils.check_credentials |
---|
43 | fileutils.check_credentials = fake_check_credentials |
---|
44 | request = Request(env={}) |
---|
45 | request.application = 'welcome' |
---|
46 | request.controller = 'appadmin' |
---|
47 | request.function = self._testMethodName.split('_')[1] |
---|
48 | request.folder = 'applications/welcome' |
---|
49 | request.env.http_host = '127.0.0.1:8000' |
---|
50 | request.env.remote_addr = '127.0.0.1' |
---|
51 | response = Response() |
---|
52 | session = Session() |
---|
53 | T = TranslatorFactory('', 'en') |
---|
54 | session.connect(request, response) |
---|
55 | current.request = request |
---|
56 | current.response = response |
---|
57 | current.session = session |
---|
58 | current.T = T |
---|
59 | db = DAL(DEFAULT_URI, check_reserved=['all']) |
---|
60 | auth = Auth(db) |
---|
61 | auth.define_tables(username=True, signature=False) |
---|
62 | db.define_table('t0', Field('tt'), auth.signature) |
---|
63 | # Create a user |
---|
64 | db.auth_user.insert(first_name='Bart', |
---|
65 | last_name='Simpson', |
---|
66 | username='user1', |
---|
67 | email='user1@test.com', |
---|
68 | password='password_123', |
---|
69 | registration_key=None, |
---|
70 | registration_id=None) |
---|
71 | self.env = locals() |
---|
72 | |
---|
73 | def tearDown(self): |
---|
74 | fileutils.check_credentials = self.original_check_credentials |
---|
75 | |
---|
76 | def run_function(self): |
---|
77 | return run_controller_in(self.env['request'].controller, self.env['request'].function, self.env) |
---|
78 | |
---|
79 | def run_view(self): |
---|
80 | return run_view_in(self.env) |
---|
81 | |
---|
82 | def run_view_file_stream(self): |
---|
83 | view_path = os.path.join(self.env['request'].folder, 'views', 'appadmin.html') |
---|
84 | self.env['response'].view = open_file(view_path, 'r') |
---|
85 | return run_view_in(self.env) |
---|
86 | |
---|
87 | def _test_index(self): |
---|
88 | result = self.run_function() |
---|
89 | self.assertTrue('db' in result['databases']) |
---|
90 | self.env.update(result) |
---|
91 | try: |
---|
92 | self.run_view() |
---|
93 | self.run_view_file_stream() |
---|
94 | except Exception as e: |
---|
95 | import traceback |
---|
96 | print(traceback.format_exc()) |
---|
97 | self.fail('Could not make the view') |
---|
98 | |
---|
99 | def test_index(self): |
---|
100 | self._test_index() |
---|
101 | |
---|
102 | def test_index_compiled(self): |
---|
103 | appname_path = os.path.join(os.getcwd(), 'applications', 'welcome') |
---|
104 | compile_application(appname_path) |
---|
105 | self._test_index() |
---|
106 | remove_compiled_application(appname_path) |
---|
107 | |
---|
108 | def test_index_minify(self): |
---|
109 | # test for gluon/contrib/minify |
---|
110 | self.env['response'].optimize_css = 'concat|minify' |
---|
111 | self.env['response'].optimize_js = 'concat|minify' |
---|
112 | self.env['current'].cache = Storage({'ram':CacheInRam()}) |
---|
113 | appname_path = os.path.join(os.getcwd(), 'applications', 'welcome') |
---|
114 | self._test_index() |
---|
115 | file_l = os.listdir(os.path.join(appname_path, 'static', 'temp')) |
---|
116 | file_l.sort() |
---|
117 | self.assertTrue(len(file_l) == 2) |
---|
118 | self.assertEqual(file_l[0][0:10], 'compressed') |
---|
119 | self.assertEqual(file_l[1][0:10], 'compressed') |
---|
120 | self.assertEqual(file_l[0][-3:], 'css') |
---|
121 | self.assertEqual(file_l[1][-2:], 'js') |
---|
122 | |
---|
123 | def test_select(self): |
---|
124 | request = self.env['request'] |
---|
125 | request.args = List(['db']) |
---|
126 | request.env.query_string = 'query=db.auth_user.id>0' |
---|
127 | result = self.run_function() |
---|
128 | self.assertTrue('table' in result and 'query' in result) |
---|
129 | self.assertTrue(result['table'] == 'auth_user') |
---|
130 | self.assertTrue(result['query'] == 'db.auth_user.id>0') |
---|
131 | self.env.update(result) |
---|
132 | try: |
---|
133 | self.run_view() |
---|
134 | except Exception as e: |
---|
135 | import traceback |
---|
136 | print(traceback.format_exc()) |
---|
137 | self.fail('Could not make the view') |
---|
138 | |
---|
139 | def test_insert(self): |
---|
140 | request = self.env['request'] |
---|
141 | request.args = List(['db', 'auth_user']) |
---|
142 | result = self.run_function() |
---|
143 | self.assertTrue('table' in result) |
---|
144 | self.assertTrue('form' in result) |
---|
145 | self.assertTrue(str(result['table']) is 'auth_user') |
---|
146 | self.env.update(result) |
---|
147 | try: |
---|
148 | self.run_view() |
---|
149 | except Exception as e: |
---|
150 | import traceback |
---|
151 | print(traceback.format_exc()) |
---|
152 | self.fail('Could not make the view') |
---|
153 | |
---|
154 | def test_insert_submit(self): |
---|
155 | request = self.env['request'] |
---|
156 | request.args = List(['db', 'auth_user']) |
---|
157 | form = self.run_function()['form'] |
---|
158 | hidden_fields = form.hidden_fields() |
---|
159 | data = {} |
---|
160 | data['_formkey'] = hidden_fields.element('input', _name='_formkey')['_value'] |
---|
161 | data['_formname'] = hidden_fields.element('input', _name='_formname')['_value'] |
---|
162 | data['first_name'] = 'Lisa' |
---|
163 | data['last_name'] = 'Simpson' |
---|
164 | data['username'] = 'lisasimpson' |
---|
165 | data['password'] = 'password_123' |
---|
166 | data['email'] = 'lisa@example.com' |
---|
167 | request._vars = data |
---|
168 | result = self.run_function() |
---|
169 | self.env.update(result) |
---|
170 | try: |
---|
171 | self.run_view() |
---|
172 | except Exception as e: |
---|
173 | import traceback |
---|
174 | print(traceback.format_exc()) |
---|
175 | self.fail('Could not make the view') |
---|
176 | db = self.env['db'] |
---|
177 | lisa_record = db(db.auth_user.username == 'lisasimpson').select().first() |
---|
178 | self.assertIsNotNone(lisa_record) |
---|
179 | del data['_formkey'] |
---|
180 | del data['_formname'] |
---|
181 | del data['password'] |
---|
182 | for key in data: |
---|
183 | self.assertEqual(data[key], lisa_record[key]) |
---|
184 | |
---|
185 | def test_update_submit(self): |
---|
186 | request = self.env['request'] |
---|
187 | request.args = List(['db', 'auth_user', '1']) |
---|
188 | form = self.run_function()['form'] |
---|
189 | hidden_fields = form.hidden_fields() |
---|
190 | data = {} |
---|
191 | data['_formkey'] = hidden_fields.element('input', _name='_formkey')['_value'] |
---|
192 | data['_formname'] = hidden_fields.element('input', _name='_formname')['_value'] |
---|
193 | for element in form.elements('input'): |
---|
194 | data[element['_name']] = element['_value'] |
---|
195 | data['email'] = 'user1@example.com' |
---|
196 | data['id'] = '1' |
---|
197 | request._vars = data |
---|
198 | self.assertRaises(HTTP, self.run_function) |
---|