2019-11-05 10:08:31 +00:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
require 'spec_helper'
|
2021-06-11 18:10:13 +00:00
|
|
|
require_migration!
|
2019-11-05 10:08:31 +00:00
|
|
|
|
2020-06-24 18:09:03 +00:00
|
|
|
RSpec.describe EncryptPlaintextAttributesOnApplicationSettings do
|
2019-11-05 10:08:31 +00:00
|
|
|
let(:migration) { described_class.new }
|
|
|
|
let(:application_settings) { table(:application_settings) }
|
|
|
|
let(:plaintext) { 'secret-token' }
|
|
|
|
|
2020-05-19 21:08:05 +00:00
|
|
|
plaintext_attributes = %w[
|
2019-11-05 10:08:31 +00:00
|
|
|
akismet_api_key
|
|
|
|
elasticsearch_aws_secret_access_key
|
|
|
|
recaptcha_private_key
|
|
|
|
recaptcha_site_key
|
|
|
|
slack_app_secret
|
|
|
|
slack_app_verification_token
|
|
|
|
].freeze
|
|
|
|
|
|
|
|
describe '#up' do
|
2019-11-22 14:29:53 +00:00
|
|
|
it 'encrypts token and saves it' do
|
2020-12-10 21:10:15 +00:00
|
|
|
application_setting = application_settings.create!
|
2019-11-05 10:08:31 +00:00
|
|
|
application_setting.update_columns(
|
2020-05-19 21:08:05 +00:00
|
|
|
plaintext_attributes.each_with_object({}) do |plaintext_attribute, attributes|
|
2019-11-05 10:08:31 +00:00
|
|
|
attributes[plaintext_attribute] = plaintext
|
|
|
|
end
|
|
|
|
)
|
|
|
|
|
|
|
|
migration.up
|
|
|
|
|
|
|
|
application_setting.reload
|
2020-05-19 21:08:05 +00:00
|
|
|
plaintext_attributes.each do |plaintext_attribute|
|
2019-11-22 14:29:53 +00:00
|
|
|
expect(application_setting[plaintext_attribute]).not_to be_nil
|
2019-11-05 10:08:31 +00:00
|
|
|
expect(application_setting["encrypted_#{plaintext_attribute}"]).not_to be_nil
|
|
|
|
expect(application_setting["encrypted_#{plaintext_attribute}_iv"]).not_to be_nil
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe '#down' do
|
|
|
|
it 'decrypts encrypted token and saves it' do
|
2020-12-10 21:10:15 +00:00
|
|
|
application_setting = application_settings.create!(
|
2020-05-19 21:08:05 +00:00
|
|
|
plaintext_attributes.each_with_object({}) do |plaintext_attribute, attributes|
|
2019-11-05 10:08:31 +00:00
|
|
|
attributes[plaintext_attribute] = plaintext
|
|
|
|
end
|
|
|
|
)
|
|
|
|
|
|
|
|
migration.down
|
|
|
|
|
|
|
|
application_setting.reload
|
2020-05-19 21:08:05 +00:00
|
|
|
plaintext_attributes.each do |plaintext_attribute|
|
2019-11-05 10:08:31 +00:00
|
|
|
expect(application_setting[plaintext_attribute]).to eq(plaintext)
|
|
|
|
expect(application_setting["encrypted_#{plaintext_attribute}"]).to be_nil
|
|
|
|
expect(application_setting["encrypted_#{plaintext_attribute}_iv"]).to be_nil
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|