Commit 6a233224beeb6f7882f488cbfc41a2858f8f00bb
1 parent
d949c00b
Exists in
master
and in
1 other branch
support latest airbrake-js
Showing
4 changed files
with
34 additions
and
55 deletions
Show diff stats
app/controllers/api/v3/notices_controller.rb
| ... | ... | @@ -5,28 +5,24 @@ class Api::V3::NoticesController < ApplicationController |
| 5 | 5 | respond_to :json |
| 6 | 6 | |
| 7 | 7 | def create |
| 8 | - response.headers['Access-Control-Allow-Origin'] = '*' | |
| 9 | - response.headers['Access-Control-Allow-Headers'] = 'origin, content-type, accept' | |
| 8 | + params.merge!(JSON.parse(request.raw_post) || {}) | |
| 9 | + report = AirbrakeApi::V3::NoticeParser.new(params).report | |
| 10 | 10 | |
| 11 | - if !request.options? | |
| 12 | - report = AirbrakeApi::V3::NoticeParser.new(params).report | |
| 13 | - | |
| 14 | - if report.valid? | |
| 15 | - if report.should_keep? | |
| 16 | - report.generate_notice! | |
| 17 | - render json: { | |
| 18 | - notice: { | |
| 19 | - id: report.notice.id | |
| 20 | - } | |
| 21 | - } | |
| 22 | - else | |
| 23 | - render text: 'Notice for old app version ignored' | |
| 24 | - end | |
| 11 | + if report.valid? | |
| 12 | + if report.should_keep? | |
| 13 | + report.generate_notice! | |
| 14 | + render json: { | |
| 15 | + id: report.notice.id, | |
| 16 | + url: app_problem_path( | |
| 17 | + report.app, | |
| 18 | + report.error.problem_id, | |
| 19 | + only_path: false) | |
| 20 | + } | |
| 25 | 21 | else |
| 26 | - render text: 'Your API key is unknown', status: 422 | |
| 22 | + render text: 'Notice for old app version ignored' | |
| 27 | 23 | end |
| 28 | 24 | else |
| 29 | - render nothing: true | |
| 25 | + render text: 'Your API key is unknown', status: 422 | |
| 30 | 26 | end |
| 31 | 27 | rescue AirbrakeApi::ParamsError |
| 32 | 28 | render text: 'Invalid request', status: 400 | ... | ... |
config/routes.rb
| ... | ... | @@ -60,7 +60,7 @@ Rails.application.routes.draw do |
| 60 | 60 | end |
| 61 | 61 | end |
| 62 | 62 | |
| 63 | - match '/api/v3/projects/:project_id/notices' => 'api/v3/notices#create', via: [:post, :options] | |
| 63 | + match '/api/v3/projects/:project_id/create-notice' => 'api/v3/notices#create', via: [:post] | |
| 64 | 64 | |
| 65 | 65 | root :to => 'apps#index' |
| 66 | 66 | end | ... | ... |
spec/controllers/api/v3/notices_controller_spec.rb
| 1 | 1 | describe Api::V3::NoticesController, type: :controller do |
| 2 | 2 | let(:app) { Fabricate(:app) } |
| 3 | - | |
| 4 | - it 'responds to OPTIONS request and sets CORS headers' do | |
| 5 | - process :create, 'OPTIONS', project_id: app.api_key | |
| 6 | - expect(response).to be_success | |
| 7 | - expect(response.headers['Access-Control-Allow-Origin']).to eq('*') | |
| 8 | - expect(response.headers['Access-Control-Allow-Headers']).to eq('origin, content-type, accept') | |
| 9 | - end | |
| 10 | - | |
| 11 | - it 'sets CORS headers on POST request' do | |
| 12 | - post :create, project_id: 'invalid id' | |
| 13 | - expect(response.headers['Access-Control-Allow-Origin']).to eq('*') | |
| 14 | - expect(response.headers['Access-Control-Allow-Headers']).to eq('origin, content-type, accept') | |
| 3 | + let(:project_id) { app.api_key } | |
| 4 | + let(:legit_params) { { project_id: project_id, key: project_id } } | |
| 5 | + let(:legit_body) do | |
| 6 | + Rails.root.join('spec', 'fixtures', 'api_v3_request.json').read | |
| 15 | 7 | end |
| 16 | 8 | |
| 17 | 9 | it 'returns created notice id in json format' do |
| 18 | - json = Rails.root.join('spec', 'fixtures', 'api_v3_request.json').read | |
| 19 | - data = JSON.parse(json) | |
| 20 | - data['project_id'] = app.api_key | |
| 21 | - data['key'] = app.api_key | |
| 22 | - post :create, data | |
| 10 | + post :create, legit_body, legit_params | |
| 23 | 11 | notice = Notice.last |
| 24 | - expect(response.body).to eq({ notice: { id: notice.id } }.to_json) | |
| 12 | + expect(JSON.parse(response.body)).to eq({ | |
| 13 | + 'id' => notice.id.to_s, | |
| 14 | + 'url' => app_problem_path(app, notice.problem, only_path: false) | |
| 15 | + }) | |
| 25 | 16 | end |
| 26 | 17 | |
| 27 | 18 | it 'responds with 400 when request attributes are not valid' do |
| 28 | - allow_any_instance_of(AirbrakeApi::V3::NoticeParser).to receive(:report).and_raise(AirbrakeApi::ParamsError) | |
| 19 | + allow_any_instance_of(AirbrakeApi::V3::NoticeParser) | |
| 20 | + .to receive(:report).and_raise(AirbrakeApi::ParamsError) | |
| 29 | 21 | post :create, project_id: 'ID' |
| 30 | 22 | expect(response.status).to eq(400) |
| 31 | 23 | expect(response.body).to eq('Invalid request') |
| 32 | 24 | end |
| 33 | 25 | |
| 34 | - it 'responds with 422 when api_key or project_id is invalid' do | |
| 35 | - json = Rails.root.join('spec', 'fixtures', 'api_v3_request.json').read | |
| 36 | - data = JSON.parse(json) | |
| 37 | - data['project_id'] = 'invalid' | |
| 38 | - data.delete('key') | |
| 39 | - post :create, data | |
| 26 | + it 'responds with 422 when project_id is invalid' do | |
| 27 | + post :create, legit_body, { project_id: 'hm?', key: 'wha?' } | |
| 28 | + | |
| 40 | 29 | expect(response.status).to eq(422) |
| 41 | 30 | expect(response.body).to eq('Your API key is unknown') |
| 42 | 31 | end |
| 43 | 32 | |
| 44 | 33 | it 'ignores notices for older api' do |
| 45 | - upgraded_app = Fabricate(:app, current_app_version: '2.0') | |
| 46 | - json = Rails.root.join('spec', 'fixtures', 'api_v3_request.json').read | |
| 47 | - data = JSON.parse(json) | |
| 48 | - data['project_id'] = upgraded_app.api_key | |
| 49 | - data['key'] = upgraded_app.api_key | |
| 50 | - post :create, data | |
| 34 | + app = Fabricate(:app, current_app_version: '2.0') | |
| 35 | + post :create, legit_body, { project_id: app.api_key, key: app.api_key } | |
| 51 | 36 | expect(response.body).to eq('Notice for old app version ignored') |
| 52 | 37 | expect(Notice.count).to eq(0) |
| 53 | 38 | end |
| 54 | -end | |
| 55 | 39 | \ No newline at end of file |
| 40 | +end | ... | ... |
spec/fixtures/api_v3_request.json
| ... | ... | @@ -32,7 +32,5 @@ |
| 32 | 32 | }, |
| 33 | 33 | "params":{"returnTo":"dashboard"}, |
| 34 | 34 | "environment":{"navigator_vendor":"Google Inc."}, |
| 35 | - "session":{"isAdmin":true}, | |
| 36 | - "key":"a3969bfbf65f073921e", | |
| 37 | - "project_id":"a3969bfbf65f073921e" | |
| 38 | -} | |
| 39 | 35 | \ No newline at end of file |
| 36 | + "session":{"isAdmin":true} | |
| 37 | +} | ... | ... |