Commit 1552fc468f291742fd33ba9b521a0b3b9fc0fb5b

Authored by Marcin Ciunelis
2 parents 483bfce2 0f2d7f55
Exists in master and in 1 other branch production

Merge pull request #228 from jmonteiro/error_code

Notice XML now returns notice ID
app/controllers/notices_controller.rb
@@ -5,9 +5,17 @@ class NoticesController < ApplicationController @@ -5,9 +5,17 @@ class NoticesController < ApplicationController
5 5
6 def create 6 def create
7 # params[:data] if the notice came from a GET request, raw_post if it came via POST 7 # params[:data] if the notice came from a GET request, raw_post if it came via POST
8 - @notice = App.report_error!(params[:data] || request.raw_post)  
9 - respond_with @notice 8 + notice = App.report_error!(params[:data] || request.raw_post)
  9 + api_xml = notice.to_xml(:only => false, :methods => [:id]) do |xml|
  10 + xml.url locate_url(notice.id, :host => Errbit::Config.host)
  11 + end
  12 + render :xml => api_xml
10 end 13 end
11 14
  15 + # Redirects a notice to the problem page. Useful when using User Information at Airbrake gem.
  16 + def locate
  17 + problem = Notice.find(params[:id]).problem
  18 + redirect_to app_err_path(problem.app, problem)
  19 + end
12 end 20 end
13 21
config/routes.rb
@@ -4,6 +4,7 @@ Errbit::Application.routes.draw do @@ -4,6 +4,7 @@ Errbit::Application.routes.draw do
4 4
5 # Hoptoad Notifier Routes 5 # Hoptoad Notifier Routes
6 match '/notifier_api/v2/notices' => 'notices#create' 6 match '/notifier_api/v2/notices' => 'notices#create'
  7 + match '/locate/:id' => 'notices#locate', :as => :locate
7 match '/deploys.txt' => 'deploys#create' 8 match '/deploys.txt' => 'deploys#create'
8 9
9 resources :notices, :only => [:show] 10 resources :notices, :only => [:show]
spec/controllers/notices_controller_spec.rb
1 require 'spec_helper' 1 require 'spec_helper'
2 2
3 describe NoticesController do 3 describe NoticesController do
  4 + it_requires_authentication :for => { :locate => :get }
  5 +
  6 + let(:app) { Fabricate(:app) }
4 7
5 context 'notices API' do 8 context 'notices API' do
6 before do 9 before do
@@ -8,25 +11,34 @@ describe NoticesController do @@ -8,25 +11,34 @@ describe NoticesController do
8 @app = Fabricate(:app_with_watcher) 11 @app = Fabricate(:app_with_watcher)
9 App.stub(:find_by_api_key!).and_return(@app) 12 App.stub(:find_by_api_key!).and_return(@app)
10 @notice = App.report_error!(@xml) 13 @notice = App.report_error!(@xml)
11 -  
12 - request.env['Content-type'] = 'text/xml'  
13 - request.env['Accept'] = 'text/xml, application/xml'  
14 end 14 end
15 15
16 it "generates a notice from xml [POST]" do 16 it "generates a notice from xml [POST]" do
17 App.should_receive(:report_error!).with(@xml).and_return(@notice) 17 App.should_receive(:report_error!).with(@xml).and_return(@notice)
18 request.should_receive(:raw_post).and_return(@xml) 18 request.should_receive(:raw_post).and_return(@xml)
19 - post :create 19 + post :create, :format => :xml
  20 + response.should be_success
  21 + # Same RegExp from Airbrake::Sender#send_to_airbrake (https://github.com/airbrake/airbrake/blob/master/lib/airbrake/sender.rb#L53)
  22 + # Inspired by https://github.com/airbrake/airbrake/blob/master/test/sender_test.rb
  23 + response.body.should match(%r{<id[^>]*>#{@notice.id}</id>})
  24 + response.body.should match(%r{<url[^>]*>(.+)#{locate_path(@notice.id)}</url>})
20 end 25 end
21 26
22 it "generates a notice from xml [GET]" do 27 it "generates a notice from xml [GET]" do
23 App.should_receive(:report_error!).with(@xml).and_return(@notice) 28 App.should_receive(:report_error!).with(@xml).and_return(@notice)
24 - get :create, {:data => @xml} 29 + get :create, :data => @xml, :format => :xml
  30 + response.should be_success
  31 + response.body.should match(%r{<id[^>]*>#{@notice.id}</id>})
  32 + response.body.should match(%r{<url[^>]*>(.+)#{locate_path(@notice.id)}</url>})
25 end 33 end
26 34
27 it "sends a notification email" do 35 it "sends a notification email" do
  36 + App.should_receive(:report_error!).with(@xml).and_return(@notice)
28 request.should_receive(:raw_post).and_return(@xml) 37 request.should_receive(:raw_post).and_return(@xml)
29 - post :create 38 + post :create, :format => :xml
  39 + response.should be_success
  40 + response.body.should match(%r{<id[^>]*>#{@notice.id}</id>})
  41 + response.body.should match(%r{<url[^>]*>(.+)#{locate_path(@notice.id)}</url>})
30 email = ActionMailer::Base.deliveries.last 42 email = ActionMailer::Base.deliveries.last
31 email.to.should include(@app.watchers.first.email) 43 email.to.should include(@app.watchers.first.email)
32 email.subject.should include(@notice.message) 44 email.subject.should include(@notice.message)
@@ -35,5 +47,21 @@ describe NoticesController do @@ -35,5 +47,21 @@ describe NoticesController do
35 end 47 end
36 end 48 end
37 49
  50 + describe "GET /locate/:id" do
  51 + context 'when logged in as an admin' do
  52 + before(:each) do
  53 + @user = Fabricate(:admin)
  54 + sign_in @user
  55 + end
  56 +
  57 + it "should locate notice and redirect to problem" do
  58 + problem = Fabricate(:problem, :app => app, :environment => "production")
  59 + notice = Fabricate(:notice, :err => Fabricate(:err, :problem => problem))
  60 + get :locate, :id => notice.id
  61 + response.should redirect_to(app_err_path(problem.app, problem))
  62 + end
  63 + end
  64 + end
  65 +
38 end 66 end
39 67