full_quote_header_spec.rb 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. require 'rails_helper'
  2. RSpec.describe 'Ticket > Update > Full Quote Header', type: :system, time_zone: 'Europe/London' do
  3. let(:group) { Group.find_by(name: 'Users') }
  4. let(:ticket) { create(:ticket, group: group) }
  5. let(:ticket_article) { create(:ticket_article, ticket: ticket, from: 'Example Name <asdf1@example.com>') }
  6. let(:customer) { create(:customer) }
  7. prepend_before do
  8. Setting.set 'ui_ticket_zoom_article_email_full_quote_header', full_quote_header_setting
  9. end
  10. before do
  11. UserInfo.current_user_id = customer.id
  12. visit "ticket/zoom/#{ticket_article.ticket.id}"
  13. end
  14. context 'when "ui_ticket_zoom_article_email_full_quote_header" is enabled' do
  15. let(:full_quote_header_setting) { true }
  16. it 'includes OP when forwarding' do
  17. within(:active_content) do
  18. click_forward
  19. within(:richtext) do
  20. expect(page).to contain_full_quote(ticket_article).formatted_for(:forward)
  21. end
  22. end
  23. end
  24. it 'includes OP when replying' do
  25. within(:active_content) do
  26. highlight_and_click_reply
  27. within(:richtext) do
  28. expect(page).to contain_full_quote(ticket_article).formatted_for(:reply)
  29. end
  30. end
  31. end
  32. context 'when customer is agent' do
  33. let(:customer) { create(:agent) }
  34. it 'includes OP without email when forwarding' do
  35. within(:active_content) do
  36. click_forward
  37. within(:richtext) do
  38. expect(page).to contain_full_quote(ticket_article).formatted_for(:forward).ensuring_privacy(true)
  39. end
  40. end
  41. end
  42. end
  43. end
  44. context 'when "ui_ticket_zoom_article_email_full_quote_header" is disabled' do
  45. let(:full_quote_header_setting) { false }
  46. it 'does not include OP when forwarding' do
  47. within(:active_content) do
  48. click_forward
  49. within(:richtext) do
  50. expect(page).not_to contain_full_quote(ticket_article).formatted_for(:forward)
  51. end
  52. end
  53. end
  54. it 'does not include OP when replying' do
  55. within(:active_content) do
  56. highlight_and_click_reply
  57. within(:richtext) do
  58. expect(page).not_to contain_full_quote(ticket_article).formatted_for(:reply)
  59. end
  60. end
  61. end
  62. end
  63. def click_forward
  64. click '.js-ArticleAction[data-type=emailForward]'
  65. end
  66. def highlight_and_click_reply
  67. find('.ticket-article-item .textBubble')
  68. .execute_script <<~JAVASCRIPT
  69. window.getSelection().removeAllRanges()
  70. var range = window.document.createRange()
  71. range.setStart(this, 0)
  72. range.setEnd(this.nextSibling, 0)
  73. window.getSelection().addRange(range)
  74. JAVASCRIPT
  75. click '.js-ArticleAction[data-type=emailReply]'
  76. end
  77. define :contain_full_quote do
  78. match do
  79. confirm_content && confirm_style
  80. end
  81. match_when_negated do
  82. confirm_no_content
  83. end
  84. # sets expected quote format
  85. # @param [Symbol] :forward or :reply, defaults to :reply if not set
  86. chain :formatted_for do |style|
  87. @style = style
  88. end
  89. def style
  90. @style || :reply # rubocop:disable RSpec/InstanceVariable
  91. end
  92. # sets expected privacy level
  93. # @param [Boolean] defaults to false if not set
  94. chain :ensuring_privacy do |flag|
  95. @ensuring_privacy = flag
  96. end
  97. def ensure_privacy?
  98. @ensuring_privacy || false # rubocop:disable RSpec/InstanceVariable
  99. end
  100. def confirm_content
  101. case style
  102. when :reply
  103. confirm_content_reply
  104. when :forward
  105. confirm_content_forward
  106. end
  107. end
  108. def confirm_content_reply
  109. citation.has_text?(name) && citation.has_no_text?(email) && citation.has_text?(timestamp_reply)
  110. end
  111. def confirm_content_forward
  112. if ensure_privacy?
  113. citation.has_text?(name) && citation.has_no_text?(email) && citation.has_text?(timestamp_forward)
  114. else
  115. citation.has_text?(name) && citation.has_text?(email) && citation.has_text?(timestamp_forward)
  116. end
  117. end
  118. def confirm_no_content
  119. citation.has_no_text?(name) && citation.has_no_text?(email) && citation.has_no_text?(timestamp_reply) && citation.has_no_text?(timestamp_forward)
  120. end
  121. def confirm_style
  122. case style
  123. when :forward
  124. citation.text.match?(/Subject(.+)\nDate(.+)/)
  125. when :reply
  126. citation.text.match?(/^On(.+)wrote:$/)
  127. end
  128. end
  129. def citation
  130. actual.first('blockquote[type=cite]')
  131. end
  132. def name
  133. expected.created_by.fullname
  134. end
  135. def email
  136. expected.created_by.email
  137. end
  138. def timestamp_reply
  139. expected
  140. .created_at
  141. .in_time_zone('Europe/London')
  142. .strftime('%A, %B %1d, %Y, %1I:%M:%S %p')
  143. end
  144. def timestamp_forward
  145. expected
  146. .created_at
  147. .in_time_zone('Europe/London')
  148. .strftime('%m/%d/%Y %H:%M')
  149. end
  150. end
  151. end