form_spec.rb 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. # Copyright (C) 2012-2022 Zammad Foundation, https://zammad-foundation.org/
  2. require 'rails_helper'
  3. RSpec.describe 'Form', type: :request, searchindex: true do
  4. before do
  5. configure_elasticsearch
  6. rebuild_searchindex
  7. end
  8. describe 'request handling' do
  9. it 'does get config call' do
  10. post '/api/v1/form_config', params: {}, as: :json
  11. expect(response).to have_http_status(:forbidden)
  12. expect(json_response).to be_a_kind_of(Hash)
  13. expect(json_response['error']).to eq('Not authorized')
  14. end
  15. it 'does get config call' do
  16. Setting.set('form_ticket_create', true)
  17. post '/api/v1/form_config', params: {}, as: :json
  18. expect(response).to have_http_status(:forbidden)
  19. expect(json_response).to be_a_kind_of(Hash)
  20. expect(json_response['error']).to eq('Not authorized')
  21. end
  22. it 'does get config call & do submit' do
  23. Setting.set('form_ticket_create', true)
  24. fingerprint = SecureRandom.hex(40)
  25. post '/api/v1/form_config', params: { fingerprint: fingerprint }, as: :json
  26. expect(response).to have_http_status(:ok)
  27. expect(json_response).to be_a_kind_of(Hash)
  28. expect(json_response['enabled']).to be(true)
  29. expect(json_response['endpoint']).to eq('http://zammad.example.com/api/v1/form_submit')
  30. expect(json_response['token']).to be_truthy
  31. token = json_response['token']
  32. post '/api/v1/form_submit', params: { fingerprint: fingerprint, token: 'invalid' }, as: :json
  33. expect(response).to have_http_status(:unauthorized)
  34. expect(json_response).to be_a_kind_of(Hash)
  35. expect(json_response['error']).to eq('Authorization failed')
  36. post '/api/v1/form_submit', params: { fingerprint: fingerprint, token: token }, as: :json
  37. expect(response).to have_http_status(:ok)
  38. expect(json_response).to be_a_kind_of(Hash)
  39. expect(json_response['errors']).to be_truthy
  40. expect(json_response['errors']['name']).to eq('required')
  41. expect(json_response['errors']['email']).to eq('required')
  42. expect(json_response['errors']['title']).to eq('required')
  43. expect(json_response['errors']['body']).to eq('required')
  44. post '/api/v1/form_submit', params: { fingerprint: fingerprint, token: token, email: 'some' }, as: :json
  45. expect(response).to have_http_status(:ok)
  46. expect(json_response).to be_a_kind_of(Hash)
  47. expect(json_response['errors']).to be_truthy
  48. expect(json_response['errors']['name']).to eq('required')
  49. expect(json_response['errors']['email']).to eq('invalid')
  50. expect(json_response['errors']['title']).to eq('required')
  51. expect(json_response['errors']['body']).to eq('required')
  52. post '/api/v1/form_submit', params: { fingerprint: fingerprint, token: token, name: 'Bob Smith', email: 'discard@znuny.com', title: 'test', body: 'hello' }, as: :json
  53. expect(response).to have_http_status(:ok)
  54. expect(json_response).to be_a_kind_of(Hash)
  55. expect(json_response['errors']).to be_falsey
  56. expect(json_response['ticket']).to be_truthy
  57. expect(json_response['ticket']['id']).to be_truthy
  58. expect(json_response['ticket']['number']).to be_truthy
  59. travel 5.hours
  60. post '/api/v1/form_submit', params: { fingerprint: fingerprint, token: token, name: 'Bob Smith', email: 'discard@znuny.com', title: 'test', body: 'hello' }, as: :json
  61. expect(response).to have_http_status(:ok)
  62. expect(json_response).to be_a_kind_of(Hash)
  63. expect(json_response['errors']).to be_falsey
  64. expect(json_response['ticket']).to be_truthy
  65. expect(json_response['ticket']['id']).to be_truthy
  66. expect(json_response['ticket']['number']).to be_truthy
  67. travel 20.hours
  68. post '/api/v1/form_submit', params: { fingerprint: fingerprint, token: token, name: 'Bob Smith', email: 'discard@znuny.com', title: 'test', body: 'hello' }, as: :json
  69. expect(response).to have_http_status(:unauthorized)
  70. end
  71. it 'does get config call & do submit' do
  72. Setting.set('form_ticket_create', true)
  73. fingerprint = SecureRandom.hex(40)
  74. post '/api/v1/form_config', params: { fingerprint: fingerprint }, as: :json
  75. expect(response).to have_http_status(:ok)
  76. expect(json_response).to be_a_kind_of(Hash)
  77. expect(json_response['enabled']).to be(true)
  78. expect(json_response['endpoint']).to eq('http://zammad.example.com/api/v1/form_submit')
  79. expect(json_response['token']).to be_truthy
  80. token = json_response['token']
  81. post '/api/v1/form_submit', params: { fingerprint: fingerprint, token: 'invalid' }, as: :json
  82. expect(response).to have_http_status(:unauthorized)
  83. expect(json_response).to be_a_kind_of(Hash)
  84. expect(json_response['error']).to eq('Authorization failed')
  85. post '/api/v1/form_submit', params: { fingerprint: fingerprint, token: token }, as: :json
  86. expect(response).to have_http_status(:ok)
  87. expect(json_response).to be_a_kind_of(Hash)
  88. expect(json_response['errors']).to be_truthy
  89. expect(json_response['errors']['name']).to eq('required')
  90. expect(json_response['errors']['email']).to eq('required')
  91. expect(json_response['errors']['title']).to eq('required')
  92. expect(json_response['errors']['body']).to eq('required')
  93. post '/api/v1/form_submit', params: { fingerprint: fingerprint, token: token, email: 'some' }, as: :json
  94. expect(response).to have_http_status(:ok)
  95. expect(json_response).to be_a_kind_of(Hash)
  96. expect(json_response['errors']).to be_truthy
  97. expect(json_response['errors']['name']).to eq('required')
  98. expect(json_response['errors']['email']).to eq('invalid')
  99. expect(json_response['errors']['title']).to eq('required')
  100. expect(json_response['errors']['body']).to eq('required')
  101. post '/api/v1/form_submit', params: { fingerprint: fingerprint, token: token, name: 'Bob Smith', email: 'somebody@somedomainthatisinvalid.com', title: 'test', body: 'hello' }, as: :json
  102. expect(response).to have_http_status(:ok)
  103. expect(json_response).to be_a_kind_of(Hash)
  104. expect(json_response['errors']).to be_truthy
  105. expect(json_response['errors']['email']).to eq('invalid')
  106. end
  107. it 'does limits' do
  108. skip('No ES configured') if !SearchIndexBackend.enabled?
  109. Setting.set('form_ticket_create', true)
  110. fingerprint = SecureRandom.hex(40)
  111. post '/api/v1/form_config', params: { fingerprint: fingerprint }, as: :json
  112. expect(response).to have_http_status(:ok)
  113. expect(json_response).to be_a_kind_of(Hash)
  114. expect(json_response['enabled']).to be(true)
  115. expect(json_response['endpoint']).to eq('http://zammad.example.com/api/v1/form_submit')
  116. expect(json_response['token']).to be_truthy
  117. token = json_response['token']
  118. (1..20).each do |count|
  119. post '/api/v1/form_submit', params: { fingerprint: fingerprint, token: token, name: 'Bob Smith', email: 'discard@znuny.com', title: "test#{count}", body: 'hello' }, as: :json
  120. expect(response).to have_http_status(:ok)
  121. expect(json_response).to be_a_kind_of(Hash)
  122. expect(json_response['errors']).to be_falsey
  123. expect(json_response['ticket']).to be_truthy
  124. expect(json_response['ticket']['id']).to be_truthy
  125. Scheduler.worker(true)
  126. end
  127. sleep 10 # wait until elasticsearch is index
  128. post '/api/v1/form_submit', params: { fingerprint: fingerprint, token: token, name: 'Bob Smith', email: 'discard@znuny.com', title: 'test-last', body: 'hello' }, as: :json
  129. expect(response).to have_http_status(:forbidden)
  130. expect(json_response).to be_a_kind_of(Hash)
  131. expect(json_response['error']).to be_truthy
  132. @headers = { 'ACCEPT' => 'application/json', 'CONTENT_TYPE' => 'application/json', 'REMOTE_ADDR' => '1.2.3.5' }
  133. (1..20).each do |count|
  134. post '/api/v1/form_submit', params: { fingerprint: fingerprint, token: token, name: 'Bob Smith', email: 'discard@znuny.com', title: "test-2-#{count}", body: 'hello' }, as: :json
  135. expect(response).to have_http_status(:ok)
  136. expect(json_response).to be_a_kind_of(Hash)
  137. expect(json_response['errors']).to be_falsey
  138. expect(json_response['ticket']).to be_truthy
  139. expect(json_response['ticket']['id']).to be_truthy
  140. Scheduler.worker(true)
  141. end
  142. sleep 10 # wait until elasticsearch is index
  143. post '/api/v1/form_submit', params: { fingerprint: fingerprint, token: token, name: 'Bob Smith', email: 'discard@znuny.com', title: 'test-2-last', body: 'hello' }, as: :json
  144. expect(response).to have_http_status(:forbidden)
  145. expect(json_response).to be_a_kind_of(Hash)
  146. expect(json_response['error']).to be_truthy
  147. @headers = { 'ACCEPT' => 'application/json', 'CONTENT_TYPE' => 'application/json', 'REMOTE_ADDR' => '::1' }
  148. (1..20).each do |count|
  149. post '/api/v1/form_submit', params: { fingerprint: fingerprint, token: token, name: 'Bob Smith', email: 'discard@znuny.com', title: "test-2-#{count}", body: 'hello' }, as: :json
  150. expect(response).to have_http_status(:ok)
  151. expect(json_response).to be_a_kind_of(Hash)
  152. expect(json_response['errors']).to be_falsey
  153. expect(json_response['ticket']).to be_truthy
  154. expect(json_response['ticket']['id']).to be_truthy
  155. Scheduler.worker(true)
  156. end
  157. sleep 10 # wait until elasticsearch is index
  158. post '/api/v1/form_submit', params: { fingerprint: fingerprint, token: token, name: 'Bob Smith', email: 'discard@znuny.com', title: 'test-2-last', body: 'hello' }, as: :json
  159. expect(response).to have_http_status(:forbidden)
  160. expect(json_response).to be_a_kind_of(Hash)
  161. expect(json_response['error']).to be_truthy
  162. end
  163. it 'does customer_ticket_create false disables form' do
  164. Setting.set('form_ticket_create', false)
  165. Setting.set('customer_ticket_create', true)
  166. fingerprint = SecureRandom.hex(40)
  167. post '/api/v1/form_config', params: { fingerprint: fingerprint }, as: :json
  168. token = json_response['token']
  169. params = {
  170. fingerprint: fingerprint,
  171. token: token,
  172. name: 'Bob Smith',
  173. email: 'discard@znuny.com',
  174. title: 'test',
  175. body: 'hello'
  176. }
  177. post '/api/v1/form_submit', params: params, as: :json
  178. expect(response).to have_http_status(:forbidden)
  179. end
  180. context 'when ApplicationHandleInfo context' do
  181. let(:fingerprint) { SecureRandom.hex(40) }
  182. let(:token) { json_response['token'] }
  183. before do
  184. Setting.set('form_ticket_create', true)
  185. post '/api/v1/form_config', params: { fingerprint: fingerprint }, as: :json
  186. end
  187. it 'gets switched to "form"' do
  188. allow(ApplicationHandleInfo).to receive('context=')
  189. post '/api/v1/form_submit', params: { fingerprint: fingerprint, token: token, name: 'Bob Smith', email: 'discard@znuny.com', title: 'test-last', body: 'hello' }, as: :json
  190. expect(ApplicationHandleInfo).to have_received('context=').with('form').at_least(1)
  191. end
  192. it 'reverts back to default' do
  193. allow(ApplicationHandleInfo).to receive('context=')
  194. post '/api/v1/form_submit', params: { fingerprint: fingerprint, token: token, name: 'Bob Smith', email: 'discard@znuny.com', title: 'test-last', body: 'hello' }, as: :json
  195. expect(ApplicationHandleInfo.context).not_to eq 'form'
  196. end
  197. end
  198. end
  199. end