notices_controller_spec.rb
4.18 KB
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
require 'spec_helper'
describe NoticesController do
it_requires_authentication :for => { :locate => :get }
let(:app) { Fabricate(:app) }
context 'notices API' do
before do
@xml = Rails.root.join('spec','fixtures','hoptoad_test_notice.xml').read
@app = Fabricate(:app_with_watcher)
App.stub(:find_by_api_key!).and_return(@app)
@notice = App.report_error!(@xml)
end
it "generates a notice from raw xml [POST]" do
App.should_receive(:report_error!).with(@xml).and_return(@notice)
request.should_receive(:raw_post).and_return(@xml)
post :create, :format => :xml
response.should be_success
# Same RegExp from Airbrake::Sender#send_to_airbrake (https://github.com/airbrake/airbrake/blob/master/lib/airbrake/sender.rb#L53)
# Inspired by https://github.com/airbrake/airbrake/blob/master/test/sender_test.rb
response.body.should match(%r{<id[^>]*>#{@notice.id}</id>})
response.body.should match(%r{<url[^>]*>(.+)#{locate_path(@notice.id)}</url>})
end
it "should trnasform xml <va> tags to hashes correctly" do
App.should_receive(:report_error!).with(@xml).and_return(@notice)
request.should_receive(:raw_post).and_return(@xml)
post :create, :format => :xml
# XML: <var key="SCRIPT_NAME"/>
@notice.env_vars.should have_key('SCRIPT_NAME')
@notice.env_vars['SCRIPT_NAME'].should be_nil # blank ends up nil
# XML representation:
# <var key="rack.session.options">
# <var key="secure">false</var>
# <var key="httponly">true</var>
# <var key="path">/</var>
# <var key="expire_after"/>
# <var key="domain"/>
# <var key="id"/>
# </var>
expected = {
'secure' => 'false',
'httponly' => 'true',
'path' => '/',
'expire_after' => nil,
'domain' => nil,
'id' => nil
}
@notice.env_vars.should have_key('rack_session_options')
@notice.env_vars['rack_session_options'].should eql(expected)
end
it "generates a notice from xml in a data param [POST]" do
App.should_receive(:report_error!).with(@xml).and_return(@notice)
post :create, :data => @xml, :format => :xml
response.should be_success
# Same RegExp from Airbrake::Sender#send_to_airbrake (https://github.com/airbrake/airbrake/blob/master/lib/airbrake/sender.rb#L53)
# Inspired by https://github.com/airbrake/airbrake/blob/master/test/sender_test.rb
response.body.should match(%r{<id[^>]*>#{@notice.id}</id>})
response.body.should match(%r{<url[^>]*>(.+)#{locate_path(@notice.id)}</url>})
end
it "generates a notice from xml [GET]" do
App.should_receive(:report_error!).with(@xml).and_return(@notice)
get :create, :data => @xml, :format => :xml
response.should be_success
response.body.should match(%r{<id[^>]*>#{@notice.id}</id>})
response.body.should match(%r{<url[^>]*>(.+)#{locate_path(@notice.id)}</url>})
end
it "sends a notification email" do
App.should_receive(:report_error!).with(@xml).and_return(@notice)
request.should_receive(:raw_post).and_return(@xml)
post :create, :format => :xml
response.should be_success
response.body.should match(%r{<id[^>]*>#{@notice.id}</id>})
response.body.should match(%r{<url[^>]*>(.+)#{locate_path(@notice.id)}</url>})
email = ActionMailer::Base.deliveries.last
email.to.should include(@app.watchers.first.email)
email.subject.should include(@notice.message.truncate(50))
email.subject.should include("[#{@app.name}]")
email.subject.should include("[#{@notice.environment_name}]")
end
end
describe "GET /locate/:id" do
context 'when logged in as an admin' do
before(:each) do
@user = Fabricate(:admin)
sign_in @user
end
it "should locate notice and redirect to problem" do
problem = Fabricate(:problem, :app => app, :environment => "production")
notice = Fabricate(:notice, :err => Fabricate(:err, :problem => problem))
get :locate, :id => notice.id
response.should redirect_to(app_problem_path(problem.app, problem))
end
end
end
end