form_spec.rb 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. # Copyright (C) 2012-2023 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_selector('.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 'with in-app form' do
  84. let(:path) { 'channels/form' }
  85. let(:feedback_modal_button) { '.js-formBtn' }
  86. context 'when form is inline' do
  87. let(:form_context) { '.js-formInline form.zammad-form' }
  88. let(:name_input) { '#zammad-form-name-inline' }
  89. let(:body_input) { '#zammad-form-body-inline' }
  90. let(:email_input) { '#zammad-form-email-inline' }
  91. before do
  92. visit path
  93. uncheck 'Start modal dialog for form.', allow_label_click: true
  94. end
  95. it_behaves_like 'validating form fields'
  96. end
  97. context 'when form is modal' do
  98. let(:form_context) { '.js-zammad-form-modal-body form.zammad-form' }
  99. let(:name_input) { '#zammad-form-name-modal' }
  100. let(:body_input) { '#zammad-form-body-modal' }
  101. let(:email_input) { '#zammad-form-email-modal' }
  102. before do
  103. visit path
  104. find(feedback_modal_button).click
  105. end
  106. it_behaves_like 'validating form fields'
  107. end
  108. it 'shows an inline form' do
  109. visit path
  110. uncheck 'Start modal dialog for form.', allow_label_click: true
  111. expect(page).to have_selector('.js-formInline').and have_no_selector('.js-formInline.hide')
  112. end
  113. end
  114. context 'with external form' do
  115. let(:path) { '/assets/form/form.html' }
  116. let(:feedback_modal_button) { '#feedback-form-modal' }
  117. let(:form_inline_selector) { '#feedback-form-inline form.zammad-form' }
  118. context 'when feature is enabled' do
  119. before do
  120. visit 'channels/form'
  121. check 'form_ticket_create', allow_label_click: true
  122. wait.until { Setting.get('form_ticket_create') == true }
  123. end
  124. context 'when form is inline' do
  125. let(:form_context) { form_inline_selector }
  126. let(:name_input) { '#zammad-form-name-inline' }
  127. let(:body_input) { '#zammad-form-body-inline' }
  128. let(:email_input) { '#zammad-form-email-inline' }
  129. before { visit path }
  130. it_behaves_like 'validating form fields'
  131. it_behaves_like 'submitting valid form fields'
  132. end
  133. context 'when form is modal' do
  134. let(:form_context) { '.js-zammad-form-modal-body form.zammad-form' }
  135. let(:name_input) { '#zammad-form-name-modal' }
  136. let(:body_input) { '#zammad-form-body-modal' }
  137. let(:email_input) { '#zammad-form-email-modal' }
  138. before do
  139. visit path
  140. find(feedback_modal_button).click
  141. end
  142. it_behaves_like 'validating form fields'
  143. it_behaves_like 'submitting valid form fields'
  144. end
  145. context 'when form is throttled with :too_many_requests' do
  146. before do
  147. Setting.set('form_ticket_create_by_ip_per_hour', 0)
  148. visit path
  149. end
  150. let(:form_context) { form_inline_selector }
  151. let(:name_input) { '#zammad-form-name-inline' }
  152. let(:body_input) { '#zammad-form-body-inline' }
  153. let(:email_input) { '#zammad-form-email-inline' }
  154. it_behaves_like 'submitting fails due to throttling'
  155. end
  156. end
  157. context 'when feature is disabled' do
  158. before do
  159. visit 'channels/form'
  160. uncheck 'form_ticket_create', allow_label_click: true
  161. wait.until { Setting.get('form_ticket_create') == false }
  162. visit path
  163. end
  164. it 'fails to load form' do
  165. expect(page).to have_text('Faild to load form config, feature is disabled')
  166. end
  167. end
  168. end
  169. end