Skip to content

Commit 4b21f68

Browse files
author
matt swanson
authored
Merge pull request stringer-rss#454 from jbrayton/fever_not_auth_compatibility
Make Handling of Missing or Incorrect Api Key Consistent With Fever Api
2 parents 353aa93 + 603efef commit 4b21f68

File tree

3 files changed

+14
-4
lines changed

3 files changed

+14
-4
lines changed

app/fever_api/response.rb

+3-1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
require_relative "write_mark_group"
1616

1717
module FeverAPI
18+
API_VERSION = 3
19+
1820
class Response
1921
ACTIONS = [
2022
Authentication,
@@ -39,7 +41,7 @@ def initialize(params)
3941
end
4042

4143
def to_json
42-
base_response = { api_version: 3 }
44+
base_response = { api_version: API_VERSION }
4345
ACTIONS
4446
.inject(base_response) { |a, e| a.merge!(e.new.call(@params)) }
4547
.to_json

fever_api.rb

+3-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,9 @@ class Endpoint < Sinatra::Base
1313
end
1414

1515
before do
16-
halt 403 unless authenticated?(params[:api_key])
16+
headers = { "Content-Type" => "application/json" }
17+
body = { api_version: FeverAPI::API_VERSION, auth: 0 }.to_json
18+
halt 200, headers, body unless authenticated?(params[:api_key])
1719
end
1820

1921
def authenticated?(api_key)

spec/fever_api_spec.rb

+8-2
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@ def app
1717
let(:standard_answer) do
1818
{ api_version: 3, auth: 1, last_refreshed_on_time: 123456789 }
1919
end
20+
let(:cannot_auth) do
21+
{ api_version: 3, auth: 0 }
22+
end
2023
let(:headers) { { api_key: api_key } }
2124

2225
before do
@@ -34,16 +37,19 @@ def last_response_as_object
3437
it "authenticates request with correct api_key" do
3538
get "/", headers
3639
expect(last_response).to be_ok
40+
expect(last_response_as_object).to include(standard_answer)
3741
end
3842

3943
it "does not authenticate request with incorrect api_key" do
4044
get "/", api_key: "foo"
41-
expect(last_response).not_to be_ok
45+
expect(last_response).to be_ok
46+
expect(last_response_as_object).to include(cannot_auth)
4247
end
4348

4449
it "does not authenticate request when api_key is not provided" do
4550
get "/"
46-
expect(last_response).not_to be_ok
51+
expect(last_response).to be_ok
52+
expect(last_response_as_object).to include(cannot_auth)
4753
end
4854
end
4955

0 commit comments

Comments
 (0)