external_sync_spec.rb 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. # Copyright (C) 2012-2022 Zammad Foundation, https://zammad-foundation.org/
  2. require 'rails_helper'
  3. RSpec.describe ExternalSync do
  4. describe '#changed?' do
  5. it 'keeps ActiveRecord instance unchanged on local but no remote changes' do
  6. object = create(:group)
  7. previous_changes = { name: 'Changed' }
  8. current_changes = previous_changes.dup
  9. result = described_class.changed?(
  10. object: object,
  11. previous_changes: previous_changes,
  12. current_changes: current_changes,
  13. )
  14. expect(result).to be false
  15. expect(object.has_changes_to_save?).to be false
  16. end
  17. it 'keeps ActiveRecord instance unchanged on local and remote changes' do
  18. object = create(:group)
  19. previous_changes = { name: 'Initial' }
  20. current_changes = { name: 'Changed' }
  21. result = described_class.changed?(
  22. object: object,
  23. previous_changes: previous_changes,
  24. current_changes: current_changes,
  25. )
  26. expect(result).to be false
  27. expect(object.has_changes_to_save?).to be false
  28. end
  29. it 'changes ActiveRecord instance attribute(s) for remote changes' do
  30. object = create(:group)
  31. previous_changes = { name: object.name }
  32. current_changes = { name: 'Changed' }
  33. result = described_class.changed?(
  34. object: object,
  35. previous_changes: previous_changes,
  36. current_changes: current_changes,
  37. )
  38. expect(result).to be true
  39. expect(object.has_changes_to_save?).to be true
  40. end
  41. it 'prevents ActiveRecord method calls' do
  42. object = create(:group)
  43. previous_changes = { name: object.name }
  44. current_changes = { destroy: 'Changed' }
  45. result = described_class.changed?(
  46. object: object,
  47. previous_changes: previous_changes,
  48. current_changes: current_changes,
  49. )
  50. expect(result).to be false
  51. expect(object.has_changes_to_save?).to be false
  52. expect(object.destroyed?).to be false
  53. end
  54. end
  55. describe '#map' do
  56. it 'maps to symbol keys' do
  57. mapping = {
  58. 'key' => 'key'
  59. }
  60. source = {
  61. 'key' => 'value'
  62. }
  63. result = {
  64. key: 'value'
  65. }
  66. expect(described_class.map(mapping: mapping, source: source)).to eq(result)
  67. end
  68. it 'resolves deep structures' do
  69. mapping = {
  70. 'sub.structure.key' => 'key',
  71. }
  72. source = {
  73. 'sub' => {
  74. 'structure' => {
  75. 'key' => 'value'
  76. }
  77. }
  78. }
  79. result = {
  80. key: 'value'
  81. }
  82. expect(described_class.map(mapping: mapping, source: source)).to eq(result)
  83. # check if sub structure is untouched
  84. expect(source['sub'].key?('structure')).to be true
  85. end
  86. it 'skips irrelevant keys' do
  87. mapping = {
  88. 'key' => 'key'
  89. }
  90. source = {
  91. 'key' => 'value',
  92. 'skipped' => 'skipped'
  93. }
  94. result = {
  95. key: 'value'
  96. }
  97. expect(described_class.map(mapping: mapping, source: source)).to eq(result)
  98. end
  99. it 'can handle object instances' do
  100. mapping = {
  101. 'name' => 'key'
  102. }
  103. source = double(name: 'value')
  104. result = {
  105. key: 'value'
  106. }
  107. expect(described_class.map(mapping: mapping, source: source)).to eq(result)
  108. end
  109. it 'can handle ActiveRecord instances' do
  110. mapping = {
  111. 'name' => 'key'
  112. }
  113. source = create(:group, name: 'value')
  114. result = {
  115. key: 'value'
  116. }
  117. expect(described_class.map(mapping: mapping, source: source)).to eq(result)
  118. end
  119. it 'prevents ActiveRecord method calls' do
  120. mapping = {
  121. 'name' => 'key',
  122. 'destroy' => 'evil'
  123. }
  124. source = create(:group, name: 'value')
  125. result = {
  126. key: 'value'
  127. }
  128. expect(described_class.map(mapping: mapping, source: source)).to eq(result)
  129. expect(source.destroyed?).to be false
  130. end
  131. end
  132. describe '.migrate' do
  133. let(:model) { 'Ticket' }
  134. let(:factory_name) { model.downcase.to_sym }
  135. let(:source) { create(factory_name) }
  136. let(:target) { create(factory_name) }
  137. let(:entries) do
  138. create_list(:external_sync, 2,
  139. object: model,
  140. o_id: source.id,)
  141. end
  142. it 'migrates entries' do
  143. expect do
  144. described_class.migrate('Ticket', source.id, target.id)
  145. end.to change {
  146. entries.each(&:reload).map(&:o_id).uniq
  147. }.from([source.id]).to([target.id])
  148. end
  149. end
  150. end