Fix #5391 - Alembic migrations would only work for SQLite
[larjonas-mediagoblin.git] / mediagoblin / tests / test_reporting.py
blob803fc84914f63203f09db57d71d5d76c57a5d363
1 # GNU MediaGoblin -- federated, autonomous media hosting
2 # Copyright (C) 2011, 2012 MediaGoblin contributors. See AUTHORS.
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.
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.
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/>.
17 import pytest
18 import six
20 from mediagoblin.tools import template
21 from mediagoblin.tests.tools import (fixture_add_user, fixture_media_entry,
22 fixture_add_comment, fixture_add_comment_report)
23 from mediagoblin.db.models import Report, User, LocalUser, TextComment
26 class TestReportFiling:
27 @pytest.fixture(autouse=True)
28 def _setup(self, test_app):
29 self.test_app = test_app
31 fixture_add_user(u'allie',
32 privileges=[u'reporter',u'active'])
33 fixture_add_user(u'natalie',
34 privileges=[u'active', u'moderator'])
36 def login(self, username):
37 self.test_app.post(
38 '/auth/login/', {
39 'username': username,
40 'password': 'toast'})
42 def logout(self):
43 self.test_app.get('/auth/logout/')
45 def do_post(self, data, *context_keys, **kwargs):
46 url = kwargs.pop('url', '/submit/')
47 do_follow = kwargs.pop('do_follow', False)
48 template.clear_test_template_context()
49 response = self.test_app.post(url, data, **kwargs)
50 if do_follow:
51 response.follow()
52 context_data = template.TEMPLATE_TEST_CONTEXT
53 for key in context_keys:
54 context_data = context_data[key]
55 return response, context_data
57 def query_for_users(self):
58 return (LocalUser.query.filter(LocalUser.username==u'allie').first(),
59 LocalUser.query.filter(LocalUser.username==u'natalie').first())
61 def testMediaReports(self):
62 self.login(u'allie')
63 allie_user, natalie_user = self.query_for_users()
64 allie_id = allie_user.id
66 media_entry = fixture_media_entry(uploader=natalie_user.id,
67 state=u'processed')
69 mid = media_entry.id
70 media_uri_slug = '/u/{0}/m/{1}/'.format(natalie_user.username,
71 media_entry.slug)
73 response = self.test_app.get(media_uri_slug + "report/")
74 assert response.status == "200 OK"
76 response, context = self.do_post(
77 {'report_reason':u'Testing Media Report',
78 'reporter_id':six.text_type(allie_id)},url= media_uri_slug + "report/")
80 assert response.status == "302 FOUND"
82 media_report = Report.query.first()
84 allie_user, natalie_user = self.query_for_users()
85 assert media_report is not None
86 assert media_report.report_content == u'Testing Media Report'
87 assert media_report.reporter_id == allie_id
88 assert media_report.reported_user_id == natalie_user.id
89 assert media_report.created is not None
91 def testCommentReports(self):
92 self.login(u'allie')
93 allie_user, natalie_user = self.query_for_users()
94 allie_id = allie_user.id
96 media_entry = fixture_media_entry(uploader=natalie_user.id,
97 state=u'processed')
98 mid = media_entry.id
99 fixture_add_comment(
100 media_entry=media_entry,
101 author=natalie_user.id
103 comment = TextComment.query.first()
105 comment_uri_slug = '/u/{0}/m/{1}/c/{2}/'.format(natalie_user.username,
106 media_entry.slug,
107 comment.id)
109 response = self.test_app.get(comment_uri_slug + "report/")
110 assert response.status == "200 OK"
112 response, context = self.do_post({
113 'report_reason':u'Testing Comment Report',
114 'reporter_id':six.text_type(allie_id)},url= comment_uri_slug + "report/")
116 assert response.status == "302 FOUND"
118 comment_report = Report.query.first()
120 allie_user, natalie_user = self.query_for_users()
121 assert comment_report is not None
122 assert comment_report.report_content == u'Testing Comment Report'
123 assert comment_report.reporter_id == allie_id
124 assert comment_report.reported_user_id == natalie_user.id
125 assert comment_report.created is not None
127 def testArchivingReports(self):
128 self.login(u'natalie')
129 allie_user, natalie_user = self.query_for_users()
130 allie_id, natalie_id = allie_user.id, natalie_user.id
132 fixture_add_comment(author=allie_user.id,
133 comment=u'Comment will be removed')
134 test_comment = TextComment.query.filter(
135 TextComment.actor==allie_user.id).first()
136 fixture_add_comment_report(comment=test_comment,
137 reported_user=allie_user,
138 report_content=u'Testing Archived Reports #1',
139 reporter=natalie_user)
140 comment_report = Report.query.filter(
141 Report.reported_user==allie_user).first()
143 assert comment_report.report_content == u'Testing Archived Reports #1'
144 response, context = self.do_post(
145 {'action_to_resolve':[u'userban', u'delete'],
146 'targeted_user':allie_user.id,
147 'resolution_content':u'This is a test of archiving reports.'},
148 url='/mod/reports/{0}/'.format(comment_report.id))
150 assert response.status == "302 FOUND"
151 allie_user, natalie_user = self.query_for_users()
153 archived_report = Report.query.filter(
154 Report.reported_user==allie_user).first()
156 assert Report.query.count() != 0
157 assert archived_report is not None
158 assert archived_report.report_content == u'Testing Archived Reports #1'
159 assert archived_report.reporter_id == natalie_id
160 assert archived_report.reported_user_id == allie_id
161 assert archived_report.created is not None
162 assert archived_report.resolved is not None
163 assert archived_report.result == u'''This is a test of archiving reports.
164 natalie banned user allie indefinitely.
165 natalie deleted the comment.'''