aboutsummaryrefslogtreecommitdiffstats
path: root/spec/controllers/admin_request_controller_spec.rb
blob: 635d73b9e42e7cd9a357290af40b078b9d8e61d5 (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
require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')

describe AdminRequestController, "when administering requests" do
    integrate_views
    fixtures :users, :public_bodies, :public_body_translations, :public_body_versions, :info_requests, :raw_emails, :incoming_messages, :outgoing_messages, :comments, :info_request_events, :track_things
    before { basic_auth_login @request }

    it "shows the index/list page" do
        get :index
    end

    it "shows a public body" do
        get :show, :id => info_requests(:fancy_dog_request)
    end

    it "edits a public body" do
        get :edit, :id => info_requests(:fancy_dog_request)
    end

    it "saves edits to a request" do
        info_requests(:fancy_dog_request).title.should == "Why do you have & such a fancy dog?"
        post :update, { :id => info_requests(:fancy_dog_request), :info_request => { :title => "Renamed", :prominence => "normal", :described_state => "waiting_response", :awaiting_description => false, :allow_new_responses_from => 'anybody', :handle_rejected_responses => 'bounce' } }
        response.flash[:notice].should include('successful')
        ir = InfoRequest.find(info_requests(:fancy_dog_request).id)
        ir.title.should == "Renamed"
    end

    it "edits an outgoing message" do
        get :edit_outgoing, :id => outgoing_messages(:useless_outgoing_message)
    end

    it "saves edits to an outgoing_message" do
        outgoing_messages(:useless_outgoing_message).body.should include("fancy dog")
        post :update_outgoing, { :id => outgoing_messages(:useless_outgoing_message), :outgoing_message => { :body => "Why do you have such a delicious cat?" } }
        response.flash[:notice].should include('successful')
        ir = OutgoingMessage.find(outgoing_messages(:useless_outgoing_message).id)
        ir.body.should include("delicious cat")
    end

end

describe AdminRequestController, "when administering the holding pen" do
    integrate_views
    fixtures :users, :public_bodies, :public_body_translations, :public_body_versions, :info_requests, :raw_emails, :incoming_messages, :outgoing_messages, :comments, :info_request_events, :track_things
    before(:each) do
        basic_auth_login @request
        load_raw_emails_data(raw_emails)
    end

    it "shows a rejection reason for an incoming message from an invalid address" do
        ir = info_requests(:fancy_dog_request)
        ir.allow_new_responses_from = 'authority_only'
        ir.handle_rejected_responses = 'holding_pen'
        ir.save!
        receive_incoming_mail('incoming-request-plain.email', ir.incoming_email, "frob@nowhere.com")
        get :show_raw_email, :id => InfoRequest.holding_pen_request.get_last_response.raw_email.id
        response.should have_text(/Only the authority can reply to this request/)
    end

    it "allows redelivery even to a closed request" do
        ir = info_requests(:fancy_dog_request)
        ir.allow_new_responses_from = 'nobody'
        ir.handle_rejected_responses = 'holding_pen'
        ir.save!
        InfoRequest.holding_pen_request.incoming_messages.length.should == 0
        ir.incoming_messages.length.should == 1
        receive_incoming_mail('incoming-request-plain.email', ir.incoming_email, "frob@nowhere.com")
        InfoRequest.holding_pen_request.incoming_messages.length.should == 1
        new_im = InfoRequest.holding_pen_request.incoming_messages[0]
        ir.incoming_messages.length.should == 1
        post :redeliver_incoming, :redeliver_incoming_message_id => new_im.id, :url_title => ir.url_title        
        ir = InfoRequest.find_by_url_title(ir.url_title)
        ir.incoming_messages.length.should == 2
        response.should redirect_to(:controller=>'admin_request', :action=>'show', :id=>101)
        InfoRequest.holding_pen_request.incoming_messages.length.should == 0
    end

    it "guesses a misdirected request" do
        ir = info_requests(:fancy_dog_request)
        ir.handle_rejected_responses = 'holding_pen'
        ir.allow_new_responses_from = 'authority_only'
        ir.save!
        mail_to = "request-#{ir.id}-asdfg@example.com"
        receive_incoming_mail('incoming-request-plain.email', mail_to)
        interesting_email = InfoRequest.holding_pen_request.get_last_response.raw_email.id
        # now we add another message to the queue, which we're not interested in
        receive_incoming_mail('incoming-request-plain.email', ir.incoming_email, "")
        InfoRequest.holding_pen_request.incoming_messages.length.should == 2
        get :show_raw_email, :id => interesting_email
        response.should have_text(/Could not identify the request/)
        assigns[:info_requests][0].should == ir
    end

    it "destroys an incoming message" do
        im = incoming_messages(:useless_incoming_message)        
        raw_email = im.raw_email.filepath
        post :destroy_incoming, :incoming_message_id => im.id
        assert_equal File.exists?(raw_email), false        
    end

end