From 96affc6c9fe40f9a8baad7e76d8f86a6db3806dd Mon Sep 17 00:00:00 2001 From: Alex Kotov Date: Sat, 14 Sep 2019 00:26:43 +0500 Subject: [PATCH] Add association RSAKey#account --- app/models/rsa_key.rb | 6 ++++++ db/migrate/20190911081459_create_x509_tables.rb | 2 ++ db/structure.sql | 16 ++++++++++++++++ factories/rsa_keys.rb | 2 ++ spec/models/rsa_key_spec.rb | 7 +++++++ 5 files changed, 33 insertions(+) diff --git a/app/models/rsa_key.rb b/app/models/rsa_key.rb index 1a8e27a..ddd4e37 100644 --- a/app/models/rsa_key.rb +++ b/app/models/rsa_key.rb @@ -3,6 +3,12 @@ class RSAKey < AsymmetricKey attr_accessor :private_key_pem, :private_key_pem_secret + ################ + # Associations # + ################ + + belongs_to :account, optional: true + ############### # Validations # ############### diff --git a/db/migrate/20190911081459_create_x509_tables.rb b/db/migrate/20190911081459_create_x509_tables.rb index 3b8dfdf..03e57f0 100644 --- a/db/migrate/20190911081459_create_x509_tables.rb +++ b/db/migrate/20190911081459_create_x509_tables.rb @@ -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 diff --git a/db/structure.sql b/db/structure.sql index dc586e1..e6cd275 100644 --- a/db/structure.sql +++ b/db/structure.sql @@ -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: - -- diff --git a/factories/rsa_keys.rb b/factories/rsa_keys.rb index 694a7f6..b3fdb83 100644 --- a/factories/rsa_keys.rb +++ b/factories/rsa_keys.rb @@ -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 } diff --git a/spec/models/rsa_key_spec.rb b/spec/models/rsa_key_spec.rb index a1b87b7..87fcc68 100644 --- a/spec/models/rsa_key_spec.rb +++ b/spec/models/rsa_key_spec.rb @@ -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 }