ticket_overview_out_of_office_test.rb 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  1. require 'test_helper'
  2. class TicketOverviewOutOfOfficeTest < ActiveSupport::TestCase
  3. setup do
  4. group = Group.create_or_update(
  5. name: 'OverviewReplacementTest',
  6. updated_at: '2015-02-05 16:37:00',
  7. updated_by_id: 1,
  8. created_by_id: 1,
  9. )
  10. roles = Role.where(name: 'Agent')
  11. @agent1 = User.create_or_update(
  12. login: 'ticket-overview-agent1@example.com',
  13. firstname: 'Overview',
  14. lastname: 'Agent1',
  15. email: 'ticket-overview-agent1@example.com',
  16. password: 'agentpw',
  17. active: true,
  18. roles: roles,
  19. groups: [group],
  20. out_of_office: false,
  21. updated_at: '2015-02-05 16:37:00',
  22. updated_by_id: 1,
  23. created_by_id: 1,
  24. )
  25. @agent2 = User.create_or_update(
  26. login: 'ticket-overview-agent2@example.com',
  27. firstname: 'Overview',
  28. lastname: 'Agent2',
  29. email: 'ticket-overview-agent2@example.com',
  30. password: 'agentpw',
  31. active: true,
  32. roles: roles,
  33. groups: [group],
  34. out_of_office: false,
  35. updated_at: '2015-02-05 16:38:00',
  36. updated_by_id: 1,
  37. created_by_id: 1,
  38. )
  39. roles = Role.where(name: 'Customer')
  40. organization1 = Organization.create_or_update(
  41. name: 'Overview Org',
  42. updated_at: '2015-02-05 16:37:00',
  43. updated_by_id: 1,
  44. created_by_id: 1,
  45. )
  46. @customer1 = User.create_or_update(
  47. login: 'ticket-overview-customer1@example.com',
  48. firstname: 'Overview',
  49. lastname: 'Customer1',
  50. email: 'ticket-overview-customer1@example.com',
  51. password: 'customerpw',
  52. active: true,
  53. organization_id: organization1.id,
  54. roles: roles,
  55. out_of_office: false,
  56. updated_at: '2015-02-05 16:37:00',
  57. updated_by_id: 1,
  58. created_by_id: 1,
  59. )
  60. Overview.destroy_all
  61. UserInfo.current_user_id = 1
  62. overview_role = Role.find_by(name: 'Agent')
  63. @overview1 = Overview.create_or_update(
  64. name: 'My replacement Tickets',
  65. link: 'my_replacement',
  66. prio: 1000,
  67. role_ids: [overview_role.id],
  68. out_of_office: true,
  69. condition: {
  70. 'ticket.state_id' => {
  71. operator: 'is',
  72. value: Ticket::State.by_category(:open).pluck(:id),
  73. },
  74. 'ticket.out_of_office_replacement_id' => {
  75. operator: 'is',
  76. pre_condition: 'current_user.id',
  77. },
  78. },
  79. order: {
  80. by: 'created_at',
  81. direction: 'ASC',
  82. },
  83. view: {
  84. d: %w[title customer group created_at],
  85. s: %w[title customer group created_at],
  86. m: %w[number title customer group created_at],
  87. view_mode_default: 's',
  88. },
  89. )
  90. @overview2 = Overview.create_if_not_exists(
  91. name: 'My assigned Tickets',
  92. link: 'my_assigned',
  93. prio: 900,
  94. role_ids: [overview_role.id],
  95. condition: {
  96. 'ticket.state_id' => {
  97. operator: 'is',
  98. value: Ticket::State.by_category(:open).pluck(:id),
  99. },
  100. 'ticket.owner_id' => {
  101. operator: 'is',
  102. pre_condition: 'current_user.id',
  103. },
  104. },
  105. order: {
  106. by: 'created_at',
  107. direction: 'ASC',
  108. },
  109. view: {
  110. d: %w[title customer group created_at],
  111. s: %w[title customer group created_at],
  112. m: %w[number title customer group created_at],
  113. view_mode_default: 's',
  114. },
  115. )
  116. overview_role = Role.find_by(name: 'Customer')
  117. @overview3 = Overview.create_or_update(
  118. name: 'My Tickets',
  119. link: 'my_tickets',
  120. prio: 1100,
  121. role_ids: [overview_role.id],
  122. out_of_office: true,
  123. condition: {
  124. 'ticket.state_id' => {
  125. operator: 'is',
  126. value: [1, 2, 3, 4, 6, 7],
  127. },
  128. 'ticket.out_of_office_replacement_id' => {
  129. operator: 'is',
  130. pre_condition: 'current_user.organization_id',
  131. },
  132. },
  133. order: {
  134. by: 'created_at',
  135. direction: 'DESC',
  136. },
  137. view: {
  138. d: %w[title customer state created_at],
  139. s: %w[number title state created_at],
  140. m: %w[number title state created_at],
  141. view_mode_default: 's',
  142. },
  143. )
  144. end
  145. test 'overview index' do
  146. result = Ticket::Overviews.all(
  147. current_user: @agent1,
  148. )
  149. assert_equal(1, result.count)
  150. assert_equal('My assigned Tickets', result[0].name)
  151. result = Ticket::Overviews.all(
  152. current_user: @agent2,
  153. )
  154. assert_equal(1, result.count)
  155. assert_equal('My assigned Tickets', result[0].name)
  156. result = Ticket::Overviews.all(
  157. current_user: @customer1,
  158. )
  159. assert_equal(1, result.count)
  160. assert_equal('My Tickets', result[0].name)
  161. @agent1.out_of_office = true
  162. @agent1.out_of_office_start_at = Time.zone.now - 2.days
  163. @agent1.out_of_office_end_at = Time.zone.now + 2.days
  164. @agent1.out_of_office_replacement_id = @agent2.id
  165. @agent1.save!
  166. result = Ticket::Overviews.all(
  167. current_user: @agent1,
  168. )
  169. assert_equal(1, result.count)
  170. assert_equal('My assigned Tickets', result[0].name)
  171. result = Ticket::Overviews.all(
  172. current_user: @agent2,
  173. )
  174. assert_equal(2, result.count)
  175. assert_equal('My assigned Tickets', result[0].name)
  176. assert_equal('My replacement Tickets', result[1].name)
  177. result = Ticket::Overviews.all(
  178. current_user: @customer1,
  179. )
  180. assert_equal(1, result.count)
  181. assert_equal('My Tickets', result[0].name)
  182. end
  183. test 'overview shown' do
  184. result = Ticket::Overviews.index(@agent1)
  185. assert(result[0])
  186. assert_equal(result[0][:overview][:name], 'My assigned Tickets')
  187. assert_equal(result[0][:overview][:view], 'my_assigned')
  188. assert_equal(result[0][:count], 0)
  189. assert_equal(result[0][:tickets].class, Array)
  190. assert(result[0][:tickets].blank?)
  191. result = Ticket::Overviews.index(@agent2)
  192. assert(result[0])
  193. assert_equal(result[0][:overview][:name], 'My assigned Tickets')
  194. assert_equal(result[0][:overview][:view], 'my_assigned')
  195. assert_equal(result[0][:count], 0)
  196. assert_equal(result[0][:tickets].class, Array)
  197. assert(result[0][:tickets].blank?)
  198. result = Ticket::Overviews.index(@customer1)
  199. assert(result[0])
  200. assert_equal(result[0][:overview][:name], 'My Tickets')
  201. assert_equal(result[0][:overview][:view], 'my_tickets')
  202. assert_equal(result[0][:count], 0)
  203. assert_equal(result[0][:tickets].class, Array)
  204. assert(result[0][:tickets].blank?)
  205. @agent1.out_of_office = true
  206. @agent1.out_of_office_start_at = Time.zone.now - 2.days
  207. @agent1.out_of_office_end_at = Time.zone.now + 2.days
  208. @agent1.out_of_office_replacement_id = @agent2.id
  209. @agent1.save!
  210. assert_equal(@agent2.out_of_office_agent_of.count, 1)
  211. assert(@agent2.out_of_office_agent_of[0])
  212. assert_equal(@agent2.out_of_office_agent_of[0].id, @agent1.id)
  213. result = Ticket::Overviews.index(@agent1)
  214. assert(result[0])
  215. assert_equal(result[0][:overview][:name], 'My assigned Tickets')
  216. assert_equal(result[0][:overview][:view], 'my_assigned')
  217. assert_equal(result[0][:count], 0)
  218. assert_equal(result[0][:tickets].class, Array)
  219. assert(result[0][:tickets].blank?)
  220. result = Ticket::Overviews.index(@agent2)
  221. assert(result[0])
  222. assert_equal(result[0][:overview][:name], 'My assigned Tickets')
  223. assert_equal(result[0][:overview][:view], 'my_assigned')
  224. assert_equal(result[0][:count], 0)
  225. assert_equal(result[0][:tickets].class, Array)
  226. assert(result[0][:tickets].blank?)
  227. assert(result[1])
  228. assert_equal(result[1][:overview][:name], 'My replacement Tickets')
  229. assert_equal(result[1][:overview][:view], 'my_replacement')
  230. assert_equal(result[1][:count], 0)
  231. assert_equal(result[1][:tickets].class, Array)
  232. assert(result[1][:tickets].blank?)
  233. result = Ticket::Overviews.index(@customer1)
  234. assert(result[0])
  235. assert_equal(result[0][:overview][:name], 'My Tickets')
  236. assert_equal(result[0][:overview][:view], 'my_tickets')
  237. assert_equal(result[0][:count], 0)
  238. assert_equal(result[0][:tickets].class, Array)
  239. assert(result[0][:tickets].blank?)
  240. ticket1 = Ticket.create!(
  241. title: 'overview test 1',
  242. group: Group.lookup(name: 'OverviewReplacementTest'),
  243. customer_id: 2,
  244. owner_id: @agent1.id,
  245. state: Ticket::State.lookup(name: 'new'),
  246. priority: Ticket::Priority.lookup(name: '2 normal'),
  247. updated_by_id: 1,
  248. created_by_id: 1,
  249. )
  250. article1 = Ticket::Article.create!(
  251. ticket_id: ticket1.id,
  252. from: 'some_sender@example.com',
  253. to: 'some_recipient@example.com',
  254. subject: 'some subject',
  255. message_id: 'some@id',
  256. body: 'some message... 123',
  257. internal: false,
  258. sender: Ticket::Article::Sender.find_by(name: 'Customer'),
  259. type: Ticket::Article::Type.find_by(name: 'email'),
  260. updated_by_id: 1,
  261. created_by_id: 1,
  262. )
  263. result = Ticket::Overviews.index(@agent1)
  264. assert(result[0])
  265. assert_equal(result[0][:overview][:name], 'My assigned Tickets')
  266. assert_equal(result[0][:overview][:view], 'my_assigned')
  267. assert_equal(result[0][:count], 1)
  268. assert_equal(result[0][:tickets].class, Array)
  269. assert(result[0][:tickets])
  270. assert_equal(result[0][:tickets][0][:id], ticket1.id)
  271. result = Ticket::Overviews.index(@agent2)
  272. assert(result[0])
  273. assert_equal(result[0][:overview][:name], 'My assigned Tickets')
  274. assert_equal(result[0][:overview][:view], 'my_assigned')
  275. assert_equal(result[0][:count], 0)
  276. assert_equal(result[0][:tickets].class, Array)
  277. assert(result[0][:tickets].blank?)
  278. assert(result[1])
  279. assert_equal(result[1][:overview][:name], 'My replacement Tickets')
  280. assert_equal(result[1][:overview][:view], 'my_replacement')
  281. assert_equal(result[1][:count], 1)
  282. assert_equal(result[1][:tickets].class, Array)
  283. assert(result[1][:tickets])
  284. assert_equal(result[1][:tickets][0][:id], ticket1.id)
  285. result = Ticket::Overviews.index(@customer1)
  286. assert(result[0])
  287. assert_equal(result[0][:overview][:name], 'My Tickets')
  288. assert_equal(result[0][:overview][:view], 'my_tickets')
  289. assert_equal(result[0][:count], 0)
  290. assert_equal(result[0][:tickets].class, Array)
  291. assert(result[0][:tickets].blank?)
  292. end
  293. end