diff --git a/app/controllers/notices_controller.rb b/app/controllers/notices_controller.rb index fe189be..93acb42 100644 --- a/app/controllers/notices_controller.rb +++ b/app/controllers/notices_controller.rb @@ -1,11 +1,14 @@ class NoticesController < ApplicationController - respond_to :xml + + class ParamsError < StandardError; end skip_before_filter :authenticate_user!, :only => :create + rescue_from ParamsError, :with => :bad_params + def create # params[:data] if the notice came from a GET request, raw_post if it came via POST - report = ErrorReport.new(params[:data] || request.raw_post) + report = ErrorReport.new(notice_params) if report.valid? report.generate_notice! @@ -24,4 +27,19 @@ class NoticesController < ApplicationController redirect_to app_problem_path(problem.app, problem) end + private + + def notice_params + return @notice_params if @notice_params + @notice_params = params[:data] || request.raw_post + if @notice_params.blank? + raise ParamsError.new('Need a data params in GET or raw post data') + end + @notice_params + end + + def bad_params(exception) + render :text => exception.message, :status => :bad_request + end + end diff --git a/spec/controllers/notices_controller_spec.rb b/spec/controllers/notices_controller_spec.rb index 6ea5372..26285ef 100644 --- a/spec/controllers/notices_controller_spec.rb +++ b/spec/controllers/notices_controller_spec.rb @@ -9,17 +9,29 @@ describe NoticesController do let(:error_report) { mock(:valid? => true, :generate_notice! => true, :notice => notice) } context 'notices API' do - before do - ErrorReport.should_receive(:new).with(xml).and_return(error_report) - end - - context "with xml pass in raw_port" do + context "with all params" do before do - request.should_receive(:raw_post).and_return(xml) - post :create, :format => :xml + ErrorReport.should_receive(:new).with(xml).and_return(error_report) + end + + context "with xml pass in raw_port" do + before do + request.should_receive(:raw_post).and_return(xml) + post :create, :format => :xml + end + + it "generates a notice from raw xml [POST]" do + 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{]*>#{notice.id}}) + response.body.should match(%r{]*>(.+)#{locate_path(notice.id)}}) + end + end - it "generates a notice from raw xml [POST]" do + it "generates a notice from xml in a data param [POST]" do + 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 @@ -27,28 +39,26 @@ describe NoticesController do response.body.should match(%r{]*>(.+)#{locate_path(notice.id)}}) end + it "generates a notice from xml [GET]" do + get :create, :data => xml, :format => :xml + response.should be_success + response.body.should match(%r{]*>#{notice.id}}) + response.body.should match(%r{]*>(.+)#{locate_path(notice.id)}}) + end + context "with an invalid API_KEY" do + let(:error_report) { mock(:valid? => false) } + it 'return 422' do + post :create, :format => :xml, :data => xml + expect(response.status).to eq 422 + end + end end - it "generates a notice from xml in a data param [POST]" do - 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{]*>#{notice.id}}) - response.body.should match(%r{]*>(.+)#{locate_path(notice.id)}}) - end - - it "generates a notice from xml [GET]" do - get :create, :data => xml, :format => :xml - response.should be_success - response.body.should match(%r{]*>#{notice.id}}) - response.body.should match(%r{]*>(.+)#{locate_path(notice.id)}}) - end - context "with an invalid API_KEY" do - let(:error_report) { mock(:valid? => false) } - it 'return 422' do - post :create, :format => :xml, :data => xml - expect(response.status).to eq 422 + context "without params needed" do + it 'return 400' do + post :create, :format => :xml + expect(response.status).to eq 400 + expect(response.body).to eq 'Need a data params in GET or raw post data' end end end -- libgit2 0.21.2