check_setup_spec.rb 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  1. # Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. require 'rails_helper'
  3. RSpec.describe Service::System::CheckSetup, :aggregate_failures do
  4. describe '.new?' do
  5. context 'with fresh install' do
  6. before do
  7. Setting.set('system_init_done', false)
  8. end
  9. it 'returns true' do
  10. expect(described_class.new?).to be(true)
  11. end
  12. end
  13. context 'with in progress setup' do
  14. before do
  15. Setting.set('system_init_done', false)
  16. Setting.set('import_mode', true)
  17. end
  18. it 'returns false' do
  19. expect(described_class.new?).to be(false)
  20. end
  21. end
  22. context 'with finished setup' do
  23. before do
  24. Setting.set('system_init_done', true)
  25. create(:admin)
  26. end
  27. it 'returns false' do
  28. expect(described_class.new?).to be(false)
  29. end
  30. end
  31. end
  32. describe '.new!' do
  33. context 'with fresh install' do
  34. before do
  35. Setting.set('system_init_done', false)
  36. end
  37. it 'raises no error' do
  38. expect { described_class.new! }.not_to raise_error
  39. end
  40. end
  41. context 'with in progress setup' do
  42. before do
  43. Setting.set('system_init_done', false)
  44. Setting.set('import_mode', true)
  45. end
  46. it 'raises error' do
  47. expect { described_class.new! }.to raise_error(Service::System::CheckSetup::SystemSetupError)
  48. end
  49. end
  50. context 'with finished setup' do
  51. before do
  52. Setting.set('system_init_done', true)
  53. create(:admin)
  54. end
  55. it 'raises error' do
  56. expect { described_class.new! }.to raise_error(Service::System::CheckSetup::SystemSetupError)
  57. end
  58. end
  59. end
  60. describe '.done?' do
  61. context 'with fresh install' do
  62. before do
  63. Setting.set('system_init_done', false)
  64. end
  65. it 'returns false' do
  66. expect(described_class.done?).to be(false)
  67. end
  68. end
  69. context 'with in progress setup' do
  70. before do
  71. Setting.set('system_init_done', false)
  72. Setting.set('import_mode', true)
  73. end
  74. it 'returns false' do
  75. expect(described_class.done?).to be(false)
  76. end
  77. end
  78. context 'with finished setup' do
  79. before do
  80. Setting.set('system_init_done', true)
  81. create(:admin)
  82. end
  83. it 'returns true' do
  84. expect(described_class.done?).to be(true)
  85. end
  86. end
  87. end
  88. describe '.done!' do
  89. context 'with fresh install' do
  90. before do
  91. Setting.set('system_init_done', false)
  92. end
  93. it 'raises error' do
  94. expect { described_class.done! }.to raise_error(Service::System::CheckSetup::SystemSetupError)
  95. end
  96. end
  97. context 'with in progress setup' do
  98. before do
  99. Setting.set('system_init_done', false)
  100. Setting.set('import_mode', true)
  101. end
  102. it 'raises error' do
  103. expect { described_class.done! }.to raise_error(Service::System::CheckSetup::SystemSetupError)
  104. end
  105. end
  106. context 'with finished setup' do
  107. before do
  108. Setting.set('system_init_done', true)
  109. create(:admin)
  110. end
  111. it 'raises no error' do
  112. expect { described_class.done! }.not_to raise_error
  113. end
  114. end
  115. end
  116. describe '#execute' do
  117. subject(:service) { described_class.new }
  118. describe 'with fresh install' do
  119. before do
  120. Setting.set('system_init_done', false)
  121. service.execute
  122. end
  123. it 'returns new status' do
  124. expect(service.status).to eq('new')
  125. expect(service.type).to be_nil
  126. end
  127. context 'when auto wizard is enabled' do
  128. before do
  129. allow(AutoWizard).to receive(:enabled?).and_return(true)
  130. service.execute
  131. end
  132. it 'returns automated status' do
  133. expect(service.status).to eq('automated')
  134. expect(service.type).to be_nil
  135. end
  136. end
  137. end
  138. context 'with finished setup' do
  139. before do
  140. Setting.set('system_init_done', true)
  141. create(:admin)
  142. end
  143. context 'with manual setup' do
  144. before do
  145. Setting.set('import_mode', false)
  146. service.execute
  147. end
  148. it 'returns done status' do
  149. expect(service.status).to eq('done')
  150. expect(service.type).to be_nil
  151. end
  152. end
  153. context 'with auto setup' do
  154. before do
  155. Setting.set('import_mode', false)
  156. allow(AutoWizard).to receive(:enabled?).and_return(true)
  157. service.execute
  158. end
  159. it 'returns done status' do
  160. expect(service.status).to eq('done')
  161. expect(service.type).to be_nil
  162. end
  163. end
  164. context 'with import setup' do
  165. before do
  166. Setting.set('import_mode', true)
  167. service.execute
  168. end
  169. it 'returns done status' do
  170. expect(service.status).to eq('in_progress')
  171. expect(service.type).to eq('import')
  172. end
  173. end
  174. end
  175. describe 'with in progress setup' do
  176. before do
  177. Setting.set('system_init_done', false)
  178. end
  179. context 'with manual setup' do
  180. before do
  181. Setting.set('import_mode', false)
  182. Service::ExecuteLockedBlock.new('Zammad::System::Setup', 10_000).execute do
  183. service.execute
  184. end
  185. end
  186. it 'returns in_progress status' do
  187. expect(service.status).to eq('in_progress')
  188. expect(service.type).to eq('manual')
  189. end
  190. end
  191. context 'with auto setup' do
  192. before do
  193. Setting.set('import_mode', false)
  194. Service::ExecuteLockedBlock.new('Zammad::System::Setup', 10_000).execute do
  195. begin
  196. json = Rails.root.join('auto_wizard.json')
  197. FileUtils.touch(json)
  198. service.execute
  199. ensure
  200. FileUtils.rm(json)
  201. end
  202. end
  203. end
  204. it 'returns in_progress status' do
  205. expect(service.status).to eq('in_progress')
  206. expect(service.type).to eq('auto')
  207. end
  208. end
  209. context 'with import setup' do
  210. before do
  211. Setting.set('import_mode', true)
  212. service.execute
  213. end
  214. it 'returns in_progress status' do
  215. expect(service.status).to eq('in_progress')
  216. expect(service.type).to eq('import')
  217. end
  218. end
  219. end
  220. describe 'dubious setup' do
  221. context 'with finished setup and no admin user' do
  222. before do
  223. Setting.set('system_init_done', true)
  224. end
  225. it 'raises error' do
  226. expect { service.execute }.to raise_error(Service::System::CheckSetup::SystemSetupError)
  227. end
  228. end
  229. context 'with not finished setup and at least one admin users' do
  230. before do
  231. Setting.set('system_init_done', false)
  232. create(:admin)
  233. end
  234. it 'raises error' do
  235. allow(Rails.logger).to receive(:warn)
  236. service.execute
  237. expect(Rails.logger).to have_received(:warn).with('The system setup is not marked as done, but at least one admin user is existing. Marking system setup as done.')
  238. end
  239. end
  240. end
  241. end
  242. end