aboutsummaryrefslogtreecommitdiffstats
path: root/spec/integration/errors_spec.rb
blob: 39f1279cebb2deb5077128638cc700bb8e87e4a7 (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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
# -*- coding: utf-8 -*-
require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')

describe "When errors occur" do

    def set_consider_all_requests_local(value)
        @requests_local = Rails.application.config.consider_all_requests_local
        Rails.application.config.consider_all_requests_local = value
    end

    def restore_consider_all_requests_local
        Rails.application.config.consider_all_requests_local = @requests_local
    end

    before(:each) do
        # This should happen automatically before each test but doesn't with these integration
        # tests for some reason.
        ActionMailer::Base.deliveries = []
    end

    after(:each) do
        restore_consider_all_requests_local
    end

    context 'when considering all requests local (by default all in development)' do

        before(:each) { set_consider_all_requests_local(true) }

        it 'should show a full trace for general errors' do
            InfoRequest.stub!(:find_by_url_title!).and_raise("An example error")
            get("/request/example")
            response.body.should have_selector('div[id=traces]')
            response.body.should match('An example error')
        end

    end

    context 'when not considering all requests local' do

        before(:each) { set_consider_all_requests_local(false) }

        it "should render a 404 for unrouteable URLs using the general/exception_caught template" do
            get("/frobsnasm")
            response.should render_template('general/exception_caught')
            response.code.should == "404"
        end

        it "should render a 404 for users or bodies that don't exist using the general/exception_caught
            template" do
            ['/user/wobsnasm', '/body/wobsnasm'].each do |non_existent_url|
                get(non_existent_url)
                response.should render_template('general/exception_caught')
                response.code.should == "404"
            end
        end

        it 'should render a 404 when given an invalid page parameter' do
           get '/body/list/all', :page => 'xoforvfmy'
           response.should render_template('general/exception_caught')
           response.code.should == '404'
           response.body.should match("Sorry, we couldn't find that page")
        end

        # it 'should handle non utf-8 parameters' do
        #     pending 'until we sanitize non utf-8 parameters for Ruby >= 1.9' do
        #         get ('/%d3')
        #         response.should render_template('general/exception_caught')
        #         response.code.should == '404'
        #         response.body.should match("Sorry, we couldn't find that page")
        #     end
        # end


        it "should render a 500 for general errors using the general/exception_caught template" do
            InfoRequest.stub!(:find_by_url_title!).and_raise("An example error")
            get("/request/example")
            response.should render_template('general/exception_caught')
            response.code.should == "500"
        end

        it 'should render a 500 for json errors' do
            InfoRequest.stub!(:find_by_url_title!).and_raise("An example error")
            get("/request/example.json")
            response.code.should == '500'
        end

        it 'should render a 404 for a non-found xml request' do
            get("/frobsnasm.xml")
            response.code.should == '404'
        end

        it 'should notify of a general error' do
            InfoRequest.stub!(:find_by_url_title!).and_raise("An example error")
            get("/request/example")
            deliveries = ActionMailer::Base.deliveries
            deliveries.size.should == 1
            mail = deliveries[0]
            mail.body.should =~ /An example error/
        end

        it 'should log a general error' do
            Rails.logger.should_receive(:fatal)
            InfoRequest.stub!(:find_by_url_title!).and_raise("An example error")
            get("/request/example")
        end

        it 'should assign the locale for the general/exception_caught template' do
            InfoRequest.stub!(:find_by_url_title!).and_raise("An example error")
            get("/es/request/example")
            response.should render_template('general/exception_caught')
            response.body.should match('Lo sentimos, hubo un problema procesando esta página')
        end

        it "should render a 403 with text body for attempts at directory listing for attachments" do
            # make a fake cache
            foi_cache_path = File.expand_path(File.join(File.dirname(__FILE__), '../../cache'))
            FileUtils.mkdir_p(File.join(foi_cache_path, "views/en/request/101/101/response/1/attach/html/1"))
            get("/request/101/response/1/attach/html/1/" )
            response.body.should include("Directory listing not allowed")
            response.code.should == "403"
            get("/request/101/response/1/attach/html" )
            response.body.should include("Directory listing not allowed")
            response.code.should == "403"
        end

        it "return a 403 for a JSON PermissionDenied error" do
            InfoRequest.stub!(:find_by_url_title!).and_raise(ApplicationController::PermissionDenied)
            get("/request/example.json")
            response.code.should == '403'
        end

        context "in the admin interface" do

            it 'should show a full trace for general errors' do
                InfoRequest.stub!(:find).and_raise("An example error")
                get("/admin/requests/333")
                response.body.should have_selector('div[id=traces]')
                response.body.should match('An example error')
            end

        end

    end

end