diff --git a/spec/models/rsa_key_spec.rb b/spec/models/rsa_key_spec.rb index 17fc813..8f32563 100644 --- a/spec/models/rsa_key_spec.rb +++ b/spec/models/rsa_key_spec.rb @@ -5,56 +5,12 @@ require 'rails_helper' RSpec.describe RSAKey do subject { create :rsa_key } - describe '#account' do - it { is_expected.to belong_to(:account).optional } - - it { is_expected.not_to validate_presence_of :account } - it { is_expected.not_to validate_uniqueness_of :account } - end - - describe '#public_key_pem' do - it { is_expected.to validate_presence_of :public_key_pem } - it { is_expected.to validate_uniqueness_of :public_key_pem } - end - - describe '#public_key_der' do - it { is_expected.to validate_presence_of :public_key_der } - it { is_expected.to validate_uniqueness_of :public_key_der } - end - - describe '#has_password' do - it { is_expected.to validate_exclusion_of(:has_password).in_array([nil]) } - end + it_behaves_like 'asymmetric_key' describe '#bits' do - it do - is_expected.to \ - validate_numericality_of(:bits) - .only_integer - .is_greater_than(0) - end - it { is_expected.to validate_inclusion_of(:bits).in_array([2048, 4096]) } end - describe '#sha1' do - it { is_expected.to validate_presence_of :sha1 } - it { is_expected.to validate_uniqueness_of(:sha1).case_insensitive } - end - - describe '#sha256' do - it { is_expected.to validate_presence_of :sha256 } - it { is_expected.to validate_uniqueness_of(:sha256).case_insensitive } - 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_key, private_key_pem: cleartext } diff --git a/spec/models/shared_examples/asymmetric_key.rb b/spec/models/shared_examples/asymmetric_key.rb new file mode 100644 index 0000000..9d1cd82 --- /dev/null +++ b/spec/models/shared_examples/asymmetric_key.rb @@ -0,0 +1,51 @@ +# frozen_string_literal: true + +RSpec.shared_examples 'asymmetric_key' do + describe '#account' do + it { is_expected.to belong_to(:account).optional } + + it { is_expected.not_to validate_presence_of :account } + it { is_expected.not_to validate_uniqueness_of :account } + end + + describe '#public_key_pem' do + it { is_expected.to validate_presence_of :public_key_pem } + it { is_expected.to validate_uniqueness_of :public_key_pem } + end + + describe '#public_key_der' do + it { is_expected.to validate_presence_of :public_key_der } + it { is_expected.to validate_uniqueness_of :public_key_der } + end + + describe '#has_password' do + it { is_expected.to validate_exclusion_of(:has_password).in_array([nil]) } + end + + describe '#bits' do + it do + is_expected.to \ + validate_numericality_of(:bits) + .only_integer + .is_greater_than(0) + end + end + + describe '#sha1' do + it { is_expected.to validate_presence_of :sha1 } + it { is_expected.to validate_uniqueness_of(:sha1).case_insensitive } + end + + describe '#sha256' do + it { is_expected.to validate_presence_of :sha256 } + it { is_expected.to validate_uniqueness_of(:sha256).case_insensitive } + 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 +end diff --git a/spec/rails_helper.rb b/spec/rails_helper.rb index 6323306..2477dc5 100644 --- a/spec/rails_helper.rb +++ b/spec/rails_helper.rb @@ -37,6 +37,7 @@ require_relative 'support/database_cleaner' require_relative 'support/devise' require_relative 'support/pundit' +require_relative 'models/shared_examples/asymmetric_key' require_relative 'models/shared_examples/nameable' require_relative 'models/shared_examples/required_nameable'