2021-06-01 12:20:20 +00:00
|
|
|
# Copyright (C) 2012-2021 Zammad Foundation, http://zammad-foundation.org/
|
|
|
|
|
2015-06-30 15:10:22 +00:00
|
|
|
require 'test_helper'
|
|
|
|
|
|
|
|
class ModelTest < ActiveSupport::TestCase
|
2015-07-28 13:19:54 +00:00
|
|
|
|
|
|
|
test 'create_if_not_exists test' do
|
|
|
|
group1 = Group.create_if_not_exists(
|
2018-12-19 17:31:51 +00:00
|
|
|
name: 'model1-create_if_not_exists',
|
|
|
|
active: true,
|
|
|
|
updated_at: '2015-02-05 16:37:00',
|
2015-07-28 13:19:54 +00:00
|
|
|
updated_by_id: 1,
|
|
|
|
created_by_id: 1,
|
|
|
|
)
|
2017-10-01 12:25:52 +00:00
|
|
|
assert_raises( ActiveRecord::RecordNotUnique ) do
|
2015-07-28 13:19:54 +00:00
|
|
|
Group.create_if_not_exists(
|
2018-12-19 17:31:51 +00:00
|
|
|
name: 'model1-Create_If_Not_Exists',
|
|
|
|
active: true,
|
|
|
|
updated_at: '2015-02-05 16:37:00',
|
2015-07-28 13:19:54 +00:00
|
|
|
updated_by_id: 1,
|
|
|
|
created_by_id: 1,
|
|
|
|
)
|
2017-10-01 12:25:52 +00:00
|
|
|
end
|
2015-07-28 13:19:54 +00:00
|
|
|
|
|
|
|
group2 = Group.create_if_not_exists(
|
2018-12-19 17:31:51 +00:00
|
|
|
name: 'model1-create_if_not_exists',
|
|
|
|
active: true,
|
|
|
|
updated_at: '2015-02-05 16:39:00',
|
2015-07-28 13:19:54 +00:00
|
|
|
updated_by_id: 1,
|
|
|
|
created_by_id: 1,
|
|
|
|
)
|
|
|
|
assert_equal(group1.id, group2.id)
|
|
|
|
assert_equal(group2.updated_at.to_s, '2015-02-05 16:37:00 UTC')
|
|
|
|
end
|
|
|
|
|
|
|
|
test 'create_or_update test' do
|
|
|
|
group1 = Group.create_or_update(
|
2018-12-19 17:31:51 +00:00
|
|
|
name: 'model1-create_or_update',
|
|
|
|
active: true,
|
|
|
|
updated_at: '2015-02-05 16:37:00',
|
2015-07-28 13:19:54 +00:00
|
|
|
updated_by_id: 1,
|
|
|
|
created_by_id: 1,
|
|
|
|
)
|
2017-10-01 12:25:52 +00:00
|
|
|
assert_raises( ActiveRecord::RecordNotUnique ) do
|
2015-07-28 13:19:54 +00:00
|
|
|
Group.create_or_update(
|
2018-12-19 17:31:51 +00:00
|
|
|
name: 'model1-Create_Or_Update',
|
|
|
|
active: true,
|
|
|
|
updated_at: '2015-02-05 16:37:00',
|
2015-07-28 13:19:54 +00:00
|
|
|
updated_by_id: 1,
|
|
|
|
created_by_id: 1,
|
|
|
|
)
|
2017-10-01 12:25:52 +00:00
|
|
|
end
|
2015-07-28 13:19:54 +00:00
|
|
|
|
|
|
|
group2 = Group.create_or_update(
|
2018-12-19 17:31:51 +00:00
|
|
|
name: 'model1-create_or_update',
|
|
|
|
active: true,
|
|
|
|
updated_at: '2015-02-05 16:39:00',
|
2015-07-28 13:19:54 +00:00
|
|
|
updated_by_id: 1,
|
|
|
|
created_by_id: 1,
|
|
|
|
)
|
|
|
|
assert_equal(group1.id, group2.id)
|
|
|
|
assert_equal(group2.updated_at.to_s, '2015-02-05 16:39:00 UTC')
|
|
|
|
end
|
|
|
|
|
2015-06-30 15:10:22 +00:00
|
|
|
test 'references test' do
|
|
|
|
|
2015-07-03 17:37:24 +00:00
|
|
|
# create base
|
2017-06-16 20:43:09 +00:00
|
|
|
groups = Group.where(name: 'Users')
|
2017-11-23 08:09:44 +00:00
|
|
|
roles = Role.where(name: %w[Agent Admin])
|
2015-07-03 15:37:27 +00:00
|
|
|
agent1 = User.create_or_update(
|
2018-12-19 17:31:51 +00:00
|
|
|
login: 'model-agent1@example.com',
|
|
|
|
firstname: 'Model',
|
|
|
|
lastname: 'Agent1',
|
|
|
|
email: 'model-agent1@example.com',
|
|
|
|
password: 'agentpw',
|
|
|
|
active: true,
|
|
|
|
roles: roles,
|
|
|
|
groups: groups,
|
|
|
|
updated_at: '2015-02-05 16:37:00',
|
2015-07-03 15:37:27 +00:00
|
|
|
updated_by_id: 1,
|
|
|
|
created_by_id: 1,
|
|
|
|
)
|
2015-07-06 00:33:43 +00:00
|
|
|
agent2 = User.create_or_update(
|
2018-12-19 17:31:51 +00:00
|
|
|
login: 'model-agent2@example.com',
|
|
|
|
firstname: 'Model',
|
|
|
|
lastname: 'Agent2',
|
|
|
|
email: 'model-agent2@example.com',
|
|
|
|
password: 'agentpw',
|
|
|
|
active: true,
|
|
|
|
roles: roles,
|
|
|
|
groups: groups,
|
|
|
|
updated_at: '2015-02-05 17:37:00',
|
2015-07-06 00:33:43 +00:00
|
|
|
updated_by_id: agent1.id,
|
|
|
|
created_by_id: 1,
|
|
|
|
)
|
2015-07-03 15:37:27 +00:00
|
|
|
organization1 = Organization.create_if_not_exists(
|
2018-12-19 17:31:51 +00:00
|
|
|
name: 'Model Org 1',
|
|
|
|
updated_at: '2015-02-05 16:37:00',
|
2015-07-03 15:37:27 +00:00
|
|
|
updated_by_id: 1,
|
|
|
|
created_by_id: 1,
|
|
|
|
)
|
|
|
|
organization2 = Organization.create_if_not_exists(
|
2018-12-19 17:31:51 +00:00
|
|
|
name: 'Model Org 2',
|
|
|
|
updated_at: '2015-02-05 16:37:00',
|
2015-07-03 15:37:27 +00:00
|
|
|
updated_by_id: agent1.id,
|
|
|
|
created_by_id: 1,
|
|
|
|
)
|
2019-06-28 11:38:49 +00:00
|
|
|
roles = Role.where(name: 'Customer')
|
|
|
|
User.create_or_update(
|
2018-12-19 17:31:51 +00:00
|
|
|
login: 'model-customer1@example.com',
|
|
|
|
firstname: 'Model',
|
|
|
|
lastname: 'Customer1',
|
|
|
|
email: 'model-customer1@example.com',
|
|
|
|
password: 'customerpw',
|
|
|
|
active: true,
|
2015-07-03 15:37:27 +00:00
|
|
|
organization_id: organization1.id,
|
2018-12-19 17:31:51 +00:00
|
|
|
roles: roles,
|
|
|
|
updated_at: '2015-02-05 16:37:00',
|
|
|
|
updated_by_id: 1,
|
|
|
|
created_by_id: 1,
|
2015-07-03 15:37:27 +00:00
|
|
|
)
|
2019-06-28 11:38:49 +00:00
|
|
|
User.create_or_update(
|
2018-12-19 17:31:51 +00:00
|
|
|
login: 'model-customer2@example.com',
|
|
|
|
firstname: 'Model',
|
|
|
|
lastname: 'Customer2',
|
|
|
|
email: 'model-customer2@example.com',
|
|
|
|
password: 'customerpw',
|
|
|
|
active: true,
|
2015-07-03 15:37:27 +00:00
|
|
|
organization_id: nil,
|
2018-12-19 17:31:51 +00:00
|
|
|
roles: roles,
|
|
|
|
updated_at: '2015-02-05 16:37:00',
|
|
|
|
updated_by_id: agent1.id,
|
|
|
|
created_by_id: 1,
|
2015-07-03 15:37:27 +00:00
|
|
|
)
|
2019-06-28 11:38:49 +00:00
|
|
|
User.create_or_update(
|
2018-12-19 17:31:51 +00:00
|
|
|
login: 'model-customer3@example.com',
|
|
|
|
firstname: 'Model',
|
|
|
|
lastname: 'Customer3',
|
|
|
|
email: 'model-customer3@example.com',
|
|
|
|
password: 'customerpw',
|
|
|
|
active: true,
|
2015-07-03 15:37:27 +00:00
|
|
|
organization_id: nil,
|
2018-12-19 17:31:51 +00:00
|
|
|
roles: roles,
|
|
|
|
updated_at: '2015-02-05 16:37:00',
|
|
|
|
updated_by_id: agent1.id,
|
|
|
|
created_by_id: agent1.id,
|
2015-07-03 15:37:27 +00:00
|
|
|
)
|
2015-06-30 15:10:22 +00:00
|
|
|
|
2015-07-06 00:33:43 +00:00
|
|
|
# user
|
|
|
|
|
|
|
|
# verify agent1
|
|
|
|
references1 = Models.references('User', agent1.id)
|
|
|
|
|
|
|
|
assert_equal(references1['User']['updated_by_id'], 3)
|
|
|
|
assert_equal(references1['User']['created_by_id'], 1)
|
|
|
|
assert_equal(references1['Organization']['updated_by_id'], 1)
|
2017-06-16 20:43:09 +00:00
|
|
|
assert_equal(references1['UserGroup']['user_id'], 1)
|
2018-10-09 06:17:41 +00:00
|
|
|
assert_not(references1['Group'])
|
2015-07-06 00:33:43 +00:00
|
|
|
|
|
|
|
references_total1 = Models.references_total('User', agent1.id)
|
2017-06-16 20:43:09 +00:00
|
|
|
assert_equal(references_total1, 8)
|
2015-07-06 00:33:43 +00:00
|
|
|
|
|
|
|
# verify agent2
|
|
|
|
references2 = Models.references('User', agent2.id)
|
|
|
|
|
2018-10-09 06:17:41 +00:00
|
|
|
assert_not(references2['User'])
|
|
|
|
assert_not(references2['Organization'])
|
|
|
|
assert_not(references2['Group'])
|
2017-06-16 20:43:09 +00:00
|
|
|
assert_equal(references2['UserGroup']['user_id'], 1)
|
2015-07-06 00:33:43 +00:00
|
|
|
|
|
|
|
references_total2 = Models.references_total('User', agent2.id)
|
2017-06-16 20:43:09 +00:00
|
|
|
assert_equal(references_total2, 1)
|
2015-07-06 00:33:43 +00:00
|
|
|
|
|
|
|
Models.merge('User', agent2.id, agent1.id)
|
|
|
|
|
|
|
|
# verify agent1
|
|
|
|
references1 = Models.references('User', agent1.id)
|
|
|
|
|
2018-10-09 06:17:41 +00:00
|
|
|
assert_not(references1['User'])
|
|
|
|
assert_not(references1['Organization'])
|
|
|
|
assert_not(references1['Group'])
|
|
|
|
assert_not(references1['UserGroup'])
|
2017-11-23 08:09:44 +00:00
|
|
|
assert(references1.blank?)
|
2015-07-06 00:33:43 +00:00
|
|
|
|
|
|
|
references_total1 = Models.references_total('User', agent1.id)
|
|
|
|
assert_equal(references_total1, 0)
|
|
|
|
|
|
|
|
# verify agent2
|
|
|
|
references2 = Models.references('User', agent2.id)
|
|
|
|
|
|
|
|
assert_equal(references2['User']['updated_by_id'], 3)
|
|
|
|
assert_equal(references2['User']['created_by_id'], 1)
|
|
|
|
assert_equal(references2['Organization']['updated_by_id'], 1)
|
2017-06-16 20:43:09 +00:00
|
|
|
assert_equal(references2['UserGroup']['user_id'], 2)
|
2018-10-09 06:17:41 +00:00
|
|
|
assert_not(references2['Group'])
|
2015-07-06 00:33:43 +00:00
|
|
|
|
|
|
|
references_total2 = Models.references_total('User', agent2.id)
|
2017-06-16 20:43:09 +00:00
|
|
|
assert_equal(references_total2, 9)
|
2015-07-06 00:33:43 +00:00
|
|
|
|
|
|
|
# org
|
|
|
|
|
|
|
|
# verify agent1
|
|
|
|
references1 = Models.references('Organization', organization1.id)
|
|
|
|
|
|
|
|
assert_equal(references1['User']['organization_id'], 1)
|
2018-10-09 06:17:41 +00:00
|
|
|
assert_not(references1['Organization'])
|
|
|
|
assert_not(references1['Group'])
|
2015-07-06 00:33:43 +00:00
|
|
|
|
|
|
|
references_total1 = Models.references_total('Organization', organization1.id)
|
|
|
|
assert_equal(references_total1, 1)
|
|
|
|
|
|
|
|
# verify agent2
|
|
|
|
references2 = Models.references('Organization', organization2.id)
|
|
|
|
|
2017-11-23 08:09:44 +00:00
|
|
|
assert(references2.blank?)
|
2015-07-06 00:33:43 +00:00
|
|
|
|
|
|
|
references_total2 = Models.references_total('Organization', organization2.id)
|
|
|
|
assert_equal(references_total2, 0)
|
|
|
|
|
|
|
|
Models.merge('Organization', organization2.id, organization1.id)
|
|
|
|
|
|
|
|
# verify agent1
|
|
|
|
references1 = Models.references('Organization', organization1.id)
|
|
|
|
|
2017-11-23 08:09:44 +00:00
|
|
|
assert(references1.blank?)
|
2015-07-06 00:33:43 +00:00
|
|
|
|
|
|
|
references_total1 = Models.references_total('Organization', organization1.id)
|
|
|
|
assert_equal(references_total1, 0)
|
|
|
|
|
|
|
|
# verify agent2
|
|
|
|
references2 = Models.references('Organization', organization2.id)
|
|
|
|
|
|
|
|
assert_equal(references2['User']['organization_id'], 1)
|
2018-10-09 06:17:41 +00:00
|
|
|
assert_not(references2['Organization'])
|
|
|
|
assert_not(references2['Group'])
|
2015-07-06 00:33:43 +00:00
|
|
|
|
|
|
|
references_total2 = Models.references_total('Organization', organization2.id)
|
|
|
|
assert_equal(references_total2, 1)
|
2015-06-30 15:10:22 +00:00
|
|
|
|
|
|
|
end
|
|
|
|
|
2015-08-16 09:14:24 +00:00
|
|
|
test 'searchable test' do
|
|
|
|
searchable = Models.searchable
|
|
|
|
assert(searchable.include?(Ticket))
|
|
|
|
assert(searchable.include?(User))
|
|
|
|
assert(searchable.include?(Organization))
|
2017-12-18 03:36:56 +00:00
|
|
|
assert(searchable.include?(Chat::Session))
|
2019-06-04 03:40:48 +00:00
|
|
|
assert(searchable.include?(KnowledgeBase::Answer::Translation))
|
|
|
|
assert_equal(5, searchable.count)
|
2015-08-16 09:14:24 +00:00
|
|
|
end
|
|
|
|
|
2018-01-30 00:19:23 +00:00
|
|
|
test 'param_cleanup test' do
|
|
|
|
params = {
|
2018-12-19 17:31:51 +00:00
|
|
|
id: 123,
|
|
|
|
abc: true,
|
|
|
|
firstname: '123',
|
2018-01-30 00:19:23 +00:00
|
|
|
created_by_id: 1,
|
2018-12-19 17:31:51 +00:00
|
|
|
created_at: Time.zone.now,
|
2018-01-30 00:19:23 +00:00
|
|
|
updated_by_id: 1,
|
2018-12-19 17:31:51 +00:00
|
|
|
updated_at: Time.zone.now,
|
|
|
|
action: 'some action',
|
|
|
|
controller: 'some controller',
|
2018-01-30 00:19:23 +00:00
|
|
|
}
|
|
|
|
result = User.param_cleanup(params, true)
|
|
|
|
assert_not(result.key?(:id))
|
|
|
|
assert_not(result.key?(:abc))
|
|
|
|
assert_equal('123', result[:firstname])
|
|
|
|
assert_not(result.key?(:created_by_id))
|
|
|
|
assert_not(result.key?(:created_at))
|
|
|
|
assert_not(result.key?(:updated_by_id))
|
|
|
|
assert_not(result.key?(:updated_at))
|
|
|
|
assert_not(result.key?(:action))
|
|
|
|
assert_not(result.key?(:controller))
|
|
|
|
|
|
|
|
params = {
|
2018-12-19 17:31:51 +00:00
|
|
|
id: 123,
|
|
|
|
abc: true,
|
|
|
|
firstname: '123',
|
2018-01-30 00:19:23 +00:00
|
|
|
created_by_id: 1,
|
2018-12-19 17:31:51 +00:00
|
|
|
created_at: Time.zone.now,
|
2018-01-30 00:19:23 +00:00
|
|
|
updated_by_id: 1,
|
2018-12-19 17:31:51 +00:00
|
|
|
updated_at: Time.zone.now,
|
|
|
|
action: 'some action',
|
|
|
|
controller: 'some controller',
|
2018-01-30 00:19:23 +00:00
|
|
|
}
|
|
|
|
result = User.param_cleanup(params)
|
|
|
|
assert_equal(123, result[:id])
|
|
|
|
assert_not(result.key?(:abc))
|
|
|
|
assert_equal('123', result[:firstname])
|
|
|
|
assert_not(result.key?(:created_by_id))
|
|
|
|
assert_not(result.key?(:created_at))
|
|
|
|
assert_not(result.key?(:updated_by_id))
|
|
|
|
assert_not(result.key?(:updated_at))
|
|
|
|
assert_not(result.key?(:action))
|
|
|
|
assert_not(result.key?(:controller))
|
|
|
|
|
|
|
|
Setting.set('import_mode', true)
|
|
|
|
|
|
|
|
params = {
|
2018-12-19 17:31:51 +00:00
|
|
|
id: 123,
|
|
|
|
abc: true,
|
|
|
|
firstname: '123',
|
2018-01-30 00:19:23 +00:00
|
|
|
created_by_id: 1,
|
2018-12-19 17:31:51 +00:00
|
|
|
created_at: Time.zone.now,
|
2018-01-30 00:19:23 +00:00
|
|
|
updated_by_id: 1,
|
2018-12-19 17:31:51 +00:00
|
|
|
updated_at: Time.zone.now,
|
|
|
|
action: 'some action',
|
|
|
|
controller: 'some controller',
|
2018-01-30 00:19:23 +00:00
|
|
|
}
|
|
|
|
result = User.param_cleanup(params, true)
|
|
|
|
assert_not(result.key?(:abc))
|
|
|
|
assert_equal('123', result[:firstname])
|
|
|
|
assert_equal(1, result[:created_by_id])
|
|
|
|
assert(result[:created_at])
|
|
|
|
assert_equal(1, result[:updated_by_id])
|
|
|
|
assert(result[:updated_at])
|
|
|
|
assert_not(result.key?(:action))
|
|
|
|
assert_not(result.key?(:controller))
|
|
|
|
|
|
|
|
params = {
|
2018-12-19 17:31:51 +00:00
|
|
|
id: 123,
|
|
|
|
abc: true,
|
|
|
|
firstname: '123',
|
2018-01-30 00:19:23 +00:00
|
|
|
created_by_id: 1,
|
2018-12-19 17:31:51 +00:00
|
|
|
created_at: Time.zone.now,
|
2018-01-30 00:19:23 +00:00
|
|
|
updated_by_id: 1,
|
2018-12-19 17:31:51 +00:00
|
|
|
updated_at: Time.zone.now,
|
|
|
|
action: 'some action',
|
|
|
|
controller: 'some controller',
|
2018-01-30 00:19:23 +00:00
|
|
|
}
|
|
|
|
result = User.param_cleanup(params)
|
|
|
|
assert_equal(123, result[:id])
|
|
|
|
assert_equal('123', result[:firstname])
|
|
|
|
assert_equal(1, result[:created_by_id])
|
|
|
|
assert(result[:created_at])
|
|
|
|
assert_equal(1, result[:updated_by_id])
|
|
|
|
assert(result[:updated_at])
|
|
|
|
assert_not(result.key?(:action))
|
|
|
|
assert_not(result.key?(:controller))
|
|
|
|
end
|
|
|
|
|
2018-07-26 14:24:31 +00:00
|
|
|
test 'param_preferences_merge test' do
|
|
|
|
params = {
|
2018-12-19 17:31:51 +00:00
|
|
|
id: 123,
|
|
|
|
firstname: '123',
|
2018-07-26 14:24:31 +00:00
|
|
|
created_by_id: 1,
|
2018-12-19 17:31:51 +00:00
|
|
|
created_at: Time.zone.now,
|
2018-07-26 14:24:31 +00:00
|
|
|
updated_by_id: 1,
|
2018-12-19 17:31:51 +00:00
|
|
|
updated_at: Time.zone.now,
|
|
|
|
preferences: {},
|
2018-07-26 14:24:31 +00:00
|
|
|
}
|
|
|
|
user = User.new(params)
|
|
|
|
assert(user.preferences.blank?)
|
|
|
|
|
|
|
|
user.preferences = { A: 1, B: 2 }
|
|
|
|
assert(user.preferences.present?)
|
|
|
|
|
|
|
|
params = {
|
2018-12-19 17:31:51 +00:00
|
|
|
firstname: '123 ABC',
|
2021-02-01 12:42:44 +00:00
|
|
|
preferences: { 'B' => 3, C: 4 },
|
2018-07-26 14:24:31 +00:00
|
|
|
}
|
|
|
|
clean_params = User.param_cleanup(params)
|
|
|
|
clean_user_params = user.param_preferences_merge(clean_params)
|
|
|
|
assert_equal(clean_user_params[:firstname], '123 ABC')
|
|
|
|
assert(clean_user_params[:preferences].present?)
|
|
|
|
assert_equal(clean_user_params[:preferences]['A'], 1)
|
|
|
|
assert_equal(clean_user_params[:preferences]['B'], 3)
|
|
|
|
assert_equal(clean_user_params[:preferences]['C'], 4)
|
|
|
|
assert_equal(clean_user_params[:preferences][:A], 1)
|
|
|
|
assert_equal(clean_user_params[:preferences][:B], 3)
|
|
|
|
assert_equal(clean_user_params[:preferences][:C], 4)
|
|
|
|
|
|
|
|
params = {
|
2018-12-19 17:31:51 +00:00
|
|
|
firstname: '123 ABCD',
|
2018-07-26 14:24:31 +00:00
|
|
|
preferences: {},
|
|
|
|
}
|
|
|
|
clean_params = User.param_cleanup(params)
|
|
|
|
clean_user_params = user.param_preferences_merge(clean_params)
|
|
|
|
assert_equal(clean_user_params[:firstname], '123 ABCD')
|
|
|
|
assert(clean_user_params[:preferences].present?)
|
|
|
|
assert_equal(clean_user_params[:preferences]['A'], 1)
|
|
|
|
assert_equal(clean_user_params[:preferences]['B'], 2)
|
|
|
|
assert_nil(clean_user_params[:preferences]['C'])
|
|
|
|
assert_equal(clean_user_params[:preferences][:A], 1)
|
|
|
|
assert_equal(clean_user_params[:preferences][:B], 2)
|
|
|
|
assert_nil(clean_user_params[:preferences][:C])
|
|
|
|
end
|
|
|
|
|
2015-06-30 15:10:22 +00:00
|
|
|
end
|