Add interactor CreateRSAKeys
This commit is contained in:
parent
016515172f
commit
e45e74f4a5
2 changed files with 67 additions and 0 deletions
19
app/interactors/create_rsa_keys.rb
Normal file
19
app/interactors/create_rsa_keys.rb
Normal file
|
@ -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
|
48
spec/interactors/create_rsa_keys_spec.rb
Normal file
48
spec/interactors/create_rsa_keys_spec.rb
Normal file
|
@ -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
|
Reference in a new issue