1 | """ |
---|
2 | Functional tests via twill |
---|
3 | """ |
---|
4 | import os, unittest, random |
---|
5 | import testlib |
---|
6 | |
---|
7 | from genetrack import logger, conf |
---|
8 | |
---|
9 | import twill |
---|
10 | from twill import commands as tc |
---|
11 | from StringIO import StringIO |
---|
12 | |
---|
13 | from django.conf import settings |
---|
14 | |
---|
15 | # django specific handlers |
---|
16 | from django.test import TestCase, utils |
---|
17 | from django.core.servers.basehttp import AdminMediaHandler |
---|
18 | from django.core.handlers.wsgi import WSGIHandler |
---|
19 | |
---|
20 | def twill_setup(): |
---|
21 | app = AdminMediaHandler(WSGIHandler()) |
---|
22 | twill.add_wsgi_intercept("127.0.0.1", 8080, lambda: app) |
---|
23 | |
---|
24 | def twill_teardown(): |
---|
25 | twill.remove_wsgi_intercept('127.0.0.1', 8080) |
---|
26 | |
---|
27 | def twill_quiet(): |
---|
28 | # suppress normal output of twill.. You don't want to |
---|
29 | # call this if you want an interactive session |
---|
30 | if testlib.TWILL_QUIET: |
---|
31 | twill.set_output(StringIO()) |
---|
32 | |
---|
33 | class TwillTest( TestCase ): |
---|
34 | """ |
---|
35 | Base class for twill tests |
---|
36 | """ |
---|
37 | fixtures = [ 'test-fixture.json' ] |
---|
38 | |
---|
39 | def setUp(self): |
---|
40 | twill_setup() |
---|
41 | twill_quiet() |
---|
42 | |
---|
43 | def tearDown(self): |
---|
44 | twill_teardown() |
---|
45 | |
---|
46 | class BaseTest( TwillTest ): |
---|
47 | """ |
---|
48 | Basic functionality of the site. Also does link checking |
---|
49 | """ |
---|
50 | def test_main(self): |
---|
51 | #testing main page |
---|
52 | tc.go( testlib.BASE_URL ) |
---|
53 | tc.code(200) |
---|
54 | tc.find("GeneTrack") |
---|
55 | tc.find("You are not logged in") |
---|
56 | |
---|
57 | def test_404(self): |
---|
58 | # testing 404 errors |
---|
59 | tc.go( testlib.BASE_URL ) |
---|
60 | tc.go( "./nosuchurl" ) |
---|
61 | tc.code(404) |
---|
62 | |
---|
63 | tc.go( testlib.BASE_URL ) |
---|
64 | tc.follow('Log in now') |
---|
65 | tc.find("Please log in") |
---|
66 | tc.code(200) |
---|
67 | |
---|
68 | # logs in on every test |
---|
69 | tc.fv("1", "email", 'admin') |
---|
70 | tc.fv("1", "password", '1') |
---|
71 | tc.submit('0') |
---|
72 | #print tc.show() |
---|
73 | |
---|
74 | tc.code(200) |
---|
75 | tc.find("Logged in as") |
---|
76 | |
---|
77 | class ServerTest( TwillTest ): |
---|
78 | """ |
---|
79 | Full server test. |
---|
80 | """ |
---|
81 | fixtures = [ 'test-fixture.json' ] |
---|
82 | |
---|
83 | def setUp(self): |
---|
84 | TwillTest.setUp(self) |
---|
85 | self.login() |
---|
86 | |
---|
87 | def tearDown(self): |
---|
88 | self.logout() |
---|
89 | TwillTest.tearDown(self) |
---|
90 | |
---|
91 | def login(self, name='admin', passwd='1'): |
---|
92 | "Performs a login" |
---|
93 | tc.go( testlib.BASE_URL ) |
---|
94 | tc.follow('Log in now') |
---|
95 | tc.find("Please log in") |
---|
96 | tc.code(200) |
---|
97 | |
---|
98 | # logs in on every test |
---|
99 | tc.fv("1", "email", name) |
---|
100 | tc.fv("1", "password", passwd) |
---|
101 | tc.submit('0') |
---|
102 | tc.code(200) |
---|
103 | tc.find("Logged in as") |
---|
104 | |
---|
105 | def logout(self): |
---|
106 | "Performs a logout" |
---|
107 | tc.go( testlib.PROJECT_LIST_URL ) |
---|
108 | tc.code(200) |
---|
109 | tc.go("/logout/") |
---|
110 | tc.code(200) |
---|
111 | tc.find("You are not logged in") |
---|
112 | |
---|
113 | def create_project(self, name, info='no info'): |
---|
114 | """ |
---|
115 | Creates a new project |
---|
116 | """ |
---|
117 | tc.go( testlib.PROJECT_LIST_URL ) |
---|
118 | tc.find("Logged in as") |
---|
119 | tc.follow('New Project') |
---|
120 | tc.code(200) |
---|
121 | tc.find("Create New Project") |
---|
122 | tc.fv("1", "name", name ) |
---|
123 | tc.fv("1", "info", info) |
---|
124 | tc.submit() |
---|
125 | tc.code(200) |
---|
126 | tc.find(name) |
---|
127 | |
---|
128 | def delete_project(self, name): |
---|
129 | """ |
---|
130 | Deletes a project |
---|
131 | """ |
---|
132 | tc.follow("Delete") |
---|
133 | tc.find("You are removing") |
---|
134 | tc.fv("1", "delete", True) |
---|
135 | tc.submit() |
---|
136 | tc.code(200) |
---|
137 | tc.find("Project deletion complete") |
---|
138 | tc.notfind(name) |
---|
139 | |
---|
140 | def test_project_actions(self): |
---|
141 | |
---|
142 | # main page |
---|
143 | tc.go( testlib.PROJECT_LIST_URL ) |
---|
144 | tc.find("Logged in as") |
---|
145 | |
---|
146 | # default project list |
---|
147 | tc.find("Fly data 19") |
---|
148 | tc.find("Human HELA 16") |
---|
149 | tc.find("Mouse project HBB 1") |
---|
150 | |
---|
151 | |
---|
152 | # create a new project |
---|
153 | name = "Rainbow Connection - New Project" |
---|
154 | self.create_project(name=name) |
---|
155 | |
---|
156 | # visit this new project |
---|
157 | tc.follow(name) |
---|
158 | tc.code(200) |
---|
159 | tc.find("Project: %s" % name) |
---|
160 | |
---|
161 | # edit and rename project |
---|
162 | newname = "Iguana Garden - New Project" |
---|
163 | tc.follow("Edit") |
---|
164 | tc.find("Edit Project") |
---|
165 | tc.fv("1", "name", newname ) |
---|
166 | tc.fv("1", "info", "Some other *markup* goes here") |
---|
167 | tc.submit() |
---|
168 | tc.code(200) |
---|
169 | tc.notfind(name) |
---|
170 | tc.find(newname) |
---|
171 | |
---|
172 | self.delete_project(name=newname) |
---|
173 | |
---|
174 | def test_project_member_sharing(self): |
---|
175 | |
---|
176 | # tests sharing as a member |
---|
177 | tc.go( testlib.PROJECT_LIST_URL ) |
---|
178 | tc.find("Logged in as") |
---|
179 | |
---|
180 | # a project list with member access |
---|
181 | tc.find("Fly data 19") |
---|
182 | tc.follow("Fly data 19") |
---|
183 | tc.follow("Sharing") |
---|
184 | tc.find("Current members") |
---|
185 | |
---|
186 | # members may not add access |
---|
187 | tc.notfind("Add access") |
---|
188 | tc.follow("<< return to project") |
---|
189 | tc.find("Project: Fly data 19") |
---|
190 | |
---|
191 | def test_project_manager_sharing(self): |
---|
192 | # test sharing as a manager |
---|
193 | |
---|
194 | # main page |
---|
195 | tc.go( testlib.PROJECT_LIST_URL ) |
---|
196 | tc.find("Logged in as") |
---|
197 | |
---|
198 | # default project list |
---|
199 | tc.find("Yeast mutant RAV 17") |
---|
200 | tc.follow("Yeast mutant RAV 17") |
---|
201 | tc.follow("Sharing") |
---|
202 | tc.find("Current members") |
---|
203 | tc.find("Add access") |
---|
204 | |
---|
205 | # search for then add Demo User to this project |
---|
206 | tc.fv("1", "text", "demo" ) |
---|
207 | tc.submit() |
---|
208 | tc.code(200) |
---|
209 | tc.find("Demo User") |
---|
210 | tc.follow("add as member") |
---|
211 | tc.find("Demo User") |
---|
212 | |
---|
213 | # back to the project view |
---|
214 | tc.follow("<< return to project") |
---|
215 | tc.find("Yeast mutant RAV 17") |
---|
216 | |
---|
217 | def test_project_stress(self): |
---|
218 | names = [ 'STRESS-NAME-%010d' % step for step in range(11) ] |
---|
219 | for name in names: |
---|
220 | self.create_project(name) |
---|
221 | |
---|
222 | for name in names: |
---|
223 | tc.go( testlib.PROJECT_LIST_URL ) |
---|
224 | tc.follow(name) |
---|
225 | self.delete_project(name) |
---|
226 | |
---|
227 | def test_project_access(self): |
---|
228 | # verifies project access |
---|
229 | |
---|
230 | # may view this project |
---|
231 | tc.go("/project/view/19/") |
---|
232 | tc.code(200) |
---|
233 | |
---|
234 | # may not edit it |
---|
235 | tc.go("/project/edit/19/") |
---|
236 | tc.code(500) |
---|
237 | |
---|
238 | # may not delete it |
---|
239 | tc.go("/project/delete/19/") |
---|
240 | tc.code(500) |
---|
241 | |
---|
242 | # project does not exist (will return no access) |
---|
243 | tc.go("/project/view/190/") |
---|
244 | tc.code(500) |
---|
245 | |
---|
246 | def test_data_uploads(self): |
---|
247 | # data upload test |
---|
248 | name = 'Upload-test-name' |
---|
249 | self.create_project(name) |
---|
250 | |
---|
251 | tc.follow(name) |
---|
252 | |
---|
253 | # find the project id |
---|
254 | url = tc.follow('Edit') |
---|
255 | pid = url.split("/")[-2] |
---|
256 | tc.go("/data/upload/simple/%s/" % pid) |
---|
257 | |
---|
258 | # search for then add Demo User to this project |
---|
259 | tc.formfile("1", "File1", conf.testdata('short-data.bed') ) |
---|
260 | tc.formfile("1", "File2", conf.testdata('short-good-input.gtrack') ) |
---|
261 | tc.formfile("1", "File3", conf.testdata('readcounts.png') ) |
---|
262 | tc.submit() |
---|
263 | |
---|
264 | # verify uploads |
---|
265 | tc.find("short-data.bed") |
---|
266 | tc.find("short-good-input.gtrack") |
---|
267 | tc.find("readcounts.png") |
---|
268 | |
---|
269 | # visit the dataset |
---|
270 | tc.follow("short-good-input.gtrack") |
---|
271 | tc.find("waiting") |
---|
272 | |
---|
273 | # edit the dataset |
---|
274 | tc.follow("Edit") |
---|
275 | tc.fv("1", "name", "short-good-input.gtrack" ) |
---|
276 | tc.fv("1", "info","extra-info" ) |
---|
277 | tc.submit() |
---|
278 | tc.find("extra-info") |
---|
279 | |
---|
280 | # upload two results for it |
---|
281 | tc.follow("Add results") |
---|
282 | tc.formfile("1", "content", conf.testdata('short-data.bed') ) |
---|
283 | tc.formfile("1", "image", conf.testdata('readcounts.png') ) |
---|
284 | tc.submit() |
---|
285 | tc.follow("short-data.bed") |
---|
286 | tc.back() |
---|
287 | |
---|
288 | # upload one image |
---|
289 | tc.follow("Add results") |
---|
290 | tc.formfile("1", "image", conf.testdata('shift.png') ) |
---|
291 | tc.submit() |
---|
292 | tc.follow("shift.png") |
---|
293 | tc.back() |
---|
294 | |
---|
295 | # back to project view |
---|
296 | tc.follow("Project view") |
---|
297 | self.delete_project(name) |
---|
298 | |
---|
299 | def get_suite(): |
---|
300 | "Returns the testsuite" |
---|
301 | return testlib.make_suite( [] ) |
---|
302 | |
---|
303 | def local_suite(): |
---|
304 | "Returns the testsuite" |
---|
305 | tests = [ |
---|
306 | BaseTest, |
---|
307 | ServerTest, |
---|
308 | ] |
---|
309 | return testlib.make_suite( tests ) |
---|
310 | |
---|
311 | def test_runner( suite, verbosity=0 ): |
---|
312 | "Runs the functional tests on a test database" |
---|
313 | from django.db import connection |
---|
314 | |
---|
315 | old_name = settings.DATABASE_NAME |
---|
316 | utils.setup_test_environment() |
---|
317 | connection.creation.create_test_db(verbosity=verbosity, autoclobber=True) |
---|
318 | result = unittest.TextTestRunner(verbosity=2).run(suite) |
---|
319 | connection.creation.destroy_test_db(old_name, verbosity) |
---|
320 | utils.teardown_test_environment() |
---|
321 | |
---|
322 | if __name__ == '__main__': |
---|
323 | logger.info("executing functional tests") |
---|
324 | suite = local_suite() |
---|
325 | test_runner( suite, verbosity=0) |
---|