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 5  
6 6 def create
7 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 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 20 end
13 21  
... ...
config/routes.rb
... ... @@ -4,6 +4,7 @@ Errbit::Application.routes.draw do
4 4  
5 5 # Hoptoad Notifier Routes
6 6 match '/notifier_api/v2/notices' => 'notices#create'
  7 + match '/locate/:id' => 'notices#locate', :as => :locate
7 8 match '/deploys.txt' => 'deploys#create'
8 9  
9 10 resources :notices, :only => [:show]
... ...
spec/controllers/notices_controller_spec.rb
1 1 require 'spec_helper'
2 2  
3 3 describe NoticesController do
  4 + it_requires_authentication :for => { :locate => :get }
  5 +
  6 + let(:app) { Fabricate(:app) }
4 7  
5 8 context 'notices API' do
6 9 before do
... ... @@ -8,25 +11,34 @@ describe NoticesController do
8 11 @app = Fabricate(:app_with_watcher)
9 12 App.stub(:find_by_api_key!).and_return(@app)
10 13 @notice = App.report_error!(@xml)
11   -
12   - request.env['Content-type'] = 'text/xml'
13   - request.env['Accept'] = 'text/xml, application/xml'
14 14 end
15 15  
16 16 it "generates a notice from xml [POST]" do
17 17 App.should_receive(:report_error!).with(@xml).and_return(@notice)
18 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 25 end
21 26  
22 27 it "generates a notice from xml [GET]" do
23 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 33 end
26 34  
27 35 it "sends a notification email" do
  36 + App.should_receive(:report_error!).with(@xml).and_return(@notice)
28 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 42 email = ActionMailer::Base.deliveries.last
31 43 email.to.should include(@app.watchers.first.email)
32 44 email.subject.should include(@notice.message)
... ... @@ -35,5 +47,21 @@ describe NoticesController do
35 47 end
36 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 66 end
39 67  
... ...