|
| 1 | +defmodule GroupherServer.Test.CMS.AbuseReports.RadarReport do |
| 2 | + @moduledoc false |
| 3 | + |
| 4 | + use GroupherServer.TestTools |
| 5 | + |
| 6 | + alias Helper.ORM |
| 7 | + alias GroupherServer.CMS |
| 8 | + alias CMS.Model.Radar |
| 9 | + |
| 10 | + setup do |
| 11 | + {:ok, user} = db_insert(:user) |
| 12 | + {:ok, user2} = db_insert(:user) |
| 13 | + |
| 14 | + {:ok, community} = db_insert(:community) |
| 15 | + radar_attrs = mock_attrs(:radar, %{community_id: community.id}) |
| 16 | + |
| 17 | + {:ok, ~m(user user2 community radar_attrs)a} |
| 18 | + end |
| 19 | + |
| 20 | + describe "[article radar report/unreport]" do |
| 21 | + test "list article reports should work", ~m(community user user2 radar_attrs)a do |
| 22 | + {:ok, radar} = CMS.create_article(community, :radar, radar_attrs, user) |
| 23 | + {:ok, _report} = CMS.report_article(:radar, radar.id, "reason", "attr_info", user) |
| 24 | + {:ok, _report} = CMS.report_article(:radar, radar.id, "reason", "attr_info", user2) |
| 25 | + |
| 26 | + filter = %{content_type: :radar, content_id: radar.id, page: 1, size: 20} |
| 27 | + {:ok, all_reports} = CMS.paged_reports(filter) |
| 28 | + |
| 29 | + report = all_reports.entries |> List.first() |
| 30 | + assert report.article.id == radar.id |
| 31 | + assert report.article.thread == "WORKS" |
| 32 | + end |
| 33 | + |
| 34 | + test "report a radar should have a abuse report record", ~m(community user radar_attrs)a do |
| 35 | + {:ok, radar} = CMS.create_article(community, :radar, radar_attrs, user) |
| 36 | + {:ok, _report} = CMS.report_article(:radar, radar.id, "reason", "attr_info", user) |
| 37 | + |
| 38 | + filter = %{content_type: :radar, content_id: radar.id, page: 1, size: 20} |
| 39 | + {:ok, all_reports} = CMS.paged_reports(filter) |
| 40 | + |
| 41 | + report = List.first(all_reports.entries) |
| 42 | + report_cases = report.report_cases |
| 43 | + |
| 44 | + assert report.article.id == radar.id |
| 45 | + assert all_reports.total_count == 1 |
| 46 | + assert report.report_cases_count == 1 |
| 47 | + assert List.first(report_cases).user.login == user.login |
| 48 | + |
| 49 | + {:ok, radar} = ORM.find(Radar, radar.id) |
| 50 | + assert radar.meta.reported_count == 1 |
| 51 | + assert user.id in radar.meta.reported_user_ids |
| 52 | + end |
| 53 | + |
| 54 | + test "can undo a report", ~m(community user radar_attrs)a do |
| 55 | + {:ok, radar} = CMS.create_article(community, :radar, radar_attrs, user) |
| 56 | + {:ok, _report} = CMS.report_article(:radar, radar.id, "reason", "attr_info", user) |
| 57 | + {:ok, _report} = CMS.undo_report_article(:radar, radar.id, user) |
| 58 | + |
| 59 | + filter = %{content_type: :radar, content_id: radar.id, page: 1, size: 20} |
| 60 | + {:ok, all_reports} = CMS.paged_reports(filter) |
| 61 | + assert all_reports.total_count == 0 |
| 62 | + |
| 63 | + {:ok, radar} = ORM.find(Radar, radar.id) |
| 64 | + assert user.id not in radar.meta.reported_user_ids |
| 65 | + end |
| 66 | + |
| 67 | + test "can undo a existed report", ~m(community user user2 radar_attrs)a do |
| 68 | + {:ok, radar} = CMS.create_article(community, :radar, radar_attrs, user) |
| 69 | + {:ok, _report} = CMS.report_article(:radar, radar.id, "reason", "attr_info", user) |
| 70 | + {:ok, _report} = CMS.report_article(:radar, radar.id, "reason", "attr_info", user2) |
| 71 | + {:ok, _report} = CMS.undo_report_article(:radar, radar.id, user) |
| 72 | + |
| 73 | + filter = %{content_type: :radar, content_id: radar.id, page: 1, size: 20} |
| 74 | + {:ok, all_reports} = CMS.paged_reports(filter) |
| 75 | + assert all_reports.total_count == 1 |
| 76 | + |
| 77 | + {:ok, radar} = ORM.find(Radar, radar.id) |
| 78 | + |
| 79 | + assert user2.id in radar.meta.reported_user_ids |
| 80 | + assert user.id not in radar.meta.reported_user_ids |
| 81 | + end |
| 82 | + |
| 83 | + test "can undo a report with other user report it too", |
| 84 | + ~m(community user user2 radar_attrs)a do |
| 85 | + {:ok, radar} = CMS.create_article(community, :radar, radar_attrs, user) |
| 86 | + {:ok, _report} = CMS.report_article(:radar, radar.id, "reason", "attr_info", user) |
| 87 | + {:ok, _report} = CMS.report_article(:radar, radar.id, "reason", "attr_info", user2) |
| 88 | + |
| 89 | + filter = %{content_type: :radar, content_id: radar.id, page: 1, size: 20} |
| 90 | + {:ok, all_reports} = CMS.paged_reports(filter) |
| 91 | + assert all_reports.total_count == 1 |
| 92 | + |
| 93 | + report = all_reports.entries |> List.first() |
| 94 | + assert report.report_cases |> length == 2 |
| 95 | + assert Enum.any?(report.report_cases, &(&1.user.login == user.login)) |
| 96 | + assert Enum.any?(report.report_cases, &(&1.user.login == user2.login)) |
| 97 | + |
| 98 | + {:ok, _report} = CMS.undo_report_article(:radar, radar.id, user) |
| 99 | + |
| 100 | + filter = %{content_type: :radar, content_id: radar.id, page: 1, size: 20} |
| 101 | + {:ok, all_reports} = CMS.paged_reports(filter) |
| 102 | + assert all_reports.total_count == 1 |
| 103 | + |
| 104 | + report = all_reports.entries |> List.first() |
| 105 | + assert report.report_cases |> length == 1 |
| 106 | + assert Enum.any?(report.report_cases, &(&1.user.login == user2.login)) |
| 107 | + end |
| 108 | + |
| 109 | + test "different user report a comment should have same report with different report cases", |
| 110 | + ~m(community user user2 radar_attrs)a do |
| 111 | + {:ok, radar} = CMS.create_article(community, :radar, radar_attrs, user) |
| 112 | + |
| 113 | + {:ok, _report} = CMS.report_article(:radar, radar.id, "reason", "attr_info", user) |
| 114 | + {:ok, _report} = CMS.report_article(:radar, radar.id, "reason2", "attr_info 2", user2) |
| 115 | + |
| 116 | + filter = %{content_type: :radar, content_id: radar.id, page: 1, size: 20} |
| 117 | + {:ok, all_reports} = CMS.paged_reports(filter) |
| 118 | + |
| 119 | + report = List.first(all_reports.entries) |
| 120 | + report_cases = report.report_cases |
| 121 | + |
| 122 | + assert all_reports.total_count == 1 |
| 123 | + assert length(report_cases) == 2 |
| 124 | + assert report.report_cases_count == 2 |
| 125 | + |
| 126 | + assert List.first(report_cases).user.login == user.login |
| 127 | + assert List.last(report_cases).user.login == user2.login |
| 128 | + end |
| 129 | + |
| 130 | + test "same user can not report a comment twice", ~m(community radar_attrs user)a do |
| 131 | + {:ok, radar} = CMS.create_article(community, :radar, radar_attrs, user) |
| 132 | + |
| 133 | + {:ok, _report} = CMS.report_article(:radar, radar.id, "reason", "attr_info", user) |
| 134 | + assert {:error, _report} = CMS.report_article(:radar, radar.id, "reason", "attr_info", user) |
| 135 | + end |
| 136 | + end |
| 137 | +end |
0 commit comments