2015-02-25 23:44:29 +00:00
|
|
|
# encoding: utf-8
|
|
|
|
require 'integration_test_helper'
|
|
|
|
|
2015-02-26 00:12:12 +00:00
|
|
|
class ElasticsearchTest < ActiveSupport::TestCase
|
2015-02-25 23:44:29 +00:00
|
|
|
|
2015-04-06 06:13:05 +00:00
|
|
|
# set config
|
2015-04-06 19:00:16 +00:00
|
|
|
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']
|
|
|
|
raise "ERROR: Need ES_INDEX - hint ES_INDEX='estest.local_zammad'"
|
|
|
|
end
|
|
|
|
Setting.set('es_index', ENV['ES_INDEX'])
|
|
|
|
|
2015-04-06 06:13:05 +00:00
|
|
|
# Setting.set('es_url', 'http://172.0.0.1:9200')
|
2015-04-06 19:00:16 +00:00
|
|
|
# Setting.set('es_index', 'estest.local_zammad')
|
2015-04-06 06:13:05 +00:00
|
|
|
# Setting.set('es_user', 'elasticsearch')
|
|
|
|
# Setting.set('es_password', 'zammad')
|
|
|
|
|
2015-04-06 19:00:16 +00:00
|
|
|
# set max attachment size in mb
|
|
|
|
Setting.set('es_attachment_max_size_in_mb', 1 )
|
2015-04-05 23:05:33 +00:00
|
|
|
|
2015-02-25 23:44:29 +00:00
|
|
|
# drop/create indexes
|
2015-02-26 00:12:12 +00:00
|
|
|
#Rake::Task["searchindex:drop"].execute
|
|
|
|
#Rake::Task["searchindex:create"].execute
|
|
|
|
system('rake searchindex:rebuild')
|
2015-02-25 23:44:29 +00:00
|
|
|
|
|
|
|
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
|
|
|
|
)
|
2015-04-05 23:05:33 +00:00
|
|
|
roles = Role.where( :name => 'Customer' )
|
|
|
|
organization1 = Organization.create_if_not_exists(
|
|
|
|
:name => 'Customer Organization Update',
|
|
|
|
: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,
|
|
|
|
)
|
2015-04-05 23:43:19 +00:00
|
|
|
sleep 1
|
2015-04-05 23:05:33 +00:00
|
|
|
customer2 = User.create_or_update(
|
2015-04-05 23:28:52 +00:00
|
|
|
:login => 'es-customer2@example.com',
|
2015-04-05 23:05:33 +00:00
|
|
|
:firstname => 'ES',
|
2015-04-05 23:28:52 +00:00
|
|
|
:lastname => 'Customer2',
|
|
|
|
:email => 'es-customer2@example.com',
|
2015-04-05 23:05:33 +00:00
|
|
|
:password => 'customerpw',
|
|
|
|
:active => true,
|
|
|
|
:organization_id => organization1.id,
|
|
|
|
:roles => roles,
|
|
|
|
:updated_by_id => 1,
|
|
|
|
:created_by_id => 1,
|
|
|
|
)
|
2015-04-05 23:43:19 +00:00
|
|
|
sleep 1
|
2015-04-05 23:05:33 +00:00
|
|
|
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,
|
|
|
|
)
|
2015-02-25 23:44:29 +00:00
|
|
|
|
|
|
|
# check tickets and search it
|
2015-04-05 23:05:33 +00:00
|
|
|
test 'a - tickets' do
|
2015-02-25 23:44:29 +00:00
|
|
|
|
|
|
|
ticket1 = Ticket.create(
|
|
|
|
:title => "some title\n äöüß",
|
|
|
|
:group => Group.lookup( :name => 'Users'),
|
2015-04-05 23:05:33 +00:00
|
|
|
:customer_id => customer1.id,
|
2015-02-25 23:44:29 +00:00
|
|
|
:state => Ticket::State.lookup( :name => 'new' ),
|
|
|
|
:priority => Ticket::Priority.lookup( :name => '2 normal' ),
|
|
|
|
:updated_by_id => 1,
|
|
|
|
:created_by_id => 1,
|
|
|
|
)
|
|
|
|
article = 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,
|
|
|
|
)
|
2015-04-06 19:00:16 +00:00
|
|
|
|
|
|
|
# simulate upload
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
form_id = '123456789'
|
|
|
|
|
|
|
|
# add attachments which should get index / .txt
|
|
|
|
# "some normal text"
|
|
|
|
Store.add(
|
2015-04-06 19:11:07 +00:00
|
|
|
:object => 'UploadCache',
|
|
|
|
:o_id => form_id,
|
|
|
|
:data => File.read("#{Rails.root.to_s}/test/fixtures/es-normal.txt"),
|
|
|
|
:filename => 'es-normal.txt',
|
|
|
|
:preferences => {},
|
|
|
|
:created_by_id => 1,
|
2015-04-06 19:00:16 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
# add attachments which should get index / .pdf
|
|
|
|
Store.add(
|
2015-04-06 19:11:07 +00:00
|
|
|
:object => 'UploadCache',
|
|
|
|
:o_id => form_id,
|
|
|
|
:data => File.read("#{Rails.root.to_s}/test/fixtures/test1.pdf"),
|
|
|
|
:filename => 'test1.pdf',
|
|
|
|
:preferences => {},
|
|
|
|
:created_by_id => 1,
|
2015-04-06 19:00:16 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
# add attachments which should get index / .box
|
|
|
|
# "Old programmers never die"
|
|
|
|
Store.add(
|
2015-04-06 19:11:07 +00:00
|
|
|
:object => 'UploadCache',
|
|
|
|
:o_id => form_id,
|
|
|
|
:data => File.read("#{Rails.root.to_s}/test/fixtures/es-box1.box"),
|
|
|
|
:filename => 'mail1.box',
|
|
|
|
:preferences => {},
|
|
|
|
:created_by_id => 1,
|
2015-04-06 19:00:16 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
# add to big attachment which should not get index
|
|
|
|
# "some too big text"
|
|
|
|
Store.add(
|
2015-04-06 19:11:07 +00:00
|
|
|
:object => 'UploadCache',
|
|
|
|
:o_id => form_id,
|
|
|
|
:data => File.read("#{Rails.root.to_s}/test/fixtures/es-too-big.txt"),
|
|
|
|
:filename => 'es-too-big.txt',
|
|
|
|
:preferences => {},
|
|
|
|
:created_by_id => 1,
|
2015-04-06 19:00:16 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
article.attachments = Store.list(
|
|
|
|
:object => 'UploadCache',
|
|
|
|
:o_id => form_id,
|
|
|
|
)
|
|
|
|
|
2015-04-05 23:46:05 +00:00
|
|
|
sleep 1
|
2015-02-25 23:44:29 +00:00
|
|
|
|
|
|
|
ticket2 = Ticket.create(
|
|
|
|
:title => "something else",
|
|
|
|
:group => Group.lookup( :name => 'Users'),
|
2015-04-05 23:05:33 +00:00
|
|
|
:customer_id => customer2.id,
|
2015-02-25 23:44:29 +00:00
|
|
|
:state => Ticket::State.lookup( :name => 'open' ),
|
|
|
|
:priority => Ticket::Priority.lookup( :name => '2 normal' ),
|
|
|
|
:updated_by_id => 1,
|
|
|
|
:created_by_id => 1,
|
|
|
|
)
|
|
|
|
article = Ticket::Article.create(
|
|
|
|
:ticket_id => ticket2.id,
|
|
|
|
:from => 'some_sender@example.org',
|
|
|
|
:to => 'some_recipient@example.org',
|
2015-02-26 00:12:12 +00:00
|
|
|
:subject => 'some subject2 / autobahn what else?',
|
2015-02-25 23:44:29 +00:00
|
|
|
:message_id => 'some@id',
|
2015-04-05 23:05:33 +00:00
|
|
|
:body => 'some other message <b>with s<u>t</u>rong text<b>',
|
|
|
|
:content_type => 'text/html',
|
2015-02-25 23:44:29 +00:00
|
|
|
: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,
|
|
|
|
)
|
2015-04-05 23:05:33 +00:00
|
|
|
|
2015-04-05 23:46:05 +00:00
|
|
|
sleep 1
|
2015-02-25 23:44:29 +00:00
|
|
|
|
|
|
|
ticket3 = Ticket.create(
|
|
|
|
:title => "something else",
|
2015-02-26 00:12:12 +00:00
|
|
|
:group => Group.lookup( :name => 'WithoutAccess'),
|
2015-04-05 23:05:33 +00:00
|
|
|
:customer_id => customer3.id,
|
2015-02-25 23:44:29 +00:00
|
|
|
:state => Ticket::State.lookup( :name => 'open' ),
|
|
|
|
:priority => Ticket::Priority.lookup( :name => '2 normal' ),
|
|
|
|
:updated_by_id => 1,
|
|
|
|
:created_by_id => 1,
|
|
|
|
)
|
|
|
|
article = Ticket::Article.create(
|
|
|
|
:ticket_id => ticket3.id,
|
|
|
|
:from => 'some_sender@example.org',
|
|
|
|
:to => 'some_recipient@example.org',
|
|
|
|
:subject => 'some subject3',
|
|
|
|
:message_id => 'some@id',
|
2015-02-26 00:12:12 +00:00
|
|
|
:body => 'some other message 3 / kindergarden what else?',
|
2015-02-25 23:44:29 +00:00
|
|
|
: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,
|
|
|
|
)
|
2015-04-06 06:13:05 +00:00
|
|
|
|
|
|
|
# execute background jobs
|
|
|
|
#puts Delayed::Job.all.inspect
|
|
|
|
Delayed::Worker.new.work_off
|
2015-02-25 23:44:29 +00:00
|
|
|
|
2015-04-05 23:43:19 +00:00
|
|
|
sleep 6
|
2015-04-05 23:05:33 +00:00
|
|
|
|
|
|
|
# search as agent
|
2015-02-25 23:44:29 +00:00
|
|
|
|
2015-04-05 23:05:33 +00:00
|
|
|
# search for article data
|
2015-02-25 23:44:29 +00:00
|
|
|
result = Ticket.search(
|
|
|
|
:current_user => agent,
|
2015-02-26 00:12:12 +00:00
|
|
|
:query => 'autobahn',
|
2015-02-25 23:44:29 +00:00
|
|
|
:limit => 15,
|
|
|
|
)
|
2015-02-26 00:12:12 +00:00
|
|
|
|
2015-04-05 23:05:33 +00:00
|
|
|
assert(!result.empty?, 'result exists not')
|
2015-02-26 00:12:12 +00:00
|
|
|
assert(result[0], 'record 1')
|
|
|
|
assert(!result[1], 'record 2')
|
|
|
|
assert_equal(result[0].id, ticket2.id)
|
|
|
|
|
2015-04-05 23:05:33 +00:00
|
|
|
# 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)
|
2015-02-26 00:12:12 +00:00
|
|
|
|
2015-04-05 23:05:33 +00:00
|
|
|
# search for indexed attachment
|
2015-04-06 19:00:16 +00:00
|
|
|
result = Ticket.search(
|
|
|
|
:current_user => agent,
|
|
|
|
:query => '"some normal text"',
|
|
|
|
:limit => 15,
|
|
|
|
)
|
|
|
|
assert(result[0], 'record 1')
|
|
|
|
assert_equal(result[0].id, ticket1.id)
|
|
|
|
|
|
|
|
result = Ticket.search(
|
|
|
|
:current_user => agent,
|
|
|
|
:query => '"otrs.org"',
|
|
|
|
:limit => 15,
|
|
|
|
)
|
|
|
|
assert(result[0], 'record 1')
|
|
|
|
assert_equal(result[0].id, ticket1.id)
|
|
|
|
|
2015-04-05 23:05:33 +00:00
|
|
|
|
|
|
|
# search for not indexed attachment
|
2015-04-06 19:00:16 +00:00
|
|
|
result = Ticket.search(
|
|
|
|
:current_user => agent,
|
|
|
|
:query => '"some too big text"',
|
|
|
|
:limit => 15,
|
|
|
|
)
|
|
|
|
assert(!result[0], 'record 1')
|
2015-04-05 23:05:33 +00:00
|
|
|
|
2015-04-06 19:00:16 +00:00
|
|
|
result = Ticket.search(
|
|
|
|
:current_user => agent,
|
|
|
|
:query => '"Old programmers never die"',
|
|
|
|
:limit => 15,
|
|
|
|
)
|
|
|
|
assert(!result[0], 'record 1')
|
2015-04-05 23:05:33 +00:00
|
|
|
|
|
|
|
|
|
|
|
# search for ticket with no permissions
|
2015-02-26 00:12:12 +00:00
|
|
|
result = Ticket.search(
|
|
|
|
:current_user => agent,
|
|
|
|
:query => 'kindergarden',
|
|
|
|
:limit => 15,
|
|
|
|
)
|
|
|
|
assert(result.empty?, 'result should be empty')
|
|
|
|
assert(!result[0], 'record 1')
|
|
|
|
|
2015-02-25 23:44:29 +00:00
|
|
|
|
2015-04-05 23:05:33 +00:00
|
|
|
# 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)
|
|
|
|
end
|
|
|
|
|
|
|
|
# check users and search it
|
|
|
|
test 'b - 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')
|
2015-04-05 23:35:07 +00:00
|
|
|
assert(!result[1], 'record 2')
|
2015-04-05 23:05:33 +00:00
|
|
|
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')
|
|
|
|
|
2015-02-25 23:44:29 +00:00
|
|
|
end
|
|
|
|
end
|