Add instructions to api key usagea and allow user generate new api key

This commit is contained in:
Eugene Burmakin 2024-05-25 13:57:50 +02:00
parent cf71df3f0a
commit b76f75a494
8 changed files with 51 additions and 6 deletions

File diff suppressed because one or more lines are too long

View file

@ -1,6 +1,6 @@
# frozen_string_literal: true
class Api::V1::PointsController < ApplicationController
class Api::V1::Owntracks::PointsController < ApplicationController
skip_forgery_protection
before_action :authenticate_api_key

View file

@ -8,4 +8,10 @@ class SettingsController < ApplicationController
redirect_back(fallback_location: root_path)
end
def generate_api_key
current_user.update(api_key: SecureRandom.hex)
redirect_back(fallback_location: root_path)
end
end

View file

@ -3,7 +3,18 @@
<code><%= current_user.api_key %></code>
<p class='py-2'>
Usage example:
<p><code><%= api_v1_points_url(api_key: current_user.api_key) %></code></p>
<div role="tablist" class="tabs tabs-boxed">
<input type="radio" name="my_tabs_2" role="tab" class="tab" aria-label="OwnTracks" />
<div role="tabpanel" class="tab-content bg-base-100 border-base-300 rounded-box p-6">
<p><code><%= api_v1_owntracks_points_url(api_key: current_user.api_key) %></code></p>
</div>
<input type="radio" name="my_tabs_2" role="tab" class="tab" aria-label="Overland" checked />
<div role="tabpanel" class="tab-content bg-base-100 border-base-300 rounded-box p-6">
<p><code><%= api_v1_overland_batches_url(api_key: current_user.api_key) %></code></p>
</div>
</div>
</p>
<p class='py-2'>
<%= link_to "Generate new API key", generate_api_key_path, data: { confirm: "Are you sure?", turbo_confirm: "Are you sure?", turbo_method: :post }, class: 'btn btn-primary' %>

View file

@ -27,7 +27,7 @@ Rails.application.routes.draw do
root to: 'home#index'
devise_for :users
post 'settings/generate_api_key', to: 'devise/api_keys#create', as: :generate_api_key
post 'settings/generate_api_key', to: 'settings#generate_api_key', as: :generate_api_key
get 'map', to: 'map#index'

View file

@ -117,7 +117,7 @@ RSpec.describe Stat, type: :model do
end
describe '#self.year_distance' do
subject { described_class.year_distance(year) }
subject { described_class.year_distance(year, user) }
let(:user) { create(:user) }
let(:expected_distance) do

View file

@ -40,4 +40,32 @@ RSpec.describe 'Settings', type: :request do
end
end
end
describe 'POST /generate_api_key' do
context 'when user is not signed in' do
it 'redirects to the sign in page' do
post '/settings/generate_api_key'
expect(response).to redirect_to(new_user_session_path)
end
end
context 'when user is signed in' do
let(:user) { create(:user) }
before do
sign_in user
end
it 'generates an API key for the user' do
expect { post '/settings/generate_api_key' }.to change { user.reload.api_key }
end
it 'redirects back' do
post '/settings/generate_api_key'
expect(response).to redirect_to(root_path)
end
end
end
end

View file

@ -180,7 +180,7 @@ paths:
lat: 52.502397
lon: 13.356718
tid: Swagger
tst: 1716636973
tst: 1716638268
servers:
- url: http://{defaultHost}
variables: