From e45e74f4a592c0d4b4e8f14647eafd8133173fda Mon Sep 17 00:00:00 2001 From: Alex Kotov Date: Tue, 10 Sep 2019 21:18:21 +0500 Subject: [PATCH] Add interactor CreateRSAKeys --- app/interactors/create_rsa_keys.rb | 19 ++++++++++ spec/interactors/create_rsa_keys_spec.rb | 48 ++++++++++++++++++++++++ 2 files changed, 67 insertions(+) create mode 100644 app/interactors/create_rsa_keys.rb create mode 100644 spec/interactors/create_rsa_keys_spec.rb diff --git a/app/interactors/create_rsa_keys.rb b/app/interactors/create_rsa_keys.rb new file mode 100644 index 0000000..151feb0 --- /dev/null +++ b/app/interactors/create_rsa_keys.rb @@ -0,0 +1,19 @@ +# frozen_string_literal: true + +class CreateRSAKeys + include Interactor + + BITS = 4096 + + def call + pkey = OpenSSL::PKey::RSA.new BITS + + context.private_key_pem = pkey.to_pem.freeze + context.public_key_pem = pkey.public_key.to_pem.freeze + + context.public_key = RSAPublicKey.create!( + bits: BITS, + pem: context.public_key_pem, + ) + end +end diff --git a/spec/interactors/create_rsa_keys_spec.rb b/spec/interactors/create_rsa_keys_spec.rb new file mode 100644 index 0000000..020673e --- /dev/null +++ b/spec/interactors/create_rsa_keys_spec.rb @@ -0,0 +1,48 @@ +# frozen_string_literal: true + +require 'rails_helper' + +RSpec.describe CreateRSAKeys do + subject { described_class.call } + + specify do + expect { subject }.to change(RSAPublicKey, :count).by(1) + end + + specify do + expect(subject.private_key_pem).to be_instance_of String + end + + specify do + expect(subject.public_key_pem).to be_instance_of String + end + + specify do + expect(subject.public_key).to be_instance_of RSAPublicKey + end + + specify do + expect(subject.private_key_pem).to be_frozen + end + + specify do + expect(subject.public_key_pem).to be_frozen + end + + specify do + expect { OpenSSL::PKey::RSA.new subject.private_key_pem }.not_to raise_error + end + + specify do + expect { OpenSSL::PKey::RSA.new subject.public_key_pem }.not_to raise_error + end + + specify do + expect(subject.public_key_pem).to \ + eq OpenSSL::PKey::RSA.new(subject.private_key_pem).public_key.to_pem + end + + specify do + expect(subject.public_key.pem).to eq subject.public_key_pem + end +end