auto_wizard_test.rb 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. require 'test_helper'
  2. class AutoWizardTest < ActiveSupport::TestCase
  3. test 'a simple' do
  4. auto_wizard_data = {
  5. Users: [
  6. {
  7. login: 'master_unit_test01@example.com',
  8. firstname: 'Test Master',
  9. lastname: 'Agent',
  10. email: 'master_unit_test01@example.com',
  11. password: 'test',
  12. },
  13. {
  14. login: 'agent1_unit_test01@example.com',
  15. firstname: 'Agent 1',
  16. lastname: 'Test',
  17. email: 'agent1_unit_test01@example.com',
  18. password: 'test',
  19. roles: ['Agent'],
  20. }
  21. ],
  22. Groups: [
  23. {
  24. name: 'some group1',
  25. users: ['master_unit_test01@example.com', 'agent1_unit_test01@example.com']
  26. }
  27. ],
  28. Settings: [
  29. {
  30. name: 'developer_mode',
  31. value: true
  32. },
  33. {
  34. name: 'product_name',
  35. value: 'Zammad UnitTest01 System'
  36. },
  37. ]
  38. }
  39. assert_equal(false, AutoWizard.enabled?)
  40. auto_wizard_file_write(auto_wizard_data)
  41. assert_equal(true, AutoWizard.enabled?)
  42. AutoWizard.setup
  43. assert_equal(false, AutoWizard.enabled?)
  44. # check first user roles
  45. auto_wizard_data[:Users][0][:roles] = %w[Agent Admin]
  46. auto_wizard_data[:Users].each do |local_user|
  47. user = User.find_by(login: local_user[:login])
  48. assert_equal(local_user[:login], user.login)
  49. assert_equal(local_user[:firstname], user.firstname)
  50. assert_equal(local_user[:lastname], user.lastname)
  51. assert_equal(local_user[:email], user.email)
  52. assert_equal(local_user[:roles].count, user.role_ids.count)
  53. next if !local_user[:roles]
  54. local_user[:roles].each do |local_role_name|
  55. local_role = Role.find_by(name: local_role_name)
  56. assert(user.role_ids.include?(local_role.id))
  57. end
  58. end
  59. auto_wizard_data[:Groups].each do |local_group|
  60. group = Group.find_by(name: local_group[:name])
  61. assert_equal(local_group[:name], group.name)
  62. next if !local_group[:users]
  63. local_group[:users].each do |local_user_login|
  64. local_user = User.find_by(login: local_user_login)
  65. assert(group.user_ids.include?(local_user.id))
  66. end
  67. end
  68. auto_wizard_data[:Settings].each do |local_setting|
  69. setting_value = Setting.get(local_setting[:name])
  70. assert_equal(local_setting[:value], setting_value)
  71. end
  72. end
  73. test 'b complex' do
  74. auto_wizard_data = {
  75. Organizations: [
  76. {
  77. name: 'Auto Wizard Test Org',
  78. shared: false,
  79. }
  80. ],
  81. Users: [
  82. {
  83. login: 'master_unit_test01@example.com',
  84. firstname: 'Test Master',
  85. lastname: 'Agent',
  86. email: 'master_unit_test01@example.com',
  87. password: 'test',
  88. organization: 'Auto Wizard Test Org',
  89. roles: ['Admin'],
  90. },
  91. {
  92. login: 'agent1_unit_test01@example.com',
  93. firstname: 'Agent 1',
  94. lastname: 'Test',
  95. email: 'agent1_unit_test01@example.com',
  96. password: 'test',
  97. roles: ['Agent'],
  98. }
  99. ],
  100. Groups: [
  101. {
  102. name: 'some group1',
  103. users: ['master_unit_test01@example.com', 'agent1_unit_test01@example.com']
  104. },
  105. {
  106. name: 'Users',
  107. users: ['master_unit_test01@example.com', 'agent1_unit_test01@example.com'],
  108. signature: 'default',
  109. email_address_id: 1,
  110. note: 'Standard Group/Pool for Tickets.',
  111. }
  112. ],
  113. Settings: [
  114. {
  115. name: 'developer_mode',
  116. value: false,
  117. },
  118. {
  119. name: 'product_name',
  120. value: 'Zammad UnitTest02 System'
  121. },
  122. ],
  123. Permissions: [
  124. {
  125. name: 'admin.session',
  126. active: false,
  127. },
  128. {
  129. name: 'admin.session.new',
  130. active: true,
  131. },
  132. ],
  133. Channels: [
  134. {
  135. id: 1,
  136. area: 'Email::Account',
  137. group: 'Users',
  138. options: {
  139. inbound: {
  140. adapter: 'imap',
  141. options: {
  142. host: 'mx1.example.com',
  143. user: 'not_existing',
  144. password: 'some_pass',
  145. ssl: true
  146. }
  147. },
  148. outbound: {
  149. adapter: 'sendmail'
  150. }
  151. },
  152. preferences: {
  153. online_service_disable: true,
  154. },
  155. active: true
  156. }
  157. ],
  158. EmailAddresses: [
  159. {
  160. id: 1,
  161. channel_id: 1,
  162. realname: 'Zammad',
  163. email: 'zammad@localhost',
  164. }
  165. ],
  166. TextModuleLocale: {
  167. Locale: 'de-de',
  168. },
  169. CalendarSetup: {
  170. Ip: '195.65.29.254',
  171. },
  172. }
  173. assert_equal(false, AutoWizard.enabled?)
  174. auto_wizard_file_write(auto_wizard_data)
  175. assert_equal(true, AutoWizard.enabled?)
  176. AutoWizard.setup
  177. assert_equal(false, AutoWizard.enabled?)
  178. assert_not_equal(0, TextModule.count)
  179. assert_equal(1, Calendar.count)
  180. assert_equal('Switzerland', Calendar.first.name)
  181. assert_equal('Europe/Zurich', Calendar.first.timezone)
  182. auto_wizard_data[:Users].each do |local_user|
  183. user = User.find_by(login: local_user[:login])
  184. assert_equal(local_user[:login], user.login)
  185. assert_equal(local_user[:firstname], user.firstname)
  186. assert_equal(local_user[:lastname], user.lastname)
  187. assert_equal(local_user[:email], user.email)
  188. next if !local_user[:roles]
  189. assert_equal(local_user[:roles].count, user.role_ids.count)
  190. local_user[:roles].each do |local_role_name|
  191. local_role = Role.find_by(name: local_role_name)
  192. assert(user.role_ids.include?(local_role.id))
  193. end
  194. end
  195. auto_wizard_data[:Groups].each do |local_group|
  196. group = Group.find_by(name: local_group[:name])
  197. assert_equal(local_group[:name], group.name)
  198. local_group[:users]&.each do |local_user_login|
  199. local_user = User.find_by(login: local_user_login)
  200. assert(group.user_ids.include?(local_user.id))
  201. end
  202. if local_group[:signature]
  203. signature = group.signature
  204. assert_equal('default', signature.name)
  205. end
  206. end
  207. auto_wizard_data[:EmailAddresses].each do |local_email_address|
  208. email_address = EmailAddress.find_by(email: local_email_address[:email])
  209. assert_equal(local_email_address[:email], email_address.email)
  210. assert_equal(local_email_address[:realname], email_address.realname)
  211. channel = email_address.channel
  212. assert_equal(local_email_address[:channel_id], email_address.channel.id)
  213. end
  214. auto_wizard_data[:Channels].each do |local_channel|
  215. channel = Channel.find_by(id: local_channel[:id])
  216. assert_equal(local_channel[:area], channel.area)
  217. group = channel.group
  218. assert_equal(local_channel[:group], group.name)
  219. end
  220. auto_wizard_data[:Settings].each do |local_setting|
  221. setting_value = Setting.get(local_setting[:name])
  222. assert_equal(local_setting[:value], setting_value)
  223. end
  224. auto_wizard_data[:Permissions].each do |local_permission|
  225. permission = Permission.find_by(name: local_permission[:name])
  226. assert_equal(local_permission[:name], permission.name)
  227. assert_equal(local_permission[:active], permission.active)
  228. end
  229. end
  230. def auto_wizard_file_write(data)
  231. location = Rails.root.join('auto_wizard.json')
  232. file = File.new(location, 'wb')
  233. file.write(data.to_json)
  234. file.close
  235. end
  236. def auto_wizard_file_exists?
  237. location = Rails.root.join('auto_wizard.json')
  238. return false if File.exist?(location)
  239. true
  240. end
  241. end