test/symmetric_encryption_test.rb in symmetric-encryption-4.0.0 vs test/symmetric_encryption_test.rb in symmetric-encryption-4.0.1

- old
+ new

@@ -2,11 +2,10 @@ # Unit Test for SymmetricEncryption # class SymmetricEncryptionTest < Minitest::Test describe 'SymmetricEncryption' do - describe 'configuration' do before do config = SymmetricEncryption::Config.new( file_name: File.join(File.dirname(__FILE__), 'config', 'symmetric-encryption.yml'), env: 'test' @@ -43,15 +42,15 @@ assert_equal @cipher_v0.version, cipher.version assert_equal true, SymmetricEncryption.secondary_ciphers.include?(cipher) end end - [:none, :base64, :base64strict, :base16].each do |encoding| + %i[none base64 base64strict base16].each do |encoding| describe "encoding: #{encoding}" do before do - @social_security_number = '987654321' - @social_security_number_encrypted = + @social_security_number = '987654321' + @social_security_number_encrypted = case encoding when :base64 "QEVuQwIAS+8X1NRrqdfEIQyFHVPuVA==\n" when :base64strict 'QEVuQwIAS+8X1NRrqdfEIQyFHVPuVA==' @@ -60,14 +59,13 @@ when :none "@EnC\x02\x00K\xEF\x17\xD4\xD4k\xA9\xD7\xC4!\f\x85\x1DS\xEET".force_encoding(Encoding.find('binary')) else raise "Add test for encoding: #{encoding}" end - @social_security_number_encrypted_with_secondary_1 = "D1UCu38pqJ3jc0GvwJHiow==\n" - @non_utf8 = "\xc2".force_encoding('binary') - @encoding = SymmetricEncryption.cipher.encoding - SymmetricEncryption.cipher.encoding = encoding + @non_utf8 = "\xc2".force_encoding('binary') + @encoding = SymmetricEncryption.cipher.encoding + SymmetricEncryption.cipher.encoding = encoding end after do SymmetricEncryption.cipher.encoding = @encoding end @@ -107,11 +105,11 @@ it "return '' when decrypting ''" do assert_equal '', SymmetricEncryption.decrypt('') end it 'determine if string is encrypted' do - if encoding == :base64strict || encoding == :base64 + if %i[base64strict base64].include?(encoding) assert SymmetricEncryption.encrypted?(@social_security_number_encrypted) refute SymmetricEncryption.encrypted?(@social_security_number) # Without a header it can only assume it is not encrypted refute SymmetricEncryption.encrypted?(SymmetricEncryption.encrypt(@social_security_number, header: false)) @@ -122,13 +120,14 @@ describe 'using select_cipher' do before do @social_security_number = '987654321' # Encrypt data without a header and encode with base64 which has a trailing '\n' - @encrypted_0_ssn = SymmetricEncryption.cipher(0).encode(SymmetricEncryption.cipher(0).binary_encrypt(@social_security_number, header: false)) + no_header = SymmetricEncryption.cipher(0).binary_encrypt(@social_security_number, header: false) + @encrypted_0_ssn = SymmetricEncryption.cipher(0).encode(no_header) - SymmetricEncryption.select_cipher do |encoded_str, decoded_str| + SymmetricEncryption.select_cipher do |encoded_str, _decoded_str| # Use cipher version 0 if the encoded string ends with "\n" otherwise # use the current default cipher encoded_str.end_with?("\n") ? SymmetricEncryption.cipher(0) : SymmetricEncryption.cipher end end @@ -145,11 +144,12 @@ describe 'without select_cipher' do before do @social_security_number = '987654321' # Encrypt data without a header and encode with base64 which has a trailing '\n' - assert @encrypted_0_ssn = SymmetricEncryption.cipher(0).encode(SymmetricEncryption.cipher(0).binary_encrypt(@social_security_number, header: false)) + no_header = SymmetricEncryption.cipher(0).binary_encrypt(@social_security_number, header: false) + assert @encrypted_0_ssn = SymmetricEncryption.cipher(0).encode(no_header) end it 'decrypt string without a header using an old cipher' do assert_raises OpenSSL::Cipher::CipherError do SymmetricEncryption.decrypt(@encrypted_0_ssn) @@ -205,16 +205,16 @@ end { integer: 21, float: 2.5, - decimal: BigDecimal.new('12.58'), - datetime: DateTime.new(2001, 11, 26, 20, 55, 54, "-5"), - time: Time.new(2013, 01, 01, 22, 30, 00, "-04:00"), - date: Date.new(1927, 04, 01), + decimal: BigDecimal('12.58'), + datetime: DateTime.new(2001, 11, 26, 20, 55, 54, '-5'), + time: Time.new(2013, 1, 1, 22, 30, 0, '-04:00'), + date: Date.new(1927, 4, 1), boolean: true, - yaml: {:a => :b}, + yaml: {a: :b}, json: {'a' => 'b'} }.each_pair do |type, value| describe type.to_s do it 'encrypt and decrypt' do assert encrypted = SymmetricEncryption.encrypt(value, type: type) @@ -232,9 +232,8 @@ it 'encrypt and decrypt' do assert encrypted = SymmetricEncryption.encrypt(false, type: :boolean) assert_equal false, SymmetricEncryption.decrypt(encrypted, type: :boolean) end end - end end end