-
Notifications
You must be signed in to change notification settings - Fork 100
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Extract api_key value to a dedicated model ApiKey
- Loading branch information
Alexander Chebotarov
committed
Oct 25, 2024
1 parent
c9bb7bc
commit d2bd8db
Showing
24 changed files
with
342 additions
and
46 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
# frozen_string_literal: true | ||
|
||
class ApiKey < ApplicationRecord | ||
include PaperTrailTraceable | ||
|
||
belongs_to :organization | ||
|
||
before_create :set_value | ||
|
||
def generate_value | ||
value = SecureRandom.uuid | ||
api_key = ApiKey.find_by(value:) | ||
|
||
return generate_value if api_key.present? | ||
|
||
value | ||
end | ||
|
||
private | ||
|
||
def set_value | ||
self.value = generate_value | ||
end | ||
end | ||
|
||
# == Schema Information | ||
# | ||
# Table name: api_keys | ||
# | ||
# id :uuid not null, primary key | ||
# value :string not null | ||
# created_at :datetime not null | ||
# updated_at :datetime not null | ||
# organization_id :uuid not null | ||
# | ||
# Indexes | ||
# | ||
# index_api_keys_on_organization_id (organization_id) | ||
# | ||
# Foreign Keys | ||
# | ||
# fk_rails_... (organization_id => organizations.id) | ||
# |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
# frozen_string_literal: true | ||
|
||
module Organizations | ||
class CreateService < BaseService | ||
def initialize(params) | ||
@params = params | ||
super | ||
end | ||
|
||
def call | ||
organization = Organization.new(params) | ||
|
||
ActiveRecord::Base.transaction do | ||
organization.save! | ||
organization.api_keys.create! | ||
end | ||
|
||
result.organization = organization | ||
result | ||
rescue ActiveRecord::RecordInvalid => e | ||
result.record_validation_failure!(record: e.record) | ||
end | ||
|
||
private | ||
|
||
attr_reader :params | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
# frozen_string_literal: true | ||
|
||
class CreateApiKeys < ActiveRecord::Migration[7.1] | ||
def up | ||
create_table :api_keys, id: :uuid do |t| | ||
t.references :organization, type: :uuid, null: false, foreign_key: true | ||
|
||
t.string :value, null: false | ||
|
||
t.timestamps | ||
end | ||
|
||
safety_assured do | ||
execute <<-SQL | ||
INSERT INTO api_keys (value, organization_id, created_at, updated_at) | ||
SELECT organizations.api_key, organizations.id, organizations.created_at, organizations.created_at | ||
FROM organizations | ||
SQL | ||
end | ||
end | ||
|
||
def down | ||
safety_assured do | ||
execute <<-SQL | ||
UPDATE organizations | ||
SET api_key = first_api_key.value | ||
FROM ( | ||
SELECT DISTINCT ON (organization_id) | ||
organization_id, | ||
value | ||
FROM api_keys | ||
ORDER BY organization_id, id ASC | ||
) first_api_key | ||
WHERE organizations.id = first_api_key.organization_id | ||
AND organizations.api_key IS NULL | ||
SQL | ||
end | ||
|
||
drop_table :api_keys | ||
end | ||
end |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
# frozen_string_literal: true | ||
|
||
FactoryBot.define do | ||
factory :api_key do | ||
organization { association(:organization) } | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'rails_helper' | ||
|
||
RSpec.describe ApiKey, type: :model do | ||
it { is_expected.to belong_to(:organization) } | ||
|
||
describe '#save' do | ||
subject { api_key.save! } | ||
|
||
before do | ||
allow(api_key).to receive(:set_value).and_call_original | ||
subject | ||
end | ||
|
||
context 'with a new record' do | ||
let(:api_key) { build(:api_key) } | ||
|
||
it 'calls #set_value' do | ||
expect(api_key).to have_received(:set_value) | ||
end | ||
end | ||
|
||
context 'with a persisted record' do | ||
let(:api_key) { create(:api_key) } | ||
|
||
it 'does not call #set_value' do | ||
expect(api_key).not_to have_received(:set_value) | ||
end | ||
end | ||
end | ||
|
||
describe '#set_value' do | ||
subject { api_key.send(:set_value) } | ||
|
||
let(:api_key) { build(:api_key) } | ||
let(:unique_value) { SecureRandom.uuid } | ||
|
||
before { allow(api_key).to receive(:generate_value).and_return(unique_value) } | ||
|
||
it 'sets result of #generate_value to the value' do | ||
expect { subject }.to change(api_key, :value).to unique_value | ||
end | ||
end | ||
|
||
describe '#generate_value' do | ||
subject { api_key.generate_value } | ||
|
||
let(:api_key) { build(:api_key) } | ||
let(:used_value) { create(:api_key).value } | ||
let(:unique_value) { SecureRandom.uuid } | ||
|
||
before do | ||
allow(SecureRandom).to receive(:uuid).and_return(used_value, unique_value) | ||
end | ||
|
||
it 'returns unique value between all ApiKeys' do | ||
expect(subject).to eq unique_value | ||
end | ||
end | ||
end |
Oops, something went wrong.