import_job_spec.rb 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  1. # Copyright (C) 2012-2022 Zammad Foundation, https://zammad-foundation.org/
  2. require 'rails_helper'
  3. RSpec.describe ImportJob do
  4. before do
  5. stub_const test_backend_name, test_backend_class
  6. stub_const test_backend_noreschedule_name, test_backend_noreschedule_class
  7. end
  8. let(:test_backend_name) { 'Import::Test' }
  9. let(:test_backend_class) do
  10. Class.new(Import::Base) do
  11. def start
  12. @import_job.result = { state: 'Done' }
  13. end
  14. end
  15. end
  16. let(:test_backend_noreschedule_name) { 'Import::NoRescheduleMethod' }
  17. let(:test_backend_noreschedule_class) do
  18. Class.new do
  19. def initialize(import_job)
  20. @import_job = import_job
  21. end
  22. def start
  23. @import_job.result = { state: 'Done' }
  24. end
  25. def reschedule?(_delayed_job)
  26. 'invalid_but_checkable_result'
  27. end
  28. end
  29. end
  30. describe '#dry_run' do
  31. it 'starts delayed dry run import job' do
  32. expect do
  33. described_class.dry_run(
  34. name: test_backend_name,
  35. payload: {}
  36. )
  37. end.to change {
  38. Delayed::Job.count
  39. }.by(1)
  40. end
  41. it 'starts dry run import job immediately' do
  42. expect do
  43. described_class.dry_run(
  44. name: test_backend_name,
  45. payload: {},
  46. delay: false
  47. )
  48. end.not_to change {
  49. Delayed::Job.count
  50. }
  51. end
  52. it "doesn't start job if one exists" do
  53. create(:import_job, dry_run: true)
  54. expect do
  55. described_class.dry_run(
  56. name: test_backend_name,
  57. payload: {},
  58. )
  59. end.not_to change {
  60. Delayed::Job.count
  61. }
  62. end
  63. end
  64. describe '#queue_registered' do
  65. it 'queues registered import jobs' do
  66. allow(Setting).to receive(:get)
  67. allow(Setting).to receive(:get).with('import_backends').and_return([test_backend_name])
  68. expect do
  69. described_class.queue_registered
  70. end.to change {
  71. described_class.exists?(name: test_backend_name)
  72. }
  73. end
  74. it "doesn't queue if backend isn't #queueable?" do
  75. allow(Setting).to receive(:get)
  76. allow(Setting).to receive(:get).with('import_backends').and_return([test_backend_name])
  77. allow(test_backend_class).to receive(:queueable?).and_return(false)
  78. expect do
  79. described_class.queue_registered
  80. end.not_to change {
  81. described_class.exists?(name: test_backend_name)
  82. }
  83. end
  84. it "doesn't queue if unfinished job entries exist" do
  85. create(:import_job)
  86. allow(Setting).to receive(:get)
  87. allow(Setting).to receive(:get).with('import_backends').and_return([test_backend_name])
  88. expect do
  89. described_class.queue_registered
  90. end.not_to change {
  91. described_class.exists?(name: test_backend_name)
  92. }
  93. end
  94. it 'logs errors for invalid registered backends' do
  95. allow(Setting).to receive(:get)
  96. allow(Setting).to receive(:get).with('import_backends').and_return(['InvalidBackend'])
  97. allow(described_class.logger).to receive(:error)
  98. described_class.queue_registered
  99. expect(described_class.logger).to have_received(:error)
  100. end
  101. end
  102. describe '#start' do
  103. it 'starts queued import jobs' do
  104. create_list(:import_job, 2)
  105. expect do
  106. described_class.start
  107. end.to change {
  108. described_class.where(started_at: nil).count
  109. }.by(-2)
  110. end
  111. it "doesn't start queued dry run import jobs" do
  112. create_list(:import_job, 2)
  113. create(:import_job, dry_run: true)
  114. expect do
  115. described_class.start
  116. end.to change {
  117. described_class.where(started_at: nil).count
  118. }.by(-2)
  119. end
  120. end
  121. describe '#start_registered' do
  122. it 'queues and starts registered import backends' do
  123. allow(Setting).to receive(:get)
  124. allow(Setting).to receive(:get).with('import_backends').and_return([test_backend_name])
  125. expect do
  126. described_class.start_registered
  127. end.to change {
  128. described_class.where.not(started_at: nil).where.not(finished_at: nil).count
  129. }.by(1)
  130. end
  131. end
  132. describe '#backend_valid?' do
  133. it 'detects existing backends' do
  134. expect(described_class.backend_valid?(test_backend_name)).to be true
  135. end
  136. it 'detects not existing backends' do
  137. expect(described_class.backend_valid?('InvalidBackend')).to be false
  138. end
  139. end
  140. describe '#backends' do
  141. it 'returns list of backend namespaces' do
  142. allow(Setting).to receive(:get).with('import_backends').and_return(['Import::Ldap'])
  143. allow(Import::Ldap).to receive(:active?).and_return(true)
  144. backends = described_class.backends
  145. expect(backends).to be_a(Array)
  146. expect(backends).not_to be_blank
  147. end
  148. it 'returns blank array if none are found' do
  149. allow(Setting).to receive(:get).with('import_backends')
  150. expect(described_class.backends).to eq([])
  151. end
  152. it "doesn't return invalid backends" do
  153. allow(Setting).to receive(:get).with('import_backends').and_return(['Import::InvalidBackend'])
  154. expect(described_class.backends).to eq([])
  155. end
  156. it "doesn't return inactive backends" do
  157. allow(Setting).to receive(:get).with('import_backends').and_return(['Import::Ldap'])
  158. allow(Import::Ldap).to receive(:active?).and_return(false)
  159. expect(described_class.backends).to eq([])
  160. end
  161. end
  162. describe '.start' do
  163. it 'runs import backend and updates started_at and finished_at' do
  164. instance = create(:import_job)
  165. expect do
  166. instance.start
  167. end.to change {
  168. instance.started_at
  169. }.and change {
  170. instance.finished_at
  171. }.and change {
  172. instance.result
  173. }
  174. end
  175. it 'handles exceptions as errors' do
  176. instance = create(:import_job)
  177. error_message = 'Some horrible error'
  178. allow_any_instance_of(test_backend_class).to receive(:start).and_raise(error_message)
  179. expect do
  180. instance.start
  181. instance.reload
  182. end.to change {
  183. instance.started_at
  184. }.and change {
  185. instance.finished_at
  186. }.and change {
  187. instance.result
  188. }
  189. expect(instance.result[:error]).to eq(error_message)
  190. end
  191. end
  192. describe '.reschedule?' do
  193. it 'returns false for already finished jobs' do
  194. instance = create(:import_job)
  195. delayed_job = double
  196. instance.update!(finished_at: Time.zone.now)
  197. expect(instance.reschedule?(delayed_job)).to be false
  198. end
  199. it 'returns false for backends not responding to reschedule?' do
  200. instance = create(:import_job)
  201. delayed_job = double
  202. expect(instance.reschedule?(delayed_job)).to be false
  203. end
  204. it 'returns the backend reschedule? value' do
  205. instance = create(:import_job, name: 'Import::NoRescheduleMethod')
  206. delayed_job = double
  207. expect(instance.reschedule?(delayed_job)).to eq 'invalid_but_checkable_result'
  208. end
  209. end
  210. end