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/models/rsa_public_key_spec.rb

204 lines
4.7 KiB
Ruby
Raw Normal View History

2019-09-10 08:08:23 -04:00
# frozen_string_literal: true
require 'rails_helper'
RSpec.describe RSAPublicKey do
subject { create :rsa_public_key }
describe '#public_key_pem' do
it { is_expected.to validate_presence_of :public_key_pem }
2019-09-10 08:08:23 -04:00
end
describe '#bits' do
it { is_expected.to validate_inclusion_of(:bits).in_array([2048, 4096]) }
end
describe '#private_key_pem_iv' do
it { is_expected.not_to validate_presence_of :private_key_pem_iv }
end
describe '#private_key_pem_ciphertext' do
it { is_expected.not_to validate_presence_of :private_key_pem_ciphertext }
end
describe '#encrypt_private_key_pem' do
subject { create :rsa_public_key, private_key_pem: cleartext }
let(:cleartext) { OpenSSL::PKey::RSA.new.to_pem.freeze }
specify do
expect(subject.encrypt_private_key_pem).to be_instance_of String
end
specify do
expect(subject.encrypt_private_key_pem).to be_frozen
end
specify do
expect(subject.encrypt_private_key_pem).to \
equal subject.private_key_pem_secret
end
specify do
expect { subject.encrypt_private_key_pem }.to \
change(subject, :private_key_pem_iv)
.from(nil)
end
specify do
expect { subject.encrypt_private_key_pem }.to \
change(subject, :private_key_pem_secret)
.from(nil)
end
specify do
expect { subject.encrypt_private_key_pem }.to \
change(subject, :private_key_pem_ciphertext)
.from(nil)
end
context 'after call' do
before { subject.encrypt_private_key_pem }
specify do
expect(subject.private_key_pem).to be_instance_of String
end
specify do
expect(subject.private_key_pem_iv).to be_instance_of String
end
specify do
expect(subject.private_key_pem_secret).to be_instance_of String
end
specify do
expect(subject.private_key_pem_ciphertext).to be_instance_of String
end
specify do
expect(subject.private_key_pem).to be_frozen
end
specify do
expect(subject.private_key_pem_iv).to be_frozen
end
specify do
expect(subject.private_key_pem_secret).to be_frozen
end
specify do
expect(subject.private_key_pem_ciphertext).to be_frozen
end
specify do
expect(subject.private_key_pem).to eq cleartext
end
specify do
expect(subject.private_key_pem_iv).not_to be_blank
end
specify do
expect(subject.private_key_pem_secret).not_to be_blank
end
specify do
expect(subject.private_key_pem_ciphertext).not_to be_blank
end
end
end
describe '#decrypt_private_key_pem' do
2019-09-11 23:02:54 -04:00
subject do
create(
:rsa_public_key,
private_key_pem_iv: iv,
private_key_pem_secret: secret,
private_key_pem_ciphertext: ciphertext,
)
end
let(:cleartext) { OpenSSL::PKey::RSA.new.to_pem.freeze }
2019-09-11 23:02:54 -04:00
let!(:cipher) { OpenSSL::Cipher::AES256.new.tap(&:encrypt) }
2019-09-11 23:02:54 -04:00
let!(:iv) { cipher.random_iv.freeze }
let!(:secret) { cipher.random_key.freeze }
2019-09-11 23:02:54 -04:00
let!(:ciphertext) { [cipher.update(cleartext), cipher.final].join.freeze }
specify do
expect(subject.decrypt_private_key_pem).to be_instance_of String
end
specify do
expect(subject.decrypt_private_key_pem).to be_frozen
end
specify do
expect(subject.decrypt_private_key_pem).to equal subject.private_key_pem
end
specify do
expect { subject.decrypt_private_key_pem }.to \
change(subject, :private_key_pem)
.from(nil)
.to(cleartext)
end
2019-09-11 23:02:54 -04:00
context 'after call' do
before { subject.decrypt_private_key_pem }
specify do
expect(subject.private_key_pem).to be_instance_of String
end
specify do
expect(subject.private_key_pem_iv).to be_instance_of String
end
specify do
expect(subject.private_key_pem_secret).to be_instance_of String
end
specify do
expect(subject.private_key_pem_ciphertext).to be_instance_of String
end
specify do
expect(subject.private_key_pem).to be_frozen
end
specify do
expect(subject.private_key_pem_iv).to be_frozen
end
specify do
expect(subject.private_key_pem_secret).to be_frozen
end
specify do
expect(subject.private_key_pem_ciphertext).to be_frozen
end
specify do
expect(subject.private_key_pem).to eq cleartext
end
specify do
expect(subject.private_key_pem_iv).to equal iv
end
specify do
expect(subject.private_key_pem_secret).to equal secret
end
specify do
expect(subject.private_key_pem_ciphertext).to equal ciphertext
end
end
end
2019-09-10 08:08:23 -04:00
end