import_job_spec.rb 6.7 KB

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