auto_wizard_spec.rb 11 KB

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