1
0
Fork 0
This repository has been archived on 2023-03-27. You can view files and clone it, but cannot push or open issues or pull requests.
lpr-partynest/spec/interactors/create_rsa_keys_spec.rb

102 lines
2.3 KiB
Ruby

# frozen_string_literal: true
require 'rails_helper'
RSpec.describe CreateRSAKeys do
subject { described_class.call }
specify do
expect { subject }.to change(AsymmetricKey, :count).by(1)
end
specify do
expect { subject }.to change(RSAKey, :count).by(1)
end
specify do
expect { subject }.to(
have_enqueued_job(ClearAsymmetricPrivateKeyJob)
.with do |asymmetric_key_id|
expect(asymmetric_key_id).to equal AsymmetricKey.last.id
end,
)
end
specify do
expect(subject.asymmetric_key).to be_instance_of RSAKey
end
specify do
expect(subject.asymmetric_key.sha1).not_to be_blank
end
specify do
expect(subject.asymmetric_key.sha256).not_to be_blank
end
specify do
expect(subject.asymmetric_key.private_key_pem).not_to be_blank
end
specify do
expect(subject.asymmetric_key.private_key_pem_secret).not_to be_blank
end
specify do
expect do
OpenSSL::PKey::RSA.new subject.asymmetric_key.private_key_pem
end.not_to raise_error
end
specify do
expect do
OpenSSL::PKey::RSA.new subject.asymmetric_key.public_key_pem
end.not_to \
raise_error
end
specify do
expect(subject.asymmetric_key.sha1).to eq(
Digest::SHA1.hexdigest(
OpenSSL::PKey::RSA.new(subject.asymmetric_key.public_key_pem).to_der,
),
)
end
specify do
expect(subject.asymmetric_key.sha256).to eq(
Digest::SHA256.hexdigest(
OpenSSL::PKey::RSA.new(subject.asymmetric_key.public_key_pem).to_der,
),
)
end
specify do
expect(subject.asymmetric_key.public_key_pem).to eq(
OpenSSL::PKey::RSA.new(subject.asymmetric_key.private_key_pem)
.public_key.to_pem,
)
end
specify do
expect(subject.asymmetric_key.private_key_pem_iv).not_to be_blank
end
specify do
expect(subject.asymmetric_key.private_key_pem_ciphertext).not_to be_blank
end
specify do
cipher = OpenSSL::Cipher::AES256.new
cipher.decrypt
cipher.iv = subject.asymmetric_key.private_key_pem_iv
cipher.key = subject.asymmetric_key.private_key_pem_secret
cleartext = [
cipher.update(subject.asymmetric_key.private_key_pem_ciphertext),
cipher.final,
].join.freeze
expect(cleartext).to eq subject.asymmetric_key.private_key_pem
end
end