Test whether video is transcoded to all resolutions
[mediagoblin.git] / mediagoblin / tests / tools.py
1 # GNU MediaGoblin -- federated, autonomous media hosting
2 # Copyright (C) 2011, 2012 MediaGoblin contributors. See AUTHORS.
3 #
4 # This program is free software: you can redistribute it and/or modify
5 # it under the terms of the GNU Affero General Public License as published by
6 # the Free Software Foundation, either version 3 of the License, or
7 # (at your option) any later version.
8 #
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 # GNU Affero General Public License for more details.
13 #
14 # You should have received a copy of the GNU Affero General Public License
15 # along with this program. If not, see <http://www.gnu.org/licenses/>.
16
17
18 import os
19 import pkg_resources
20 import shutil
21
22 import six
23
24 from paste.deploy import loadapp
25 from webtest import TestApp
26
27 from mediagoblin import mg_globals
28 from mediagoblin.db.models import User, LocalUser, MediaEntry, Collection, TextComment, \
29 CommentSubscription, Notification, Privilege, Report, Client, \
30 RequestToken, AccessToken, Activity, Generator, Comment
31 from mediagoblin.tools import testing
32 from mediagoblin.init.config import read_mediagoblin_config
33 from mediagoblin.db.base import Session
34 from mediagoblin.meddleware import BaseMeddleware
35 from mediagoblin.auth import gen_password_hash
36 from mediagoblin.gmg_commands.dbupdate import run_dbupdate
37 from mediagoblin.tools.crypto import random_string
38
39 from datetime import datetime
40
41
42 MEDIAGOBLIN_TEST_DB_NAME = u'__mediagoblin_tests__'
43 TEST_SERVER_CONFIG = pkg_resources.resource_filename(
44 'mediagoblin.tests', 'test_paste.ini')
45 TEST_APP_CONFIG = pkg_resources.resource_filename(
46 'mediagoblin.tests', 'test_mgoblin_app.ini')
47
48
49 USER_DEV_DIRECTORIES_TO_SETUP = ['media/public', 'media/queue']
50
51
52 class TestingMeddleware(BaseMeddleware):
53 """
54 Meddleware for the Unit tests
55
56 It might make sense to perform some tests on all
57 requests/responses. Or prepare them in a special
58 manner. For example all html responses could be tested
59 for being valid html *after* being rendered.
60
61 This module is getting inserted at the front of the
62 meddleware list, which means: requests are handed here
63 first, responses last. So this wraps up the "normal"
64 app.
65
66 If you need to add a test, either add it directly to
67 the appropiate process_request or process_response, or
68 create a new method and call it from process_*.
69 """
70
71 def process_response(self, request, response):
72 # All following tests should be for html only!
73 if getattr(response, 'content_type', None) != "text/html":
74 # Get out early
75 return
76
77 # If the template contains a reference to
78 # /mgoblin_static/ instead of using
79 # /request.staticdirect(), error out here.
80 # This could probably be implemented as a grep on
81 # the shipped templates easier...
82 if response.text.find("/mgoblin_static/") >= 0:
83 raise AssertionError(
84 "Response HTML contains reference to /mgoblin_static/ "
85 "instead of staticdirect. Request was for: "
86 + request.full_path)
87
88 return
89
90
91 def get_app(request, paste_config=None, mgoblin_config=None):
92 """Create a MediaGoblin app for testing.
93
94 Args:
95 - request: Not an http request, but a pytest fixture request. We
96 use this to make temporary directories that pytest
97 automatically cleans up as needed.
98 - paste_config: particular paste config used by this application.
99 - mgoblin_config: particular mediagoblin config used by this
100 application.
101 """
102 paste_config = paste_config or TEST_SERVER_CONFIG
103 mgoblin_config = mgoblin_config or TEST_APP_CONFIG
104
105 # This is the directory we're copying the paste/mgoblin config stuff into
106 run_dir = request.config._tmpdirhandler.mktemp(
107 'mgoblin_app', numbered=True)
108 user_dev_dir = run_dir.mkdir('user_dev').strpath
109
110 new_paste_config = run_dir.join('paste.ini').strpath
111 new_mgoblin_config = run_dir.join('mediagoblin.ini').strpath
112 shutil.copyfile(paste_config, new_paste_config)
113 shutil.copyfile(mgoblin_config, new_mgoblin_config)
114
115 Session.rollback()
116 Session.remove()
117
118 # install user_dev directories
119 for directory in USER_DEV_DIRECTORIES_TO_SETUP:
120 full_dir = os.path.join(user_dev_dir, directory)
121 os.makedirs(full_dir)
122
123 # Get app config
124 global_config, validation_result = read_mediagoblin_config(new_mgoblin_config)
125 app_config = global_config['mediagoblin']
126
127 # Run database setup/migrations
128 # @@: The *only* test that doesn't pass if we remove this is in
129 # test_persona.py... why?
130 run_dbupdate(app_config, global_config)
131
132 # setup app and return
133 test_app = loadapp(
134 'config:' + new_paste_config)
135
136 # Insert the TestingMeddleware, which can do some
137 # sanity checks on every request/response.
138 # Doing it this way is probably not the cleanest way.
139 # We'll fix it, when we have plugins!
140 mg_globals.app.meddleware.insert(0, TestingMeddleware(mg_globals.app))
141
142 app = TestApp(test_app)
143 return app
144
145
146 def install_fixtures_simple(db, fixtures):
147 """
148 Very simply install fixtures in the database
149 """
150 for collection_name, collection_fixtures in six.iteritems(fixtures):
151 collection = db[collection_name]
152 for fixture in collection_fixtures:
153 collection.insert(fixture)
154
155
156 def assert_db_meets_expected(db, expected):
157 """
158 Assert a database contains the things we expect it to.
159
160 Objects are found via 'id', so you should make sure your document
161 has an id.
162
163 Args:
164 - db: pymongo or mongokit database connection
165 - expected: the data we expect. Formatted like:
166 {'collection_name': [
167 {'id': 'foo',
168 'some_field': 'some_value'},]}
169 """
170 for collection_name, collection_data in six.iteritems(expected):
171 collection = db[collection_name]
172 for expected_document in collection_data:
173 document = collection.query.filter_by(id=expected_document['id']).first()
174 assert document is not None # make sure it exists
175 assert document == expected_document # make sure it matches
176
177
178 def fixture_add_user(username=u'chris', password=u'toast',
179 privileges=[], wants_comment_notification=True):
180 # Reuse existing user or create a new one
181 test_user = LocalUser.query.filter(LocalUser.username==username).first()
182 if test_user is None:
183 test_user = LocalUser()
184 test_user.username = username
185 test_user.email = username + u'@example.com'
186 if password is not None:
187 test_user.pw_hash = gen_password_hash(password)
188 test_user.wants_comment_notification = wants_comment_notification
189 for privilege in privileges:
190 query = Privilege.query.filter(Privilege.privilege_name==privilege)
191 if query.count():
192 test_user.all_privileges.append(query.one())
193
194 test_user.save()
195
196 # Reload - The `with_polymorphic` needs to be there to eagerly load
197 # the attributes on the LocalUser as this can't be done post detachment.
198 user_query = LocalUser.query.with_polymorphic(LocalUser)
199 test_user = user_query.filter(LocalUser.username==username).first()
200
201 # ... and detach from session:
202 Session.expunge(test_user)
203
204 return test_user
205
206
207 def fixture_comment_subscription(entry, notify=True, send_email=None):
208 if send_email is None:
209 actor = LocalUser.query.filter_by(id=entry.actor).first()
210 send_email = actor.wants_comment_notification
211
212 cs = CommentSubscription(
213 media_entry_id=entry.id,
214 user_id=entry.actor,
215 notify=notify,
216 send_email=send_email)
217
218 cs.save()
219
220 cs = CommentSubscription.query.filter_by(id=cs.id).first()
221
222 Session.expunge(cs)
223
224 return cs
225
226
227 def fixture_add_comment_notification(entry, subject, user,
228 seen=False):
229 cn = Notification(
230 user_id=user,
231 seen=seen,
232 )
233 cn.obj = subject
234 cn.save()
235
236 cn = Notification.query.filter_by(id=cn.id).first()
237
238 Session.expunge(cn)
239
240 return cn
241
242
243 def fixture_media_entry(title=u"Some title", slug=None,
244 uploader=None, save=True, gen_slug=True,
245 state=u'unprocessed', fake_upload=True,
246 expunge=True):
247 """
248 Add a media entry for testing purposes.
249
250 Caution: if you're adding multiple entries with fake_upload=True,
251 make sure you save between them... otherwise you'll hit an
252 IntegrityError from multiple newly-added-MediaEntries adding
253 FileKeynames at once. :)
254 """
255 if uploader is None:
256 uploader = fixture_add_user().id
257
258 entry = MediaEntry()
259 entry.title = title
260 entry.slug = slug
261 entry.actor = uploader
262 entry.media_type = u'image'
263 entry.state = state
264
265 if fake_upload:
266 entry.media_files = {'thumb': ['a', 'b', 'c.jpg'],
267 'medium': ['d', 'e', 'f.png'],
268 'original': ['g', 'h', 'i.png']}
269 entry.media_type = u'mediagoblin.media_types.image'
270
271 if gen_slug:
272 entry.generate_slug()
273
274 if save:
275 entry.save()
276
277 if expunge:
278 entry = MediaEntry.query.filter_by(id=entry.id).first()
279
280 Session.expunge(entry)
281
282 return entry
283
284
285 def fixture_add_collection(name=u"My first Collection", user=None,
286 collection_type=Collection.USER_DEFINED_TYPE):
287 if user is None:
288 user = fixture_add_user()
289 coll = Collection.query.filter_by(
290 actor=user.id,
291 title=name,
292 type=collection_type
293 ).first()
294 if coll is not None:
295 return coll
296 coll = Collection()
297 coll.actor = user.id
298 coll.title = name
299 coll.type = collection_type
300 coll.generate_slug()
301 coll.save()
302
303 # Reload
304 Session.refresh(coll)
305
306 # ... and detach from session:
307 Session.expunge(coll)
308
309 return coll
310
311 def fixture_add_comment(author=None, media_entry=None, comment=None):
312 if author is None:
313 author = fixture_add_user().id
314
315 if media_entry is None:
316 media_entry = fixture_media_entry()
317
318 if comment is None:
319 comment = \
320 'Auto-generated test comment by user #{0} on media #{0}'.format(
321 author, media_entry)
322
323 text_comment = TextComment(
324 actor=author,
325 content=comment
326 )
327 text_comment.save()
328
329 comment_link = Comment()
330 comment_link.target = media_entry
331 comment_link.comment = text_comment
332 comment_link.save()
333
334 Session.expunge(comment_link)
335
336 return text_comment
337
338 def fixture_add_comment_report(comment=None, reported_user=None,
339 reporter=None, created=None, report_content=None):
340 if comment is None:
341 comment = fixture_add_comment()
342
343 if reported_user is None:
344 reported_user = fixture_add_user()
345
346 if reporter is None:
347 reporter = fixture_add_user()
348
349 if created is None:
350 created=datetime.now()
351
352 if report_content is None:
353 report_content = \
354 'Auto-generated test report'
355
356 comment_report = Report()
357 comment_report.obj = comment
358 comment_report.reported_user = reported_user
359 comment_report.reporter = reporter
360 comment_report.created = created
361 comment_report.report_content = report_content
362 comment_report.obj = comment
363 comment_report.save()
364
365 Session.expunge(comment_report)
366
367 return comment_report
368
369 def fixture_add_activity(obj, verb="post", target=None, generator=None, actor=None):
370 if generator is None:
371 generator = Generator(
372 name="GNU MediaGoblin",
373 object_type="service"
374 )
375 generator.save()
376
377 if actor is None:
378 actor = fixture_add_user()
379
380 activity = Activity(
381 verb=verb,
382 actor=actor.id,
383 generator=generator.id,
384 )
385
386 activity.set_object(obj)
387
388 if target is not None:
389 activity.set_target(target)
390
391 activity.save()
392 return activity