From acfc2025652551df3f166d37cab38b2f298cf070 Mon Sep 17 00:00:00 2001 From: Alex Kotov Date: Wed, 11 Sep 2019 03:57:12 +0500 Subject: [PATCH] Add model X509Certificate --- app/models/x509_certificate.rb | 15 +++++ ...20190910225118_create_x509_certificates.rb | 13 ++++ db/structure.sql | 65 ++++++++++++++++++- factories/x509_certificates.rb | 7 ++ spec/models/x509_certificate_spec.rb | 15 +++++ 5 files changed, 114 insertions(+), 1 deletion(-) create mode 100644 app/models/x509_certificate.rb create mode 100644 db/migrate/20190910225118_create_x509_certificates.rb create mode 100644 factories/x509_certificates.rb create mode 100644 spec/models/x509_certificate_spec.rb diff --git a/app/models/x509_certificate.rb b/app/models/x509_certificate.rb new file mode 100644 index 0000000..1801b42 --- /dev/null +++ b/app/models/x509_certificate.rb @@ -0,0 +1,15 @@ +# frozen_string_literal: true + +class X509Certificate < ApplicationRecord + ################ + # Associations # + ################ + + belongs_to :x509_certificate_request, optional: true + + ############### + # Validations # + ############### + + validates :pem, presence: true +end diff --git a/db/migrate/20190910225118_create_x509_certificates.rb b/db/migrate/20190910225118_create_x509_certificates.rb new file mode 100644 index 0000000..dcd3422 --- /dev/null +++ b/db/migrate/20190910225118_create_x509_certificates.rb @@ -0,0 +1,13 @@ +# frozen_string_literal: true + +class CreateX509Certificates < ActiveRecord::Migration[6.0] + def change + create_table :x509_certificates do |t| + t.timestamps null: false + + t.references :x509_certificate_request, null: true, foreign_key: true + + t.text :pem, null: false + end + end +end diff --git a/db/structure.sql b/db/structure.sql index 677caee..2ee1282 100644 --- a/db/structure.sql +++ b/db/structure.sql @@ -915,6 +915,38 @@ CREATE SEQUENCE public.x509_certificate_requests_id_seq ALTER SEQUENCE public.x509_certificate_requests_id_seq OWNED BY public.x509_certificate_requests.id; +-- +-- Name: x509_certificates; Type: TABLE; Schema: public; Owner: - +-- + +CREATE TABLE public.x509_certificates ( + id bigint NOT NULL, + created_at timestamp(6) without time zone NOT NULL, + updated_at timestamp(6) without time zone NOT NULL, + x509_certificate_request_id bigint, + pem text NOT NULL +); + + +-- +-- Name: x509_certificates_id_seq; Type: SEQUENCE; Schema: public; Owner: - +-- + +CREATE SEQUENCE public.x509_certificates_id_seq + START WITH 1 + INCREMENT BY 1 + NO MINVALUE + NO MAXVALUE + CACHE 1; + + +-- +-- Name: x509_certificates_id_seq; Type: SEQUENCE OWNED BY; Schema: public; Owner: - +-- + +ALTER SEQUENCE public.x509_certificates_id_seq OWNED BY public.x509_certificates.id; + + -- -- Name: accounts id; Type: DEFAULT; Schema: public; Owner: - -- @@ -1034,6 +1066,13 @@ ALTER TABLE ONLY public.users ALTER COLUMN id SET DEFAULT nextval('public.users_ ALTER TABLE ONLY public.x509_certificate_requests ALTER COLUMN id SET DEFAULT nextval('public.x509_certificate_requests_id_seq'::regclass); +-- +-- Name: x509_certificates id; Type: DEFAULT; Schema: public; Owner: - +-- + +ALTER TABLE ONLY public.x509_certificates ALTER COLUMN id SET DEFAULT nextval('public.x509_certificates_id_seq'::regclass); + + -- -- Name: accounts accounts_pkey; Type: CONSTRAINT; Schema: public; Owner: - -- @@ -1186,6 +1225,14 @@ ALTER TABLE ONLY public.x509_certificate_requests ADD CONSTRAINT x509_certificate_requests_pkey PRIMARY KEY (id); +-- +-- Name: x509_certificates x509_certificates_pkey; Type: CONSTRAINT; Schema: public; Owner: - +-- + +ALTER TABLE ONLY public.x509_certificates + ADD CONSTRAINT x509_certificates_pkey PRIMARY KEY (id); + + -- -- Name: index_accounts_on_contact_list_id; Type: INDEX; Schema: public; Owner: - -- @@ -1473,6 +1520,13 @@ CREATE UNIQUE INDEX index_users_on_unlock_token ON public.users USING btree (unl CREATE INDEX index_x509_certificate_requests_on_rsa_public_key_id ON public.x509_certificate_requests USING btree (rsa_public_key_id); +-- +-- Name: index_x509_certificates_on_x509_certificate_request_id; Type: INDEX; Schema: public; Owner: - +-- + +CREATE INDEX index_x509_certificates_on_x509_certificate_request_id ON public.x509_certificates USING btree (x509_certificate_request_id); + + -- -- Name: accounts ensure_contact_list_id_matches_related_person; Type: TRIGGER; Schema: public; Owner: - -- @@ -1510,6 +1564,14 @@ ALTER TABLE ONLY public.relationships ADD CONSTRAINT fk_rails_124c042ac0 FOREIGN KEY (initiator_account_id) REFERENCES public.accounts(id); +-- +-- Name: x509_certificates fk_rails_4958020bc7; Type: FK CONSTRAINT; Schema: public; Owner: - +-- + +ALTER TABLE ONLY public.x509_certificates + ADD CONSTRAINT fk_rails_4958020bc7 FOREIGN KEY (x509_certificate_request_id) REFERENCES public.x509_certificate_requests(id); + + -- -- Name: people fk_rails_4f02f930eb; Type: FK CONSTRAINT; Schema: public; Owner: - -- @@ -1641,6 +1703,7 @@ INSERT INTO "schema_migrations" (version) VALUES ('20181130024918'), ('20190910040709'), ('20190910115511'), -('20190910133430'); +('20190910133430'), +('20190910225118'); diff --git a/factories/x509_certificates.rb b/factories/x509_certificates.rb new file mode 100644 index 0000000..036a71f --- /dev/null +++ b/factories/x509_certificates.rb @@ -0,0 +1,7 @@ +# frozen_string_literal: true + +FactoryBot.define do + factory :self_signed_x509_certificate, class: X509Certificate do + pem { OpenSSL::X509::Certificate.new.to_pem } + end +end diff --git a/spec/models/x509_certificate_spec.rb b/spec/models/x509_certificate_spec.rb new file mode 100644 index 0000000..7afdfdc --- /dev/null +++ b/spec/models/x509_certificate_spec.rb @@ -0,0 +1,15 @@ +# frozen_string_literal: true + +require 'rails_helper' + +RSpec.describe X509Certificate do + subject { create :self_signed_x509_certificate } + + describe '#x509_certificate_request' do + it { is_expected.not_to validate_presence_of :x509_certificate_request } + end + + describe '#pem' do + it { is_expected.to validate_presence_of :pem } + end +end