auto_wizard_spec.rb 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377
  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 { Auth.new(User.last.email, 'test').valid? }.from(false)
  77. end
  78. end
  79. context 'when "auto_wizard.json" contains a set of User attributes without associations' do
  80. let(:seed_data) do
  81. {
  82. Users: [
  83. {
  84. login: 'master_unit_test01@example.com',
  85. firstname: 'Test Admin',
  86. lastname: 'Agent',
  87. email: 'master_unit_test01@example.com',
  88. password: 'test'
  89. }
  90. ]
  91. }
  92. end
  93. it 'creates a user with those attributes and Admin + Agent roles' do
  94. expect { described_class.setup }
  95. .to change(User, :count).by(1)
  96. .and change { User.last.roles }.to(Role.where(name: %w[Admin Agent]))
  97. .and change { User.last.login }.to('master_unit_test01@example.com')
  98. .and change { User.last.firstname }.to('Test Admin')
  99. .and change { User.last.lastname }.to('Agent')
  100. .and change { User.last.email }.to('master_unit_test01@example.com')
  101. .and change { Auth.new(User.last.email, 'test').valid? }.from(false)
  102. end
  103. end
  104. context 'when "auto_wizard.json" contains a set of Group attributes and associations (User emails, Signature name, & EmailAddress id)' do
  105. let(:seed_data) do
  106. {
  107. Groups: [
  108. {
  109. name: 'some group1',
  110. note: 'Lorem ipsum dolor',
  111. users: [group_agent.email],
  112. signature: group_signature.name,
  113. email_address_id: group_email.id,
  114. }
  115. ]
  116. }
  117. end
  118. let(:group_agent) { create(:agent) }
  119. let(:group_signature) { create(:signature) }
  120. let(:group_email) { create(:email_address) }
  121. it 'creates a group with those attributes and associations' do
  122. expect { described_class.setup }
  123. .to change(Group, :count).by(1)
  124. .and change { Group.last.name }.to('some group1')
  125. .and change { Group.last.note }.to('Lorem ipsum dolor')
  126. .and change { Group.last.users }.to([group_agent])
  127. .and change { Group.last.signature }.to(group_signature)
  128. end
  129. end
  130. context 'when "auto_wizard.json" contains a set of EmailAddress attributes' do
  131. let(:seed_data) do
  132. {
  133. EmailAddresses: [
  134. {
  135. channel_id: channel.id,
  136. name: 'John Doe',
  137. email: 'johndoe@example.com',
  138. }
  139. ],
  140. }
  141. end
  142. let(:channel) { create(:email_channel) }
  143. it 'creates an email address with the given attributes' do
  144. expect { described_class.setup }
  145. .to change(EmailAddress, :count)
  146. .and change { EmailAddress.last&.name }.to('John Doe')
  147. .and change { EmailAddress.last&.email }.to('johndoe@example.com')
  148. .and change { EmailAddress.last&.channel }.to(channel)
  149. end
  150. end
  151. context 'when "auto_wizard.json" contains a set of EmailAddress attributes, including an existing ID' do
  152. let(:seed_data) do
  153. {
  154. EmailAddresses: [
  155. {
  156. id: email_address.id,
  157. channel_id: new_channel.id,
  158. name: 'John Doe',
  159. email: 'johndoe@example.com',
  160. }
  161. ],
  162. }
  163. end
  164. let(:email_address) { create(:email_address) }
  165. let(:new_channel) { create(:email_channel) }
  166. it 'updates the specified email address with the given attributes' do
  167. expect { described_class.setup }
  168. .to not_change(EmailAddress, :count)
  169. .and change { email_address.reload.name }.to('John Doe')
  170. .and change { email_address.reload.email }.to('johndoe@example.com')
  171. .and change { email_address.reload.channel }.to(new_channel)
  172. end
  173. end
  174. context 'when "auto_wizard.json" contains a set of Channel attributes' do
  175. let(:seed_data) do
  176. {
  177. Channels: [
  178. {
  179. id: 100,
  180. area: 'Email::Account',
  181. group: 'Users',
  182. options: {
  183. inbound: {
  184. adapter: 'imap',
  185. options: {
  186. host: 'mx1.example.com',
  187. user: 'not_existing',
  188. password: 'some_pass',
  189. ssl: 'ssl'
  190. }
  191. },
  192. outbound: {
  193. adapter: 'sendmail'
  194. }
  195. },
  196. preferences: {
  197. online_service_disable: true,
  198. },
  199. active: true
  200. }
  201. ],
  202. }
  203. end
  204. it 'creates a new channel with the given attributes' do
  205. expect { described_class.setup }
  206. .to change(Channel, :count)
  207. .and change { Channel.last&.group }.to(Group.find_by(name: 'Users'))
  208. .and change { Channel.last&.area }.to('Email::Account')
  209. end
  210. end
  211. context 'when "auto_wizard.json" contains a set of Channel attributes, including an existing ID' do
  212. let(:seed_data) do
  213. {
  214. Channels: [
  215. {
  216. id: channel.id,
  217. area: 'Email::Account',
  218. group: new_group.name,
  219. options: {
  220. inbound: {
  221. adapter: 'imap',
  222. options: {
  223. host: 'mx1.example.com',
  224. user: 'not_existing',
  225. password: 'some_pass',
  226. ssl: 'ssl'
  227. }
  228. },
  229. outbound: {
  230. adapter: 'sendmail'
  231. }
  232. },
  233. preferences: {
  234. online_service_disable: true,
  235. },
  236. active: true
  237. }
  238. ],
  239. }
  240. end
  241. let(:channel) { create(:twitter_channel) }
  242. let(:new_group) { create(:group) }
  243. it 'updates the specified channel with the given attributes' do
  244. expect { described_class.setup }
  245. .to not_change(Channel, :count)
  246. .and change { channel.reload.group }.to(new_group)
  247. .and change { channel.reload.area }.to('Email::Account')
  248. end
  249. end
  250. context 'when "auto_wizard.json" contains a set of existing permission names and active-statuses' do
  251. let(:seed_data) do
  252. {
  253. Permissions: [
  254. {
  255. name: 'admin.session',
  256. active: false,
  257. },
  258. ],
  259. }
  260. end
  261. it 'sets the specified permissions to the given active-statuses' do
  262. expect { described_class.setup }
  263. .to not_change(Permission, :count)
  264. .and change { Permission.find_by(name: 'admin.session').active }.to(false)
  265. end
  266. end
  267. context 'when "auto_wizard.json" contains a set of new permission names and active-statuses' do
  268. let(:seed_data) do
  269. {
  270. Permissions: [
  271. {
  272. name: 'admin.session.new',
  273. active: false,
  274. },
  275. ],
  276. }
  277. end
  278. it 'creates a new permission with the given active-status' do
  279. expect { described_class.setup }
  280. .to change(Permission, :count).by(1)
  281. .and change { Permission.last.name }.to('admin.session.new')
  282. .and change { Permission.last.active }.to(false)
  283. end
  284. end
  285. context 'when "auto_wizard.json" contains sets of existing Setting names and values' do
  286. let(:seed_data) do
  287. {
  288. Settings: [
  289. {
  290. name: 'developer_mode',
  291. value: true
  292. },
  293. {
  294. name: 'product_name',
  295. value: 'Zammad UnitTest01 System'
  296. }
  297. ]
  298. }
  299. end
  300. it 'sets the specified settings to the given values' do
  301. expect { described_class.setup }
  302. .to change { Setting.get('developer_mode') }.to(true)
  303. .and change { Setting.get('product_name') }.to('Zammad UnitTest01 System')
  304. end
  305. end
  306. context 'when "auto_wizard.json" contains a TextModule locale' do
  307. let(:seed_data) do
  308. {
  309. TextModuleLocale: {
  310. Locale: 'de-de'
  311. }
  312. }
  313. end
  314. it 'creates a full set of text modules for the specified locale' do
  315. expect { described_class.setup }
  316. .to change(TextModule, :count)
  317. end
  318. end
  319. context 'when "auto_wizard.json" contains a Calendar IP' do
  320. let(:seed_data) do
  321. {
  322. CalendarSetup: {
  323. Ip: '195.65.29.254',
  324. },
  325. }
  326. end
  327. it 'updates the existing calendar with the specified IP' do
  328. expect { described_class.setup }
  329. .to not_change(Calendar, :count)
  330. .and change { Calendar.last.name }.to('Switzerland')
  331. .and change { Calendar.last.timezone }.to('Europe/Zurich')
  332. end
  333. end
  334. end
  335. end