Commit 7b12c407a7bb936fb1d9cd8c9920bc5b2f761026

Authored by Stephen Crosby
1 parent c4402823
Exists in master

Refs #1024 return 422 on invalid app version

README.md
... ... @@ -97,6 +97,13 @@ Changing the fingerprinter (under the 'config' menu) applies to all apps and
97 97 the change affects only notices that arrive after the change. If you want to
98 98 refingerprint old notices, you can run `rake errbit:notice_refingerprint`.
99 99  
  100 +Managing apps
  101 +---------------------
  102 +An Errbit app is a place to collect error notifications from your external
  103 +application deployments.
  104 +
  105 +See [apps](docs/apps.md)
  106 +
100 107 Authentication
101 108 --------------
102 109 ### Configuring GitHub authentication:
... ...
app/controllers/api/v3/notices_controller.rb
1 1 class Api::V3::NoticesController < ApplicationController
  2 + VERSION_TOO_OLD = 'Notice for old app version ignored'.freeze
  3 + UNKNOWN_API_KEY = 'Your API key is unknown'.freeze
  4 +
2 5 skip_before_action :verify_authenticity_token
3 6 skip_before_action :authenticate_user!
4 7  
... ... @@ -8,24 +11,17 @@ class Api::V3::NoticesController &lt; ApplicationController
8 11 response.headers['Access-Control-Allow-Origin'] = '*'
9 12 response.headers['Access-Control-Allow-Headers'] = 'origin, content-type, accept'
10 13  
11   - params.merge!(JSON.parse(request.raw_post) || {})
12   - report = AirbrakeApi::V3::NoticeParser.new(params).report
  14 + report = AirbrakeApi::V3::NoticeParser.new(
  15 + params.merge(JSON.parse(request.raw_post) || {})).report
  16 +
  17 + return render text: UNKNOWN_API_KEY, status: 422 unless report.valid?
  18 + return render text: VERSION_TOO_OLD, status: 422 unless report.should_keep?
13 19  
14   - if report.valid?
15   - if report.should_keep?
16   - report.generate_notice!
17   - render status: 201, json: {
18   - id: report.notice.id,
19   - url: app_problem_url(
20   - report.app,
21   - report.error.problem_id)
22   - }
23   - else
24   - render text: 'Notice for old app version ignored'
25   - end
26   - else
27   - render text: 'Your API key is unknown', status: 422
28   - end
  20 + report.generate_notice!
  21 + render status: 200, json: {
  22 + id: report.notice.id,
  23 + url: report.problem.url
  24 + }
29 25 rescue AirbrakeApi::ParamsError
30 26 render text: 'Invalid request', status: 400
31 27 end
... ...
docs/apps.md 0 → 100644
... ... @@ -0,0 +1,13 @@
  1 +# Apps
  2 +An Errbit app is a place to collect error notifications from your
  3 +external application deployments. Each one has a name and a unique API
  4 +key that your notifiers can use to send notices to Errbit.
  5 +
  6 +## Old Application Versions
  7 +You may have many versions of an application running at a given time and
  8 +some of them may be old enough that you no longer care about errors from
  9 +those applications. If that's the case, set the LATEST APP VERSION field
  10 +for your Errbit app, and Errbit will ignore notices from older
  11 +application versions. Be sure your notifier is setting the
  12 +context.version field in its notifications (see
  13 +[https://airbrake.io/docs/](https://airbrake.io/docs/)).
... ...
spec/controllers/api/v3/notices_controller_spec.rb
... ... @@ -17,13 +17,13 @@ describe Api::V3::NoticesController, type: :controller do
17 17 notice = Notice.last
18 18 expect(JSON.parse(response.body)).to eq(
19 19 'id' => notice.id.to_s,
20   - 'url' => app_problem_url(app, notice.problem)
  20 + 'url' => notice.problem.url
21 21 )
22 22 end
23 23  
24   - it 'responds with 201 created on success' do
  24 + it 'responds with 200 created on success' do
25 25 post :create, legit_body, legit_params
26   - expect(response.status).to be(201)
  26 + expect(response.status).to be(200)
27 27 end
28 28  
29 29 it 'responds with 400 when request attributes are not valid' do
... ... @@ -34,6 +34,13 @@ describe Api::V3::NoticesController, type: :controller do
34 34 expect(response.body).to eq('Invalid request')
35 35 end
36 36  
  37 + it 'responds with 422 when notice comes from an old app' do
  38 + app.current_app_version = '1.1.0'
  39 + app.save!
  40 + post :create, legit_body, legit_params
  41 + expect(response.status).to eq(422)
  42 + end
  43 +
37 44 it 'responds with 422 when project_id is invalid' do
38 45 post :create, legit_body, project_id: 'hm?', key: 'wha?'
39 46  
... ...