samnang
10/9/2013 - 3:18 AM

Migration RSpec

Migration RSpec

require 'spec_helper'
require File.expand_path('../../../db/migrate/20130910173340_decrypt_case_identifiers', __FILE__)

describe 'Case identifiers decryption migration' do
  subject(:migration) { DecryptCaseIdentifiers.new }

  describe ".up" do
    it "decrypts identifier of each case" do
      cases = create_list(:case, 2)
      cases[0].update_column(:identifier, encrypt('12345'))
      cases[1].update_column(:identifier, encrypt('abcde'))

      migration.up

      expect(cases[0].reload.identifier).to eq('12345')
      expect(cases[1].reload.identifier).to eq('abcde')
    end
  end

  describe ".down" do
    it "encrypts identifier of each case" do
      cases = create_list(:case, 2)
      cases[0].update_column(:identifier, '12345')
      cases[1].update_column(:identifier, 'abcde')

      migration.down

      expect(cases[0].reload.identifier).to eq(encrypt('12345'))
      expect(cases[1].reload.identifier).to eq(encrypt('abcde'))
    end
  end

  def encrypt(val)
    Base64.encode64(val.encrypt)
  end
end