form_controller_test.rb 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. require 'test_helper'
  2. require 'rake'
  3. class FormControllerTest < ActionDispatch::IntegrationTest
  4. setup do
  5. @headers = { 'ACCEPT' => 'application/json', 'CONTENT_TYPE' => 'application/json', 'REMOTE_ADDR' => '1.2.3.4' }
  6. if ENV['ES_URL'].present?
  7. #fail "ERROR: Need ES_URL - hint ES_URL='http://127.0.0.1:9200'"
  8. Setting.set('es_url', ENV['ES_URL'])
  9. # Setting.set('es_url', 'http://127.0.0.1:9200')
  10. # Setting.set('es_index', 'estest.local_zammad')
  11. # Setting.set('es_user', 'elasticsearch')
  12. # Setting.set('es_password', 'zammad')
  13. if ENV['ES_INDEX_RAND'].present?
  14. ENV['ES_INDEX'] = "es_index_#{rand(999_999_999)}"
  15. end
  16. if ENV['ES_INDEX'].blank?
  17. raise "ERROR: Need ES_INDEX - hint ES_INDEX='estest.local_zammad'"
  18. end
  19. Setting.set('es_index', ENV['ES_INDEX'])
  20. end
  21. Ticket.destroy_all
  22. # drop/create indexes
  23. Setting.reload
  24. Rake::Task.clear
  25. Zammad::Application.load_tasks
  26. Rake::Task['searchindex:rebuild'].execute
  27. end
  28. teardown do
  29. if ENV['ES_URL'].present?
  30. Rake::Task['searchindex:drop'].execute
  31. end
  32. end
  33. test '01 - get config call' do
  34. post '/api/v1/form_config', params: {}.to_json, headers: @headers
  35. assert_response(401)
  36. result = JSON.parse(@response.body)
  37. assert_equal(result.class, Hash)
  38. assert_equal(result['error'], 'Not authorized')
  39. end
  40. test '02 - get config call' do
  41. Setting.set('form_ticket_create', true)
  42. post '/api/v1/form_config', params: {}.to_json, headers: @headers
  43. assert_response(401)
  44. result = JSON.parse(@response.body)
  45. assert_equal(result.class, Hash)
  46. assert_equal(result['error'], 'Not authorized')
  47. end
  48. test '03 - get config call & do submit' do
  49. Setting.set('form_ticket_create', true)
  50. fingerprint = SecureRandom.hex(40)
  51. post '/api/v1/form_config', params: { fingerprint: fingerprint }.to_json, headers: @headers
  52. assert_response(200)
  53. result = JSON.parse(@response.body)
  54. assert_equal(result.class, Hash)
  55. assert_equal(result['enabled'], true)
  56. assert_equal(result['endpoint'], 'http://zammad.example.com/api/v1/form_submit')
  57. assert(result['token'])
  58. token = result['token']
  59. post '/api/v1/form_submit', params: { fingerprint: fingerprint, token: 'invalid' }.to_json, headers: @headers
  60. assert_response(401)
  61. result = JSON.parse(@response.body)
  62. assert_equal(result.class, Hash)
  63. assert_equal(result['error'], 'Not authorized')
  64. post '/api/v1/form_submit', params: { fingerprint: fingerprint, token: token }.to_json, headers: @headers
  65. assert_response(200)
  66. result = JSON.parse(@response.body)
  67. assert_equal(result.class, Hash)
  68. assert(result['errors'])
  69. assert_equal(result['errors']['name'], 'required')
  70. assert_equal(result['errors']['email'], 'required')
  71. assert_equal(result['errors']['title'], 'required')
  72. assert_equal(result['errors']['body'], 'required')
  73. post '/api/v1/form_submit', params: { fingerprint: fingerprint, token: token, email: 'some' }.to_json, headers: @headers
  74. assert_response(200)
  75. result = JSON.parse(@response.body)
  76. assert_equal(result.class, Hash)
  77. assert(result['errors'])
  78. assert_equal(result['errors']['name'], 'required')
  79. assert_equal(result['errors']['email'], 'invalid')
  80. assert_equal(result['errors']['title'], 'required')
  81. assert_equal(result['errors']['body'], 'required')
  82. post '/api/v1/form_submit', params: { fingerprint: fingerprint, token: token, name: 'Bob Smith', email: 'discard@znuny.com', title: 'test', body: 'hello' }.to_json, headers: @headers
  83. assert_response(200)
  84. result = JSON.parse(@response.body)
  85. assert_equal(result.class, Hash)
  86. assert_not(result['errors'])
  87. assert(result['ticket'])
  88. assert(result['ticket']['id'])
  89. assert(result['ticket']['number'])
  90. travel 5.hours
  91. post '/api/v1/form_submit', params: { fingerprint: fingerprint, token: token, name: 'Bob Smith', email: 'discard@znuny.com', title: 'test', body: 'hello' }.to_json, headers: @headers
  92. assert_response(200)
  93. result = JSON.parse(@response.body)
  94. assert_equal(result.class, Hash)
  95. assert_not(result['errors'])
  96. assert(result['ticket'])
  97. assert(result['ticket']['id'])
  98. assert(result['ticket']['number'])
  99. travel 20.hours
  100. post '/api/v1/form_submit', params: { fingerprint: fingerprint, token: token, name: 'Bob Smith', email: 'discard@znuny.com', title: 'test', body: 'hello' }.to_json, headers: @headers
  101. assert_response(401)
  102. end
  103. test '04 - get config call & do submit' do
  104. Setting.set('form_ticket_create', true)
  105. fingerprint = SecureRandom.hex(40)
  106. post '/api/v1/form_config', params: { fingerprint: fingerprint }.to_json, headers: @headers
  107. assert_response(200)
  108. result = JSON.parse(@response.body)
  109. assert_equal(result.class, Hash)
  110. assert_equal(result['enabled'], true)
  111. assert_equal(result['endpoint'], 'http://zammad.example.com/api/v1/form_submit')
  112. assert(result['token'])
  113. token = result['token']
  114. post '/api/v1/form_submit', params: { fingerprint: fingerprint, token: 'invalid' }.to_json, headers: @headers
  115. assert_response(401)
  116. result = JSON.parse(@response.body)
  117. assert_equal(result.class, Hash)
  118. assert_equal(result['error'], 'Not authorized')
  119. post '/api/v1/form_submit', params: { fingerprint: fingerprint, token: token }.to_json, headers: @headers
  120. assert_response(200)
  121. result = JSON.parse(@response.body)
  122. assert_equal(result.class, Hash)
  123. assert(result['errors'])
  124. assert_equal(result['errors']['name'], 'required')
  125. assert_equal(result['errors']['email'], 'required')
  126. assert_equal(result['errors']['title'], 'required')
  127. assert_equal(result['errors']['body'], 'required')
  128. post '/api/v1/form_submit', params: { fingerprint: fingerprint, token: token, email: 'some' }.to_json, headers: @headers
  129. assert_response(200)
  130. result = JSON.parse(@response.body)
  131. assert_equal(result.class, Hash)
  132. assert(result['errors'])
  133. assert_equal(result['errors']['name'], 'required')
  134. assert_equal(result['errors']['email'], 'invalid')
  135. assert_equal(result['errors']['title'], 'required')
  136. assert_equal(result['errors']['body'], 'required')
  137. post '/api/v1/form_submit', params: { fingerprint: fingerprint, token: token, name: 'Bob Smith', email: 'somebody@example.com', title: 'test', body: 'hello' }.to_json, headers: @headers
  138. assert_response(200)
  139. result = JSON.parse(@response.body)
  140. assert_equal(result.class, Hash)
  141. assert(result['errors'])
  142. assert_equal(result['errors']['email'], 'invalid')
  143. end
  144. test '05 - limits' do
  145. return if !SearchIndexBackend.enabled?
  146. Setting.set('form_ticket_create', true)
  147. fingerprint = SecureRandom.hex(40)
  148. post '/api/v1/form_config', params: { fingerprint: fingerprint }.to_json, headers: @headers
  149. assert_response(200)
  150. result = JSON.parse(@response.body)
  151. assert_equal(result.class, Hash)
  152. assert_equal(result['enabled'], true)
  153. assert_equal(result['endpoint'], 'http://zammad.example.com/api/v1/form_submit')
  154. assert(result['token'])
  155. token = result['token']
  156. (1..20).each do |count|
  157. travel 10.seconds
  158. post '/api/v1/form_submit', params: { fingerprint: fingerprint, token: token, name: 'Bob Smith', email: 'discard@znuny.com', title: "test#{count}", body: 'hello' }.to_json, headers: @headers
  159. assert_response(200)
  160. result = JSON.parse(@response.body)
  161. assert_equal(result.class, Hash)
  162. assert_not(result['errors'])
  163. assert(result['ticket'])
  164. assert(result['ticket']['id'])
  165. assert(result['ticket']['number'])
  166. Scheduler.worker(true)
  167. sleep 1 # wait until elasticsearch is index
  168. end
  169. sleep 10 # wait until elasticsearch is index
  170. post '/api/v1/form_submit', params: { fingerprint: fingerprint, token: token, name: 'Bob Smith', email: 'discard@znuny.com', title: 'test-last', body: 'hello' }.to_json, headers: @headers
  171. assert_response(401)
  172. result = JSON.parse(@response.body)
  173. assert_equal(result.class, Hash)
  174. assert(result['error'])
  175. @headers = { 'ACCEPT' => 'application/json', 'CONTENT_TYPE' => 'application/json', 'REMOTE_ADDR' => '1.2.3.5' }
  176. (1..20).each do |count|
  177. travel 10.seconds
  178. post '/api/v1/form_submit', params: { fingerprint: fingerprint, token: token, name: 'Bob Smith', email: 'discard@znuny.com', title: "test-2-#{count}", body: 'hello' }.to_json, headers: @headers
  179. assert_response(200)
  180. result = JSON.parse(@response.body)
  181. assert_equal(result.class, Hash)
  182. assert_not(result['errors'])
  183. assert(result['ticket'])
  184. assert(result['ticket']['id'])
  185. assert(result['ticket']['number'])
  186. Scheduler.worker(true)
  187. sleep 1 # wait until elasticsearch is index
  188. end
  189. sleep 10 # wait until elasticsearch is index
  190. post '/api/v1/form_submit', params: { fingerprint: fingerprint, token: token, name: 'Bob Smith', email: 'discard@znuny.com', title: 'test-2-last', body: 'hello' }.to_json, headers: @headers
  191. assert_response(401)
  192. result = JSON.parse(@response.body)
  193. assert_equal(result.class, Hash)
  194. assert(result['error'])
  195. end
  196. test '06 - customer_ticket_create false disables form' do
  197. Setting.set('form_ticket_create', false)
  198. Setting.set('customer_ticket_create', true)
  199. fingerprint = SecureRandom.hex(40)
  200. post '/api/v1/form_config', params: { fingerprint: fingerprint }.to_json, headers: @headers
  201. result = JSON.parse(@response.body)
  202. token = result['token']
  203. params = {
  204. fingerprint: fingerprint,
  205. token: token,
  206. name: 'Bob Smith',
  207. email: 'discard@znuny.com',
  208. title: 'test',
  209. body: 'hello'
  210. }
  211. post '/api/v1/form_submit', params: params.to_json, headers: @headers
  212. assert_response(401)
  213. end
  214. end