form_spec.rb 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. # Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. require 'rails_helper'
  3. RSpec.describe 'Form', authenticated_as: true, type: :system do
  4. shared_examples 'validating form fields' do
  5. it 'validate name input' do
  6. within form_context do
  7. fill_in 'Email', with: 'discard@discard.zammad.org'
  8. fill_in 'Message', with: 'message here'
  9. click_on 'Submit'
  10. expect(page).to have_validation_message_for(name_input)
  11. end
  12. end
  13. it 'validate email input' do
  14. within form_context do
  15. fill_in 'Name', with: 'some sender'
  16. fill_in 'Message', with: 'message here'
  17. click_on 'Submit'
  18. expect(page).to have_validation_message_for(email_input)
  19. end
  20. end
  21. it 'validate message input' do
  22. within form_context do
  23. fill_in 'Name', with: 'some sender'
  24. fill_in 'Email', with: 'discard@discard.zammad.org'
  25. click_on 'Submit'
  26. expect(page).to have_validation_message_for(body_input)
  27. end
  28. end
  29. it 'validate email format' do
  30. within form_context do
  31. fill_in 'Name', with: 'some sender'
  32. fill_in 'Email', with: 'invalidformat'
  33. click_on 'Submit'
  34. expect(page).to have_validation_message_for(email_input)
  35. end
  36. end
  37. it 'validate email field with non existing domain space' do
  38. within form_context do
  39. fill_in 'Name', with: 'some sender'
  40. fill_in 'Message', with: 'message here'
  41. fill_in 'Email', with: 'somebody@notexistinginanydomainspacealsonothere.nowhere'
  42. # We need to wait 10 seconds, because otherwise we are detected as a robot.
  43. sleep 10
  44. click_on 'Submit'
  45. expect(page).to have_css('.has-error [name=email]').and have_no_button(type: 'submit', disabled: true)
  46. end
  47. end
  48. end
  49. shared_examples 'submitting valid form fields' do
  50. it 'submits form filled slowly succesfully' do
  51. within form_context do
  52. fill_in 'Name', with: 'some sender'
  53. fill_in 'Message', with: 'message here'
  54. fill_in 'Email', with: 'discard@discard.zammad.org'
  55. sleep 10
  56. click_on 'Submit'
  57. expect(page).to have_text('Thank you for your inquiry')
  58. end
  59. end
  60. it 'fails to submit form filled too fast' do
  61. within form_context do
  62. fill_in 'Name', with: 'some sender'
  63. fill_in 'Message', with: 'message here'
  64. fill_in 'Email', with: 'discard@discard.zammad.org'
  65. click_on 'Submit'
  66. accept_alert('Sorry, you look like a robot!')
  67. end
  68. end
  69. end
  70. shared_examples 'submitting fails due to throttling' do
  71. it 'rejects form submission due to throttling' do
  72. within form_context do
  73. fill_in 'Name', with: 'some sender'
  74. fill_in 'Message', with: 'message here'
  75. fill_in 'Email', with: 'discard@discard.zammad.org'
  76. sleep 10
  77. # Avoid await_empty_ajax_queue.
  78. execute_script('$("button:submit").trigger("click")')
  79. accept_alert('The form could not be submitted!')
  80. end
  81. end
  82. end
  83. context 'when settings are changed' do
  84. let(:path) { 'channels/form' }
  85. let(:group) { create(:group) }
  86. before do
  87. group
  88. visit path
  89. end
  90. it 'stores settings correctly' do
  91. set_tree_select_value('group_id', group.name)
  92. check_input_field_value('group_id', group.id.to_s, visible: :all)
  93. wait.until { Setting.get('form_ticket_create_group_id') == group.id.to_s }
  94. end
  95. end
  96. context 'with in-app form' do
  97. let(:path) { 'channels/form' }
  98. let(:feedback_modal_button) { '.js-formBtn' }
  99. context 'when form is inline' do
  100. let(:form_context) { '.js-formInline form.zammad-form' }
  101. let(:name_input) { '#zammad-form-name-inline' }
  102. let(:body_input) { '#zammad-form-body-inline' }
  103. let(:email_input) { '#zammad-form-email-inline' }
  104. before do
  105. visit path
  106. uncheck 'Start modal dialog for form.', allow_label_click: true
  107. end
  108. it_behaves_like 'validating form fields'
  109. end
  110. context 'when form is modal' do
  111. let(:form_context) { '.js-zammad-form-modal-body form.zammad-form' }
  112. let(:name_input) { '#zammad-form-name-modal' }
  113. let(:body_input) { '#zammad-form-body-modal' }
  114. let(:email_input) { '#zammad-form-email-modal' }
  115. before do
  116. visit path
  117. find(feedback_modal_button).click
  118. end
  119. it_behaves_like 'validating form fields'
  120. end
  121. it 'shows an inline form' do
  122. visit path
  123. uncheck 'Start modal dialog for form.', allow_label_click: true
  124. expect(page).to have_css('.js-formInline').and have_no_selector('.js-formInline.hide')
  125. end
  126. end
  127. context 'with external form' do
  128. let(:path) { '/assets/form/form.html' }
  129. let(:feedback_modal_button) { '#feedback-form-modal' }
  130. let(:form_inline_selector) { '#feedback-form-inline form.zammad-form' }
  131. context 'when feature is enabled' do
  132. before do
  133. visit 'channels/form'
  134. check 'form_ticket_create', allow_label_click: true
  135. wait.until { Setting.get('form_ticket_create') == true }
  136. end
  137. context 'when form is inline' do
  138. let(:form_context) { form_inline_selector }
  139. let(:name_input) { '#zammad-form-name-inline' }
  140. let(:body_input) { '#zammad-form-body-inline' }
  141. let(:email_input) { '#zammad-form-email-inline' }
  142. before { visit path }
  143. it_behaves_like 'validating form fields'
  144. it_behaves_like 'submitting valid form fields'
  145. end
  146. context 'when form is modal' do
  147. let(:form_context) { '.js-zammad-form-modal-body form.zammad-form' }
  148. let(:name_input) { '#zammad-form-name-modal' }
  149. let(:body_input) { '#zammad-form-body-modal' }
  150. let(:email_input) { '#zammad-form-email-modal' }
  151. before do
  152. visit path
  153. find(feedback_modal_button).click
  154. end
  155. it_behaves_like 'validating form fields'
  156. it_behaves_like 'submitting valid form fields'
  157. end
  158. context 'when form is throttled with :too_many_requests' do
  159. before do
  160. Setting.set('form_ticket_create_by_ip_per_hour', 0)
  161. visit path
  162. end
  163. let(:form_context) { form_inline_selector }
  164. let(:name_input) { '#zammad-form-name-inline' }
  165. let(:body_input) { '#zammad-form-body-inline' }
  166. let(:email_input) { '#zammad-form-email-inline' }
  167. it_behaves_like 'submitting fails due to throttling'
  168. end
  169. end
  170. context 'when feature is disabled' do
  171. before do
  172. visit 'channels/form'
  173. uncheck 'form_ticket_create', allow_label_click: true
  174. wait.until { Setting.get('form_ticket_create') == false }
  175. visit path
  176. end
  177. it 'fails to load form' do
  178. expect(page).to have_text('Faild to load form config, feature is disabled')
  179. end
  180. end
  181. end
  182. end