Add user endpoint

This commit is contained in:
Eugene Burmakin 2025-01-20 15:17:56 +01:00
parent 46a30dc6a2
commit 41bb2e07fb
5 changed files with 107 additions and 0 deletions

View file

@ -0,0 +1,7 @@
# frozen_string_literal: true
class Api::V1::UsersController < ApiController
def me
render json: { user: current_api_user }
end
end

View file

@ -65,6 +65,7 @@ Rails.application.routes.draw do
get 'health', to: 'health#index'
patch 'settings', to: 'settings#update'
get 'settings', to: 'settings#index'
get 'users/me', to: 'users#me'
resources :areas, only: %i[index create update destroy]
resources :points, only: %i[index destroy update]

View file

@ -0,0 +1,18 @@
# frozen_string_literal: true
require 'rails_helper'
RSpec.describe 'Api::V1::Users', type: :request do
describe 'GET /me' do
let(:user) { create(:user) }
let(:headers) { { 'Authorization' => "Bearer #{user.api_key}" } }
it 'returns http success' do
get '/api/v1/users/me', headers: headers
expect(response).to have_http_status(:success)
expect(response.body).to include(user.email)
expect(response.body).to include(user.id.to_s)
end
end
end

View file

@ -0,0 +1,64 @@
# frozen_string_literal: true
require 'swagger_helper'
describe 'Users API', type: :request do
path '/api/v1/users/me' do
get 'Returns the current user' do
tags 'Users'
consumes 'application/json'
security [bearer_auth: []]
parameter name: 'Authorization', in: :header, type: :string, required: true,
description: 'Bearer token in the format: Bearer {api_key}'
response '200', 'user found' do
let(:user) { create(:user) }
let(:Authorization) { "Bearer #{user.api_key}" }
schema type: :object,
properties: {
user: {
type: :object,
properties: {
id: { type: :integer },
email: { type: :string },
created_at: { type: :string, format: 'date-time' },
updated_at: { type: :string, format: 'date-time' },
api_key: { type: :string },
theme: { type: :string },
settings: {
type: :object,
properties: {
immich_url: { type: :string },
route_opacity: { type: :string },
immich_api_key: { type: :string },
live_map_enabled: { type: :boolean },
fog_of_war_meters: { type: :string },
preferred_map_layer: { type: :string },
speed_colored_routes: { type: :boolean },
meters_between_routes: { type: :string },
points_rendering_mode: { type: :string },
minutes_between_routes: { type: :string },
time_threshold_minutes: { type: :string },
merge_threshold_minutes: { type: :string },
speed_colored_polylines: { type: :boolean }
}
},
admin: { type: :boolean }
}
}
}
after do |example|
example.metadata[:response][:content] = {
'application/json' => {
example: JSON.parse(response.body)
}
}
end
run_test!
end
end
end
end

View file

@ -892,6 +892,23 @@ paths:
- totalCountriesVisited
- totalCitiesVisited
- yearlyStats
"/api/v1/users/me":
get:
summary: Returns the current user
tags:
- Users
security:
- bearer_auth: []
parameters:
- name: Authorization
in: header
required: true
description: 'Bearer token in the format: Bearer {api_key}'
schema:
type: string
responses:
'200':
description: user found
servers:
- url: http://{defaultHost}
variables: