summaryrefslogtreecommitdiff
path: root/spec/controllers/abuse_reports_controller_spec.rb
blob: 0faab8d7ff0b2e3885325bf5a9f48947bcc55111 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
require 'spec_helper'

describe AbuseReportsController do
  let(:reporter)    { create(:user) }
  let(:user)        { create(:user) }
  let(:message)     { "This user is a spammer" }

  before do
    sign_in(reporter)
  end

  describe "POST create" do
    context "with admin notification email set" do
      let(:admin_email) { "admin@example.com"}

      before(:each) do
        stub_application_setting(admin_notification_email: admin_email)
      end

      it "sends a notification email" do
        post :create,
          abuse_report: {
            user_id: user.id,
            message: message
          }

        email = ActionMailer::Base.deliveries.last

        expect(email.to).to eq([admin_email])
        expect(email.subject).to include(user.username)
        expect(email.text_part.body).to include(message)
      end

      it "saves the abuse report" do
        expect do
          post :create,
            abuse_report: {
              user_id: user.id,
              message: message
            }
        end.to change { AbuseReport.count }.by(1)
      end
    end

    context "without admin notification email set" do
      before(:each) do
        stub_application_setting(admin_notification_email: nil)
      end

      it "does not send a notification email" do
        expect do
          post :create,
            abuse_report: {
              user_id: user.id,
              message: message
            }
        end.not_to change { ActionMailer::Base.deliveries.count }
      end

      it "saves the abuse report" do
        expect do
          post :create,
            abuse_report: {
              user_id: user.id,
              message: message
            }
        end.to change { AbuseReport.count }.by(1)
      end
    end
  end

end