auto_wizard_test.rb 7.1 KB

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