Commit | Line | Data |
---|---|---|
968dd9e7 | 1 | # GNU MediaGoblin -- federated, autonomous media hosting |
cf29e8a8 | 2 | # Copyright (C) 2011, 2012 MediaGoblin contributors. See AUTHORS. |
968dd9e7 E |
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 | ||
5e5d4458 JT |
17 | import pytz |
18 | import datetime | |
19 | ||
20 | from werkzeug.datastructures import FileStorage | |
21 | ||
22 | from .resources import GOOD_JPG | |
3f931680 | 23 | from mediagoblin.db.base import Session |
5e5d4458 JT |
24 | from mediagoblin.media_types import sniff_media |
25 | from mediagoblin.submit.lib import new_upload_entry | |
26 | from mediagoblin.submit.task import collect_garbage | |
64a456a4 | 27 | from mediagoblin.db.models import User, MediaEntry, TextComment, Comment |
5c2ece74 | 28 | from mediagoblin.tests.tools import fixture_add_user, fixture_media_entry |
e9b4e500 | 29 | |
968dd9e7 | 30 | |
5c2ece74 | 31 | def test_404_for_non_existent(test_app): |
b97144dc | 32 | res = test_app.get('/does-not-exist/', expect_errors=True) |
7d503a89 | 33 | assert res.status_int == 404 |
e9b4e500 E |
34 | |
35 | ||
5c2ece74 | 36 | def test_user_deletes_other_comments(test_app): |
e9b4e500 E |
37 | user_a = fixture_add_user(u"chris_a") |
38 | user_b = fixture_add_user(u"chris_b") | |
39 | ||
2d7b6bde | 40 | media_a = fixture_media_entry(uploader=user_a.id, save=False, |
56d13467 | 41 | expunge=False, fake_upload=False) |
2d7b6bde | 42 | media_b = fixture_media_entry(uploader=user_b.id, save=False, |
56d13467 | 43 | expunge=False, fake_upload=False) |
3f931680 E |
44 | Session.add(media_a) |
45 | Session.add(media_b) | |
46 | Session.flush() | |
e9b4e500 E |
47 | |
48 | # Create all 4 possible comments: | |
64a456a4 JT |
49 | for u in (user_a, user_b): |
50 | for m in (media_a, media_b): | |
51 | cmt = TextComment() | |
52 | cmt.actor = u.id | |
e9b4e500 | 53 | cmt.content = u"Some Comment" |
3f931680 | 54 | Session.add(cmt) |
64a456a4 JT |
55 | # think i need this to get the command ID |
56 | Session.flush() | |
57 | ||
58 | link = Comment() | |
59 | link.target = m | |
60 | link.comment = cmt | |
61 | Session.add(link) | |
3f931680 E |
62 | |
63 | Session.flush() | |
e9b4e500 E |
64 | |
65 | usr_cnt1 = User.query.count() | |
66 | med_cnt1 = MediaEntry.query.count() | |
161bc6b2 | 67 | cmt_cnt1 = Comment.query.count() |
e9b4e500 | 68 | |
3f931680 | 69 | User.query.get(user_a.id).delete(commit=False) |
e9b4e500 E |
70 | |
71 | usr_cnt2 = User.query.count() | |
72 | med_cnt2 = MediaEntry.query.count() | |
161bc6b2 | 73 | cmt_cnt2 = Comment.query.count() |
e9b4e500 E |
74 | |
75 | # One user deleted | |
7d503a89 | 76 | assert usr_cnt2 == usr_cnt1 - 1 |
e9b4e500 | 77 | # One media gone |
7d503a89 | 78 | assert med_cnt2 == med_cnt1 - 1 |
e9b4e500 | 79 | # Three of four comments gone. |
7d503a89 | 80 | assert cmt_cnt2 == cmt_cnt1 - 3 |
e9b4e500 E |
81 | |
82 | User.query.get(user_b.id).delete() | |
83 | ||
84 | usr_cnt2 = User.query.count() | |
85 | med_cnt2 = MediaEntry.query.count() | |
161bc6b2 | 86 | cmt_cnt2 = Comment.query.count() |
e9b4e500 E |
87 | |
88 | # All users gone | |
7d503a89 | 89 | assert usr_cnt2 == usr_cnt1 - 2 |
e9b4e500 | 90 | # All media gone |
7d503a89 | 91 | assert med_cnt2 == med_cnt1 - 2 |
e9b4e500 | 92 | # All comments gone |
7d503a89 | 93 | assert cmt_cnt2 == cmt_cnt1 - 4 |
df5b142a E |
94 | |
95 | ||
5c2ece74 | 96 | def test_media_deletes_broken_attachment(test_app): |
df5b142a E |
97 | user_a = fixture_add_user(u"chris_a") |
98 | ||
2d7b6bde | 99 | media = fixture_media_entry(uploader=user_a.id, save=False, expunge=False) |
df5b142a E |
100 | media.attachment_files.append(dict( |
101 | name=u"some name", | |
102 | filepath=[u"does", u"not", u"exist"], | |
103 | )) | |
104 | Session.add(media) | |
105 | Session.flush() | |
106 | ||
107 | MediaEntry.query.get(media.id).delete() | |
108 | User.query.get(user_a.id).delete() | |
5e5d4458 JT |
109 | |
110 | def test_garbage_collection_task(test_app): | |
111 | """ Test old media entry are removed by GC task """ | |
112 | user = fixture_add_user() | |
113 | ||
114 | # Create a media entry that's unprocessed and over an hour old. | |
115 | entry_id = 72 | |
116 | now = datetime.datetime.now(pytz.UTC) | |
117 | file_data = FileStorage( | |
118 | stream=open(GOOD_JPG, "rb"), | |
119 | filename="mah_test.jpg", | |
120 | content_type="image/jpeg" | |
121 | ) | |
122 | ||
123 | # Find media manager | |
124 | media_type, media_manager = sniff_media(file_data, "mah_test.jpg") | |
125 | entry = new_upload_entry(user) | |
126 | entry.id = entry_id | |
127 | entry.title = "Mah Image" | |
128 | entry.slug = "slugy-slug-slug" | |
129 | entry.media_type = 'image' | |
130 | entry.created = now - datetime.timedelta(days=2) | |
131 | entry.save() | |
132 | ||
133 | # Validate the model exists | |
134 | assert MediaEntry.query.filter_by(id=entry_id).first() is not None | |
135 | ||
136 | # Call the garbage collection task | |
137 | collect_garbage() | |
138 | ||
139 | # Now validate the image has been deleted | |
140 | assert MediaEntry.query.filter_by(id=entry_id).first() is None | |
2104c3e0 JT |
141 | |
142 | def test_comments_removed_when_graveyarded(test_app): | |
143 | """ Checks comments which are tombstones are removed from collection """ | |
144 | user = fixture_add_user() | |
145 | media = fixture_media_entry( | |
146 | uploader=user.id, | |
147 | expunge=False, | |
148 | fake_upload=False | |
149 | ) | |
150 | ||
151 | # Add the TextComment | |
152 | comment = TextComment() | |
153 | comment.actor = user.id | |
154 | comment.content = u"This is a comment that will be deleted." | |
155 | comment.save() | |
156 | ||
157 | # Add a link for the comment | |
158 | link = Comment() | |
159 | link.target = media | |
160 | link.comment = comment | |
161 | link.save() | |
162 | ||
163 | # First double check it's there and all is well... | |
164 | assert Comment.query.filter_by(target_id=link.target_id).first() is not None | |
165 | ||
166 | # Now delete the comment. | |
167 | comment.delete() | |
168 | ||
169 | # Verify this also deleted the Comment link, ergo there is no comment left. | |
170 | assert Comment.query.filter_by(target_id=link.target_id).first() is None | |
171 |