auto_wizard_spec.rb 11 KB

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