Skip to content

Commit e25ca39

Browse files
authored
JSON API: New Version Endpoint (#3140)
* Implementation * Update API docs
1 parent 8f85171 commit e25ca39

File tree

8 files changed

+111
-5
lines changed

8 files changed

+111
-5
lines changed

app/controllers/api/base_controller.rb

+4
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,10 @@ def require_api_authentication
1111
if (user = user_from_token)
1212
sign_in user, store: false
1313

14+
elsif params["controller"] == "api/v1/version"
15+
# Version endpoint is public
16+
nil
17+
1418
elsif request.headers.key?("Authorization") || request.headers.key?("X-User-Token")
1519
# The user is trying to authenticate with a bad token
1620
head :unauthorized
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
class Api::V1::VersionController < Api::BaseController
2+
api :GET, "/api/v1/version.json", "Get the version details of the application and the API."
3+
formats ["JSON"]
4+
description <<-EOS
5+
== Version Information
6+
7+
Retrieves the current application version, API version, edition and other information.
8+
9+
=== Example Request
10+
11+
curl -X GET \\
12+
-H "Authorization: Bearer MyAPIToken" \\
13+
https://pwpush.com/api/v1/version.json
14+
15+
=== Example Response
16+
17+
{
18+
"application_version": "2.1.0",
19+
"api_version": "1",
20+
"edition": "oss"
21+
}
22+
EOS
23+
def show
24+
render json: {
25+
application_version: Version.current.to_s,
26+
api_version: Apipie.configuration.default_version,
27+
edition: "oss"
28+
}
29+
end
30+
end

config/initializers/apipie.rb

+8-3
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,11 @@
44
config.app_name = "Password Pusher"
55
config.copyright = "&copy; 2011-Present Peter Giacomo Lombardo"
66
config.api_base_url = ""
7-
config.api_base_url["1.3"] = ""
7+
config.api_base_url["1.4"] = ""
88
config.doc_base_url = "/api"
99
config.api_controllers_matcher = Rails.root.join("app/controllers/**/*.rb").to_s
1010
config.validate = false
11-
config.default_version = "1.3"
11+
config.default_version = "1.4"
1212
config.app_info = <<-APPINFO
1313
The Password Pusher JSON API.
1414
@@ -31,7 +31,12 @@
3131
3232
For more information including language-specific examples to copy, see: https://docs.pwpush.com/docs/json-api/
3333
34-
== February 2025 Update
34+
== February 2025 Update (v1.4)
35+
36+
Added a new version endpoint to get the current application version, API version, and edition information. This will be expanded
37+
soon to include more information.
38+
39+
== February 2025 Update (v1.3)
3540
3641
The API has been updated to use Bearer tokens for authentication and a general cleanup of the API.
3742

config/routes.rb

+2-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@
1111
draw :admin
1212
draw :users
1313
draw :pushes
14-
draw :pushes_api
14+
draw :pwp_api
15+
1516
apipie
1617

1718
get "/pages/*id" => "pages#show", :as => :page, :format => false

config/routes/pushes_api.rb config/routes/pwp_api.rb

+6
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,10 @@
11
constraints(format: :json) do
2+
namespace :api do
3+
namespace :v1 do
4+
get :version, to: "version#show"
5+
end
6+
end
7+
28
resources :p, controller: "api/v1/passwords", as: :passwords, except: %i[new index edit update] do
39
get "preview", on: :member
410
get "audit", on: :member

test/fixtures/users.yml

+6-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,12 @@ luca:
99
encrypted_password: $2a$12$mvenK9BFqCuzPIg.gV.u8egtR4iyNGgrHtQHX5g9E6I5Ehrz4cCD6
1010
confirmed_at: 2021-01-01 00:00:00
1111
authentication_token: 1234567890
12-
confirmed_at: 2021-01-01 00:00:00
12+
13+
one:
14+
15+
encrypted_password: <%= Devise::Encryptor.digest(User, 'password12345') %>
16+
confirmed_at: 2025-02-28 18:00:00
17+
authentication_token: 0987654321
1318

1419
two:
1520
+49
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
# frozen_string_literal: true
2+
3+
require "test_helper"
4+
5+
class VersionEndpointTest < ActionDispatch::IntegrationTest
6+
def test_anonymous_version_endpoint
7+
get "/api/v1/version.json"
8+
assert_response :success
9+
assert_equal Version.current.to_s, JSON.parse(@response.body)["application_version"]
10+
assert_equal Apipie.configuration.default_version, JSON.parse(@response.body)["api_version"]
11+
assert_equal "oss", JSON.parse(@response.body)["edition"]
12+
13+
get "/api/v1/version"
14+
assert_response :success
15+
assert_equal Version.current.to_s, JSON.parse(@response.body)["application_version"]
16+
assert_equal Apipie.configuration.default_version, JSON.parse(@response.body)["api_version"]
17+
assert_equal "oss", JSON.parse(@response.body)["edition"]
18+
end
19+
20+
def test_authenticated_version_endpoint
21+
@user_one = users(:one)
22+
sign_in @user_one
23+
24+
get "/api/v1/version.json",
25+
headers: {
26+
"Accept" => "application/json",
27+
"Authorization" => "Bearer #{@user_one.authentication_token}"
28+
}
29+
30+
assert_response :success
31+
assert_equal Version.current.to_s, JSON.parse(@response.body)["application_version"]
32+
assert_equal Apipie.configuration.default_version, JSON.parse(@response.body)["api_version"]
33+
assert_equal "oss", JSON.parse(@response.body)["edition"]
34+
35+
@user_one = users(:one)
36+
sign_in @user_one
37+
38+
get "/api/v1/version",
39+
headers: {
40+
"Accept" => "application/json",
41+
"Authorization" => "Bearer #{@user_one.authentication_token}"
42+
}
43+
44+
assert_response :success
45+
assert_equal Version.current.to_s, JSON.parse(@response.body)["application_version"]
46+
assert_equal Apipie.configuration.default_version, JSON.parse(@response.body)["api_version"]
47+
assert_equal "oss", JSON.parse(@response.body)["edition"]
48+
end
49+
end

test/test_helper.rb

+6
Original file line numberDiff line numberDiff line change
@@ -22,3 +22,9 @@ class ActiveSupport::TestCase
2222
fixtures :all
2323
# Add more helper methods to be used by all tests here...
2424
end
25+
26+
module ActionDispatch
27+
class IntegrationTest
28+
include Devise::Test::IntegrationHelpers
29+
end
30+
end

0 commit comments

Comments
 (0)