123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533 |
- # encoding: utf-8
- require 'integration_test_helper'
- class ElasticsearchTest < ActiveSupport::TestCase
- # set config
- if !ENV['ES_URL']
- raise "ERROR: Need ES_URL - hint ES_URL='http://172.0.0.1:9200'"
- end
- Setting.set('es_url', ENV['ES_URL'])
- if !ENV['ES_INDEX'] && !ENV['ES_INDEX_RAND']
- raise "ERROR: Need ES_INDEX - hint ES_INDEX='estest.local_zammad'"
- end
- if ENV['ES_INDEX_RAND']
- ENV['ES_INDEX'] = "es_index_#{rand(999_999_999)}"
- end
- Setting.set('es_index', ENV['ES_INDEX'])
- # Setting.set('es_url', 'http://172.0.0.1:9200')
- # Setting.set('es_index', 'estest.local_zammad')
- # Setting.set('es_user', 'elasticsearch')
- # Setting.set('es_password', 'zammad')
- # set max attachment size in mb
- Setting.set('es_attachment_max_size_in_mb', 1)
- # drop/create indexes
- #Rake::Task["searchindex:drop"].execute
- #Rake::Task["searchindex:create"].execute
- system('rake searchindex:rebuild')
- groups = Group.where(name: 'Users')
- roles = Role.where(name: 'Agent')
- agent = User.create_or_update(
- login: 'es-agent@example.com',
- firstname: 'E',
- lastname: 'S',
- email: 'es-agent@example.com',
- password: 'agentpw',
- active: true,
- roles: roles,
- groups: groups,
- updated_by_id: 1,
- created_by_id: 1,
- )
- group_without_access = Group.create_if_not_exists(
- name: 'WithoutAccess',
- note: 'Test for not access check.',
- updated_by_id: 1,
- created_by_id: 1
- )
- roles = Role.where(name: 'Customer')
- organization1 = Organization.create_if_not_exists(
- name: 'Customer Organization Update',
- note: 'some note',
- updated_by_id: 1,
- created_by_id: 1,
- )
- customer1 = User.create_or_update(
- login: 'es-customer1@example.com',
- firstname: 'ES',
- lastname: 'Customer1',
- email: 'es-customer1@example.com',
- password: 'customerpw',
- active: true,
- organization_id: organization1.id,
- roles: roles,
- updated_by_id: 1,
- created_by_id: 1,
- )
- sleep 1
- customer2 = User.create_or_update(
- login: 'es-customer2@example.com',
- firstname: 'ES',
- lastname: 'Customer2',
- email: 'es-customer2@example.com',
- password: 'customerpw',
- active: true,
- organization_id: organization1.id,
- roles: roles,
- updated_by_id: 1,
- created_by_id: 1,
- )
- sleep 1
- customer3 = User.create_or_update(
- login: 'es-customer3@example.com',
- firstname: 'ES',
- lastname: 'Customer3',
- email: 'es-customer3@example.com',
- password: 'customerpw',
- active: true,
- roles: roles,
- updated_by_id: 1,
- created_by_id: 1,
- )
- # check search attributes
- test 'a - objects' do
- # user
- attributes = agent.search_index_data
- assert_equal('E', attributes['firstname'])
- assert_equal('S', attributes['lastname'])
- assert_equal('es-agent@example.com', attributes['email'])
- assert_not(attributes['password'])
- assert_not(attributes['organization'])
- attributes = agent.search_index_attribute_lookup
- assert_equal('E', attributes['firstname'])
- assert_equal('S', attributes['lastname'])
- assert_equal('es-agent@example.com', attributes['email'])
- assert_not(attributes['password'])
- assert_not(attributes['organization'])
- # organization
- attributes = organization1.search_index_data
- assert_equal('Customer Organization Update', attributes['name'])
- assert_equal('some note', attributes['note'])
- assert_not(attributes['members'])
- attributes = organization1.search_index_attribute_lookup
- assert_equal('Customer Organization Update', attributes['name'])
- assert_equal('some note', attributes['note'])
- assert(attributes['members'])
- # ticket/article
- ticket1 = Ticket.create(
- title: 'some title äöüß',
- group: Group.lookup(name: 'Users'),
- customer_id: customer1.id,
- state: Ticket::State.lookup(name: 'new'),
- priority: Ticket::Priority.lookup(name: '2 normal'),
- updated_by_id: 1,
- created_by_id: 1,
- )
- article1 = Ticket::Article.create(
- ticket_id: ticket1.id,
- from: 'some_sender@example.com',
- to: 'some_recipient@example.com',
- subject: 'some subject',
- message_id: 'some@id',
- body: 'some message',
- internal: false,
- sender: Ticket::Article::Sender.where(name: 'Customer').first,
- type: Ticket::Article::Type.where(name: 'email').first,
- updated_by_id: 1,
- created_by_id: 1,
- )
- attributes = ticket1.search_index_attribute_lookup
- assert_equal('Users', attributes['group'])
- assert_equal('new', attributes['state'])
- assert_equal('2 normal', attributes['priority'])
- assert_equal('ES', attributes['customer']['firstname'])
- assert_equal('Customer1', attributes['customer']['lastname'])
- assert_equal('es-customer1@example.com', attributes['customer']['email'])
- assert_not(attributes['customer']['password'])
- assert_equal('Customer Organization Update', attributes['customer']['organization'])
- assert_equal('-', attributes['owner']['login'])
- assert_equal('-', attributes['owner']['firstname'])
- assert_not(attributes['owner']['password'])
- assert_not(attributes['owner']['organization'])
- ticket1.destroy
- # execute background jobs
- Scheduler.worker(true)
- end
- # check tickets and search it
- test 'b - tickets' do
- system('rake searchindex:rebuild')
- ticket1 = Ticket.create(
- title: "some title\n äöüß",
- group: Group.lookup(name: 'Users'),
- customer_id: customer1.id,
- state: Ticket::State.lookup(name: 'new'),
- priority: Ticket::Priority.lookup(name: '2 normal'),
- updated_by_id: 1,
- created_by_id: 1,
- )
- article1 = Ticket::Article.create(
- ticket_id: ticket1.id,
- from: 'some_sender@example.com',
- to: 'some_recipient@example.com',
- subject: 'some subject',
- message_id: 'some@id',
- body: 'some message',
- internal: false,
- sender: Ticket::Article::Sender.where(name: 'Customer').first,
- type: Ticket::Article::Type.where(name: 'email').first,
- updated_by_id: 1,
- created_by_id: 1,
- )
- # add attachments which should get index / .txt
- # "some normal text"
- Store.add(
- object: 'Ticket::Article',
- o_id: article1.id,
- data: IO.binread("#{Rails.root}/test/fixtures/es-normal.txt"),
- filename: 'es-normal.txt',
- preferences: {},
- created_by_id: 1,
- )
- # add attachments which should get index / .pdf
- # "Zammad Test77"
- Store.add(
- object: 'Ticket::Article',
- o_id: article1.id,
- data: IO.binread("#{Rails.root}/test/fixtures/es-pdf1.pdf"),
- filename: 'es-pdf1.pdf',
- preferences: {},
- created_by_id: 1,
- )
- # add attachments which should get index / .box
- # "Old programmers never die test99"
- Store.add(
- object: 'Ticket::Article',
- o_id: article1.id,
- data: IO.binread("#{Rails.root}/test/fixtures/es-box1.box"),
- filename: 'mail1.box',
- preferences: {},
- created_by_id: 1,
- )
- # add to big attachment which should not get index
- # "some too big text88"
- Store.add(
- object: 'Ticket::Article',
- o_id: article1.id,
- data: IO.binread("#{Rails.root}/test/fixtures/es-too-big.txt"),
- filename: 'es-too-big.txt',
- preferences: {},
- created_by_id: 1,
- )
- Tag.tag_add(
- object: 'Ticket',
- o_id: ticket1.id,
- item: 'someTagA',
- created_by_id: 1,
- )
- sleep 1
- ticket2 = Ticket.create(
- title: 'something else',
- group: Group.lookup(name: 'Users'),
- customer_id: customer2.id,
- state: Ticket::State.lookup(name: 'open'),
- priority: Ticket::Priority.lookup(name: '2 normal'),
- updated_by_id: 1,
- created_by_id: 1,
- )
- article2 = Ticket::Article.create(
- ticket_id: ticket2.id,
- from: 'some_sender@example.org',
- to: 'some_recipient@example.org',
- subject: 'some subject2 / autobahn what else?',
- message_id: 'some@id',
- body: 'some other message <b>with s<u>t</u>rong text<b>',
- content_type: 'text/html',
- internal: false,
- sender: Ticket::Article::Sender.where(name: 'Customer').first,
- type: Ticket::Article::Type.where(name: 'email').first,
- updated_by_id: 1,
- created_by_id: 1,
- )
- Tag.tag_add(
- object: 'Ticket',
- o_id: ticket2.id,
- item: 'someTagB',
- created_by_id: 1,
- )
- sleep 1
- ticket3 = Ticket.create(
- title: 'something else',
- group: Group.lookup(name: 'WithoutAccess'),
- customer_id: customer3.id,
- state: Ticket::State.lookup(name: 'open'),
- priority: Ticket::Priority.lookup(name: '2 normal'),
- updated_by_id: 1,
- created_by_id: 1,
- )
- article3 = Ticket::Article.create(
- ticket_id: ticket3.id,
- from: 'some_sender@example.org',
- to: 'some_recipient@example.org',
- subject: 'some subject3',
- message_id: 'some@id',
- body: 'some other message 3 / kindergarden what else?',
- internal: false,
- sender: Ticket::Article::Sender.where(name: 'Customer').first,
- type: Ticket::Article::Type.where(name: 'email').first,
- updated_by_id: 1,
- created_by_id: 1,
- )
- # execute background jobs
- Scheduler.worker(true)
- sleep 4
- # search as agent
- # search for article data
- result = Ticket.search(
- current_user: agent,
- query: 'autobahn',
- limit: 15,
- )
- assert(!result.empty?, 'result exists not')
- assert(result[0], 'record 1')
- assert(!result[1], 'record 2')
- assert_equal(result[0].id, ticket2.id)
- # search for html content
- result = Ticket.search(
- current_user: agent,
- query: 'strong',
- limit: 15,
- )
- assert(!result.empty?, 'result exists not')
- assert(result[0], 'record 1')
- assert(!result[1], 'record 2')
- assert_equal(result[0].id, ticket2.id)
- # search for indexed attachment
- result = Ticket.search(
- current_user: agent,
- query: '"some normal text66"',
- limit: 15,
- )
- assert(result[0], 'record 1')
- assert_equal(result[0].id, ticket1.id)
- result = Ticket.search(
- current_user: agent,
- query: 'test77',
- limit: 15,
- )
- assert(result[0], 'record 1')
- assert_equal(result[0].id, ticket1.id)
- # search for not indexed attachment
- result = Ticket.search(
- current_user: agent,
- query: 'test88',
- limit: 15,
- )
- assert(!result[0], 'record 1')
- result = Ticket.search(
- current_user: agent,
- query: 'test99',
- limit: 15,
- )
- assert(!result[0], 'record 1')
- # search for ticket with no permissions
- result = Ticket.search(
- current_user: agent,
- query: 'kindergarden',
- limit: 15,
- )
- assert(result.empty?, 'result should be empty')
- assert(!result[0], 'record 1')
- # search as customer1
- result = Ticket.search(
- current_user: customer1,
- query: 'title OR else',
- limit: 15,
- )
- assert(!result.empty?, 'result exists not')
- assert(result[0], 'record 1')
- assert(result[1], 'record 2')
- assert(!result[2], 'record 3')
- assert_equal(result[0].id, ticket2.id)
- assert_equal(result[1].id, ticket1.id)
- # search as customer2
- result = Ticket.search(
- current_user: customer2,
- query: 'title OR else',
- limit: 15,
- )
- assert(!result.empty?, 'result exists not')
- assert(result[0], 'record 1')
- assert(result[1], 'record 2')
- assert(!result[2], 'record 3')
- assert_equal(result[0].id, ticket2.id)
- assert_equal(result[1].id, ticket1.id)
- # search as customer3
- result = Ticket.search(
- current_user: customer3,
- query: 'title OR else',
- limit: 15,
- )
- assert(!result.empty?, 'result exists not')
- assert(result[0], 'record 1')
- assert(!result[1], 'record 2')
- assert_equal(result[0].id, ticket3.id)
- # search for tags
- result = Ticket.search(
- current_user: agent,
- query: 'tag:someTagA',
- limit: 15,
- )
- assert(result[0], 'record 1')
- assert(!result[1], 'record 1')
- assert_equal(result[0].id, ticket1.id)
- result = Ticket.search(
- current_user: agent,
- query: 'tag:someTagB',
- limit: 15,
- )
- assert(result[0], 'record 2')
- assert(!result[1], 'record 2')
- assert_equal(result[0].id, ticket2.id)
- # rename tag (e. g. via admin interface)
- tag_item = Tag::Item.lookup(name: 'someTagA')
- Tag::Item.rename(
- id: tag_item.id,
- name: 'someTagC',
- updated_by_id: 1,
- )
- # execute background jobs
- Scheduler.worker(true)
- sleep 4
- # search for tags
- result = Ticket.search(
- current_user: agent,
- query: 'tag:someTagA',
- limit: 15,
- )
- assert(!result[0], 'record 1')
- assert(!result[1], 'record 1')
- result = Ticket.search(
- current_user: agent,
- query: 'tag:someTagB',
- limit: 15,
- )
- assert(result[0], 'record 2')
- assert(!result[1], 'record 2')
- assert_equal(result[0].id, ticket2.id)
- result = Ticket.search(
- current_user: agent,
- query: 'tag:someTagC',
- limit: 15,
- )
- assert(result[0], 'record 1')
- assert(!result[1], 'record 2')
- assert_equal(result[0].id, ticket1.id)
- result = Ticket.search(
- current_user: agent,
- query: 'state:open',
- limit: 15,
- )
- assert(result[0], 'record 1')
- assert(!result[1], 'record 2')
- assert_equal(result[0].id, ticket2.id)
- result = Ticket.search(
- current_user: agent,
- query: '"some_sender@example.com"',
- limit: 15,
- )
- assert(result[0], 'record 1')
- assert(!result[1], 'record 2')
- assert_equal(result[0].id, ticket1.id)
- result = Ticket.search(
- current_user: agent,
- query: 'article.from:"some_sender@example.com"',
- limit: 15,
- )
- assert(result[0], 'record 1')
- assert(!result[1], 'record 2')
- assert_equal(result[0].id, ticket1.id)
- end
- # check users and search it
- test 'c - users' do
- # search as agent
- result = User.search(
- current_user: agent,
- query: 'customer1',
- limit: 15,
- )
- assert(!result.empty?, 'result should not be empty')
- assert(result[0], 'record 1')
- assert(!result[1], 'record 2')
- assert_equal(result[0].id, customer1.id)
- # search as customer1
- result = User.search(
- current_user: customer1,
- query: 'customer1',
- limit: 15,
- )
- assert(result.empty?, 'result should be empty')
- assert(!result[0], 'record 1')
- # cleanup
- system('rake searchindex:drop')
- end
- end
|