1
0
Fork 0

Add association RSAKey#account

This commit is contained in:
Alex Kotov 2019-09-14 00:26:43 +05:00
parent 3dc9678043
commit 96affc6c9f
Signed by: kotovalexarian
GPG key ID: 553C0EBBEB5D5F08
5 changed files with 33 additions and 0 deletions

View file

@ -3,6 +3,12 @@
class RSAKey < AsymmetricKey
attr_accessor :private_key_pem, :private_key_pem_secret
################
# Associations #
################
belongs_to :account, optional: true
###############
# Validations #
###############

View file

@ -9,6 +9,8 @@ class CreateX509Tables < ActiveRecord::Migration[6.0]
t.string :type, null: false
t.index %i[type id], unique: true
t.references :account, foreign_key: true
t.text :public_key_pem, null: false
t.binary :public_key_der, null: false

View file

@ -373,6 +373,7 @@ CREATE TABLE public.asymmetric_keys (
created_at timestamp(6) without time zone NOT NULL,
updated_at timestamp(6) without time zone NOT NULL,
type character varying NOT NULL,
account_id bigint,
public_key_pem text NOT NULL,
public_key_der bytea NOT NULL,
private_key_pem_iv bytea,
@ -1236,6 +1237,13 @@ CREATE UNIQUE INDEX index_active_storage_attachments_uniqueness ON public.active
CREATE UNIQUE INDEX index_active_storage_blobs_on_key ON public.active_storage_blobs USING btree (key);
--
-- Name: index_asymmetric_keys_on_account_id; Type: INDEX; Schema: public; Owner: -
--
CREATE INDEX index_asymmetric_keys_on_account_id ON public.asymmetric_keys USING btree (account_id);
--
-- Name: index_asymmetric_keys_on_public_key_der; Type: INDEX; Schema: public; Owner: -
--
@ -1610,6 +1618,14 @@ ALTER TABLE ONLY public.regional_offices
ADD CONSTRAINT fk_rails_7a6d5fdd9a FOREIGN KEY (federal_subject_id) REFERENCES public.federal_subjects(id);
--
-- Name: asymmetric_keys fk_rails_7d85781ea1; Type: FK CONSTRAINT; Schema: public; Owner: -
--
ALTER TABLE ONLY public.asymmetric_keys
ADD CONSTRAINT fk_rails_7d85781ea1 FOREIGN KEY (account_id) REFERENCES public.accounts(id);
--
-- Name: user_omniauths fk_rails_8c1c9cb22e; Type: FK CONSTRAINT; Schema: public; Owner: -
--

View file

@ -2,6 +2,8 @@
FactoryBot.define do
factory :rsa_key do
association :account, factory: :usual_account
public_key_pem { OpenSSL::PKey::RSA.new(bits).public_key.to_pem }
public_key_der { OpenSSL::PKey::RSA.new(bits).public_key.to_der }
bits { [2048, 4096].sample }

View file

@ -5,6 +5,13 @@ require 'rails_helper'
RSpec.describe RSAKey do
subject { create :rsa_key }
describe '#account' do
it { is_expected.to belong_to(:account).optional }
it { is_expected.not_to validate_presence_of :account }
it { is_expected.not_to validate_uniqueness_of :account }
end
describe '#public_key_pem' do
it { is_expected.to validate_presence_of :public_key_pem }
it { is_expected.to validate_uniqueness_of :public_key_pem }