form_spec.rb 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. require 'rails_helper'
  2. RSpec.describe 'Form', type: :request, searchindex: true do
  3. before(:each) do
  4. rebuild_searchindex
  5. end
  6. describe 'request handling' do
  7. it 'does get config call' do
  8. post '/api/v1/form_config', params: {}, as: :json
  9. expect(response).to have_http_status(401)
  10. expect(json_response).to be_a_kind_of(Hash)
  11. expect(json_response['error']).to eq('Not authorized')
  12. end
  13. it 'does get config call' do
  14. Setting.set('form_ticket_create', true)
  15. post '/api/v1/form_config', params: {}, as: :json
  16. expect(response).to have_http_status(401)
  17. expect(json_response).to be_a_kind_of(Hash)
  18. expect(json_response['error']).to eq('Not authorized')
  19. end
  20. it 'does get config call & do submit' do
  21. Setting.set('form_ticket_create', true)
  22. fingerprint = SecureRandom.hex(40)
  23. post '/api/v1/form_config', params: { fingerprint: fingerprint }, as: :json
  24. expect(response).to have_http_status(200)
  25. expect(json_response).to be_a_kind_of(Hash)
  26. expect(json_response['enabled']).to eq(true)
  27. expect(json_response['endpoint']).to eq('http://zammad.example.com/api/v1/form_submit')
  28. expect(json_response['token']).to be_truthy
  29. token = json_response['token']
  30. post '/api/v1/form_submit', params: { fingerprint: fingerprint, token: 'invalid' }, as: :json
  31. expect(response).to have_http_status(401)
  32. expect(json_response).to be_a_kind_of(Hash)
  33. expect(json_response['error']).to eq('Not authorized')
  34. post '/api/v1/form_submit', params: { fingerprint: fingerprint, token: token }, as: :json
  35. expect(response).to have_http_status(200)
  36. expect(json_response).to be_a_kind_of(Hash)
  37. expect(json_response['errors']).to be_truthy
  38. expect(json_response['errors']['name']).to eq('required')
  39. expect(json_response['errors']['email']).to eq('required')
  40. expect(json_response['errors']['title']).to eq('required')
  41. expect(json_response['errors']['body']).to eq('required')
  42. post '/api/v1/form_submit', params: { fingerprint: fingerprint, token: token, email: 'some' }, as: :json
  43. expect(response).to have_http_status(200)
  44. expect(json_response).to be_a_kind_of(Hash)
  45. expect(json_response['errors']).to be_truthy
  46. expect(json_response['errors']['name']).to eq('required')
  47. expect(json_response['errors']['email']).to eq('invalid')
  48. expect(json_response['errors']['title']).to eq('required')
  49. expect(json_response['errors']['body']).to eq('required')
  50. post '/api/v1/form_submit', params: { fingerprint: fingerprint, token: token, name: 'Bob Smith', email: 'discard@znuny.com', title: 'test', body: 'hello' }, as: :json
  51. expect(response).to have_http_status(200)
  52. expect(json_response).to be_a_kind_of(Hash)
  53. expect(json_response['errors']).to be_falsey
  54. expect(json_response['ticket']).to be_truthy
  55. expect(json_response['ticket']['id']).to be_truthy
  56. expect(json_response['ticket']['number']).to be_truthy
  57. travel 5.hours
  58. post '/api/v1/form_submit', params: { fingerprint: fingerprint, token: token, name: 'Bob Smith', email: 'discard@znuny.com', title: 'test', body: 'hello' }, as: :json
  59. expect(response).to have_http_status(200)
  60. expect(json_response).to be_a_kind_of(Hash)
  61. expect(json_response['errors']).to be_falsey
  62. expect(json_response['ticket']).to be_truthy
  63. expect(json_response['ticket']['id']).to be_truthy
  64. expect(json_response['ticket']['number']).to be_truthy
  65. travel 20.hours
  66. post '/api/v1/form_submit', params: { fingerprint: fingerprint, token: token, name: 'Bob Smith', email: 'discard@znuny.com', title: 'test', body: 'hello' }, as: :json
  67. expect(response).to have_http_status(401)
  68. end
  69. it 'does get config call & do submit' do
  70. Setting.set('form_ticket_create', true)
  71. fingerprint = SecureRandom.hex(40)
  72. post '/api/v1/form_config', params: { fingerprint: fingerprint }, as: :json
  73. expect(response).to have_http_status(200)
  74. expect(json_response).to be_a_kind_of(Hash)
  75. expect(json_response['enabled']).to eq(true)
  76. expect(json_response['endpoint']).to eq('http://zammad.example.com/api/v1/form_submit')
  77. expect(json_response['token']).to be_truthy
  78. token = json_response['token']
  79. post '/api/v1/form_submit', params: { fingerprint: fingerprint, token: 'invalid' }, as: :json
  80. expect(response).to have_http_status(401)
  81. expect(json_response).to be_a_kind_of(Hash)
  82. expect(json_response['error']).to eq('Not authorized')
  83. post '/api/v1/form_submit', params: { fingerprint: fingerprint, token: token }, as: :json
  84. expect(response).to have_http_status(200)
  85. expect(json_response).to be_a_kind_of(Hash)
  86. expect(json_response['errors']).to be_truthy
  87. expect(json_response['errors']['name']).to eq('required')
  88. expect(json_response['errors']['email']).to eq('required')
  89. expect(json_response['errors']['title']).to eq('required')
  90. expect(json_response['errors']['body']).to eq('required')
  91. post '/api/v1/form_submit', params: { fingerprint: fingerprint, token: token, email: 'some' }, as: :json
  92. expect(response).to have_http_status(200)
  93. expect(json_response).to be_a_kind_of(Hash)
  94. expect(json_response['errors']).to be_truthy
  95. expect(json_response['errors']['name']).to eq('required')
  96. expect(json_response['errors']['email']).to eq('invalid')
  97. expect(json_response['errors']['title']).to eq('required')
  98. expect(json_response['errors']['body']).to eq('required')
  99. post '/api/v1/form_submit', params: { fingerprint: fingerprint, token: token, name: 'Bob Smith', email: 'somebody@example.com', title: 'test', body: 'hello' }, as: :json
  100. expect(response).to have_http_status(200)
  101. expect(json_response).to be_a_kind_of(Hash)
  102. expect(json_response['errors']).to be_truthy
  103. expect(json_response['errors']['email']).to eq('invalid')
  104. end
  105. it 'does limits' do
  106. skip('No ES configured') if !SearchIndexBackend.enabled?
  107. Setting.set('form_ticket_create', true)
  108. fingerprint = SecureRandom.hex(40)
  109. post '/api/v1/form_config', params: { fingerprint: fingerprint }, as: :json
  110. expect(response).to have_http_status(200)
  111. expect(json_response).to be_a_kind_of(Hash)
  112. expect(json_response['enabled']).to eq(true)
  113. expect(json_response['endpoint']).to eq('http://zammad.example.com/api/v1/form_submit')
  114. expect(json_response['token']).to be_truthy
  115. token = json_response['token']
  116. (1..20).each do |count|
  117. travel 10.seconds
  118. post '/api/v1/form_submit', params: { fingerprint: fingerprint, token: token, name: 'Bob Smith', email: 'discard@znuny.com', title: "test#{count}", body: 'hello' }, as: :json
  119. expect(response).to have_http_status(200)
  120. expect(json_response).to be_a_kind_of(Hash)
  121. expect(json_response['errors']).to be_falsey
  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. expect(json_response['ticket']['number']).to be_truthy
  126. Scheduler.worker(true)
  127. sleep 1 # wait until elasticsearch is index
  128. end
  129. sleep 10 # wait until elasticsearch is index
  130. post '/api/v1/form_submit', params: { fingerprint: fingerprint, token: token, name: 'Bob Smith', email: 'discard@znuny.com', title: 'test-last', body: 'hello' }, as: :json
  131. expect(response).to have_http_status(401)
  132. expect(json_response).to be_a_kind_of(Hash)
  133. expect(json_response['error']).to be_truthy
  134. @headers = { 'ACCEPT' => 'application/json', 'CONTENT_TYPE' => 'application/json', 'REMOTE_ADDR' => '1.2.3.5' }
  135. (1..20).each do |count|
  136. travel 10.seconds
  137. 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
  138. expect(response).to have_http_status(200)
  139. expect(json_response).to be_a_kind_of(Hash)
  140. expect(json_response['errors']).to be_falsey
  141. expect(json_response['ticket']).to be_truthy
  142. expect(json_response['ticket']['id']).to be_truthy
  143. expect(json_response['ticket']['number']).to be_truthy
  144. Scheduler.worker(true)
  145. sleep 1 # wait until elasticsearch is index
  146. end
  147. sleep 10 # wait until elasticsearch is index
  148. 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
  149. expect(response).to have_http_status(401)
  150. expect(json_response).to be_a_kind_of(Hash)
  151. expect(json_response['error']).to be_truthy
  152. end
  153. it 'does customer_ticket_create false disables form' do
  154. Setting.set('form_ticket_create', false)
  155. Setting.set('customer_ticket_create', true)
  156. fingerprint = SecureRandom.hex(40)
  157. post '/api/v1/form_config', params: { fingerprint: fingerprint }, as: :json
  158. token = json_response['token']
  159. params = {
  160. fingerprint: fingerprint,
  161. token: token,
  162. name: 'Bob Smith',
  163. email: 'discard@znuny.com',
  164. title: 'test',
  165. body: 'hello'
  166. }
  167. post '/api/v1/form_submit', params: params, as: :json
  168. expect(response).to have_http_status(401)
  169. end
  170. end
  171. end