/
test_controller.py
206 lines (178 loc) · 9.26 KB
/
test_controller.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
from nose import SkipTest
import os, sys
from .t_config import TestConfig, app_from_config
from tests.model import User, Group, Town
from tg.util import Bunch
from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker
from tests.model import metadata, DBSession
root = os.path.abspath(os.path.join(os.path.dirname(__file__), '..'))
test_db_path = 'sqlite:///'+root+'/test.db'
base_config = TestConfig()
base_config.renderers = ['genshi', 'json', 'mako']
def setup_records(session):
#session.expunge_all()
user = User()
user.user_name = 'asdf'
user.email_address = "asdf@asdf.com"
user.password = "asdf"
session.add(user)
arvada = Town(name='Arvada')
session.add(arvada)
session.flush()
user.town = arvada
session.add(Town(name='Denver'))
session.add(Town(name='Golden'))
session.add(Town(name='Boulder'))
#test_table.insert(values=dict(BLOB=FieldStorage('asdf', StringIO()).value)).execute()
#user_reference_table.insert(values=dict(user_id=user.user_id)).execute()
# print user.user_id
for i in ['managers', 'users']:
group = Group(group_name=i)
session.add(group)
user.groups.append(group)
session.flush()
return user
def setup():
engine = create_engine(test_db_path)
metadata.bind = engine
metadata.drop_all()
metadata.create_all()
session = sessionmaker(bind=engine)()
setup_records(session)
session.commit()
print('finished setting up db')
def _teardown():
os.remove(test_db_path)
class TestAdminController:
def __init__(self, *args, **kargs):
self.app = app_from_config(base_config)
def test_index(self):
resp = self.app.get('/admin/')
assert 'Document' in resp, resp
def test_list_documents(self):
resp = self.app.get('/admin/documents').follow()
assert """<thead>
<tr>
<th class="col_0">
actions
</th><th class="col_1">
<a href="http://localhost/admin/documents/?order_by=document_id">document_id</a>
</th><th class="col_2">
<a href="http://localhost/admin/documents/?order_by=created">created</a>
</th><th class="col_3">
<a href="http://localhost/admin/documents/?order_by=blob">blob</a>
</th><th class="col_4">
<a href="http://localhost/admin/documents/?order_by=owner">owner</a>
</th><th class="col_5">
<a href="http://localhost/admin/documents/?order_by=url">url</a>
</th><th class="col_6">
<a href="http://localhost/admin/documents/?order_by=address">address</a>
</th>
</tr>
</thead>""" in resp, resp
def _test_documents_new(self):
resp = self.app.get('/admin/documents/new')
assert """<tr id="blob.container" class="even" title="">
<td class="labelcol">
<label id="blob.label" for="blob" class="fieldlabel">Blob</label>
</td>
<td class="fieldcol">
<input type="file" name="blob" class="filefield" id="blob" value="" />
</td>
</tr>""" in resp, resp
def test_get_users(self):
resp = self.app.get('/admin/users/')
assert """<thead>
<tr>
<th class="col_0">
actions
</th><th class="col_1">
<a href="http://localhost/admin/users/?order_by=_password">_password</a>
</th><th class="col_2">
<a href="http://localhost/admin/users/?order_by=user_id">user_id</a>
</th><th class="col_3">
<a href="http://localhost/admin/users/?order_by=user_name">user_name</a>
</th><th class="col_4">
<a href="http://localhost/admin/users/?order_by=email_address">email_address</a>
</th><th class="col_5">
<a href="http://localhost/admin/users/?order_by=display_name">display_name</a>
</th><th class="col_6">
<a href="http://localhost/admin/users/?order_by=created">created</a>
</th><th class="col_7">
<a href="http://localhost/admin/users/?order_by=town_id">town_id</a>
</th><th class="col_8">
town
</th><th class="col_9">
<a href="http://localhost/admin/users/?order_by=password">password</a>
</th><th class="col_10">
groups
</th>
</tr>
</thead>""" in resp, resp
def test_get_users_json(self):
resp = self.app.get('/admin/users.json')
json = resp.json
values = json.get('value_list', {})
assert values.get('total', 0) == 1
assert values.get('page', 0) == 1
def test_edit_user(self):
resp = self.app.get('/admin/users/1/edit')
# Check password field available in edit
assert """<tr class="odd" id="sx_password:container">
<th><label for="sx_password">Password</label></th>
<td>""" in resp, resp
def test_edit_user_success(self):
resp = self.app.post('/admin/users/1/', params={'sprox_id':'put__User',
'_method':'PUT',
'user_name':'someone',
'display_name':'someone2',
'email_address':'asdf2@asdf2.com',
'_password':'pass',
'password':'pass',
'town':'1',
'town_id':'1',
'user_id':'1',
'created':'2009-01-11 13:54:01'}).follow()
#resp = self.app.get('/admin/user.json')
#assert """"email_address": "asdf2@asdf2.com",""" in resp, resp
resp = self.app.post('/admin/users/1/', params={'sprox_id':'put__User',
'_method':'PUT',
'user_name':'someone',
'display_name':'someone2',
'email_address':'asdf@asdf.com',
'_password':'pass',
'password':'pass',
'town':'1',
'town_id':'1',
'user_id':'1',
'created':'2009-01-11 13:54:01'}).follow()
# resp = self.app.get('/admin/user.json')
# assert """"email_address": "asdf@asdf.com",""" in resp, resp
#this tests causes other tests to fail, so, no go
def _test_add_and_remove_user(self):
resp = self.app.post('/admin/users/', params={'sprox_id':'add__User',
'user_name':'someone',
'display_name':'someone2',
'email_address':'asdf2@asdf2.com',
'_password':'pass',
'password':'pass',
'town':'1',
'town_id':'1',
'user_id':'2',
'created':'2009-01-11 13:54:01'}).follow()
#assert '<td>asdf2@asdf2' in resp, resp
resp = self.app.get('/admin/users/2/', params={'user_id':'2', '_method':'DELETE'}).follow()
#assert 'asdf2@asdf2' not in resp, resp
def test_add_user_existing_username(self):
resp = self.app.post('/admin/users/create', params={'sprox_id':'add__User',
'user_name':'asdf',
'display_name':'someone2',
'email_address':'asdf2@asdf2.com',
'_password':'pass',
'password':'pass',
'town':'1',
'town_id':'1',
'user_id':'2',
'created':'2009-01-11 13:54:01'})
assert 'That value already exists' in resp, resp