| 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 | from nose.tools import assert_equal |
| 18 | |
| 19 | from mediagoblin.db.base import Session |
| 20 | from mediagoblin.db.models import User, MediaEntry, MediaComment |
| 21 | from mediagoblin.tests.tools import get_app, \ |
| 22 | fixture_add_user, fixture_media_entry |
| 23 | |
| 24 | |
| 25 | def test_404_for_non_existent(): |
| 26 | test_app = get_app(dump_old_app=False) |
| 27 | res = test_app.get('/does-not-exist/', expect_errors=True) |
| 28 | assert_equal(res.status_int, 404) |
| 29 | |
| 30 | |
| 31 | def test_user_deletes_other_comments(): |
| 32 | get_app() # gotta init the db and etc |
| 33 | user_a = fixture_add_user(u"chris_a") |
| 34 | user_b = fixture_add_user(u"chris_b") |
| 35 | |
| 36 | media_a = fixture_media_entry(uploader=user_a.id, save=False) |
| 37 | media_b = fixture_media_entry(uploader=user_b.id, save=False) |
| 38 | Session.add(media_a) |
| 39 | Session.add(media_b) |
| 40 | Session.flush() |
| 41 | |
| 42 | # Create all 4 possible comments: |
| 43 | for u_id in (user_a.id, user_b.id): |
| 44 | for m_id in (media_a.id, media_b.id): |
| 45 | cmt = MediaComment() |
| 46 | cmt.media_entry = m_id |
| 47 | cmt.author = u_id |
| 48 | cmt.content = u"Some Comment" |
| 49 | Session.add(cmt) |
| 50 | |
| 51 | Session.flush() |
| 52 | |
| 53 | usr_cnt1 = User.query.count() |
| 54 | med_cnt1 = MediaEntry.query.count() |
| 55 | cmt_cnt1 = MediaComment.query.count() |
| 56 | |
| 57 | User.query.get(user_a.id).delete(commit=False) |
| 58 | |
| 59 | usr_cnt2 = User.query.count() |
| 60 | med_cnt2 = MediaEntry.query.count() |
| 61 | cmt_cnt2 = MediaComment.query.count() |
| 62 | |
| 63 | # One user deleted |
| 64 | assert_equal(usr_cnt2, usr_cnt1 - 1) |
| 65 | # One media gone |
| 66 | assert_equal(med_cnt2, med_cnt1 - 1) |
| 67 | # Three of four comments gone. |
| 68 | assert_equal(cmt_cnt2, cmt_cnt1 - 3) |
| 69 | |
| 70 | User.query.get(user_b.id).delete() |
| 71 | |
| 72 | usr_cnt2 = User.query.count() |
| 73 | med_cnt2 = MediaEntry.query.count() |
| 74 | cmt_cnt2 = MediaComment.query.count() |
| 75 | |
| 76 | # All users gone |
| 77 | assert_equal(usr_cnt2, usr_cnt1 - 2) |
| 78 | # All media gone |
| 79 | assert_equal(med_cnt2, med_cnt1 - 2) |
| 80 | # All comments gone |
| 81 | assert_equal(cmt_cnt2, cmt_cnt1 - 4) |
| 82 | |
| 83 | |
| 84 | def test_media_deletes_broken_attachment(): |
| 85 | get_app() # gotta init the db and etc |
| 86 | user_a = fixture_add_user(u"chris_a") |
| 87 | |
| 88 | media = fixture_media_entry(uploader=user_a.id, save=False) |
| 89 | media.attachment_files.append(dict( |
| 90 | name=u"some name", |
| 91 | filepath=[u"does", u"not", u"exist"], |
| 92 | )) |
| 93 | Session.add(media) |
| 94 | Session.flush() |
| 95 | |
| 96 | MediaEntry.query.get(media.id).delete() |
| 97 | User.query.get(user_a.id).delete() |