web.py
8.61 KB
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
# -*- coding: utf8 -*-
# This file is part of PyBossa.
#
# Copyright (C) 2015 SciFabric LTD.
#
# PyBossa is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# PyBossa is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with PyBossa. If not, see <http://www.gnu.org/licenses/>.
from mock import patch
from default import Test, db, Fixtures, with_context
from pybossa.model.category import Category
from pybossa.model.task import Task
from pybossa.model.task_run import TaskRun
class Helper(Test):
"""Class to help testing the web interface"""
def html_title(self, title=None):
"""Helper function to create an HTML title"""
if title is None:
return "<title>PyBossa</title>"
else:
return "<title>PyBossa · %s</title>" % title
@patch('pybossa.view.account.signer')
def register(self, mock, fullname="John Doe", name="johndoe",
password="p4ssw0rd", email=None):
"""Helper function to register and sign in a user"""
if email is None:
email = name + '@example.com'
userdict = {'fullname': fullname, 'name': name,
'email_addr': email, 'password': password}
mock.loads.return_value = userdict
return self.app.get('/account/register/confirmation?key=fake-key',
follow_redirects=True)
def signin(self, method="POST", email="johndoe@example.com", password="p4ssw0rd",
next=None):
"""Helper function to sign in current user"""
url = '/account/signin'
if next is not None:
url = url + '?next=' + next
if method == "POST":
return self.app.post(url, data={'email': email,
'password': password},
follow_redirects=True)
else:
return self.app.get(url, follow_redirects=True)
def profile(self, name="johndoe"):
"""Helper function to check profile of signed in user"""
url = "/account/%s" % name
return self.app.get(url, follow_redirects=True)
def update_profile(self, method="POST", id=1, fullname="John Doe",
name="johndoe", locale="es",
email_addr="johndoe@example.com",
subscribed=False,
new_name=None,
btn='Profile'):
"""Helper function to update the profile of users"""
url = "/account/%s/update" % name
if new_name:
name = new_name
if (method == "POST"):
return self.app.post(url,
data={'id': id,
'fullname': fullname,
'name': name,
'locale': locale,
'email_addr': email_addr,
'btn': btn},
follow_redirects=True)
else:
return self.app.get(url,
follow_redirects=True)
def signout(self):
"""Helper function to sign out current user"""
return self.app.get('/account/signout', follow_redirects=True)
def create_categories(self):
with self.flask_app.app_context():
categories = db.session.query(Category).all()
if len(categories) == 0:
print "Categories 0"
print "Creating default ones"
self._create_categories()
def new_project(self, method="POST", name="Sample Project",
short_name="sampleapp", description="Description",
long_description=u'Long Description\n================'):
"""Helper function to create a project"""
if method == "POST":
self.create_categories()
return self.app.post("/project/new", data={
'name': name,
'short_name': short_name,
'description': description,
'long_description': long_description,
}, follow_redirects=True)
else:
return self.app.get("/project/new", follow_redirects=True)
def new_task(self, project_id):
"""Helper function to create tasks for a project"""
tasks = []
for i in range(0, 10):
tasks.append(Task(project_id=project_id, state='0', info={}))
db.session.add_all(tasks)
db.session.commit()
def delete_task_runs(self, project_id=1):
"""Deletes all TaskRuns for a given project_id"""
db.session.query(TaskRun).filter_by(project_id=project_id).delete()
db.session.commit()
def task_settings_scheduler(self, method="POST", short_name='sampleapp',
sched="default"):
"""Helper function to modify task scheduler"""
url = "/project/%s/tasks/scheduler" % short_name
if method == "POST":
return self.app.post(url, data={
'sched': sched,
}, follow_redirects=True)
else:
return self.app.get(url, follow_redirects=True)
def task_settings_redundancy(self, method="POST", short_name='sampleapp',
n_answers=30):
"""Helper function to modify task redundancy"""
url = "/project/%s/tasks/redundancy" % short_name
if method == "POST":
return self.app.post(url, data={
'n_answers': n_answers,
}, follow_redirects=True)
else:
return self.app.get(url, follow_redirects=True)
def task_settings_priority(self, method="POST", short_name='sampleapp',
task_ids="1", priority_0=0.0):
"""Helper function to modify task redundancy"""
url = "/project/%s/tasks/priority" % short_name
if method == "POST":
return self.app.post(url, data={
'task_ids': task_ids,
'priority_0': priority_0
}, follow_redirects=True)
else:
return self.app.get(url, follow_redirects=True)
def delete_project(self, method="POST", short_name="sampleapp"):
"""Helper function to delete a project"""
if method == "POST":
return self.app.post("/project/%s/delete" % short_name,
follow_redirects=True)
else:
return self.app.get("/project/%s/delete" % short_name,
follow_redirects=True)
def update_project(self, method="POST", short_name="sampleapp", id=1,
new_name="Sample Project", new_short_name="sampleapp",
new_description="Description",
new_allow_anonymous_contributors='false',
new_category_id="1",
new_long_description="Long desc",
new_sched="random",
new_webhook='http://server.com',
new_protect='false',
new_password=''):
"""Helper function to update a project"""
if method == "POST":
return self.app.post("/project/%s/update" % short_name,
data={
'id': id,
'name': new_name,
'short_name': new_short_name,
'description': new_description,
'allow_anonymous_contributors': new_allow_anonymous_contributors,
'category_id': new_category_id,
'long_description': new_long_description,
'sched': new_sched,
'webhook': new_webhook,
'protect': new_protect,
'password': new_password,
'btn': 'Save'},
follow_redirects=True)
else:
return self.app.get("/project/%s/update" % short_name,
follow_redirects=True)