20171023000001_fixed_store_upgrade_ror_45_spec.rb 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. require 'rails_helper'
  2. # Rails 5.0 has changed to only store and read ActiveSupport::HashWithIndifferentAccess from stores
  3. # we extended lib/core_ext/active_record/store/indifferent_coder.rb to read also ActionController::Parameters
  4. # and convert them to ActiveSupport::HashWithIndifferentAccess for migration in db/migrate/20171023000001_fixed_store_upgrade_ror_45.rb.
  5. RSpec.describe FixedStoreUpgradeRor45, type: :db_migration do
  6. subject(:taskbar) { Taskbar.last }
  7. context 'when DB contains `store`d attributes saved as unpermitted ActionController::Parameters' do
  8. before do
  9. ActiveRecord::Base.connection.execute(<<~SQL.tap { |sql| sql.delete!('`') if !mysql? }) # rubocop:disable Rails/SquishedSQLHeredocs
  10. INSERT INTO taskbars (`user_id`, `client_id`, `key`, `callback`, `state`, `params`, `prio`, `notify`, `active`, `preferences`, `last_contact`, `updated_at`, `created_at`)
  11. VALUES (#{user.id},
  12. '123',
  13. 'Ticket-123',
  14. 'TicketZoom',
  15. '#{state.to_yaml}',
  16. '#{params.to_yaml}',
  17. 1,
  18. FALSE,
  19. TRUE,
  20. '#{preferences.to_yaml}',
  21. '#{last_contact}',
  22. '#{last_contact}',
  23. '#{last_contact}')
  24. SQL
  25. end
  26. let(:mysql?) { ActiveRecord::Base.connection_config[:adapter] == 'mysql2' }
  27. let(:user) { User.last }
  28. let(:last_contact) { '2017-09-01 10:10:00' }
  29. let(:state) { ActionController::Parameters.new('ticket' => {}, 'article' => {}) }
  30. let(:params) { ActionController::Parameters.new('ticket_id' => 1234, 'shown' => true) }
  31. let(:preferences) do
  32. ActionController::Parameters.new(
  33. 'tasks' => [
  34. {
  35. 'id' => 99_282,
  36. 'user_id' => 85_370,
  37. 'last_contact' => 1.week.after(Time.zone.parse(last_contact)),
  38. 'changed' => true
  39. }
  40. ]
  41. )
  42. end
  43. it 'converts `store`d attributes to ActiveSupport::HashWithIndifferentAccess, preserving original values' do
  44. expect { migrate }
  45. .to change { taskbar.reload.read_attribute_before_type_cast(:state) }
  46. .and not_change { taskbar.reload.state }
  47. .and change { taskbar.reload.read_attribute_before_type_cast(:params) }
  48. .and not_change { taskbar.reload.params }
  49. .and change { taskbar.reload.read_attribute_before_type_cast(:preferences) }
  50. expect(taskbar.read_attribute_before_type_cast(:state))
  51. .to start_with('--- !ruby/hash:ActiveSupport::HashWithIndifferentAccess')
  52. expect(taskbar.read_attribute_before_type_cast(:params))
  53. .to start_with('--- !ruby/hash:ActiveSupport::HashWithIndifferentAccess')
  54. expect(taskbar.read_attribute_before_type_cast(:preferences))
  55. .to start_with('--- !ruby/hash:ActiveSupport::HashWithIndifferentAccess')
  56. end
  57. end
  58. end