elasticsearch_test.rb 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533
  1. # encoding: utf-8
  2. require 'integration_test_helper'
  3. class ElasticsearchTest < ActiveSupport::TestCase
  4. # set config
  5. if !ENV['ES_URL']
  6. raise "ERROR: Need ES_URL - hint ES_URL='http://172.0.0.1:9200'"
  7. end
  8. Setting.set('es_url', ENV['ES_URL'])
  9. if !ENV['ES_INDEX'] && !ENV['ES_INDEX_RAND']
  10. raise "ERROR: Need ES_INDEX - hint ES_INDEX='estest.local_zammad'"
  11. end
  12. if ENV['ES_INDEX_RAND']
  13. ENV['ES_INDEX'] = "es_index_#{rand(999_999_999)}"
  14. end
  15. Setting.set('es_index', ENV['ES_INDEX'])
  16. # Setting.set('es_url', 'http://172.0.0.1:9200')
  17. # Setting.set('es_index', 'estest.local_zammad')
  18. # Setting.set('es_user', 'elasticsearch')
  19. # Setting.set('es_password', 'zammad')
  20. # set max attachment size in mb
  21. Setting.set('es_attachment_max_size_in_mb', 1)
  22. # drop/create indexes
  23. #Rake::Task["searchindex:drop"].execute
  24. #Rake::Task["searchindex:create"].execute
  25. system('rake searchindex:rebuild')
  26. groups = Group.where(name: 'Users')
  27. roles = Role.where(name: 'Agent')
  28. agent = User.create_or_update(
  29. login: 'es-agent@example.com',
  30. firstname: 'E',
  31. lastname: 'S',
  32. email: 'es-agent@example.com',
  33. password: 'agentpw',
  34. active: true,
  35. roles: roles,
  36. groups: groups,
  37. updated_by_id: 1,
  38. created_by_id: 1,
  39. )
  40. group_without_access = Group.create_if_not_exists(
  41. name: 'WithoutAccess',
  42. note: 'Test for not access check.',
  43. updated_by_id: 1,
  44. created_by_id: 1
  45. )
  46. roles = Role.where(name: 'Customer')
  47. organization1 = Organization.create_if_not_exists(
  48. name: 'Customer Organization Update',
  49. note: 'some note',
  50. updated_by_id: 1,
  51. created_by_id: 1,
  52. )
  53. customer1 = User.create_or_update(
  54. login: 'es-customer1@example.com',
  55. firstname: 'ES',
  56. lastname: 'Customer1',
  57. email: 'es-customer1@example.com',
  58. password: 'customerpw',
  59. active: true,
  60. organization_id: organization1.id,
  61. roles: roles,
  62. updated_by_id: 1,
  63. created_by_id: 1,
  64. )
  65. sleep 1
  66. customer2 = User.create_or_update(
  67. login: 'es-customer2@example.com',
  68. firstname: 'ES',
  69. lastname: 'Customer2',
  70. email: 'es-customer2@example.com',
  71. password: 'customerpw',
  72. active: true,
  73. organization_id: organization1.id,
  74. roles: roles,
  75. updated_by_id: 1,
  76. created_by_id: 1,
  77. )
  78. sleep 1
  79. customer3 = User.create_or_update(
  80. login: 'es-customer3@example.com',
  81. firstname: 'ES',
  82. lastname: 'Customer3',
  83. email: 'es-customer3@example.com',
  84. password: 'customerpw',
  85. active: true,
  86. roles: roles,
  87. updated_by_id: 1,
  88. created_by_id: 1,
  89. )
  90. # check search attributes
  91. test 'a - objects' do
  92. # user
  93. attributes = agent.search_index_data
  94. assert_equal('E', attributes['firstname'])
  95. assert_equal('S', attributes['lastname'])
  96. assert_equal('es-agent@example.com', attributes['email'])
  97. assert_not(attributes['password'])
  98. assert_not(attributes['organization'])
  99. attributes = agent.search_index_attribute_lookup
  100. assert_equal('E', attributes['firstname'])
  101. assert_equal('S', attributes['lastname'])
  102. assert_equal('es-agent@example.com', attributes['email'])
  103. assert_not(attributes['password'])
  104. assert_not(attributes['organization'])
  105. # organization
  106. attributes = organization1.search_index_data
  107. assert_equal('Customer Organization Update', attributes['name'])
  108. assert_equal('some note', attributes['note'])
  109. assert_not(attributes['members'])
  110. attributes = organization1.search_index_attribute_lookup
  111. assert_equal('Customer Organization Update', attributes['name'])
  112. assert_equal('some note', attributes['note'])
  113. assert(attributes['members'])
  114. # ticket/article
  115. ticket1 = Ticket.create(
  116. title: 'some title äöüß',
  117. group: Group.lookup(name: 'Users'),
  118. customer_id: customer1.id,
  119. state: Ticket::State.lookup(name: 'new'),
  120. priority: Ticket::Priority.lookup(name: '2 normal'),
  121. updated_by_id: 1,
  122. created_by_id: 1,
  123. )
  124. article1 = Ticket::Article.create(
  125. ticket_id: ticket1.id,
  126. from: 'some_sender@example.com',
  127. to: 'some_recipient@example.com',
  128. subject: 'some subject',
  129. message_id: 'some@id',
  130. body: 'some message',
  131. internal: false,
  132. sender: Ticket::Article::Sender.where(name: 'Customer').first,
  133. type: Ticket::Article::Type.where(name: 'email').first,
  134. updated_by_id: 1,
  135. created_by_id: 1,
  136. )
  137. attributes = ticket1.search_index_attribute_lookup
  138. assert_equal('Users', attributes['group'])
  139. assert_equal('new', attributes['state'])
  140. assert_equal('2 normal', attributes['priority'])
  141. assert_equal('ES', attributes['customer']['firstname'])
  142. assert_equal('Customer1', attributes['customer']['lastname'])
  143. assert_equal('es-customer1@example.com', attributes['customer']['email'])
  144. assert_not(attributes['customer']['password'])
  145. assert_equal('Customer Organization Update', attributes['customer']['organization'])
  146. assert_equal('-', attributes['owner']['login'])
  147. assert_equal('-', attributes['owner']['firstname'])
  148. assert_not(attributes['owner']['password'])
  149. assert_not(attributes['owner']['organization'])
  150. ticket1.destroy
  151. # execute background jobs
  152. Scheduler.worker(true)
  153. end
  154. # check tickets and search it
  155. test 'b - tickets' do
  156. system('rake searchindex:rebuild')
  157. ticket1 = Ticket.create(
  158. title: "some title\n äöüß",
  159. group: Group.lookup(name: 'Users'),
  160. customer_id: customer1.id,
  161. state: Ticket::State.lookup(name: 'new'),
  162. priority: Ticket::Priority.lookup(name: '2 normal'),
  163. updated_by_id: 1,
  164. created_by_id: 1,
  165. )
  166. article1 = Ticket::Article.create(
  167. ticket_id: ticket1.id,
  168. from: 'some_sender@example.com',
  169. to: 'some_recipient@example.com',
  170. subject: 'some subject',
  171. message_id: 'some@id',
  172. body: 'some message',
  173. internal: false,
  174. sender: Ticket::Article::Sender.where(name: 'Customer').first,
  175. type: Ticket::Article::Type.where(name: 'email').first,
  176. updated_by_id: 1,
  177. created_by_id: 1,
  178. )
  179. # add attachments which should get index / .txt
  180. # "some normal text"
  181. Store.add(
  182. object: 'Ticket::Article',
  183. o_id: article1.id,
  184. data: IO.binread("#{Rails.root}/test/fixtures/es-normal.txt"),
  185. filename: 'es-normal.txt',
  186. preferences: {},
  187. created_by_id: 1,
  188. )
  189. # add attachments which should get index / .pdf
  190. # "Zammad Test77"
  191. Store.add(
  192. object: 'Ticket::Article',
  193. o_id: article1.id,
  194. data: IO.binread("#{Rails.root}/test/fixtures/es-pdf1.pdf"),
  195. filename: 'es-pdf1.pdf',
  196. preferences: {},
  197. created_by_id: 1,
  198. )
  199. # add attachments which should get index / .box
  200. # "Old programmers never die test99"
  201. Store.add(
  202. object: 'Ticket::Article',
  203. o_id: article1.id,
  204. data: IO.binread("#{Rails.root}/test/fixtures/es-box1.box"),
  205. filename: 'mail1.box',
  206. preferences: {},
  207. created_by_id: 1,
  208. )
  209. # add to big attachment which should not get index
  210. # "some too big text88"
  211. Store.add(
  212. object: 'Ticket::Article',
  213. o_id: article1.id,
  214. data: IO.binread("#{Rails.root}/test/fixtures/es-too-big.txt"),
  215. filename: 'es-too-big.txt',
  216. preferences: {},
  217. created_by_id: 1,
  218. )
  219. Tag.tag_add(
  220. object: 'Ticket',
  221. o_id: ticket1.id,
  222. item: 'someTagA',
  223. created_by_id: 1,
  224. )
  225. sleep 1
  226. ticket2 = Ticket.create(
  227. title: 'something else',
  228. group: Group.lookup(name: 'Users'),
  229. customer_id: customer2.id,
  230. state: Ticket::State.lookup(name: 'open'),
  231. priority: Ticket::Priority.lookup(name: '2 normal'),
  232. updated_by_id: 1,
  233. created_by_id: 1,
  234. )
  235. article2 = Ticket::Article.create(
  236. ticket_id: ticket2.id,
  237. from: 'some_sender@example.org',
  238. to: 'some_recipient@example.org',
  239. subject: 'some subject2 / autobahn what else?',
  240. message_id: 'some@id',
  241. body: 'some other message <b>with s<u>t</u>rong text<b>',
  242. content_type: 'text/html',
  243. internal: false,
  244. sender: Ticket::Article::Sender.where(name: 'Customer').first,
  245. type: Ticket::Article::Type.where(name: 'email').first,
  246. updated_by_id: 1,
  247. created_by_id: 1,
  248. )
  249. Tag.tag_add(
  250. object: 'Ticket',
  251. o_id: ticket2.id,
  252. item: 'someTagB',
  253. created_by_id: 1,
  254. )
  255. sleep 1
  256. ticket3 = Ticket.create(
  257. title: 'something else',
  258. group: Group.lookup(name: 'WithoutAccess'),
  259. customer_id: customer3.id,
  260. state: Ticket::State.lookup(name: 'open'),
  261. priority: Ticket::Priority.lookup(name: '2 normal'),
  262. updated_by_id: 1,
  263. created_by_id: 1,
  264. )
  265. article3 = Ticket::Article.create(
  266. ticket_id: ticket3.id,
  267. from: 'some_sender@example.org',
  268. to: 'some_recipient@example.org',
  269. subject: 'some subject3',
  270. message_id: 'some@id',
  271. body: 'some other message 3 / kindergarden what else?',
  272. internal: false,
  273. sender: Ticket::Article::Sender.where(name: 'Customer').first,
  274. type: Ticket::Article::Type.where(name: 'email').first,
  275. updated_by_id: 1,
  276. created_by_id: 1,
  277. )
  278. # execute background jobs
  279. Scheduler.worker(true)
  280. sleep 4
  281. # search as agent
  282. # search for article data
  283. result = Ticket.search(
  284. current_user: agent,
  285. query: 'autobahn',
  286. limit: 15,
  287. )
  288. assert(!result.empty?, 'result exists not')
  289. assert(result[0], 'record 1')
  290. assert(!result[1], 'record 2')
  291. assert_equal(result[0].id, ticket2.id)
  292. # search for html content
  293. result = Ticket.search(
  294. current_user: agent,
  295. query: 'strong',
  296. limit: 15,
  297. )
  298. assert(!result.empty?, 'result exists not')
  299. assert(result[0], 'record 1')
  300. assert(!result[1], 'record 2')
  301. assert_equal(result[0].id, ticket2.id)
  302. # search for indexed attachment
  303. result = Ticket.search(
  304. current_user: agent,
  305. query: '"some normal text66"',
  306. limit: 15,
  307. )
  308. assert(result[0], 'record 1')
  309. assert_equal(result[0].id, ticket1.id)
  310. result = Ticket.search(
  311. current_user: agent,
  312. query: 'test77',
  313. limit: 15,
  314. )
  315. assert(result[0], 'record 1')
  316. assert_equal(result[0].id, ticket1.id)
  317. # search for not indexed attachment
  318. result = Ticket.search(
  319. current_user: agent,
  320. query: 'test88',
  321. limit: 15,
  322. )
  323. assert(!result[0], 'record 1')
  324. result = Ticket.search(
  325. current_user: agent,
  326. query: 'test99',
  327. limit: 15,
  328. )
  329. assert(!result[0], 'record 1')
  330. # search for ticket with no permissions
  331. result = Ticket.search(
  332. current_user: agent,
  333. query: 'kindergarden',
  334. limit: 15,
  335. )
  336. assert(result.empty?, 'result should be empty')
  337. assert(!result[0], 'record 1')
  338. # search as customer1
  339. result = Ticket.search(
  340. current_user: customer1,
  341. query: 'title OR else',
  342. limit: 15,
  343. )
  344. assert(!result.empty?, 'result exists not')
  345. assert(result[0], 'record 1')
  346. assert(result[1], 'record 2')
  347. assert(!result[2], 'record 3')
  348. assert_equal(result[0].id, ticket2.id)
  349. assert_equal(result[1].id, ticket1.id)
  350. # search as customer2
  351. result = Ticket.search(
  352. current_user: customer2,
  353. query: 'title OR else',
  354. limit: 15,
  355. )
  356. assert(!result.empty?, 'result exists not')
  357. assert(result[0], 'record 1')
  358. assert(result[1], 'record 2')
  359. assert(!result[2], 'record 3')
  360. assert_equal(result[0].id, ticket2.id)
  361. assert_equal(result[1].id, ticket1.id)
  362. # search as customer3
  363. result = Ticket.search(
  364. current_user: customer3,
  365. query: 'title OR else',
  366. limit: 15,
  367. )
  368. assert(!result.empty?, 'result exists not')
  369. assert(result[0], 'record 1')
  370. assert(!result[1], 'record 2')
  371. assert_equal(result[0].id, ticket3.id)
  372. # search for tags
  373. result = Ticket.search(
  374. current_user: agent,
  375. query: 'tag:someTagA',
  376. limit: 15,
  377. )
  378. assert(result[0], 'record 1')
  379. assert(!result[1], 'record 1')
  380. assert_equal(result[0].id, ticket1.id)
  381. result = Ticket.search(
  382. current_user: agent,
  383. query: 'tag:someTagB',
  384. limit: 15,
  385. )
  386. assert(result[0], 'record 2')
  387. assert(!result[1], 'record 2')
  388. assert_equal(result[0].id, ticket2.id)
  389. # rename tag (e. g. via admin interface)
  390. tag_item = Tag::Item.lookup(name: 'someTagA')
  391. Tag::Item.rename(
  392. id: tag_item.id,
  393. name: 'someTagC',
  394. updated_by_id: 1,
  395. )
  396. # execute background jobs
  397. Scheduler.worker(true)
  398. sleep 4
  399. # search for tags
  400. result = Ticket.search(
  401. current_user: agent,
  402. query: 'tag:someTagA',
  403. limit: 15,
  404. )
  405. assert(!result[0], 'record 1')
  406. assert(!result[1], 'record 1')
  407. result = Ticket.search(
  408. current_user: agent,
  409. query: 'tag:someTagB',
  410. limit: 15,
  411. )
  412. assert(result[0], 'record 2')
  413. assert(!result[1], 'record 2')
  414. assert_equal(result[0].id, ticket2.id)
  415. result = Ticket.search(
  416. current_user: agent,
  417. query: 'tag:someTagC',
  418. limit: 15,
  419. )
  420. assert(result[0], 'record 1')
  421. assert(!result[1], 'record 2')
  422. assert_equal(result[0].id, ticket1.id)
  423. result = Ticket.search(
  424. current_user: agent,
  425. query: 'state:open',
  426. limit: 15,
  427. )
  428. assert(result[0], 'record 1')
  429. assert(!result[1], 'record 2')
  430. assert_equal(result[0].id, ticket2.id)
  431. result = Ticket.search(
  432. current_user: agent,
  433. query: '"some_sender@example.com"',
  434. limit: 15,
  435. )
  436. assert(result[0], 'record 1')
  437. assert(!result[1], 'record 2')
  438. assert_equal(result[0].id, ticket1.id)
  439. result = Ticket.search(
  440. current_user: agent,
  441. query: 'article.from:"some_sender@example.com"',
  442. limit: 15,
  443. )
  444. assert(result[0], 'record 1')
  445. assert(!result[1], 'record 2')
  446. assert_equal(result[0].id, ticket1.id)
  447. end
  448. # check users and search it
  449. test 'c - users' do
  450. # search as agent
  451. result = User.search(
  452. current_user: agent,
  453. query: 'customer1',
  454. limit: 15,
  455. )
  456. assert(!result.empty?, 'result should not be empty')
  457. assert(result[0], 'record 1')
  458. assert(!result[1], 'record 2')
  459. assert_equal(result[0].id, customer1.id)
  460. # search as customer1
  461. result = User.search(
  462. current_user: customer1,
  463. query: 'customer1',
  464. limit: 15,
  465. )
  466. assert(result.empty?, 'result should be empty')
  467. assert(!result[0], 'record 1')
  468. # cleanup
  469. system('rake searchindex:drop')
  470. end
  471. end