auto_wizard_spec.rb 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385
  1. # Copyright (C) 2012-2023 Zammad Foundation, https://zammad-foundation.org/
  2. require 'rails_helper'
  3. RSpec.describe AutoWizard do
  4. describe '.enabled?' do
  5. context 'without "auto_wizard.json" file in project root' do
  6. before { FileUtils.rm(Rails.root.join('auto_wizard.json'), force: true) }
  7. it 'returns false' do
  8. expect(described_class.enabled?).to be(false)
  9. end
  10. end
  11. context 'with "auto_wizard.json" file in project root' do
  12. around do |example|
  13. FileUtils.touch(Rails.root.join('auto_wizard.json'))
  14. example.run
  15. FileUtils.rm(Rails.root.join('auto_wizard.json'))
  16. end
  17. it 'returns true' do
  18. expect(described_class.enabled?).to be(true)
  19. end
  20. end
  21. context 'with "auto_wizard.json" file in custom directory' do
  22. before do
  23. allow(ENV).to receive(:[]).with('AUTOWIZARD_RELATIVE_PATH').and_return('var/auto_wizard.json')
  24. end
  25. context 'with file present' do
  26. around do |example|
  27. FileUtils.touch(Rails.root.join('var/auto_wizard.json'))
  28. example.run
  29. FileUtils.rm(Rails.root.join('var/auto_wizard.json'))
  30. end
  31. it 'returns true' do
  32. expect(described_class.enabled?).to be(true)
  33. end
  34. end
  35. context 'with file missing' do
  36. it 'returns true' do
  37. expect(described_class.enabled?).to be(false)
  38. end
  39. end
  40. end
  41. end
  42. describe '.setup' do
  43. around do |example|
  44. Rails.root.join('auto_wizard.json').write(seed_data.to_json)
  45. example.run
  46. FileUtils.rm(Rails.root.join('auto_wizard.json'), force: true)
  47. end
  48. let(:seed_data) { {} }
  49. it 'removes "auto_wizard.json" file when complete' do
  50. expect { described_class.setup }
  51. .to change { Rails.root.join('auto_wizard.json').exist? }.to(false)
  52. end
  53. context 'when "auto_wizard.json" contains a set of User attributes and associations (Role names)' do
  54. let(:seed_data) do
  55. {
  56. Users: [
  57. {
  58. login: 'master_unit_test01@example.com',
  59. firstname: 'Test Admin',
  60. lastname: 'Agent',
  61. email: 'master_unit_test01@example.com',
  62. password: 'test',
  63. roles: ['Agent']
  64. }
  65. ]
  66. }
  67. end
  68. it 'creates a user with those attributes and roles' do
  69. expect { described_class.setup }
  70. .to change(User, :count).by(1)
  71. .and change { User.last.roles }.to(Role.where(name: 'Agent'))
  72. .and change { User.last.login }.to('master_unit_test01@example.com')
  73. .and change { User.last.firstname }.to('Test Admin')
  74. .and change { User.last.lastname }.to('Agent')
  75. .and change { User.last.email }.to('master_unit_test01@example.com')
  76. .and(change do
  77. Auth.new(User.last.email, 'test').valid!
  78. rescue Auth::Error::AuthenticationFailed
  79. false
  80. end.from(false))
  81. end
  82. end
  83. context 'when "auto_wizard.json" contains a set of User attributes without associations' do
  84. let(:seed_data) do
  85. {
  86. Users: [
  87. {
  88. login: 'master_unit_test01@example.com',
  89. firstname: 'Test Admin',
  90. lastname: 'Agent',
  91. email: 'master_unit_test01@example.com',
  92. password: 'test'
  93. }
  94. ]
  95. }
  96. end
  97. it 'creates a user with those attributes and Admin + Agent roles' do
  98. expect { described_class.setup }
  99. .to change(User, :count).by(1)
  100. .and change { User.last.roles }.to(Role.where(name: %w[Admin Agent]))
  101. .and change { User.last.login }.to('master_unit_test01@example.com')
  102. .and change { User.last.firstname }.to('Test Admin')
  103. .and change { User.last.lastname }.to('Agent')
  104. .and change { User.last.email }.to('master_unit_test01@example.com')
  105. .and(change do
  106. Auth.new(User.last.email, 'test').valid!
  107. rescue Auth::Error::AuthenticationFailed
  108. false
  109. end.from(false))
  110. end
  111. end
  112. context 'when "auto_wizard.json" contains a set of Group attributes and associations (User emails, Signature name, & EmailAddress id)' do
  113. let(:seed_data) do
  114. {
  115. Groups: [
  116. {
  117. name: 'some group1',
  118. note: 'Lorem ipsum dolor',
  119. users: [group_agent.email],
  120. signature: group_signature.name,
  121. email_address_id: group_email.id,
  122. }
  123. ]
  124. }
  125. end
  126. let(:group_agent) { create(:agent) }
  127. let(:group_signature) { create(:signature) }
  128. let(:group_email) { create(:email_address) }
  129. it 'creates a group with those attributes and associations' do
  130. expect { described_class.setup }
  131. .to change(Group, :count).by(1)
  132. .and change { Group.last.name }.to('some group1')
  133. .and change { Group.last.note }.to('Lorem ipsum dolor')
  134. .and change { Group.last.users }.to([group_agent])
  135. .and change { Group.last.signature }.to(group_signature)
  136. end
  137. end
  138. context 'when "auto_wizard.json" contains a set of EmailAddress attributes' do
  139. let(:seed_data) do
  140. {
  141. EmailAddresses: [
  142. {
  143. channel_id: channel.id,
  144. name: 'John Doe',
  145. email: 'johndoe@example.com',
  146. }
  147. ],
  148. }
  149. end
  150. let(:channel) { create(:email_channel) }
  151. it 'creates an email address with the given attributes' do
  152. expect { described_class.setup }
  153. .to change(EmailAddress, :count)
  154. .and change { EmailAddress.last&.name }.to('John Doe')
  155. .and change { EmailAddress.last&.email }.to('johndoe@example.com')
  156. .and change { EmailAddress.last&.channel }.to(channel)
  157. end
  158. end
  159. context 'when "auto_wizard.json" contains a set of EmailAddress attributes, including an existing ID' do
  160. let(:seed_data) do
  161. {
  162. EmailAddresses: [
  163. {
  164. id: email_address.id,
  165. channel_id: new_channel.id,
  166. name: 'John Doe',
  167. email: 'johndoe@example.com',
  168. }
  169. ],
  170. }
  171. end
  172. let(:email_address) { create(:email_address) }
  173. let(:new_channel) { create(:email_channel) }
  174. it 'updates the specified email address with the given attributes' do
  175. expect { described_class.setup }
  176. .to not_change(EmailAddress, :count)
  177. .and change { email_address.reload.name }.to('John Doe')
  178. .and change { email_address.reload.email }.to('johndoe@example.com')
  179. .and change { email_address.reload.channel }.to(new_channel)
  180. end
  181. end
  182. context 'when "auto_wizard.json" contains a set of Channel attributes' do
  183. let(:seed_data) do
  184. {
  185. Channels: [
  186. {
  187. id: 100,
  188. area: 'Email::Account',
  189. group: 'Users',
  190. options: {
  191. inbound: {
  192. adapter: 'imap',
  193. options: {
  194. host: 'mx1.example.com',
  195. user: 'not_existing',
  196. password: 'some_pass',
  197. ssl: 'ssl'
  198. }
  199. },
  200. outbound: {
  201. adapter: 'sendmail'
  202. }
  203. },
  204. preferences: {
  205. online_service_disable: true,
  206. },
  207. active: true
  208. }
  209. ],
  210. }
  211. end
  212. it 'creates a new channel with the given attributes' do
  213. expect { described_class.setup }
  214. .to change(Channel, :count)
  215. .and change { Channel.last&.group }.to(Group.find_by(name: 'Users'))
  216. .and change { Channel.last&.area }.to('Email::Account')
  217. end
  218. end
  219. context 'when "auto_wizard.json" contains a set of Channel attributes, including an existing ID' do
  220. let(:seed_data) do
  221. {
  222. Channels: [
  223. {
  224. id: channel.id,
  225. area: 'Email::Account',
  226. group: new_group.name,
  227. options: {
  228. inbound: {
  229. adapter: 'imap',
  230. options: {
  231. host: 'mx1.example.com',
  232. user: 'not_existing',
  233. password: 'some_pass',
  234. ssl: 'ssl'
  235. }
  236. },
  237. outbound: {
  238. adapter: 'sendmail'
  239. }
  240. },
  241. preferences: {
  242. online_service_disable: true,
  243. },
  244. active: true
  245. }
  246. ],
  247. }
  248. end
  249. let(:channel) { create(:twitter_channel) }
  250. let(:new_group) { create(:group) }
  251. it 'updates the specified channel with the given attributes' do
  252. expect { described_class.setup }
  253. .to not_change(Channel, :count)
  254. .and change { channel.reload.group }.to(new_group)
  255. .and change { channel.reload.area }.to('Email::Account')
  256. end
  257. end
  258. context 'when "auto_wizard.json" contains a set of existing permission names and active-statuses' do
  259. let(:seed_data) do
  260. {
  261. Permissions: [
  262. {
  263. name: 'admin.session',
  264. active: false,
  265. },
  266. ],
  267. }
  268. end
  269. it 'sets the specified permissions to the given active-statuses' do
  270. expect { described_class.setup }
  271. .to not_change(Permission, :count)
  272. .and change { Permission.find_by(name: 'admin.session').active }.to(false)
  273. end
  274. end
  275. context 'when "auto_wizard.json" contains a set of new permission names and active-statuses' do
  276. let(:seed_data) do
  277. {
  278. Permissions: [
  279. {
  280. name: 'admin.session.new',
  281. active: false,
  282. },
  283. ],
  284. }
  285. end
  286. it 'creates a new permission with the given active-status' do
  287. expect { described_class.setup }
  288. .to change(Permission, :count).by(1)
  289. .and change { Permission.last.name }.to('admin.session.new')
  290. .and change { Permission.last.active }.to(false)
  291. end
  292. end
  293. context 'when "auto_wizard.json" contains sets of existing Setting names and values' do
  294. let(:seed_data) do
  295. {
  296. Settings: [
  297. {
  298. name: 'developer_mode',
  299. value: true
  300. },
  301. {
  302. name: 'product_name',
  303. value: 'Zammad UnitTest01 System'
  304. }
  305. ]
  306. }
  307. end
  308. it 'sets the specified settings to the given values' do
  309. expect { described_class.setup }
  310. .to change { Setting.get('developer_mode') }.to(true)
  311. .and change { Setting.get('product_name') }.to('Zammad UnitTest01 System')
  312. end
  313. end
  314. context 'when "auto_wizard.json" contains a TextModule locale' do
  315. let(:seed_data) do
  316. {
  317. TextModuleLocale: {
  318. Locale: 'de-de'
  319. }
  320. }
  321. end
  322. it 'creates a full set of text modules for the specified locale' do
  323. expect { described_class.setup }
  324. .to change(TextModule, :count)
  325. end
  326. end
  327. context 'when "auto_wizard.json" contains a Calendar IP' do
  328. let(:seed_data) do
  329. {
  330. CalendarSetup: {
  331. Ip: '195.65.29.254',
  332. },
  333. }
  334. end
  335. it 'updates the existing calendar with the specified IP' do
  336. expect { described_class.setup }
  337. .to not_change(Calendar, :count)
  338. .and change { Calendar.last.name }.to('Switzerland')
  339. .and change { Calendar.last.timezone }.to('Europe/Zurich')
  340. end
  341. end
  342. end
  343. end