can_param_examples.rb 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. # Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. RSpec.shared_examples 'ApplicationModel::CanParam' do |sample_data_attribute: :name|
  3. describe '.param_cleanup' do
  4. let(:params) do
  5. attributes_for(described_class.name.underscore)
  6. .merge(abc: true, id: 123, created_at: Time.current, updated_at: Time.current)
  7. end
  8. context 'when a new object' do
  9. let(:clean_params) { described_class.param_cleanup(params, true) }
  10. context 'when import mode is off' do
  11. it 'does not include id' do
  12. expect(clean_params).not_to have_key(:id)
  13. end
  14. it 'includes data fields' do
  15. expect(clean_params).to include(sample_data_attribute => clean_params[sample_data_attribute])
  16. end
  17. it 'does not include a non-existing field' do
  18. expect(clean_params).not_to have_key(:abc)
  19. end
  20. it 'does not include created and updated fields' do
  21. expect(clean_params.keys)
  22. .not_to include(:created_by_id, :updated_by_id, :created_at, :updated_at)
  23. end
  24. it 'does not include action and controller' do
  25. expect(clean_params.keys)
  26. .not_to include(:action, :controler)
  27. end
  28. end
  29. context 'when import mode is on' do
  30. before { Setting.set('import_mode', true) }
  31. it 'does not include id' do
  32. expect(clean_params).not_to have_key(:id)
  33. end
  34. it 'includes data fields' do
  35. expect(clean_params).to include(sample_data_attribute => clean_params[sample_data_attribute])
  36. end
  37. it 'does not include a non-existing field' do
  38. expect(clean_params).not_to have_key(:abc)
  39. end
  40. it 'include created and updated fields' do
  41. expect(clean_params).to include(
  42. created_by_id: params[:created_by_id],
  43. updated_by_id: params[:updated_by_id],
  44. created_at: params[:created_at],
  45. updated_at: params[:updated_at],
  46. )
  47. end
  48. it 'does not include action and controller' do
  49. expect(clean_params.keys)
  50. .not_to include(:action, :controler)
  51. end
  52. end
  53. end
  54. context 'when an existing object' do
  55. let(:clean_params) { described_class.param_cleanup(params, false) }
  56. context 'when import mode is off' do
  57. it 'includes id' do
  58. expect(clean_params).to include(id: 123)
  59. end
  60. it 'includes data fields' do
  61. expect(clean_params).to include(sample_data_attribute => clean_params[sample_data_attribute])
  62. end
  63. it 'does not include a non-existing field' do
  64. expect(clean_params).not_to have_key(:abc)
  65. end
  66. it 'does not include created and updated fields' do
  67. expect(clean_params.keys)
  68. .not_to include(:created_by_id, :updated_by_id, :created_at, :updated_at)
  69. end
  70. it 'does not include action and controller' do
  71. expect(clean_params.keys)
  72. .not_to include(:action, :controler)
  73. end
  74. end
  75. context 'when import mode is on' do
  76. before { Setting.set('import_mode', true) }
  77. it 'includes id' do
  78. expect(clean_params).to include(id: 123)
  79. end
  80. it 'includes data fields' do
  81. expect(clean_params).to include(sample_data_attribute => clean_params[sample_data_attribute])
  82. end
  83. it 'does not include a non-existing field' do
  84. expect(clean_params).not_to have_key(:abc)
  85. end
  86. it 'include created and updated fields' do
  87. expect(clean_params).to include(
  88. created_by_id: params[:created_by_id],
  89. updated_by_id: params[:updated_by_id],
  90. created_at: params[:created_at],
  91. updated_at: params[:updated_at],
  92. )
  93. end
  94. it 'does not include action and controller' do
  95. expect(clean_params.keys)
  96. .not_to include(:action, :controler)
  97. end
  98. end
  99. end
  100. end
  101. if described_class.has_attribute?(:preferences)
  102. describe '.param_preferences_merge' do
  103. subject(:object) { create(described_class.name.underscore.downcase, preferences:) }
  104. let(:preferences) do
  105. { A: 1, B: 2 }
  106. end
  107. let(:clean_params) { described_class.param_cleanup(params) }
  108. let(:merged_params) { object.param_preferences_merge(clean_params) }
  109. context 'when attribute contains other data' do
  110. let(:params) do
  111. { sample_data_attribute => '123' }
  112. end
  113. it 'keeps that data' do
  114. expect(merged_params).to include(sample_data_attribute => '123')
  115. end
  116. end
  117. context 'when preferences hash is given' do
  118. let(:params) do
  119. { preferences: { 'B' => 123, C: 256 } }
  120. end
  121. it 'merges hashes' do
  122. expect(merged_params[:preferences])
  123. .to include(
  124. 'A' => 1,
  125. 'B' => 123,
  126. 'C' => 256
  127. )
  128. end
  129. end
  130. context 'when empty preferences hash is given' do
  131. let(:params) do
  132. { preferences: {} }
  133. end
  134. it 'keeps original hash' do
  135. expect(merged_params[:preferences])
  136. .to include(
  137. 'A' => 1,
  138. 'B' => 2,
  139. )
  140. end
  141. end
  142. end
  143. end
  144. end