form_spec.rb 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. # Copyright (C) 2012-2021 Zammad Foundation, http://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. sleep 10
  43. click_on 'Submit'
  44. expect(page).to have_selector('.has-error [name=email]').and have_no_selector('button[type="submit"][disabled]')
  45. end
  46. end
  47. end
  48. shared_examples 'submitting valid form fields' do
  49. it 'submits form filled slowly succesfully' do
  50. within form_context do
  51. fill_in 'Name', with: 'some sender'
  52. fill_in 'Message', with: 'message here'
  53. fill_in 'Email', with: 'discard@znuny.com'
  54. sleep 10
  55. click_on 'Submit'
  56. expect(page).to have_text('Thank you for your inquiry')
  57. end
  58. end
  59. it 'fails to submit form filled too fast' do
  60. within form_context do
  61. fill_in 'Name', with: 'some sender'
  62. fill_in 'Message', with: 'message here'
  63. fill_in 'Email', with: 'discard@znuny.com'
  64. click_on 'Submit'
  65. accept_alert('Sorry, you look like an robot!')
  66. end
  67. end
  68. end
  69. context 'with in-app form' do
  70. let(:path) { 'channels/form' }
  71. let(:feedback_modal_button) { '.js-formBtn' }
  72. context 'when form is inline' do
  73. let(:form_context) { '.js-formInline form.zammad-form' }
  74. let(:name_input) { '#zammad-form-name-inline' }
  75. let(:body_input) { '#zammad-form-body-inline' }
  76. let(:email_input) { '#zammad-form-email-inline' }
  77. before do
  78. visit path
  79. uncheck 'Start modal dialog for form.', { allow_label_click: true }
  80. end
  81. it_behaves_like 'validating form fields'
  82. end
  83. context 'when form is modal' do
  84. let(:form_context) { '.js-zammad-form-modal-body form.zammad-form' }
  85. let(:name_input) { '#zammad-form-name-modal' }
  86. let(:body_input) { '#zammad-form-body-modal' }
  87. let(:email_input) { '#zammad-form-email-modal' }
  88. before do
  89. visit path
  90. find(feedback_modal_button).click
  91. end
  92. it_behaves_like 'validating form fields'
  93. end
  94. it 'shows an inline form' do
  95. visit path
  96. uncheck 'Start modal dialog for form.', { allow_label_click: true }
  97. expect(page).to have_selector('.js-formInline').and have_no_selector('.js-formInline.hide')
  98. end
  99. end
  100. context 'with external form' do
  101. let(:path) { '/assets/form/form.html' }
  102. let(:feedback_modal_button) { '#feedback-form-modal' }
  103. let(:form_inline_selector) { '#feedback-form-inline form.zammad-form' }
  104. context 'when feature is enabled' do
  105. before do
  106. visit 'channels/form'
  107. check 'form_ticket_create', { allow_label_click: true }
  108. end
  109. context 'when form is inline' do
  110. let(:form_context) { form_inline_selector }
  111. let(:name_input) { '#zammad-form-name-inline' }
  112. let(:body_input) { '#zammad-form-body-inline' }
  113. let(:email_input) { '#zammad-form-email-inline' }
  114. before { visit path }
  115. it_behaves_like 'validating form fields'
  116. it_behaves_like 'submitting valid form fields'
  117. end
  118. context 'when form is modal' do
  119. let(:form_context) { '.js-zammad-form-modal-body form.zammad-form' }
  120. let(:name_input) { '#zammad-form-name-modal' }
  121. let(:body_input) { '#zammad-form-body-modal' }
  122. let(:email_input) { '#zammad-form-email-modal' }
  123. before do
  124. visit path
  125. find(feedback_modal_button).click
  126. end
  127. it_behaves_like 'validating form fields'
  128. it_behaves_like 'submitting valid form fields'
  129. end
  130. end
  131. context 'when feature is disabled' do
  132. before do
  133. visit 'channels/form'
  134. uncheck 'form_ticket_create', { allow_label_click: true }
  135. visit path
  136. end
  137. it 'fails to load form' do
  138. expect(page).to have_text('Faild to load form config, feature is disabled')
  139. end
  140. end
  141. end
  142. end