test_reporting.py 6.6 KB

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