form_spec.rb 11 KB

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