Added attachment tests.

This commit is contained in:
Martin Edenhofer 2015-04-06 21:00:16 +02:00
parent eac41c1ab7
commit de7d403d16
6 changed files with 153 additions and 30 deletions

View file

@ -20,9 +20,9 @@ returns
# default ignored attributes # default ignored attributes
ignore_attributes = { ignore_attributes = {
:created_by_id => true, :created_by_id => true,
:updated_by_id => true, :updated_by_id => true,
:active => true, :active => true,
} }
if self.class.search_index_support_config[:ignore_attributes] if self.class.search_index_support_config[:ignore_attributes]
self.class.search_index_support_config[:ignore_attributes].each {|key, value| self.class.search_index_support_config[:ignore_attributes].each {|key, value|
@ -51,11 +51,14 @@ returns
attributes = search_index_attribute_lookup( attributes, ticket ) attributes = search_index_attribute_lookup( attributes, ticket )
# list ignored file extentions # list ignored file extentions
ignore_attachments = [ '.png', '.jpg', '.jpeg', '.mpeg', '.mov' ] attachments_ignore = Setting.set('es_attachment_ignore') || [ '.png', '.jpg', '.jpeg', '.mpeg', '.mpg', '.mov', '.bin', '.exe' ]
# max attachment size
attachment_max_size_in_mb = Setting.set('es_attachment_max_size_in_mb') || 40
# collect article data # collect article data
articles = Ticket::Article.where( :ticket_id => self.id ) articles = Ticket::Article.where( :ticket_id => self.id )
attributes['articles_all'] = [] attributes['articles'] = []
articles.each {|article| articles.each {|article|
article_attributes = article.attributes article_attributes = article.attributes
@ -80,21 +83,23 @@ returns
end end
# check file size # check file size
if true
# check ignored files # check ignored files
if attachment.filename if attachment.filename
filename_extention = attachment.filename.downcase filename_extention = attachment.filename.downcase
filename_extention.gsub!(/^.*(\..+?)$/, "\\1") filename_extention.gsub!(/^.*(\..+?)$/, "\\1")
if !ignore_attachments.include?( filename_extention.downcase ) if !attachments_ignore.include?( filename_extention.downcase )
data = { data = {
"_name" => attachment.filename, '_name' => attachment.filename,
"content" => Base64.encode64( attachment.content ) 'content' => Base64.encode64( attachment.content )
} }
attributes['attachments'].push data attributes['attachments'].push data
end
end end
end end
} }
attributes['articles_all'].push article_attributes attributes['articles'].push article_attributes
} }
return if !attributes return if !attributes

View file

@ -12,7 +12,7 @@ create/update/delete index
:mappings => { :mappings => {
:Ticket => { :Ticket => {
:properties => { :properties => {
:articles_all => { :articles => {
:type => 'nested', :type => 'nested',
:properties => { :properties => {
'attachments' => { :type => 'attachment' } 'attachments' => { :type => 'attachment' }
@ -121,7 +121,8 @@ remove whole data from index
) )
#puts "# #{response.code.to_s}" #puts "# #{response.code.to_s}"
return true if response.success? return true if response.success?
puts "NOTICE: can't drop index: " + response.inspect #puts "NOTICE: can't drop index: " + response.inspect
false
end end
=begin =begin
@ -209,8 +210,8 @@ return search result
puts "# #{response.code.to_s}" puts "# #{response.code.to_s}"
if !response.success? if !response.success?
puts "ERROR: #{response.inspect}"
return [] return []
# raise data.inspect
end end
data = response.data data = response.data

17
test/fixtures/es-box1.box vendored Normal file
View file

@ -0,0 +1,17 @@
From: Martin Edenhofer <martin@example.com>
Content-Type: text/plain;
charset=iso-8859-1
Content-Transfer-Encoding: quoted-printable
Subject: =?iso-8859-1?Q?aa=E4=F6=FC=DFad_asd?=
X-Universally-Unique-Identifier: d12c15d2-e6d6-4ccd-86c7-abc2c3d0a2a2
Date: Fri, 4 May 2012 14:01:03 +0200
Message-Id: <BC182994-03FA-4DC5-8202-98CBFACA0887@example.com>
To: metest@znuny.com
Mime-Version: 1.0 (Apple Message framework v1257)
=E4=F6=FC=DF ad asd
-Martin
--
Old programmers never die. They just branch to a new address.

1
test/fixtures/es-normal.txt vendored Normal file
View file

@ -0,0 +1 @@
some normal text

18
test/fixtures/es-too-big.txt vendored Normal file

File diff suppressed because one or more lines are too long

View file

@ -4,15 +4,25 @@ require 'integration_test_helper'
class ElasticsearchTest < ActiveSupport::TestCase class ElasticsearchTest < ActiveSupport::TestCase
# set config # 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']
raise "ERROR: Need ES_INDEX - hint ES_INDEX='estest.local_zammad'"
end
Setting.set('es_index', ENV['ES_INDEX'])
# Setting.set('es_url', 'http://172.0.0.1:9200') # Setting.set('es_url', 'http://172.0.0.1:9200')
Setting.set('es_url', 'http://10.240.2.11:9200') # Setting.set('es_index', 'estest.local_zammad')
Setting.set('es_index', 'estest.local_zammad')
# Setting.set('es_user', 'elasticsearch') # Setting.set('es_user', 'elasticsearch')
# Setting.set('es_password', 'zammad') # Setting.set('es_password', 'zammad')
# set max attachment size # set max attachment size in mb
Setting.set('es_attachment_max_size_in_mb', 1 )
# set attachment types # set attachment types
Setting.set('es_attachment_ignore', [ '.png', '.jpg', '.jpeg', '.mpeg', '.mpg', '.mov', '.bin', '.exe' ] )
# drop/create indexes # drop/create indexes
#Rake::Task["searchindex:drop"].execute #Rake::Task["searchindex:drop"].execute
@ -108,6 +118,57 @@ class ElasticsearchTest < ActiveSupport::TestCase
:updated_by_id => 1, :updated_by_id => 1,
:created_by_id => 1, :created_by_id => 1,
) )
# simulate upload
form_id = '123456789'
# add attachments which should get index / .txt
# "some normal text"
Store.add(
:object => 'UploadCache',
:o_id => form_id,
:data => File.read("#{Rails.root.to_s}/test/fixtures/es-normal.txt"),
:filename => 'es-normal.txt',
:preferences => {},
)
# add attachments which should get index / .pdf
Store.add(
:object => 'UploadCache',
:o_id => form_id,
:data => File.read("#{Rails.root.to_s}/test/fixtures/test1.pdf"),
:filename => 'test1.pdf',
:preferences => {},
)
# add attachments which should get index / .box
# "Old programmers never die"
Store.add(
:object => 'UploadCache',
:o_id => form_id,
:data => File.read("#{Rails.root.to_s}/test/fixtures/es-box1.box"),
:filename => 'mail1.box',
:preferences => {},
)
# add to big attachment which should not get index
# "some too big text"
Store.add(
: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 => {},
)
article.attachments = Store.list(
:object => 'UploadCache',
:o_id => form_id,
)
sleep 1 sleep 1
ticket2 = Ticket.create( ticket2 = Ticket.create(
@ -134,14 +195,6 @@ class ElasticsearchTest < ActiveSupport::TestCase
:created_by_id => 1, :created_by_id => 1,
) )
# add attachments which should get index
# add attachments which should not get index
# add to big attachment which should not get index
sleep 1 sleep 1
ticket3 = Ticket.create( ticket3 = Ticket.create(
@ -200,9 +253,37 @@ class ElasticsearchTest < ActiveSupport::TestCase
assert_equal(result[0].id, ticket2.id) assert_equal(result[0].id, ticket2.id)
# search for indexed attachment # search for indexed attachment
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)
# search for not indexed attachment # search for not indexed attachment
result = Ticket.search(
:current_user => agent,
:query => '"some too big text"',
:limit => 15,
)
assert(!result[0], 'record 1')
result = Ticket.search(
:current_user => agent,
:query => '"Old programmers never die"',
:limit => 15,
)
assert(!result[0], 'record 1')
# search for ticket with no permissions # search for ticket with no permissions