1
0
Fork 0

Add interactor CreateRSAKeys

This commit is contained in:
Alex Kotov 2019-09-10 21:18:21 +05:00
parent 016515172f
commit e45e74f4a5
Signed by: kotovalexarian
GPG key ID: 553C0EBBEB5D5F08
2 changed files with 67 additions and 0 deletions

View 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

View 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