ticket_test.rb 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. # encoding: utf-8
  2. require 'test_helper'
  3. class TicketTest < ActiveSupport::TestCase
  4. test 'ticket create' do
  5. ticket = Ticket.create(
  6. title: "some title\n äöüß",
  7. group: Group.lookup( name: 'Users'),
  8. customer_id: 2,
  9. state: Ticket::State.lookup( name: 'new' ),
  10. priority: Ticket::Priority.lookup( name: '2 normal' ),
  11. updated_by_id: 1,
  12. created_by_id: 1,
  13. )
  14. assert( ticket, 'ticket created' )
  15. assert_equal( ticket.title, 'some title äöüß', 'ticket.title verify' )
  16. assert_equal( ticket.group.name, 'Users', 'ticket.group verify' )
  17. assert_equal( ticket.state.name, 'new', 'ticket.state verify' )
  18. # create inbound article
  19. article_inbound = Ticket::Article.create(
  20. ticket_id: ticket.id,
  21. from: 'some_sender@example.com',
  22. to: 'some_recipient@example.com',
  23. subject: 'some subject',
  24. message_id: 'some@id',
  25. body: 'some message article_inbound 😍😍😍',
  26. internal: false,
  27. sender: Ticket::Article::Sender.where(name: 'Customer').first,
  28. type: Ticket::Article::Type.where(name: 'email').first,
  29. updated_by_id: 1,
  30. created_by_id: 1,
  31. )
  32. assert_equal( article_inbound.body, 'some message article_inbound 😍😍😍'.utf8_to_3bytesutf8, 'article_inbound.body verify - inbound' )
  33. ticket = Ticket.find(ticket.id)
  34. assert_equal( ticket.article_count, 1, 'ticket.article_count verify - inbound' )
  35. assert_equal( ticket.last_contact.to_s, article_inbound.created_at.to_s, 'ticket.last_contact verify - inbound' )
  36. assert_equal( ticket.last_contact_customer.to_s, article_inbound.created_at.to_s, 'ticket.last_contact_customer verify - inbound' )
  37. assert_equal( ticket.last_contact_agent, nil, 'ticket.last_contact_agent verify - inbound' )
  38. assert_equal( ticket.first_response, nil, 'ticket.first_response verify - inbound' )
  39. assert_equal( ticket.close_time, nil, 'ticket.close_time verify - inbound' )
  40. # create note article
  41. article_note = Ticket::Article.create(
  42. ticket_id: ticket.id,
  43. from: 'some person',
  44. subject: "some\nnote",
  45. body: "some\n message",
  46. internal: true,
  47. sender: Ticket::Article::Sender.where(name: 'Agent').first,
  48. type: Ticket::Article::Type.where(name: 'note').first,
  49. updated_by_id: 1,
  50. created_by_id: 1,
  51. )
  52. assert_equal( article_note.subject, 'some note', 'article_note.subject verify - inbound' )
  53. assert_equal( article_note.body, "some\n message", 'article_note.body verify - inbound' )
  54. ticket = Ticket.find(ticket.id)
  55. assert_equal( ticket.article_count, 2, 'ticket.article_count verify - note' )
  56. assert_equal( ticket.last_contact.to_s, article_inbound.created_at.to_s, 'ticket.last_contact verify - note' )
  57. assert_equal( ticket.last_contact_customer.to_s, article_inbound.created_at.to_s, 'ticket.last_contact_customer verify - note' )
  58. assert_equal( ticket.last_contact_agent, nil, 'ticket.last_contact_agent verify - note' )
  59. assert_equal( ticket.first_response, nil, 'ticket.first_response verify - note' )
  60. assert_equal( ticket.close_time, nil, 'ticket.close_time verify - note' )
  61. # create outbound article
  62. sleep 10
  63. article_outbound = Ticket::Article.create(
  64. ticket_id: ticket.id,
  65. from: 'some_recipient@example.com',
  66. to: 'some_sender@example.com',
  67. subject: 'some subject',
  68. message_id: 'some@id2',
  69. body: 'some message 2',
  70. internal: false,
  71. sender: Ticket::Article::Sender.where(name: 'Agent').first,
  72. type: Ticket::Article::Type.where(name: 'email').first,
  73. updated_by_id: 1,
  74. created_by_id: 1,
  75. )
  76. ticket = Ticket.find(ticket.id)
  77. assert_equal( ticket.article_count, 3, 'ticket.article_count verify - outbound' )
  78. assert_equal( ticket.last_contact.to_s, article_outbound.created_at.to_s, 'ticket.last_contact verify - outbound' )
  79. assert_equal( ticket.last_contact_customer.to_s, article_inbound.created_at.to_s, 'ticket.last_contact_customer verify - outbound' )
  80. assert_equal( ticket.last_contact_agent.to_s, article_outbound.created_at.to_s, 'ticket.last_contact_agent verify - outbound' )
  81. assert_equal( ticket.first_response.to_s, article_outbound.created_at.to_s, 'ticket.first_response verify - outbound' )
  82. assert_equal( ticket.close_time, nil, 'ticket.close_time verify - outbound' )
  83. ticket.state_id = Ticket::State.where(name: 'closed').first.id
  84. ticket.save
  85. ticket = Ticket.find(ticket.id)
  86. assert_equal( ticket.article_count, 3, 'ticket.article_count verify - state update' )
  87. assert_equal( ticket.last_contact.to_s, article_outbound.created_at.to_s, 'ticket.last_contact verify - state update' )
  88. assert_equal( ticket.last_contact_customer.to_s, article_inbound.created_at.to_s, 'ticket.last_contact_customer verify - state update' )
  89. assert_equal( ticket.last_contact_agent.to_s, article_outbound.created_at.to_s, 'ticket.last_contact_agent verify - state update' )
  90. assert_equal( ticket.first_response.to_s, article_outbound.created_at.to_s, 'ticket.first_response verify - state update' )
  91. assert( ticket.close_time, 'ticket.close_time verify - state update' )
  92. # set pending time
  93. ticket.state_id = Ticket::State.where(name: 'pending reminder').first.id
  94. ticket.pending_time = Time.zone.parse('1977-10-27 22:00:00 +0000')
  95. ticket.save
  96. ticket = Ticket.find(ticket.id)
  97. assert_equal( ticket.state.name, 'pending reminder', 'state verify' )
  98. assert_equal( ticket.pending_time, Time.zone.parse('1977-10-27 22:00:00 +0000'), 'pending_time verify' )
  99. # reset pending state, should also reset pending time
  100. ticket.state_id = Ticket::State.where(name: 'closed').first.id
  101. ticket.save
  102. ticket = Ticket.find(ticket.id)
  103. assert_equal( ticket.state.name, 'closed', 'state verify' )
  104. assert_equal( ticket.pending_time, nil )
  105. delete = ticket.destroy
  106. assert( delete, 'ticket destroy' )
  107. end
  108. test 'ticket latest change' do
  109. ticket1 = Ticket.create(
  110. title: 'latest change 1',
  111. group: Group.lookup( name: 'Users'),
  112. customer_id: 2,
  113. state: Ticket::State.lookup( name: 'new' ),
  114. priority: Ticket::Priority.lookup( name: '2 normal' ),
  115. updated_by_id: 1,
  116. created_by_id: 1,
  117. )
  118. assert_equal( Ticket.latest_change.to_s, ticket1.updated_at.to_s )
  119. sleep 1
  120. ticket2 = Ticket.create(
  121. title: 'latest change 2',
  122. group: Group.lookup( name: 'Users'),
  123. customer_id: 2,
  124. state: Ticket::State.lookup( name: 'new' ),
  125. priority: Ticket::Priority.lookup( name: '2 normal' ),
  126. updated_by_id: 1,
  127. created_by_id: 1,
  128. )
  129. assert_equal( Ticket.latest_change.to_s, ticket2.updated_at.to_s )
  130. sleep 1
  131. ticket1.title = 'latest change 1 - 1'
  132. ticket1.save
  133. assert_equal( Ticket.latest_change.to_s, ticket1.updated_at.to_s )
  134. sleep 1
  135. ticket1.touch
  136. assert_equal( Ticket.latest_change.to_s, ticket1.updated_at.to_s )
  137. ticket1.destroy
  138. assert_equal( Ticket.latest_change.to_s, ticket2.updated_at.to_s )
  139. end
  140. end