aboutsummaryrefslogtreecommitdiffstats
path: root/spec/controllers/reports_controller_spec.rb
blob: fa8c72eaa9e03c9c4f9c8e0d0dc647caded36170 (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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
require 'spec_helper'

describe ReportsController, "when reporting a request when not logged in" do
    it "should only allow logged-in users to report requests" do
        post :create, :request_id => info_requests(:badger_request).url_title, :reason => "my reason"

        flash[:notice].should =~ /You need to be logged in/
        response.should redirect_to show_request_path(:url_title => info_requests(:badger_request).url_title)
    end
end

describe ReportsController, "when reporting a request (logged in)" do
    render_views

    before do
        @user = users(:robin_user)
        session[:user_id] = @user.id
    end

    it "should 404 for non-existent requests" do
      lambda {
        post :create, :request_id => "hjksfdhjk_louytu_qqxxx"
      }.should raise_error(ActiveRecord::RecordNotFound)
    end

    it "should mark a request as having been reported" do
        ir = info_requests(:badger_request)
        title = ir.url_title
        ir.attention_requested.should == false

        post :create, :request_id => title, :reason => "my reason"
        response.should redirect_to show_request_path(:url_title => title)

        ir.reload
        ir.attention_requested.should == true
        ir.described_state.should == "attention_requested"
    end

    it "should pass on the reason and message" do
        info_request = mock_model(InfoRequest, :url_title => "foo", :attention_requested= => nil, :save! => nil)
        InfoRequest.should_receive(:find_by_url_title!).with("foo").and_return(info_request)
        info_request.should_receive(:report!).with("Not valid request", "It's just not", @user)
        post :create, :request_id => "foo", :reason => "Not valid request", :message => "It's just not"
    end

    it "should not allow a request to be reported twice" do
        title = info_requests(:badger_request).url_title

        post :create, :request_id => title, :reason => "my reason"
        response.should redirect_to show_request_url(:url_title => title)

        post :create, :request_id => title, :reason => "my reason"
        response.should redirect_to show_request_url(:url_title => title)
        flash[:notice].should =~ /has already been reported/
    end

    it "should send an email from the reporter to admins" do
        ir = info_requests(:badger_request)
        title = ir.url_title
        post :create, :request_id => title, :reason => "my reason"
        deliveries = ActionMailer::Base.deliveries
        deliveries.size.should == 1
        mail = deliveries[0]
        mail.subject.should =~ /attention_requested/
        mail.from.should include(@user.email)
        mail.body.should include(@user.name)
    end

    it "should force the user to pick a reason" do
        info_request = mock_model(InfoRequest, :report! => nil, :url_title => "foo",
            :report_reasons => ["Not FOIish enough"])
        InfoRequest.should_receive(:find_by_url_title!).with("foo").and_return(info_request)

        post :create, :request_id => "foo", :reason => ""
        response.should render_template("new")
        flash[:error].should == "Please choose a reason"
    end
end

describe ReportsController, "#new_report_request" do
    let(:info_request) { mock_model(InfoRequest, :url_title => "foo") }
    before :each do
        InfoRequest.should_receive(:find_by_url_title!).with("foo").and_return(info_request)
    end

    context "not logged in" do
        it "should require the user to be logged in" do
            get :new, :request_id => "foo"
            response.should_not render_template("new")
        end
    end

    context "logged in" do
        before :each do
            session[:user_id] = users(:bob_smith_user).id
        end
        it "should show the form" do
            get :new, :request_id => "foo"
            response.should render_template("new")
        end
    end
end