Fix #984 - Improvements to Activity and ActivityIntermediator
[mediagoblin.git] / mediagoblin / tests / test_reporting.py
CommitLineData
dfd66b78 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
17import pytest
e49b7e02 18import six
dfd66b78 19
20from mediagoblin.tools import template
21from mediagoblin.tests.tools import (fixture_add_user, fixture_media_entry,
22 fixture_add_comment, fixture_add_comment_report)
23from mediagoblin.db.models import (MediaReport, CommentReport, User,
c9068870 24 MediaComment)
dfd66b78 25
26
27class TestReportFiling:
28 @pytest.fixture(autouse=True)
29 def _setup(self, test_app):
30 self.test_app = test_app
31
32 fixture_add_user(u'allie',
33 privileges=[u'reporter',u'active'])
34 fixture_add_user(u'natalie',
35 privileges=[u'active', u'moderator'])
36
37 def login(self, username):
38 self.test_app.post(
39 '/auth/login/', {
40 'username': username,
41 'password': 'toast'})
42
43 def logout(self):
44 self.test_app.get('/auth/logout/')
45
46 def do_post(self, data, *context_keys, **kwargs):
47 url = kwargs.pop('url', '/submit/')
48 do_follow = kwargs.pop('do_follow', False)
49 template.clear_test_template_context()
50 response = self.test_app.post(url, data, **kwargs)
51 if do_follow:
52 response.follow()
53 context_data = template.TEMPLATE_TEST_CONTEXT
54 for key in context_keys:
55 context_data = context_data[key]
56 return response, context_data
57
58 def query_for_users(self):
59 return (User.query.filter(User.username==u'allie').first(),
60 User.query.filter(User.username==u'natalie').first())
61
62 def testMediaReports(self):
63 self.login(u'allie')
64 allie_user, natalie_user = self.query_for_users()
65 allie_id = allie_user.id
66
67 media_entry = fixture_media_entry(uploader=natalie_user.id,
68 state=u'processed')
69
70 mid = media_entry.id
71 media_uri_slug = '/u/{0}/m/{1}/'.format(natalie_user.username,
72 media_entry.slug)
73
74 response = self.test_app.get(media_uri_slug + "report/")
75 assert response.status == "200 OK"
76
77 response, context = self.do_post(
78 {'report_reason':u'Testing Media Report',
e49b7e02 79 'reporter_id':six.text_type(allie_id)},url= media_uri_slug + "report/")
dfd66b78 80
81 assert response.status == "302 FOUND"
82
83 media_report = MediaReport.query.first()
84
85 allie_user, natalie_user = self.query_for_users()
86 assert media_report is not None
87 assert media_report.report_content == u'Testing Media Report'
88 assert media_report.reporter_id == allie_id
89 assert media_report.reported_user_id == natalie_user.id
90 assert media_report.created is not None
91 assert media_report.discriminator == 'media_report'
92
93 def testCommentReports(self):
94 self.login(u'allie')
95 allie_user, natalie_user = self.query_for_users()
96 allie_id = allie_user.id
97
98 media_entry = fixture_media_entry(uploader=natalie_user.id,
99 state=u'processed')
100 mid = media_entry.id
101 fixture_add_comment(media_entry=mid,
102 author=natalie_user.id)
103 comment = MediaComment.query.first()
104
105 comment_uri_slug = '/u/{0}/m/{1}/c/{2}/'.format(natalie_user.username,
106 media_entry.slug,
107 comment.id)
108
109 response = self.test_app.get(comment_uri_slug + "report/")
110 assert response.status == "200 OK"
111
112 response, context = self.do_post({
113 'report_reason':u'Testing Comment Report',
e49b7e02 114 'reporter_id':six.text_type(allie_id)},url= comment_uri_slug + "report/")
dfd66b78 115
116 assert response.status == "302 FOUND"
117
118 comment_report = CommentReport.query.first()
119
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
126 assert comment_report.discriminator == 'comment_report'
127
128 def testArchivingReports(self):
129 self.login(u'natalie')
130 allie_user, natalie_user = self.query_for_users()
131 allie_id, natalie_id = allie_user.id, natalie_user.id
132
133 fixture_add_comment(author=allie_user.id,
134 comment=u'Comment will be removed')
135 test_comment = MediaComment.query.filter(
136 MediaComment.author==allie_user.id).first()
137 fixture_add_comment_report(comment=test_comment,
138 reported_user=allie_user,
139 report_content=u'Testing Archived Reports #1',
140 reporter=natalie_user)
141 comment_report = CommentReport.query.filter(
142 CommentReport.reported_user==allie_user).first()
143
144 assert comment_report.report_content == u'Testing Archived Reports #1'
145 response, context = self.do_post(
146 {'action_to_resolve':[u'userban', u'delete'],
147 'targeted_user':allie_user.id,
148 'resolution_content':u'This is a test of archiving reports.'},
149 url='/mod/reports/{0}/'.format(comment_report.id))
150
151 assert response.status == "302 FOUND"
c9068870 152 allie_user, natalie_user = self.query_for_users()
dfd66b78 153
c9068870 154 archived_report = CommentReport.query.filter(
155 CommentReport.reported_user==allie_user).first()
dfd66b78 156
c9068870 157 assert CommentReport.query.count() != 0
dfd66b78 158 assert archived_report is not None
159 assert archived_report.report_content == u'Testing Archived Reports #1'
160 assert archived_report.reporter_id == natalie_id
161 assert archived_report.reported_user_id == allie_id
162 assert archived_report.created is not None
163 assert archived_report.resolved is not None
6483b370 164 assert archived_report.result == u'''This is a test of archiving reports.
165natalie banned user allie indefinitely.
166natalie deleted the comment.'''
c9068870 167 assert archived_report.discriminator == 'comment_report'
dfd66b78 168