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