In this commit, I'm deleting the ArchivedReports object, at paroneyea's recom-
[mediagoblin:mediagoblin.git] / mediagoblin / tests / test_reporting.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 import pytest
18
19 from mediagoblin.tools import template
20 from mediagoblin.tests.tools import (fixture_add_user, fixture_media_entry,
21         fixture_add_comment, fixture_add_comment_report)
22 from mediagoblin.db.models import (MediaReport, CommentReport, User,
23     MediaComment)
24
25
26 class TestReportFiling:
27     @pytest.fixture(autouse=True)
28     def _setup(self, test_app):
29         self.test_app = test_app
30
31         fixture_add_user(u'allie',
32             privileges=[u'reporter',u'active'])
33         fixture_add_user(u'natalie',
34             privileges=[u'active', u'moderator'])
35
36     def login(self, username):
37         self.test_app.post(
38             '/auth/login/', {
39                 'username': username,
40                 'password': 'toast'})
41
42     def logout(self):
43         self.test_app.get('/auth/logout/')
44
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
56
57     def query_for_users(self):
58         return (User.query.filter(User.username==u'allie').first(),
59         User.query.filter(User.username==u'natalie').first())
60
61     def testMediaReports(self):
62         self.login(u'allie')
63         allie_user, natalie_user = self.query_for_users()
64         allie_id = allie_user.id
65
66         media_entry = fixture_media_entry(uploader=natalie_user.id,
67             state=u'processed')
68
69         mid = media_entry.id
70         media_uri_slug = '/u/{0}/m/{1}/'.format(natalie_user.username,
71                                                 media_entry.slug)
72
73         response = self.test_app.get(media_uri_slug + "report/")
74         assert response.status == "200 OK"
75
76         response, context = self.do_post(
77             {'report_reason':u'Testing Media Report',
78             'reporter_id':unicode(allie_id)},url= media_uri_slug + "report/")
79
80         assert response.status == "302 FOUND"
81
82         media_report = MediaReport.query.first()
83
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
90         assert media_report.discriminator == 'media_report'
91
92     def testCommentReports(self):
93         self.login(u'allie')
94         allie_user, natalie_user = self.query_for_users()
95         allie_id = allie_user.id
96
97         media_entry = fixture_media_entry(uploader=natalie_user.id,
98             state=u'processed')
99         mid = media_entry.id
100         fixture_add_comment(media_entry=mid,
101             author=natalie_user.id)
102         comment = MediaComment.query.first()
103
104         comment_uri_slug = '/u/{0}/m/{1}/c/{2}/'.format(natalie_user.username,
105                                                 media_entry.slug,
106                                                 comment.id)
107
108         response = self.test_app.get(comment_uri_slug + "report/")
109         assert response.status == "200 OK"
110
111         response, context = self.do_post({
112             'report_reason':u'Testing Comment Report',
113             'reporter_id':unicode(allie_id)},url= comment_uri_slug + "report/")
114
115         assert response.status == "302 FOUND"
116
117         comment_report = CommentReport.query.first()
118
119         allie_user, natalie_user = self.query_for_users()
120         assert comment_report is not None
121         assert comment_report.report_content == u'Testing Comment Report'
122         assert comment_report.reporter_id == allie_id
123         assert comment_report.reported_user_id == natalie_user.id
124         assert comment_report.created is not None
125         assert comment_report.discriminator == 'comment_report'
126
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
131
132         fixture_add_comment(author=allie_user.id,
133             comment=u'Comment will be removed')
134         test_comment = MediaComment.query.filter(
135             MediaComment.author==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 = CommentReport.query.filter(
141             CommentReport.reported_user==allie_user).first()
142
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))
149
150         assert response.status == "302 FOUND"
151         allie_user, natalie_user = self.query_for_users()
152
153         archived_report = CommentReport.query.filter(
154             CommentReport.reported_user==allie_user).first()
155
156         assert CommentReport.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 .<br>natalie banned user allie indefinitely.<br>natalie deleted the comment.'
165         assert archived_report.discriminator == 'comment_report'
166