form_spec.rb 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. # Copyright (C) 2012-2022 Zammad Foundation, https://zammad-foundation.org/
  2. require 'rails_helper'
  3. RSpec.describe 'Form', type: :system, authenticated_as: true 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@znuny.com'
  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@znuny.com'
  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_selector('button[type="submit"][disabled]')
  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@znuny.com'
  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@znuny.com'
  65. click_on 'Submit'
  66. accept_alert('Sorry, you look like an robot!')
  67. end
  68. end
  69. end
  70. context 'with in-app form' do
  71. let(:path) { 'channels/form' }
  72. let(:feedback_modal_button) { '.js-formBtn' }
  73. context 'when form is inline' do
  74. let(:form_context) { '.js-formInline form.zammad-form' }
  75. let(:name_input) { '#zammad-form-name-inline' }
  76. let(:body_input) { '#zammad-form-body-inline' }
  77. let(:email_input) { '#zammad-form-email-inline' }
  78. before do
  79. visit path
  80. uncheck 'Start modal dialog for form.', { allow_label_click: true }
  81. end
  82. it_behaves_like 'validating form fields'
  83. end
  84. context 'when form is modal' do
  85. let(:form_context) { '.js-zammad-form-modal-body form.zammad-form' }
  86. let(:name_input) { '#zammad-form-name-modal' }
  87. let(:body_input) { '#zammad-form-body-modal' }
  88. let(:email_input) { '#zammad-form-email-modal' }
  89. before do
  90. visit path
  91. find(feedback_modal_button).click
  92. end
  93. it_behaves_like 'validating form fields'
  94. end
  95. it 'shows an inline form' do
  96. visit path
  97. uncheck 'Start modal dialog for form.', { allow_label_click: true }
  98. expect(page).to have_selector('.js-formInline').and have_no_selector('.js-formInline.hide')
  99. end
  100. end
  101. context 'with external form' do
  102. let(:path) { '/assets/form/form.html' }
  103. let(:feedback_modal_button) { '#feedback-form-modal' }
  104. let(:form_inline_selector) { '#feedback-form-inline form.zammad-form' }
  105. context 'when feature is enabled' do
  106. before do
  107. visit 'channels/form'
  108. check 'form_ticket_create', { allow_label_click: true }
  109. wait.until { Setting.get('form_ticket_create') == true }
  110. end
  111. context 'when form is inline' do
  112. let(:form_context) { form_inline_selector }
  113. let(:name_input) { '#zammad-form-name-inline' }
  114. let(:body_input) { '#zammad-form-body-inline' }
  115. let(:email_input) { '#zammad-form-email-inline' }
  116. before { visit path }
  117. it_behaves_like 'validating form fields'
  118. it_behaves_like 'submitting valid form fields'
  119. end
  120. context 'when form is modal' do
  121. let(:form_context) { '.js-zammad-form-modal-body form.zammad-form' }
  122. let(:name_input) { '#zammad-form-name-modal' }
  123. let(:body_input) { '#zammad-form-body-modal' }
  124. let(:email_input) { '#zammad-form-email-modal' }
  125. before do
  126. visit path
  127. find(feedback_modal_button).click
  128. end
  129. it_behaves_like 'validating form fields'
  130. it_behaves_like 'submitting valid form fields'
  131. end
  132. end
  133. context 'when feature is disabled' do
  134. before do
  135. visit 'channels/form'
  136. uncheck 'form_ticket_create', { allow_label_click: true }
  137. wait.until { Setting.get('form_ticket_create') == false }
  138. visit path
  139. end
  140. it 'fails to load form' do
  141. expect(page).to have_text('Faild to load form config, feature is disabled')
  142. end
  143. end
  144. end
  145. end