Added postgresql support.

This commit is contained in:
Martin Edenhofer 2016-01-19 23:30:23 +01:00
parent 7b8fef1991
commit a00be78195
13 changed files with 280 additions and 136 deletions

View file

@ -54,6 +54,7 @@ gem 'therubyracer'
# e. g. for mysql you need to load mysql # e. g. for mysql you need to load mysql
gem 'mysql2', '~> 0.3.20' gem 'mysql2', '~> 0.3.20'
gem 'pg'
gem 'net-ldap' gem 'net-ldap'

View file

@ -194,6 +194,7 @@ GEM
omniauth-oauth (~> 1.1) omniauth-oauth (~> 1.1)
parser (2.3.0.1) parser (2.3.0.1)
ast (~> 2.2) ast (~> 2.2)
pg (0.18.4)
pluginator (1.3.0) pluginator (1.3.0)
polyglot (0.3.5) polyglot (0.3.5)
power_assert (0.2.7) power_assert (0.2.7)
@ -355,6 +356,7 @@ DEPENDENCIES
omniauth-google-oauth2 omniauth-google-oauth2
omniauth-linkedin omniauth-linkedin
omniauth-twitter omniauth-twitter
pg
pre-commit pre-commit
puma puma
rack-livereload rack-livereload

View file

@ -303,11 +303,12 @@ returns
=begin =begin
lookup model from cache (if exists) or retrieve it from db, id, name or login possible lookup model from cache (if exists) or retrieve it from db, id, name, login or email possible
result = Model.lookup(id: 123) result = Model.lookup(id: 123)
result = Model.lookup(name: 'some name') result = Model.lookup(name: 'some name')
result = Model.lookup(login: 'some login') result = Model.lookup(login: 'some login')
result = Model.lookup(email: 'some login')
returns returns
@ -328,7 +329,11 @@ returns
return cache if cache return cache if cache
# do lookup with == to handle case insensitive databases # do lookup with == to handle case insensitive databases
records = where(name: data[:name]) records = if Rails.application.config.db_case_sensitive
where('LOWER(name) = LOWER(?)', data[:name])
else
where(name: data[:name])
end
records.each {|loop_record| records.each {|loop_record|
if loop_record.name == data[:name] if loop_record.name == data[:name]
cache_set(data[:name], loop_record) cache_set(data[:name], loop_record)
@ -341,17 +346,38 @@ returns
return cache if cache return cache if cache
# do lookup with == to handle case insensitive databases # do lookup with == to handle case insensitive databases
records = where(login: data[:login]) records = if Rails.application.config.db_case_sensitive
where('LOWER(login) = LOWER(?)', data[:login])
else
where(login: data[:login])
end
records.each {|loop_record| records.each {|loop_record|
if loop_record.login == data[:login] if loop_record.login == data[:login]
cache_set( data[:login], loop_record) cache_set(data[:login], loop_record)
return loop_record
end
}
return
elsif data[:email]
cache = cache_get(data[:email])
return cache if cache
# do lookup with == to handle case insensitive databases
records = if Rails.application.config.db_case_sensitive
where('LOWER(email) = LOWER(?)', data[:email])
else
where(email: data[:email])
end
records.each {|loop_record|
if loop_record.email == data[:email]
cache_set(data[:email], loop_record)
return loop_record return loop_record
end end
} }
return return
end end
fail 'Need name, id or login for lookup()' fail 'Need name, id, login or email for lookup()'
end end
=begin =begin
@ -373,28 +399,44 @@ returns
elsif data[:name] elsif data[:name]
# do lookup with == to handle case insensitive databases # do lookup with == to handle case insensitive databases
records = where(name: data[:name]) records = if Rails.application.config.db_case_sensitive
where('LOWER(name) = LOWER(?)', data[:name])
else
where(name: data[:name])
end
records.each {|loop_record| records.each {|loop_record|
return loop_record if loop_record.name == data[:name] return loop_record if loop_record.name == data[:name]
} }
elsif data[:login] elsif data[:login]
# do lookup with == to handle case insensitive databases # do lookup with == to handle case insensitive databases
records = where(login: data[:login]) records = if Rails.application.config.db_case_sensitive
where('LOWER(login) = LOWER(?)', data[:login])
else
where(login: data[:login])
end
records.each {|loop_record| records.each {|loop_record|
return loop_record if loop_record.login == data[:login] return loop_record if loop_record.login == data[:login]
} }
elsif data[:email] elsif data[:email]
# do lookup with == to handle case insensitive databases # do lookup with == to handle case insensitive databases
records = where(email: data[:email]) records = if Rails.application.config.db_case_sensitive
where('LOWER(email) = LOWER(?)', data[:email])
else
where(email: data[:email])
end
records.each {|loop_record| records.each {|loop_record|
return loop_record if loop_record.email == data[:email] return loop_record if loop_record.email == data[:email]
} }
elsif data[:locale] && data[:source] elsif data[:locale] && data[:source]
# do lookup with == to handle case insensitive databases # do lookup with == to handle case insensitive databases
records = where(locale: data[:locale], source: data[:source]) records = if Rails.application.config.db_case_sensitive
where('LOWER(locale) = LOWER(?) AND LOWER(source) = LOWER(?)', data[:locale], data[:source])
else
where(locale: data[:locale], source: data[:source])
end
records.each {|loop_record| records.each {|loop_record|
return loop_record if loop_record.source == data[:source] return loop_record if loop_record.source == data[:source]
} }
@ -427,7 +469,11 @@ returns
elsif data[:name] elsif data[:name]
# do lookup with == to handle case insensitive databases # do lookup with == to handle case insensitive databases
records = where(name: data[:name]) records = if Rails.application.config.db_case_sensitive
where('LOWER(name) = LOWER(?)', data[:name])
else
where(name: data[:name])
end
records.each {|loop_record| records.each {|loop_record|
if loop_record.name == data[:name] if loop_record.name == data[:name]
loop_record.update_attributes(data) loop_record.update_attributes(data)
@ -440,7 +486,11 @@ returns
elsif data[:login] elsif data[:login]
# do lookup with == to handle case insensitive databases # do lookup with == to handle case insensitive databases
records = where(login: data[:login]) records = if Rails.application.config.db_case_sensitive
where('LOWER(login) = LOWER(?)', data[:login])
else
where(login: data[:login])
end
records.each {|loop_record| records.each {|loop_record|
if loop_record.login.casecmp(data[:login]).zero? if loop_record.login.casecmp(data[:login]).zero?
loop_record.update_attributes(data) loop_record.update_attributes(data)
@ -453,7 +503,11 @@ returns
elsif data[:email] elsif data[:email]
# do lookup with == to handle case insensitive databases # do lookup with == to handle case insensitive databases
records = where(email: data[:email]) records = if Rails.application.config.db_case_sensitive
where('LOWER(email) = LOWER(?)', data[:email])
else
where(email: data[:email])
end
records.each {|loop_record| records.each {|loop_record|
if loop_record.email.casecmp(data[:email]).zero? if loop_record.email.casecmp(data[:email]).zero?
loop_record.update_attributes(data) loop_record.update_attributes(data)
@ -466,7 +520,11 @@ returns
elsif data[:locale] elsif data[:locale]
# do lookup with == to handle case insensitive databases # do lookup with == to handle case insensitive databases
records = where(locale: data[:locale]) records = if Rails.application.config.db_case_sensitive
where('LOWER(locale) = LOWER(?)', data[:locale])
else
where(locale: data[:locale])
end
records.each {|loop_record| records.each {|loop_record|
if loop_record.locale.casecmp(data[:locale]).zero? if loop_record.locale.casecmp(data[:locale]).zero?
loop_record.update_attributes(data) loop_record.update_attributes(data)

View file

@ -346,6 +346,7 @@ condition example
# remember query and bind params # remember query and bind params
query = '' query = ''
bind_params = [] bind_params = []
like = Rails.application.config.db_like
# get tables to join # get tables to join
tables = '' tables = ''
@ -437,11 +438,11 @@ condition example
bind_params.push selector['value'] bind_params.push selector['value']
end end
elsif selector['operator'] == 'contains' elsif selector['operator'] == 'contains'
query += "#{attribute} LIKE (?)" query += "#{attribute} #{like} (?)"
value = "%#{selector['value']}%" value = "%#{selector['value']}%"
bind_params.push value bind_params.push value
elsif selector['operator'] == 'contains not' elsif selector['operator'] == 'contains not'
query += "#{attribute} NOT LIKE (?)" query += "#{attribute} NOT #{like} (?)"
value = "%#{selector['value']}%" value = "%#{selector['value']}%"
bind_params.push value bind_params.push value
elsif selector['operator'] == 'before (absolute)' elsif selector['operator'] == 'before (absolute)'

View file

@ -0,0 +1,17 @@
# set database preferences
# defaults
Rails.application.config.db_case_sensitive = false
Rails.application.config.db_like = 'LIKE'
Rails.application.config.db_4bytes_utf8 = true
# postgresql
if ActiveRecord::Base.connection_config[:adapter] == 'postgresql'
Rails.application.config.db_case_sensitive = true
Rails.application.config.db_like = 'ILIKE'
end
# mysql
if ActiveRecord::Base.connection_config[:adapter] == 'mysql2'
Rails.application.config.db_4bytes_utf8 = false
end

View file

@ -3352,6 +3352,13 @@ Scheduler.create_or_update(
created_by_id: 1, created_by_id: 1,
) )
# reset primary key sequences
if ActiveRecord::Base.connection_config[:adapter] == 'postgresql'
ActiveRecord::Base.connection.tables.each do |t|
ActiveRecord::Base.connection.reset_pk_sequence!(t)
end
end
# install locales and translations # install locales and translations
Locale.create_if_not_exists( Locale.create_if_not_exists(
locale: 'en-us', locale: 'en-us',

View file

@ -0,0 +1,31 @@
require 'active_record/connection_adapters/postgresql/schema_statements'
module ActiveRecord
module ConnectionAdapters
module PostgreSQL
module SchemaStatements
# on postgres create lower indexes to support case insensetive wherer conditions
def add_index(table_name, column_name, options = {}) #:nodoc:
index_name, index_type, index_columns, index_options, index_algorithm, index_using = add_index_options(table_name, column_name, options)
column_names = index_columns.split ', '
if column_names.class == Array
index_columns_new = []
column_names.each {|i|
if i =~ /^"(name|login|locale|alias)"$/ || i =~ /name"$/
index_columns_new.push "LOWER(#{i})"
else
index_columns_new.push i
end
}
index_columns = index_columns_new.join ', '
end
execute "CREATE #{index_type} INDEX #{index_algorithm} #{quote_column_name(index_name)} ON #{quote_table_name(table_name)} #{index_using} (#{index_columns})#{index_options}"
end
end
end
end
end

View file

@ -57,7 +57,7 @@ class String
# unfortunaly UTF8mb4 will raise other limitaions of max varchar and lower index sizes # unfortunaly UTF8mb4 will raise other limitaions of max varchar and lower index sizes
# More details: http://pjambet.github.io/blog/emojis-and-mysql/ # More details: http://pjambet.github.io/blog/emojis-and-mysql/
def utf8_to_3bytesutf8 def utf8_to_3bytesutf8
return self if ActiveRecord::Base.connection_config[:adapter] != 'mysql2' return self if Rails.application.config.db_4bytes_utf8
each_char.select {|c| each_char.select {|c|
if c.bytes.count > 3 if c.bytes.count > 3
Rails.logger.warn "strip out 4 bytes utf8 chars '#{c}' of '#{self}'" Rails.logger.warn "strip out 4 bytes utf8 chars '#{c}' of '#{self}'"

View file

@ -1,7 +1,8 @@
# encoding: utf-8 # encoding: utf-8
# inital data set as extention to db/seeds.rb # inital data set as extention to db/seeds.rb
# create email address and apply it to all groups
email_address = EmailAddress.create_if_not_exists( email_address = EmailAddress.create_if_not_exists(
id: 1,
realname: 'Zammad', realname: 'Zammad',
email: 'zammad@localhost', email: 'zammad@localhost',
updated_by_id: 1, updated_by_id: 1,

View file

@ -2,8 +2,8 @@
require 'test_helper' require 'test_helper'
class ActivityStreamTest < ActiveSupport::TestCase class ActivityStreamTest < ActiveSupport::TestCase
role = Role.lookup( name: 'Admin' ) role = Role.lookup(name: 'Admin')
group = Group.lookup( name: 'Users' ) group = Group.lookup(name: 'Users')
admin_user = User.create_or_update( admin_user = User.create_or_update(
login: 'admin', login: 'admin',
firstname: 'Bob', firstname: 'Bob',
@ -16,7 +16,7 @@ class ActivityStreamTest < ActiveSupport::TestCase
updated_by_id: 1, updated_by_id: 1,
created_by_id: 1 created_by_id: 1
) )
current_user = User.lookup( login: 'nicole.braun@zammad.org' ) current_user = User.lookup(email: 'nicole.braun@zammad.org')
test 'ticket+user' do test 'ticket+user' do
tests = [ tests = [
@ -25,20 +25,20 @@ class ActivityStreamTest < ActiveSupport::TestCase
{ {
create: { create: {
ticket: { ticket: {
group_id: Group.lookup( name: 'Users' ).id, group_id: Group.lookup(name: 'Users').id,
customer_id: current_user.id, customer_id: current_user.id,
owner_id: User.lookup( login: '-' ).id, owner_id: User.lookup(login: '-').id,
title: 'Unit Test 1 (äöüß)!', title: 'Unit Test 1 (äöüß)!',
state_id: Ticket::State.lookup( name: 'new' ).id, state_id: Ticket::State.lookup(name: 'new').id,
priority_id: Ticket::Priority.lookup( name: '2 normal' ).id, priority_id: Ticket::Priority.lookup(name: '2 normal').id,
updated_by_id: current_user.id, updated_by_id: current_user.id,
created_by_id: current_user.id, created_by_id: current_user.id,
}, },
article: { article: {
updated_by_id: current_user.id, updated_by_id: current_user.id,
created_by_id: current_user.id, created_by_id: current_user.id,
type_id: Ticket::Article::Type.lookup( name: 'phone' ).id, type_id: Ticket::Article::Type.lookup(name: 'phone').id,
sender_id: Ticket::Article::Sender.lookup( name: 'Customer' ).id, sender_id: Ticket::Article::Sender.lookup(name: 'Customer').id,
from: 'Unit Test <unittest@example.com>', from: 'Unit Test <unittest@example.com>',
body: 'Unit Test 123', body: 'Unit Test 123',
internal: false, internal: false,
@ -47,14 +47,14 @@ class ActivityStreamTest < ActiveSupport::TestCase
update: { update: {
ticket: { ticket: {
title: 'Unit Test 1 (äöüß) - update!', title: 'Unit Test 1 (äöüß) - update!',
state_id: Ticket::State.lookup( name: 'open' ).id, state_id: Ticket::State.lookup(name: 'open').id,
priority_id: Ticket::Priority.lookup( name: '1 low' ).id, priority_id: Ticket::Priority.lookup(name: '1 low').id,
}, },
}, },
update2: { update2: {
ticket: { ticket: {
title: 'Unit Test 2 (äöüß) - update!', title: 'Unit Test 2 (äöüß) - update!',
priority_id: Ticket::Priority.lookup( name: '2 normal' ).id, priority_id: Ticket::Priority.lookup(name: '2 normal').id,
}, },
}, },
check: [ check: [
@ -85,7 +85,7 @@ class ActivityStreamTest < ActiveSupport::TestCase
tickets = [] tickets = []
tests.each { |test| tests.each { |test|
ticket = Ticket.create( test[:create][:ticket] ) ticket = Ticket.create(test[:create][:ticket])
test[:check][0][:o_id] = ticket.id test[:check][0][:o_id] = ticket.id
test[:check][2][:o_id] = ticket.id test[:check][2][:o_id] = ticket.id
test[:check][2][:created_at] = ticket.created_at test[:check][2][:created_at] = ticket.created_at
@ -93,17 +93,17 @@ class ActivityStreamTest < ActiveSupport::TestCase
sleep 2 sleep 2
test[:create][:article][:ticket_id] = ticket.id test[:create][:article][:ticket_id] = ticket.id
article = Ticket::Article.create( test[:create][:article] ) article = Ticket::Article.create(test[:create][:article])
test[:check][1][:o_id] = article.id test[:check][1][:o_id] = article.id
test[:check][1][:created_at] = article.created_at test[:check][1][:created_at] = article.created_at
test[:check][1][:created_by_id] = current_user.id test[:check][1][:created_by_id] = current_user.id
assert_equal( ticket.class.to_s, 'Ticket' ) assert_equal(ticket.class.to_s, 'Ticket')
assert_equal( article.class.to_s, 'Ticket::Article' ) assert_equal(article.class.to_s, 'Ticket::Article')
# update ticket # update ticket
if test[:update][:ticket] if test[:update][:ticket]
ticket.update_attributes( test[:update][:ticket] ) ticket.update_attributes(test[:update][:ticket])
# check updated user # check updated user
test[:check][3][:o_id] = current_user.id test[:check][3][:o_id] = current_user.id
@ -111,34 +111,34 @@ class ActivityStreamTest < ActiveSupport::TestCase
test[:check][3][:created_by_id] = current_user.id test[:check][3][:created_by_id] = current_user.id
end end
if test[:update2][:ticket] if test[:update2][:ticket]
ticket = Ticket.find( ticket.id ) ticket = Ticket.find(ticket.id)
ticket.update_attributes( test[:update2][:ticket] ) ticket.update_attributes(test[:update2][:ticket])
end end
if test[:update][:article] if test[:update][:article]
article.update_attributes( test[:update][:article] ) article.update_attributes(test[:update][:article])
end end
sleep 15 sleep 15
if test[:update][:ticket] if test[:update][:ticket]
ticket.update_attributes( test[:update][:ticket] ) ticket.update_attributes(test[:update][:ticket])
end end
if test[:update2][:ticket] if test[:update2][:ticket]
ticket.update_attributes( test[:update2][:ticket] ) ticket.update_attributes(test[:update2][:ticket])
end end
# remember ticket # remember ticket
tickets.push ticket tickets.push ticket
# check activity_stream # check activity_stream
activity_stream_check( admin_user.activity_stream(3), test[:check] ) activity_stream_check(admin_user.activity_stream(3), test[:check])
} }
# delete tickets # delete tickets
tickets.each { |ticket| tickets.each { |ticket|
ticket_id = ticket.id ticket_id = ticket.id
ticket.destroy ticket.destroy
found = Ticket.where( id: ticket_id ).first found = Ticket.where(id: ticket_id).first
assert( !found, 'Ticket destroyed') assert_not(found, 'Ticket destroyed')
} }
end end
@ -181,16 +181,16 @@ class ActivityStreamTest < ActiveSupport::TestCase
organizations = [] organizations = []
tests.each { |test| tests.each { |test|
organization = Organization.create( test[:create][:organization] ) organization = Organization.create(test[:create][:organization])
test[:check][0][:o_id] = organization.id test[:check][0][:o_id] = organization.id
test[:check][0][:created_at] = organization.created_at test[:check][0][:created_at] = organization.created_at
test[:check][0][:created_by_id] = current_user.id test[:check][0][:created_by_id] = current_user.id
sleep 2 sleep 2
assert_equal( organization.class.to_s, 'Organization' ) assert_equal(organization.class.to_s, 'Organization')
if test[:update1][:organization] if test[:update1][:organization]
organization.update_attributes( test[:update1][:organization] ) organization.update_attributes(test[:update1][:organization])
test[:check][1][:o_id] = organization.id test[:check][1][:o_id] = organization.id
test[:check][1][:updated_at] = organization.updated_at test[:check][1][:updated_at] = organization.updated_at
test[:check][1][:created_by_id] = current_user.id test[:check][1][:created_by_id] = current_user.id
@ -198,21 +198,21 @@ class ActivityStreamTest < ActiveSupport::TestCase
end end
if test[:update2][:organization] if test[:update2][:organization]
organization.update_attributes( test[:update2][:organization] ) organization.update_attributes(test[:update2][:organization])
end end
# remember organization # remember organization
organizations.push organization organizations.push organization
# check activity_stream # check activity_stream
activity_stream_check( admin_user.activity_stream(2), test[:check] ) activity_stream_check(admin_user.activity_stream(2), test[:check])
} }
# delete tickets # delete tickets
organizations.each { |organization| organizations.each { |organization|
organization_id = organization.id organization_id = organization.id
organization.destroy organization.destroy
found = Organization.where( id: organization_id ).first found = Organization.where(id: organization_id).first
assert( !found, 'Organization destroyed') assert( !found, 'Organization destroyed')
} }
end end
@ -253,15 +253,15 @@ class ActivityStreamTest < ActiveSupport::TestCase
users = [] users = []
tests.each { |test| tests.each { |test|
user = User.create( test[:create][:user] ) user = User.create(test[:create][:user])
test[:check][0][:o_id] = user.id test[:check][0][:o_id] = user.id
test[:check][0][:created_at] = user.created_at test[:check][0][:created_at] = user.created_at
test[:check][0][:created_by_id] = current_user.id test[:check][0][:created_by_id] = current_user.id
assert_equal( user.class.to_s, 'User' ) assert_equal(user.class.to_s, 'User')
if test[:update1][:user] if test[:update1][:user]
user.update_attributes( test[:update1][:user] ) user.update_attributes(test[:update1][:user])
test[:check][1][:o_id] = user.id test[:check][1][:o_id] = user.id
test[:check][1][:updated_at] = user.updated_at test[:check][1][:updated_at] = user.updated_at
test[:check][1][:created_by_id] = current_user.id test[:check][1][:created_by_id] = current_user.id
@ -271,7 +271,7 @@ class ActivityStreamTest < ActiveSupport::TestCase
users.push user users.push user
# check activity_stream # check activity_stream
activity_stream_check( admin_user.activity_stream(3), test[:check] ) activity_stream_check(admin_user.activity_stream(3), test[:check])
} }
# delete tickets # delete tickets
@ -279,7 +279,7 @@ class ActivityStreamTest < ActiveSupport::TestCase
user_id = user.id user_id = user.id
user.destroy user.destroy
found = User.where( id: user_id ).first found = User.where( id: user_id ).first
assert( !found, 'User destroyed') assert_not(found, 'User destroyed')
} }
end end
@ -325,15 +325,15 @@ class ActivityStreamTest < ActiveSupport::TestCase
users = [] users = []
tests.each { |test| tests.each { |test|
user = User.create( test[:create][:user] ) user = User.create(test[:create][:user])
test[:check][0][:o_id] = user.id test[:check][0][:o_id] = user.id
test[:check][0][:created_at] = user.created_at test[:check][0][:created_at] = user.created_at
test[:check][0][:created_by_id] = current_user.id test[:check][0][:created_by_id] = current_user.id
assert_equal( user.class.to_s, 'User' ) assert_equal(user.class.to_s, 'User')
if test[:update1][:user] if test[:update1][:user]
user.update_attributes( test[:update1][:user] ) user.update_attributes(test[:update1][:user])
test[:check][1][:o_id] = user.id test[:check][1][:o_id] = user.id
test[:check][1][:updated_at] = user.updated_at test[:check][1][:updated_at] = user.updated_at
test[:check][1][:created_by_id] = current_user.id test[:check][1][:created_by_id] = current_user.id
@ -343,26 +343,26 @@ class ActivityStreamTest < ActiveSupport::TestCase
sleep 14 sleep 14
if test[:update2][:user] if test[:update2][:user]
user.update_attributes( test[:update2][:user] ) user.update_attributes(test[:update2][:user])
end end
# remember organization # remember organization
users.push user users.push user
# check activity_stream # check activity_stream
activity_stream_check( admin_user.activity_stream(2), test[:check] ) activity_stream_check(admin_user.activity_stream(2), test[:check])
} }
# delete tickets # delete tickets
users.each { |user| users.each { |user|
user_id = user.id user_id = user.id
user.destroy user.destroy
found = User.where( id: user_id ).first found = User.where(id: user_id).first
assert( !found, 'User destroyed') assert(!found, 'User destroyed')
} }
end end
def activity_stream_check( activity_stream_list, checks ) def activity_stream_check(activity_stream_list, checks)
#activity_stream_list = activity_stream_list.reverse #activity_stream_list = activity_stream_list.reverse
#puts 'AS ' + activity_stream_list.inspect #puts 'AS ' + activity_stream_list.inspect
check_count = 0 check_count = 0
@ -380,11 +380,11 @@ class ActivityStreamTest < ActiveSupport::TestCase
#puts item.inspect #puts item.inspect
#puts check_item.inspect #puts check_item.inspect
if check_item[:result] if check_item[:result]
assert_equal( check_item[:object], item['object'] ) assert_equal(check_item[:object], item['object'])
assert_equal( check_item[:type], item['type'] ) assert_equal(check_item[:type], item['type'])
assert_equal( check_item[:o_id], item['o_id'] ) assert_equal(check_item[:o_id], item['o_id'])
elsif check_item[:object] == item['object'] && check_item[:type] == item['type'] && check_item[:o_id] == item['o_id'] elsif check_item[:object] == item['object'] && check_item[:type] == item['type'] && check_item[:o_id] == item['o_id']
assert( false, "entry should not exist #{item['object']}/#{item['type']}/#{item['o_id']}" ) assert(false, "entry should not exist #{item['object']}/#{item['type']}/#{item['o_id']}")
end end
} }
} }

View file

@ -32,15 +32,16 @@ Setting.create_or_update(
frontend: false frontend: false
) )
user = User.lookup( login: 'nicole.braun@zammad.org' ) user = User.lookup(email: 'nicole.braun@zammad.org')
if user if user
user.update_attributes( user.update_attributes(
login: 'nicole.braun',
password: 'some_pass', password: 'some_pass',
active: true, active: true,
) )
else else
User.create_if_not_exists( User.create_if_not_exists(
login: 'nicole.braun@zammad.org', login: 'nicole.braun',
firstname: 'Nicole', firstname: 'Nicole',
lastname: 'Braun', lastname: 'Braun',
email: 'nicole.braun@zammad.org', email: 'nicole.braun@zammad.org',
@ -63,6 +64,42 @@ class AuthTest < ActiveSupport::TestCase
}, },
# test 2 # test 2
{
username: 'nicole.braun@zammad.org',
password: 'some_pass',
result: true,
verify: {
firstname: 'Nicole',
lastname: 'Braun',
email: 'nicole.braun@zammad.org',
}
},
# test 3
{
username: 'nicole.bRaUn@zammad.org',
password: 'some_pass',
result: true,
verify: {
firstname: 'Nicole',
lastname: 'Braun',
email: 'nicole.braun@zammad.org',
}
},
# test 4
{
username: 'nicole.bRaUn',
password: 'some_pass',
result: true,
verify: {
firstname: 'Nicole',
lastname: 'Braun',
email: 'nicole.braun@zammad.org',
}
},
# test 5
{ {
username: 'paige.chen@example.org', username: 'paige.chen@example.org',
password: 'password', password: 'password',
@ -74,30 +111,19 @@ class AuthTest < ActiveSupport::TestCase
} }
}, },
# test 3
{
username: 'nicole.braun@zammad.org',
password: 'some_pass',
result: true,
verify: {
firstname: 'Nicole',
lastname: 'Braun',
email: 'nicole.braun@zammad.org',
}
},
] ]
tests.each { |test| tests.each { |test|
user = User.authenticate( test[:username], test[:password] ) user = User.authenticate(test[:username], test[:password])
if test[:result] == true if test[:result] == true
if !user if !user
assert( false, 'auth faild' ) assert(false, 'auth faild')
else else
test[:verify].each {|key, value| test[:verify].each {|key, value|
assert_equal( user[key], value, 'verify' ) assert_equal(user[key], value, 'verify')
} }
end end
else else
assert_equal( test[:result], user, 'faild or not existing' ) assert_equal(test[:result], user, 'faild or not existing')
end end
} }
end end

View file

@ -2,7 +2,7 @@
require 'test_helper' require 'test_helper'
class HistoryTest < ActiveSupport::TestCase class HistoryTest < ActiveSupport::TestCase
current_user = User.lookup( login: 'nicole.braun@zammad.org' ) current_user = User.lookup(email: 'nicole.braun@zammad.org')
test 'ticket' do test 'ticket' do
tests = [ tests = [
@ -11,20 +11,20 @@ class HistoryTest < ActiveSupport::TestCase
{ {
ticket_create: { ticket_create: {
ticket: { ticket: {
group_id: Group.lookup( name: 'Users' ).id, group_id: Group.lookup(name: 'Users').id,
customer_id: current_user.id, customer_id: current_user.id,
owner_id: User.lookup( login: '-' ).id, owner_id: User.lookup(login: '-').id,
title: 'Unit Test 1 (äöüß)!', title: 'Unit Test 1 (äöüß)!',
state_id: Ticket::State.lookup( name: 'new' ).id, state_id: Ticket::State.lookup(name: 'new').id,
priority_id: Ticket::Priority.lookup( name: '2 normal' ).id, priority_id: Ticket::Priority.lookup(name: '2 normal').id,
updated_by_id: current_user.id, updated_by_id: current_user.id,
created_by_id: current_user.id, created_by_id: current_user.id,
}, },
article: { article: {
updated_by_id: current_user.id, updated_by_id: current_user.id,
created_by_id: current_user.id, created_by_id: current_user.id,
type_id: Ticket::Article::Type.lookup( name: 'phone' ).id, type_id: Ticket::Article::Type.lookup(name: 'phone').id,
sender_id: Ticket::Article::Sender.lookup( name: 'Customer' ).id, sender_id: Ticket::Article::Sender.lookup(name: 'Customer').id,
from: 'Unit Test <unittest@example.com>', from: 'Unit Test <unittest@example.com>',
body: 'Unit Test 123', body: 'Unit Test 123',
internal: false, internal: false,
@ -33,8 +33,8 @@ class HistoryTest < ActiveSupport::TestCase
ticket_update: { ticket_update: {
ticket: { ticket: {
title: 'Unit Test 1 (äöüß) - update!', title: 'Unit Test 1 (äöüß) - update!',
state_id: Ticket::State.lookup( name: 'open' ).id, state_id: Ticket::State.lookup(name: 'open').id,
priority_id: Ticket::Priority.lookup( name: '1 low' ).id, priority_id: Ticket::Priority.lookup(name: '1 low').id,
}, },
}, },
history_check: [ history_check: [
@ -58,8 +58,8 @@ class HistoryTest < ActiveSupport::TestCase
history_attribute: 'state', history_attribute: 'state',
value_from: 'new', value_from: 'new',
value_to: 'open', value_to: 'open',
id_from: Ticket::State.lookup( name: 'new' ).id, id_from: Ticket::State.lookup(name: 'new').id,
id_to: Ticket::State.lookup( name: 'open' ).id, id_to: Ticket::State.lookup(name: 'open').id,
}, },
{ {
result: true, result: true,
@ -78,20 +78,20 @@ class HistoryTest < ActiveSupport::TestCase
{ {
ticket_create: { ticket_create: {
ticket: { ticket: {
group_id: Group.lookup( name: 'Users' ).id, group_id: Group.lookup(name: 'Users').id,
customer_id: current_user.id, customer_id: current_user.id,
owner_id: User.lookup( login: '-' ).id, owner_id: User.lookup(login: '-').id,
title: 'Unit Test 2 (äöüß)!', title: 'Unit Test 2 (äöüß)!',
state_id: Ticket::State.lookup( name: 'new' ).id, state_id: Ticket::State.lookup(name: 'new').id,
priority_id: Ticket::Priority.lookup( name: '2 normal' ).id, priority_id: Ticket::Priority.lookup(name: '2 normal').id,
updated_by_id: current_user.id, updated_by_id: current_user.id,
created_by_id: current_user.id, created_by_id: current_user.id,
}, },
article: { article: {
created_by_id: current_user.id, created_by_id: current_user.id,
updated_by_id: current_user.id, updated_by_id: current_user.id,
type_id: Ticket::Article::Type.lookup(name: 'phone' ).id, type_id: Ticket::Article::Type.lookup(name: 'phone').id,
sender_id: Ticket::Article::Sender.lookup(name: 'Customer' ).id, sender_id: Ticket::Article::Sender.lookup(name: 'Customer').id,
from: 'Unit Test <unittest@example.com>', from: 'Unit Test <unittest@example.com>',
body: 'Unit Test 123', body: 'Unit Test 123',
internal: false, internal: false,
@ -100,7 +100,7 @@ class HistoryTest < ActiveSupport::TestCase
ticket_update: { ticket_update: {
ticket: { ticket: {
title: 'Unit Test 2 (äöüß) - update!', title: 'Unit Test 2 (äöüß) - update!',
state_id: Ticket::State.lookup( name: 'open' ).id, state_id: Ticket::State.lookup(name: 'open').id,
owner_id: current_user.id, owner_id: current_user.id,
}, },
article: { article: {
@ -128,7 +128,7 @@ class HistoryTest < ActiveSupport::TestCase
history_attribute: 'owner', history_attribute: 'owner',
value_from: '-', value_from: '-',
value_to: 'Nicole Braun', value_to: 'Nicole Braun',
id_from: User.lookup( login: '-' ).id, id_from: User.lookup(login: '-').id,
id_to: current_user.id, id_to: current_user.id,
}, },
{ {
@ -155,19 +155,19 @@ class HistoryTest < ActiveSupport::TestCase
# use transaction # use transaction
ActiveRecord::Base.transaction do ActiveRecord::Base.transaction do
ticket = Ticket.create( test[:ticket_create][:ticket]) ticket = Ticket.create(test[:ticket_create][:ticket])
test[:ticket_create][:article][:ticket_id] = ticket.id test[:ticket_create][:article][:ticket_id] = ticket.id
article = Ticket::Article.create( test[:ticket_create][:article] ) article = Ticket::Article.create(test[:ticket_create][:article])
assert_equal( ticket.class.to_s, 'Ticket' ) assert_equal(ticket.class.to_s, 'Ticket')
assert_equal( article.class.to_s, 'Ticket::Article' ) assert_equal(article.class.to_s, 'Ticket::Article')
# update ticket # update ticket
if test[:ticket_update][:ticket] if test[:ticket_update][:ticket]
ticket.update_attributes( test[:ticket_update][:ticket] ) ticket.update_attributes(test[:ticket_update][:ticket])
end end
if test[:ticket_update][:article] if test[:ticket_update][:article]
article.update_attributes( test[:ticket_update][:article] ) article.update_attributes(test[:ticket_update][:article])
end end
end end
@ -181,15 +181,15 @@ class HistoryTest < ActiveSupport::TestCase
tickets.push ticket tickets.push ticket
# check history # check history
history_check( ticket.history_get, test[:history_check] ) history_check(ticket.history_get, test[:history_check])
} }
# delete tickets # delete tickets
tickets.each { |ticket| tickets.each { |ticket|
ticket_id = ticket.id ticket_id = ticket.id
ticket.destroy ticket.destroy
found = Ticket.where( id: ticket_id ).first found = Ticket.where(id: ticket_id).first
assert( !found, 'Ticket destroyed') assert_not(found, 'Ticket destroyed')
} }
end end
@ -258,14 +258,14 @@ class HistoryTest < ActiveSupport::TestCase
# user transaction # user transaction
ActiveRecord::Base.transaction do ActiveRecord::Base.transaction do
user = User.create( test[:user_create][:user]) user = User.create(test[:user_create][:user])
assert_equal( user.class.to_s, 'User' ) assert_equal(user.class.to_s, 'User')
# update user # update user
if test[:user_update][:user] if test[:user_update][:user]
test[:user_update][:user][:active] = false test[:user_update][:user][:active] = false
user.update_attributes( test[:user_update][:user] ) user.update_attributes(test[:user_update][:user])
end end
end end
@ -273,15 +273,15 @@ class HistoryTest < ActiveSupport::TestCase
users.push user users.push user
# check history # check history
history_check( user.history_get, test[:history_check] ) history_check(user.history_get, test[:history_check])
} }
# delete user # delete user
users.each { |user| users.each { |user|
user_id = user.id user_id = user.id
user.destroy user.destroy
found = User.where( id: user_id ).first found = User.where(id: user_id).first
assert( !found, 'User destroyed') assert_not(found, 'User destroyed')
} }
end end
@ -328,13 +328,13 @@ class HistoryTest < ActiveSupport::TestCase
# user transaction # user transaction
ActiveRecord::Base.transaction do ActiveRecord::Base.transaction do
organization = Organization.create( test[:organization_create][:organization]) organization = Organization.create(test[:organization_create][:organization])
assert_equal( organization.class.to_s, 'Organization' ) assert_equal(organization.class.to_s, 'Organization')
# update organization # update organization
if test[:organization_update][:organization] if test[:organization_update][:organization]
organization.update_attributes( test[:organization_update][:organization] ) organization.update_attributes(test[:organization_update][:organization])
end end
end end
@ -342,19 +342,19 @@ class HistoryTest < ActiveSupport::TestCase
organizations.push organization organizations.push organization
# check history # check history
history_check( organization.history_get, test[:history_check] ) history_check(organization.history_get, test[:history_check])
} }
# delete user # delete user
organizations.each { |organization| organizations.each { |organization|
organization_id = organization.id organization_id = organization.id
organization.destroy organization.destroy
found = Organization.where( id: organization_id ).first found = Organization.where(id: organization_id).first
assert( !found, 'Organization destroyed') assert_not(found, 'Organization destroyed')
} }
end end
def history_check( history_list, history_check ) def history_check(history_list, history_check)
history_check.each { |check_item| history_check.each { |check_item|
match = false match = false
history_list.each { |history_item| history_list.each { |history_item|
@ -366,28 +366,28 @@ class HistoryTest < ActiveSupport::TestCase
end end
match = true match = true
if history_item['type'] == check_item[:history_type] if history_item['type'] == check_item[:history_type]
assert( true, "History type #{history_item['type']} found!") assert(true, "History type #{history_item['type']} found!")
end end
if check_item[:history_attribute] if check_item[:history_attribute]
assert_equal( check_item[:history_attribute], history_item['attribute'], "check history attribute #{check_item[:history_attribute]}") assert_equal(check_item[:history_attribute], history_item['attribute'], "check history attribute #{check_item[:history_attribute]}")
end end
if check_item[:value_from] if check_item[:value_from]
assert_equal( check_item[:value_from], history_item['value_from'], "check history :value_from #{history_item['value_from']} ok") assert_equal(check_item[:value_from], history_item['value_from'], "check history :value_from #{history_item['value_from']} ok")
end end
if check_item[:value_to] if check_item[:value_to]
assert_equal( check_item[:value_to], history_item['value_to'], "check history :value_to #{history_item['value_to']} ok") assert_equal(check_item[:value_to], history_item['value_to'], "check history :value_to #{history_item['value_to']} ok")
end end
if check_item[:id_from] if check_item[:id_from]
assert_equal( check_item[:id_from], history_item['id_from'], "check history :id_from #{history_item['id_from']} ok") assert_equal(check_item[:id_from], history_item['id_from'], "check history :id_from #{history_item['id_from']} ok")
end end
if check_item[:id_to] if check_item[:id_to]
assert_equal( check_item[:id_to], history_item['id_to'], "check history :id_to #{history_item['id_to']} ok") assert_equal(check_item[:id_to], history_item['id_to'], "check history :id_to #{history_item['id_to']} ok")
end end
} }
if check_item[:result] if check_item[:result]
assert( match, "history check not matched! #{check_item.inspect}") assert(match, "history check not matched! #{check_item.inspect}")
else else
assert( !match, "history check matched but should not! #{check_item.inspect}") assert_not(match, "history check matched but should not! #{check_item.inspect}")
end end
} }
end end

View file

@ -2,8 +2,8 @@
require 'test_helper' require 'test_helper'
class OnlineNotificationTest < ActiveSupport::TestCase class OnlineNotificationTest < ActiveSupport::TestCase
role = Role.lookup( name: 'Agent' ) role = Role.lookup(name: 'Agent')
group = Group.lookup( name: 'Users' ) group = Group.lookup(name: 'Users')
agent_user1 = User.create_or_update( agent_user1 = User.create_or_update(
login: 'agent_online_notify1', login: 'agent_online_notify1',
firstname: 'Bob', firstname: 'Bob',
@ -28,7 +28,7 @@ class OnlineNotificationTest < ActiveSupport::TestCase
updated_by_id: 1, updated_by_id: 1,
created_by_id: 1 created_by_id: 1
) )
customer_user = User.lookup( login: 'nicole.braun@zammad.org' ) customer_user = User.lookup(email: 'nicole.braun@zammad.org')
test 'ticket notification' do test 'ticket notification' do
tests = [ tests = [
@ -407,7 +407,7 @@ class OnlineNotificationTest < ActiveSupport::TestCase
end end
def notification_check( online_notifications, checks ) def notification_check(online_notifications, checks)
checks.each { |check_item| checks.each { |check_item|
hit = false hit = false
online_notifications.each {|onine_notification| online_notifications.each {|onine_notification|
@ -426,7 +426,7 @@ class OnlineNotificationTest < ActiveSupport::TestCase
} }
end end
def notification_seen_only_exists_exists( online_notifications ) def notification_seen_only_exists_exists(online_notifications)
online_notifications.each {|onine_notification| online_notifications.each {|onine_notification|
return false if !onine_notification['seen'] return false if !onine_notification['seen']
} }