search_controller_test.rb 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448
  1. require 'test_helper'
  2. class SearchControllerTest < ActionDispatch::IntegrationTest
  3. include SearchindexHelper
  4. setup do
  5. # set current user
  6. UserInfo.current_user_id = 1
  7. # set accept header
  8. @headers = { 'ACCEPT' => 'application/json', 'CONTENT_TYPE' => 'application/json' }
  9. # create agent
  10. roles = Role.where(name: %w[Admin Agent])
  11. groups = Group.all
  12. @admin = User.create!(
  13. login: 'search-admin',
  14. firstname: 'Search',
  15. lastname: 'Admin',
  16. email: 'search-admin@example.com',
  17. password: 'adminpw',
  18. active: true,
  19. roles: roles,
  20. groups: groups,
  21. )
  22. # create agent
  23. roles = Role.where(name: 'Agent')
  24. @agent = User.create!(
  25. login: 'search-agent@example.com',
  26. firstname: 'Search 1234',
  27. lastname: 'Agent',
  28. email: 'search-agent@example.com',
  29. password: 'agentpw',
  30. active: true,
  31. roles: roles,
  32. groups: groups,
  33. )
  34. # create customer without org
  35. roles = Role.where(name: 'Customer')
  36. @customer_without_org = User.create!(
  37. login: 'search-customer1@example.com',
  38. firstname: 'Search',
  39. lastname: 'Customer1',
  40. email: 'search-customer1@example.com',
  41. password: 'customer1pw',
  42. active: true,
  43. roles: roles,
  44. )
  45. # create orgs
  46. @organization = Organization.create!(
  47. name: 'Rest Org',
  48. )
  49. @organization2 = Organization.create!(
  50. name: 'Rest Org #2',
  51. )
  52. @organization3 = Organization.create!(
  53. name: 'Rest Org #3',
  54. )
  55. @organization4 = Organization.create!(
  56. name: 'Tes.t. Org',
  57. )
  58. @organization5 = Organization.create!(
  59. name: 'ABC_D Org',
  60. )
  61. # create customer with org
  62. @customer_with_org2 = User.create!(
  63. login: 'search-customer2@example.com',
  64. firstname: 'Search',
  65. lastname: 'Customer2',
  66. email: 'search-customer2@example.com',
  67. password: 'customer2pw',
  68. active: true,
  69. roles: roles,
  70. organization_id: @organization.id,
  71. )
  72. @customer_with_org3 = User.create!(
  73. login: 'search-customer3@example.com',
  74. firstname: 'Search',
  75. lastname: 'Customer3',
  76. email: 'search-customer3@example.com',
  77. password: 'customer3pw',
  78. active: true,
  79. roles: roles,
  80. organization_id: @organization.id,
  81. )
  82. @ticket1 = Ticket.create!(
  83. title: 'test 1234-1',
  84. group: Group.lookup(name: 'Users'),
  85. customer_id: @customer_without_org.id,
  86. state: Ticket::State.lookup(name: 'new'),
  87. priority: Ticket::Priority.lookup(name: '2 normal'),
  88. )
  89. @article1 = Ticket::Article.create!(
  90. ticket_id: @ticket1.id,
  91. from: 'some_sender1@example.com',
  92. to: 'some_recipient1@example.com',
  93. subject: 'some subject1',
  94. message_id: 'some@id',
  95. body: 'some message1',
  96. internal: false,
  97. sender: Ticket::Article::Sender.where(name: 'Customer').first,
  98. type: Ticket::Article::Type.where(name: 'email').first,
  99. )
  100. travel 1.second
  101. @ticket2 = Ticket.create!(
  102. title: 'test 1234-2',
  103. group: Group.lookup(name: 'Users'),
  104. customer_id: @customer_with_org2.id,
  105. state: Ticket::State.lookup(name: 'new'),
  106. priority: Ticket::Priority.lookup(name: '2 normal'),
  107. )
  108. @article2 = Ticket::Article.create!(
  109. ticket_id: @ticket2.id,
  110. from: 'some_sender2@example.com',
  111. to: 'some_recipient2@example.com',
  112. subject: 'some subject2',
  113. message_id: 'some@id',
  114. body: 'some message2',
  115. internal: false,
  116. sender: Ticket::Article::Sender.where(name: 'Customer').first,
  117. type: Ticket::Article::Type.where(name: 'email').first,
  118. )
  119. travel 1.second
  120. @ticket3 = Ticket.create!(
  121. title: 'test 1234-2',
  122. group: Group.lookup(name: 'Users'),
  123. customer_id: @customer_with_org3.id,
  124. state: Ticket::State.lookup(name: 'new'),
  125. priority: Ticket::Priority.lookup(name: '2 normal'),
  126. )
  127. @article3 = Ticket::Article.create!(
  128. ticket_id: @ticket3.id,
  129. from: 'some_sender3@example.com',
  130. to: 'some_recipient3@example.com',
  131. subject: 'some subject3',
  132. message_id: 'some@id',
  133. body: 'some message3',
  134. internal: false,
  135. sender: Ticket::Article::Sender.where(name: 'Customer').first,
  136. type: Ticket::Article::Type.where(name: 'email').first,
  137. )
  138. configure_elasticsearch do
  139. travel 1.minute
  140. rebuild_searchindex
  141. # execute background jobs
  142. Scheduler.worker(true)
  143. sleep 6
  144. end
  145. end
  146. test 'settings index with nobody' do
  147. params = {
  148. query: 'test 1234',
  149. limit: 2,
  150. }
  151. post '/api/v1/search/ticket', params: params.to_json, headers: @headers
  152. assert_response(401)
  153. result = JSON.parse(@response.body)
  154. assert_equal(Hash, result.class)
  155. assert_not(result.blank?)
  156. assert_equal('authentication failed', result['error'])
  157. post '/api/v1/search/user', params: params.to_json, headers: @headers
  158. assert_response(401)
  159. result = JSON.parse(@response.body)
  160. assert_equal(Hash, result.class)
  161. assert_not(result.blank?)
  162. assert_equal('authentication failed', result['error'])
  163. post '/api/v1/search', params: params.to_json, headers: @headers
  164. assert_response(401)
  165. result = JSON.parse(@response.body)
  166. assert_equal(Hash, result.class)
  167. assert_not(result.blank?)
  168. assert_equal('authentication failed', result['error'])
  169. end
  170. test 'settings index with admin' do
  171. credentials = ActionController::HttpAuthentication::Basic.encode_credentials('search-admin@example.com', 'adminpw')
  172. params = {
  173. query: '1234*',
  174. limit: 1,
  175. }
  176. post '/api/v1/search', params: params.to_json, headers: @headers.merge('Authorization' => credentials)
  177. assert_response(200)
  178. result = JSON.parse(@response.body)
  179. assert_equal(Hash, result.class)
  180. assert(result)
  181. assert_equal('Ticket', result['result'][0]['type'])
  182. assert_equal(@ticket3.id, result['result'][0]['id'])
  183. assert_equal('User', result['result'][1]['type'])
  184. assert_equal(@agent.id, result['result'][1]['id'])
  185. assert_not(result['result'][2])
  186. params = {
  187. query: '1234*',
  188. limit: 10,
  189. }
  190. post '/api/v1/search', params: params.to_json, headers: @headers.merge('Authorization' => credentials)
  191. assert_response(200)
  192. result = JSON.parse(@response.body)
  193. assert_equal(Hash, result.class)
  194. assert(result)
  195. assert_equal('Ticket', result['result'][0]['type'])
  196. assert_equal(@ticket3.id, result['result'][0]['id'])
  197. assert_equal('Ticket', result['result'][1]['type'])
  198. assert_equal(@ticket2.id, result['result'][1]['id'])
  199. assert_equal('Ticket', result['result'][2]['type'])
  200. assert_equal(@ticket1.id, result['result'][2]['id'])
  201. assert_equal('User', result['result'][3]['type'])
  202. assert_equal(@agent.id, result['result'][3]['id'])
  203. assert_not(result['result'][4])
  204. params = {
  205. query: '1234*',
  206. limit: 10,
  207. }
  208. post '/api/v1/search/ticket', params: params.to_json, headers: @headers.merge('Authorization' => credentials)
  209. assert_response(200)
  210. result = JSON.parse(@response.body)
  211. assert_equal(Hash, result.class)
  212. assert(result)
  213. assert_equal('Ticket', result['result'][0]['type'])
  214. assert_equal(@ticket3.id, result['result'][0]['id'])
  215. assert_equal('Ticket', result['result'][1]['type'])
  216. assert_equal(@ticket2.id, result['result'][1]['id'])
  217. assert_equal('Ticket', result['result'][2]['type'])
  218. assert_equal(@ticket1.id, result['result'][2]['id'])
  219. assert_not(result['result'][3])
  220. params = {
  221. query: '1234*',
  222. limit: 10,
  223. }
  224. post '/api/v1/search/user', params: params.to_json, headers: @headers.merge('Authorization' => credentials)
  225. assert_response(200)
  226. result = JSON.parse(@response.body)
  227. assert_equal(Hash, result.class)
  228. assert_equal('User', result['result'][0]['type'])
  229. assert_equal(@agent.id, result['result'][0]['id'])
  230. assert_not(result['result'][1])
  231. end
  232. test 'settings index with agent' do
  233. credentials = ActionController::HttpAuthentication::Basic.encode_credentials('search-agent@example.com', 'agentpw')
  234. params = {
  235. query: '1234*',
  236. limit: 1,
  237. }
  238. post '/api/v1/search', params: params.to_json, headers: @headers.merge('Authorization' => credentials)
  239. assert_response(200)
  240. result = JSON.parse(@response.body)
  241. assert_equal(Hash, result.class)
  242. assert(result)
  243. assert_equal('Ticket', result['result'][0]['type'])
  244. assert_equal(@ticket3.id, result['result'][0]['id'])
  245. assert_equal('User', result['result'][1]['type'])
  246. assert_equal(@agent.id, result['result'][1]['id'])
  247. assert_not(result['result'][2])
  248. params = {
  249. query: '1234*',
  250. limit: 10,
  251. }
  252. post '/api/v1/search', params: params.to_json, headers: @headers.merge('Authorization' => credentials)
  253. assert_response(200)
  254. result = JSON.parse(@response.body)
  255. assert_equal(Hash, result.class)
  256. assert(result)
  257. assert_equal('Ticket', result['result'][0]['type'])
  258. assert_equal(@ticket3.id, result['result'][0]['id'])
  259. assert_equal('Ticket', result['result'][1]['type'])
  260. assert_equal(@ticket2.id, result['result'][1]['id'])
  261. assert_equal('Ticket', result['result'][2]['type'])
  262. assert_equal(@ticket1.id, result['result'][2]['id'])
  263. assert_equal('User', result['result'][3]['type'])
  264. assert_equal(@agent.id, result['result'][3]['id'])
  265. assert_not(result['result'][4])
  266. params = {
  267. query: '1234*',
  268. limit: 10,
  269. }
  270. post '/api/v1/search/ticket', params: params.to_json, headers: @headers.merge('Authorization' => credentials)
  271. assert_response(200)
  272. result = JSON.parse(@response.body)
  273. assert_equal(Hash, result.class)
  274. assert(result)
  275. assert_equal('Ticket', result['result'][0]['type'])
  276. assert_equal(@ticket3.id, result['result'][0]['id'])
  277. assert_equal('Ticket', result['result'][1]['type'])
  278. assert_equal(@ticket2.id, result['result'][1]['id'])
  279. assert_equal('Ticket', result['result'][2]['type'])
  280. assert_equal(@ticket1.id, result['result'][2]['id'])
  281. assert_not(result['result'][3])
  282. params = {
  283. query: '1234*',
  284. limit: 10,
  285. }
  286. post '/api/v1/search/user', params: params.to_json, headers: @headers.merge('Authorization' => credentials)
  287. assert_response(200)
  288. result = JSON.parse(@response.body)
  289. assert_equal(Hash, result.class)
  290. assert_equal('User', result['result'][0]['type'])
  291. assert_equal(@agent.id, result['result'][0]['id'])
  292. assert_not(result['result'][1])
  293. end
  294. test 'settings index with customer 1' do
  295. credentials = ActionController::HttpAuthentication::Basic.encode_credentials('search-customer1@example.com', 'customer1pw')
  296. params = {
  297. query: '1234*',
  298. limit: 10,
  299. }
  300. post '/api/v1/search', params: params.to_json, headers: @headers.merge('Authorization' => credentials)
  301. assert_response(200)
  302. result = JSON.parse(@response.body)
  303. assert_equal(Hash, result.class)
  304. assert(result)
  305. assert_equal('Ticket', result['result'][0]['type'])
  306. assert_equal(@ticket1.id, result['result'][0]['id'])
  307. assert_not(result['result'][1])
  308. params = {
  309. query: '1234*',
  310. limit: 10,
  311. }
  312. post '/api/v1/search/ticket', params: params.to_json, headers: @headers.merge('Authorization' => credentials)
  313. assert_response(200)
  314. result = JSON.parse(@response.body)
  315. assert_equal(Hash, result.class)
  316. assert(result)
  317. assert_equal('Ticket', result['result'][0]['type'])
  318. assert_equal(@ticket1.id, result['result'][0]['id'])
  319. assert_not(result['result'][1])
  320. params = {
  321. query: '1234*',
  322. limit: 10,
  323. }
  324. post '/api/v1/search/user', params: params.to_json, headers: @headers.merge('Authorization' => credentials)
  325. assert_response(200)
  326. result = JSON.parse(@response.body)
  327. assert_equal(Hash, result.class)
  328. assert_not(result['result'][0])
  329. end
  330. test 'settings index with customer 2' do
  331. credentials = ActionController::HttpAuthentication::Basic.encode_credentials('search-customer2@example.com', 'customer2pw')
  332. params = {
  333. query: '1234*',
  334. limit: 10,
  335. }
  336. post '/api/v1/search', params: params.to_json, headers: @headers.merge('Authorization' => credentials)
  337. assert_response(200)
  338. result = JSON.parse(@response.body)
  339. assert_equal(Hash, result.class)
  340. assert(result)
  341. assert_equal('Ticket', result['result'][0]['type'])
  342. assert_equal(@ticket3.id, result['result'][0]['id'])
  343. assert_equal('Ticket', result['result'][1]['type'])
  344. assert_equal(@ticket2.id, result['result'][1]['id'])
  345. assert_not(result['result'][2])
  346. params = {
  347. query: '1234*',
  348. limit: 10,
  349. }
  350. post '/api/v1/search/ticket', params: params.to_json, headers: @headers.merge('Authorization' => credentials)
  351. assert_response(200)
  352. result = JSON.parse(@response.body)
  353. assert_equal(Hash, result.class)
  354. assert(result)
  355. assert_equal('Ticket', result['result'][0]['type'])
  356. assert_equal(@ticket3.id, result['result'][0]['id'])
  357. assert_equal('Ticket', result['result'][1]['type'])
  358. assert_equal(@ticket2.id, result['result'][1]['id'])
  359. assert_not(result['result'][2])
  360. params = {
  361. query: '1234*',
  362. limit: 10,
  363. }
  364. post '/api/v1/search/user', params: params.to_json, headers: @headers.merge('Authorization' => credentials)
  365. assert_response(200)
  366. result = JSON.parse(@response.body)
  367. assert_equal(Hash, result.class)
  368. assert_not(result['result'][0])
  369. end
  370. # Verify fix for Github issue #2058 - Autocomplete hangs on dot in the new user form
  371. test 'searching for organization with a dot in its name' do
  372. credentials = ActionController::HttpAuthentication::Basic.encode_credentials('search-agent@example.com', 'agentpw')
  373. get '/api/v1/search/organization?query=tes.', headers: @headers.merge('Authorization' => credentials)
  374. assert_response(200)
  375. result = JSON.parse(@response.body)
  376. assert_equal(1, result['result'].size)
  377. assert_equal('Organization', result['result'][0]['type'])
  378. target_id = result['result'][0]['id']
  379. assert_equal('Tes.t. Org', result['assets']['Organization'][target_id.to_s]['name'])
  380. end
  381. # Search query H& should correctly match H&M
  382. test 'searching for organization with _ in its name' do
  383. credentials = ActionController::HttpAuthentication::Basic.encode_credentials('search-agent@example.com', 'agentpw')
  384. get '/api/v1/search/organization?query=abc_', headers: @headers.merge('Authorization' => credentials)
  385. assert_response(200)
  386. result = JSON.parse(@response.body)
  387. assert_equal(1, result['result'].size)
  388. assert_equal('Organization', result['result'][0]['type'])
  389. target_id = result['result'][0]['id']
  390. assert_equal('ABC_D Org', result['assets']['Organization'][target_id.to_s]['name'])
  391. end
  392. end