activity_stream_test.rb 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399
  1. # encoding: utf-8
  2. require 'test_helper'
  3. class ActivityStreamTest < ActiveSupport::TestCase
  4. admin_user = nil
  5. current_user = nil
  6. test 'aaa - setup' do
  7. role = Role.lookup(name: 'Admin')
  8. group = Group.lookup(name: 'Users')
  9. admin_user = User.create_or_update(
  10. login: 'admin',
  11. firstname: 'Bob',
  12. lastname: 'Smith',
  13. email: 'bob@example.com',
  14. password: 'some_pass',
  15. active: true,
  16. role_ids: [role.id],
  17. group_ids: [group.id],
  18. updated_by_id: 1,
  19. created_by_id: 1
  20. )
  21. current_user = User.lookup(email: 'nicole.braun@zammad.org')
  22. end
  23. test 'ticket+user' do
  24. tests = [
  25. # test 1
  26. {
  27. create: {
  28. ticket: {
  29. group_id: Group.lookup(name: 'Users').id,
  30. customer_id: current_user.id,
  31. owner_id: User.lookup(login: '-').id,
  32. title: 'Unit Test 1 (äöüß)!',
  33. state_id: Ticket::State.lookup(name: 'new').id,
  34. priority_id: Ticket::Priority.lookup(name: '2 normal').id,
  35. updated_by_id: current_user.id,
  36. created_by_id: current_user.id,
  37. },
  38. article: {
  39. updated_by_id: current_user.id,
  40. created_by_id: current_user.id,
  41. type_id: Ticket::Article::Type.lookup(name: 'phone').id,
  42. sender_id: Ticket::Article::Sender.lookup(name: 'Customer').id,
  43. from: 'Unit Test <unittest@example.com>',
  44. body: 'Unit Test 123',
  45. internal: false,
  46. },
  47. },
  48. update: {
  49. ticket: {
  50. title: 'Unit Test 1 (äöüß) - update!',
  51. state_id: Ticket::State.lookup(name: 'open').id,
  52. priority_id: Ticket::Priority.lookup(name: '1 low').id,
  53. },
  54. },
  55. update2: {
  56. ticket: {
  57. title: 'Unit Test 2 (äöüß) - update!',
  58. priority_id: Ticket::Priority.lookup(name: '2 normal').id,
  59. },
  60. },
  61. check: [
  62. {
  63. result: true,
  64. object: 'Ticket',
  65. type: 'update',
  66. },
  67. {
  68. result: true,
  69. object: 'Ticket::Article',
  70. type: 'create',
  71. },
  72. {
  73. result: true,
  74. object: 'Ticket',
  75. type: 'create',
  76. },
  77. {
  78. result: false,
  79. object: 'User',
  80. type: 'update',
  81. o_id: current_user.id,
  82. },
  83. ]
  84. },
  85. ]
  86. tickets = []
  87. tests.each { |test|
  88. ticket = Ticket.create(test[:create][:ticket])
  89. test[:check][0][:o_id] = ticket.id
  90. test[:check][2][:o_id] = ticket.id
  91. test[:check][2][:created_at] = ticket.created_at
  92. test[:check][2][:created_by_id] = current_user.id
  93. travel 2.seconds
  94. test[:create][:article][:ticket_id] = ticket.id
  95. article = Ticket::Article.create(test[:create][:article])
  96. test[:check][1][:o_id] = article.id
  97. test[:check][1][:created_at] = article.created_at
  98. test[:check][1][:created_by_id] = current_user.id
  99. assert_equal(ticket.class.to_s, 'Ticket')
  100. assert_equal(article.class.to_s, 'Ticket::Article')
  101. # update ticket
  102. if test[:update][:ticket]
  103. ticket.update_attributes(test[:update][:ticket])
  104. # check updated user
  105. test[:check][3][:o_id] = current_user.id
  106. test[:check][3][:created_at] = ticket.created_at
  107. test[:check][3][:created_by_id] = current_user.id
  108. end
  109. if test[:update2][:ticket]
  110. ticket = Ticket.find(ticket.id)
  111. ticket.update_attributes(test[:update2][:ticket])
  112. end
  113. if test[:update][:article]
  114. article.update_attributes(test[:update][:article])
  115. end
  116. travel 1.second
  117. if test[:update][:ticket]
  118. ticket.update_attributes(test[:update][:ticket])
  119. end
  120. if test[:update2][:ticket]
  121. ticket.update_attributes(test[:update2][:ticket])
  122. end
  123. # remember ticket
  124. tickets.push ticket
  125. # check activity_stream
  126. activity_stream_check(admin_user.activity_stream(3), test[:check])
  127. }
  128. # delete tickets
  129. tickets.each { |ticket|
  130. ticket_id = ticket.id
  131. ticket.destroy
  132. found = Ticket.where(id: ticket_id).first
  133. assert_not(found, 'Ticket destroyed')
  134. }
  135. end
  136. test 'organization' do
  137. tests = [
  138. # test 1
  139. {
  140. create: {
  141. organization: {
  142. name: 'some name',
  143. updated_by_id: current_user.id,
  144. created_by_id: current_user.id,
  145. },
  146. },
  147. update1: {
  148. organization: {
  149. name: 'some name (äöüß)',
  150. },
  151. },
  152. update2: {
  153. organization: {
  154. name: 'some name 2 (äöüß)',
  155. },
  156. },
  157. check: [
  158. {
  159. result: true,
  160. object: 'Organization',
  161. type: 'update',
  162. },
  163. {
  164. result: true,
  165. object: 'Organization',
  166. type: 'create',
  167. },
  168. ]
  169. },
  170. ]
  171. organizations = []
  172. tests.each { |test|
  173. organization = Organization.create(test[:create][:organization])
  174. test[:check][0][:o_id] = organization.id
  175. test[:check][0][:created_at] = organization.created_at
  176. test[:check][0][:created_by_id] = current_user.id
  177. travel 2.seconds
  178. assert_equal(organization.class.to_s, 'Organization')
  179. if test[:update1][:organization]
  180. organization.update_attributes(test[:update1][:organization])
  181. test[:check][1][:o_id] = organization.id
  182. test[:check][1][:updated_at] = organization.updated_at
  183. test[:check][1][:created_by_id] = current_user.id
  184. travel 1.second
  185. end
  186. if test[:update2][:organization]
  187. organization.update_attributes(test[:update2][:organization])
  188. end
  189. # remember organization
  190. organizations.push organization
  191. # check activity_stream
  192. activity_stream_check(admin_user.activity_stream(2), test[:check])
  193. }
  194. # delete tickets
  195. organizations.each { |organization|
  196. organization_id = organization.id
  197. organization.destroy
  198. found = Organization.where(id: organization_id).first
  199. assert( !found, 'Organization destroyed')
  200. }
  201. end
  202. test 'user with update check false' do
  203. tests = [
  204. # test 1
  205. {
  206. create: {
  207. user: {
  208. login: 'someemail@example.com',
  209. email: 'someemail@example.com',
  210. firstname: 'Bob Smith II',
  211. updated_by_id: current_user.id,
  212. created_by_id: current_user.id,
  213. },
  214. },
  215. update1: {
  216. user: {
  217. firstname: 'Bob U',
  218. lastname: 'Smith U',
  219. },
  220. },
  221. check: [
  222. {
  223. result: true,
  224. object: 'User',
  225. type: 'create',
  226. },
  227. {
  228. result: false,
  229. object: 'User',
  230. type: 'update',
  231. },
  232. ]
  233. },
  234. ]
  235. users = []
  236. tests.each { |test|
  237. user = User.create(test[:create][:user])
  238. test[:check][0][:o_id] = user.id
  239. test[:check][0][:created_at] = user.created_at
  240. test[:check][0][:created_by_id] = current_user.id
  241. assert_equal(user.class.to_s, 'User')
  242. if test[:update1][:user]
  243. user.update_attributes(test[:update1][:user])
  244. test[:check][1][:o_id] = user.id
  245. test[:check][1][:updated_at] = user.updated_at
  246. test[:check][1][:created_by_id] = current_user.id
  247. end
  248. # remember organization
  249. users.push user
  250. # check activity_stream
  251. activity_stream_check(admin_user.activity_stream(3), test[:check])
  252. }
  253. # delete tickets
  254. users.each { |user|
  255. user_id = user.id
  256. user.destroy
  257. found = User.where( id: user_id ).first
  258. assert_not(found, 'User destroyed')
  259. }
  260. end
  261. test 'user with update check true' do
  262. tests = [
  263. # test 1
  264. {
  265. create: {
  266. user: {
  267. login: 'someemail@example.com',
  268. email: 'someemail@example.com',
  269. firstname: 'Bob Smith II',
  270. updated_by_id: current_user.id,
  271. created_by_id: current_user.id,
  272. },
  273. },
  274. update1: {
  275. user: {
  276. firstname: 'Bob U',
  277. lastname: 'Smith U',
  278. },
  279. },
  280. update2: {
  281. user: {
  282. firstname: 'Bob',
  283. lastname: 'Smith',
  284. },
  285. },
  286. check: [
  287. {
  288. result: true,
  289. object: 'User',
  290. type: 'update',
  291. },
  292. {
  293. result: true,
  294. object: 'User',
  295. type: 'create',
  296. },
  297. ]
  298. },
  299. ]
  300. users = []
  301. tests.each { |test|
  302. user = User.create(test[:create][:user])
  303. test[:check][0][:o_id] = user.id
  304. test[:check][0][:created_at] = user.created_at
  305. test[:check][0][:created_by_id] = current_user.id
  306. assert_equal(user.class.to_s, 'User')
  307. if test[:update1][:user]
  308. user.update_attributes(test[:update1][:user])
  309. test[:check][1][:o_id] = user.id
  310. test[:check][1][:updated_at] = user.updated_at
  311. test[:check][1][:created_by_id] = current_user.id
  312. end
  313. # to verify update which need to be logged
  314. travel 1.second
  315. if test[:update2][:user]
  316. user.update_attributes(test[:update2][:user])
  317. end
  318. # remember organization
  319. users.push user
  320. # check activity_stream
  321. activity_stream_check(admin_user.activity_stream(2), test[:check])
  322. }
  323. # delete tickets
  324. users.each { |user|
  325. user_id = user.id
  326. user.destroy
  327. found = User.where(id: user_id).first
  328. assert(!found, 'User destroyed')
  329. }
  330. end
  331. def activity_stream_check(activity_stream_list, checks)
  332. #activity_stream_list = activity_stream_list.reverse
  333. #puts 'AS ' + activity_stream_list.inspect
  334. check_count = 0
  335. checks.each { |check_item|
  336. check_count += 1
  337. #puts '+++++++++++'
  338. #puts check_item.inspect
  339. check_list = 0
  340. activity_stream_list.each { |item|
  341. check_list += 1
  342. next if check_list != check_count
  343. #next if match
  344. #puts '--------'
  345. #puts item.inspect
  346. #puts check_item.inspect
  347. if check_item[:result]
  348. assert_equal(check_item[:object], item['object'])
  349. assert_equal(check_item[:type], item['type'])
  350. assert_equal(check_item[:o_id], item['o_id'])
  351. elsif check_item[:object] == item['object'] && check_item[:type] == item['type'] && check_item[:o_id] == item['o_id']
  352. assert(false, "entry should not exist #{item['object']}/#{item['type']}/#{item['o_id']}")
  353. end
  354. }
  355. }
  356. end
  357. end