Add image URL's (thumb & full)
[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
23 from paste.deploy import loadapp
24 from webtest import TestApp
25
26 from mediagoblin import mg_globals
27 from mediagoblin.db.models import User, MediaEntry, Collection, MediaComment, \
28 CommentSubscription, CommentNotification, Privilege, CommentReport
29 from mediagoblin.tools import testing
30 from mediagoblin.init.config import read_mediagoblin_config
31 from mediagoblin.db.base import Session
32 from mediagoblin.meddleware import BaseMeddleware
33 from mediagoblin.auth import gen_password_hash
34 from mediagoblin.gmg_commands.dbupdate import run_dbupdate
35
36 from datetime import datetime
37
38
39 MEDIAGOBLIN_TEST_DB_NAME = u'__mediagoblin_tests__'
40 TEST_SERVER_CONFIG = pkg_resources.resource_filename(
41 'mediagoblin.tests', 'test_paste.ini')
42 TEST_APP_CONFIG = pkg_resources.resource_filename(
43 'mediagoblin.tests', 'test_mgoblin_app.ini')
44
45
46 USER_DEV_DIRECTORIES_TO_SETUP = ['media/public', 'media/queue']
47
48
49 class TestingMeddleware(BaseMeddleware):
50 """
51 Meddleware for the Unit tests
52
53 It might make sense to perform some tests on all
54 requests/responses. Or prepare them in a special
55 manner. For example all html responses could be tested
56 for being valid html *after* being rendered.
57
58 This module is getting inserted at the front of the
59 meddleware list, which means: requests are handed here
60 first, responses last. So this wraps up the "normal"
61 app.
62
63 If you need to add a test, either add it directly to
64 the appropiate process_request or process_response, or
65 create a new method and call it from process_*.
66 """
67
68 def process_response(self, request, response):
69 # All following tests should be for html only!
70 if getattr(response, 'content_type', None) != "text/html":
71 # Get out early
72 return
73
74 # If the template contains a reference to
75 # /mgoblin_static/ instead of using
76 # /request.staticdirect(), error out here.
77 # This could probably be implemented as a grep on
78 # the shipped templates easier...
79 if response.text.find("/mgoblin_static/") >= 0:
80 raise AssertionError(
81 "Response HTML contains reference to /mgoblin_static/ "
82 "instead of staticdirect. Request was for: "
83 + request.full_path)
84
85 return
86
87
88 def get_app(request, paste_config=None, mgoblin_config=None):
89 """Create a MediaGoblin app for testing.
90
91 Args:
92 - request: Not an http request, but a pytest fixture request. We
93 use this to make temporary directories that pytest
94 automatically cleans up as needed.
95 - paste_config: particular paste config used by this application.
96 - mgoblin_config: particular mediagoblin config used by this
97 application.
98 """
99 paste_config = paste_config or TEST_SERVER_CONFIG
100 mgoblin_config = mgoblin_config or TEST_APP_CONFIG
101
102 # This is the directory we're copying the paste/mgoblin config stuff into
103 run_dir = request.config._tmpdirhandler.mktemp(
104 'mgoblin_app', numbered=True)
105 user_dev_dir = run_dir.mkdir('user_dev').strpath
106
107 new_paste_config = run_dir.join('paste.ini').strpath
108 new_mgoblin_config = run_dir.join('mediagoblin.ini').strpath
109 shutil.copyfile(paste_config, new_paste_config)
110 shutil.copyfile(mgoblin_config, new_mgoblin_config)
111
112 Session.rollback()
113 Session.remove()
114
115 # install user_dev directories
116 for directory in USER_DEV_DIRECTORIES_TO_SETUP:
117 full_dir = os.path.join(user_dev_dir, directory)
118 os.makedirs(full_dir)
119
120 # Get app config
121 global_config, validation_result = read_mediagoblin_config(new_mgoblin_config)
122 app_config = global_config['mediagoblin']
123
124 # Run database setup/migrations
125 run_dbupdate(app_config, global_config)
126
127 # setup app and return
128 test_app = loadapp(
129 'config:' + new_paste_config)
130
131 # Insert the TestingMeddleware, which can do some
132 # sanity checks on every request/response.
133 # Doing it this way is probably not the cleanest way.
134 # We'll fix it, when we have plugins!
135 mg_globals.app.meddleware.insert(0, TestingMeddleware(mg_globals.app))
136
137 app = TestApp(test_app)
138 return app
139
140
141 def install_fixtures_simple(db, fixtures):
142 """
143 Very simply install fixtures in the database
144 """
145 for collection_name, collection_fixtures in fixtures.iteritems():
146 collection = db[collection_name]
147 for fixture in collection_fixtures:
148 collection.insert(fixture)
149
150
151 def assert_db_meets_expected(db, expected):
152 """
153 Assert a database contains the things we expect it to.
154
155 Objects are found via 'id', so you should make sure your document
156 has an id.
157
158 Args:
159 - db: pymongo or mongokit database connection
160 - expected: the data we expect. Formatted like:
161 {'collection_name': [
162 {'id': 'foo',
163 'some_field': 'some_value'},]}
164 """
165 for collection_name, collection_data in expected.iteritems():
166 collection = db[collection_name]
167 for expected_document in collection_data:
168 document = collection.query.filter_by(id=expected_document['id']).first()
169 assert document is not None # make sure it exists
170 assert document == expected_document # make sure it matches
171
172
173 def fixture_add_user(username=u'chris', password=u'toast',
174 privileges=[], wants_comment_notification=True):
175 # Reuse existing user or create a new one
176 test_user = User.query.filter_by(username=username).first()
177 if test_user is None:
178 test_user = User()
179 test_user.username = username
180 test_user.email = username + u'@example.com'
181 if password is not None:
182 test_user.pw_hash = gen_password_hash(password)
183 test_user.wants_comment_notification = wants_comment_notification
184 for privilege in privileges:
185 query = Privilege.query.filter(Privilege.privilege_name==privilege)
186 if query.count():
187 test_user.all_privileges.append(query.one())
188
189 test_user.save()
190 # Reload
191 test_user = User.query.filter_by(username=username).first()
192
193 # ... and detach from session:
194 Session.expunge(test_user)
195
196 return test_user
197
198
199 def fixture_comment_subscription(entry, notify=True, send_email=None):
200 if send_email is None:
201 uploader = User.query.filter_by(id=entry.uploader).first()
202 send_email = uploader.wants_comment_notification
203
204 cs = CommentSubscription(
205 media_entry_id=entry.id,
206 user_id=entry.uploader,
207 notify=notify,
208 send_email=send_email)
209
210 cs.save()
211
212 cs = CommentSubscription.query.filter_by(id=cs.id).first()
213
214 Session.expunge(cs)
215
216 return cs
217
218
219 def fixture_add_comment_notification(entry_id, subject_id, user_id,
220 seen=False):
221 cn = CommentNotification(user_id=user_id,
222 seen=seen,
223 subject_id=subject_id)
224 cn.save()
225
226 cn = CommentNotification.query.filter_by(id=cn.id).first()
227
228 Session.expunge(cn)
229
230 return cn
231
232
233 def fixture_media_entry(title=u"Some title", slug=None,
234 uploader=None, save=True, gen_slug=True,
235 state=u'unprocessed', fake_upload=True,
236 expunge=True):
237 """
238 Add a media entry for testing purposes.
239
240 Caution: if you're adding multiple entries with fake_upload=True,
241 make sure you save between them... otherwise you'll hit an
242 IntegrityError from multiple newly-added-MediaEntries adding
243 FileKeynames at once. :)
244 """
245 if uploader is None:
246 uploader = fixture_add_user().id
247
248 entry = MediaEntry()
249 entry.title = title
250 entry.slug = slug
251 entry.uploader = uploader
252 entry.media_type = u'image'
253 entry.state = state
254
255 if fake_upload:
256 entry.media_files = {'thumb': ['a', 'b', 'c.jpg'],
257 'medium': ['d', 'e', 'f.png'],
258 'original': ['g', 'h', 'i.png']}
259 entry.media_type = u'mediagoblin.media_types.image'
260
261 if gen_slug:
262 entry.generate_slug()
263
264 if save:
265 entry.save()
266
267 if expunge:
268 entry = MediaEntry.query.filter_by(id=entry.id).first()
269
270 Session.expunge(entry)
271
272 return entry
273
274
275 def fixture_add_collection(name=u"My first Collection", user=None):
276 if user is None:
277 user = fixture_add_user()
278 coll = Collection.query.filter_by(creator=user.id, title=name).first()
279 if coll is not None:
280 return coll
281 coll = Collection()
282 coll.creator = user.id
283 coll.title = name
284 coll.generate_slug()
285 coll.save()
286
287 # Reload
288 Session.refresh(coll)
289
290 # ... and detach from session:
291 Session.expunge(coll)
292
293 return coll
294
295 def fixture_add_comment(author=None, media_entry=None, comment=None):
296 if author is None:
297 author = fixture_add_user().id
298
299 if media_entry is None:
300 media_entry = fixture_media_entry().id
301
302 if comment is None:
303 comment = \
304 'Auto-generated test comment by user #{0} on media #{0}'.format(
305 author, media_entry)
306
307 comment = MediaComment(author=author,
308 media_entry=media_entry,
309 content=comment)
310
311 comment.save()
312
313 Session.expunge(comment)
314
315 return comment
316
317 def fixture_add_comment_report(comment=None, reported_user=None,
318 reporter=None, created=None, report_content=None):
319 if comment is None:
320 comment = fixture_add_comment()
321
322 if reported_user is None:
323 reported_user = fixture_add_user()
324
325 if reporter is None:
326 reporter = fixture_add_user()
327
328 if created is None:
329 created=datetime.now()
330
331 if report_content is None:
332 report_content = \
333 'Auto-generated test report'
334
335 comment_report = CommentReport(comment=comment,
336 reported_user = reported_user,
337 reporter = reporter,
338 created = created,
339 report_content=report_content)
340
341 comment_report.save()
342
343 Session.expunge(comment_report)
344
345 return comment_report