2021-06-01 12:20:20 +00:00
|
|
|
# Copyright (C) 2012-2021 Zammad Foundation, http://zammad-foundation.org/
|
|
|
|
|
2016-06-18 14:54:26 +00:00
|
|
|
require 'test_helper'
|
|
|
|
|
|
|
|
class ObjectCreateUpdateWithRefNameTest < ActiveSupport::TestCase
|
|
|
|
test 'organization' do
|
2017-11-23 08:09:44 +00:00
|
|
|
roles = Role.where(name: %w[Agent Admin])
|
2016-06-18 14:54:26 +00:00
|
|
|
groups = Group.all
|
|
|
|
user1 = User.create_or_update(
|
2018-12-19 17:31:51 +00:00
|
|
|
login: 'object_ref_name1@example.org',
|
|
|
|
firstname: 'object_ref_name1',
|
|
|
|
lastname: 'object_ref_name1',
|
|
|
|
email: 'object_ref_name1@example.org',
|
|
|
|
password: 'some_pass',
|
|
|
|
active: true,
|
2016-06-18 14:54:26 +00:00
|
|
|
updated_by_id: 1,
|
|
|
|
created_by_id: 1,
|
2018-12-19 17:31:51 +00:00
|
|
|
roles: roles,
|
|
|
|
groups: groups,
|
2016-06-18 14:54:26 +00:00
|
|
|
)
|
|
|
|
user2 = User.create_or_update(
|
2018-12-19 17:31:51 +00:00
|
|
|
login: 'object_ref_name2@example.org',
|
|
|
|
firstname: 'object_ref_name2',
|
|
|
|
lastname: 'object_ref_name2',
|
|
|
|
email: 'object_ref_name2@example.org',
|
|
|
|
password: 'some_pass',
|
|
|
|
active: true,
|
2016-06-18 14:54:26 +00:00
|
|
|
updated_by_id: 1,
|
|
|
|
created_by_id: 1,
|
2018-12-19 17:31:51 +00:00
|
|
|
roles: roles,
|
|
|
|
groups: groups,
|
2016-06-18 14:54:26 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
org1 = Organization.create_if_not_exists_with_ref(
|
2018-12-19 17:31:51 +00:00
|
|
|
name: 'some org update_with_ref member',
|
|
|
|
members: ['object_ref_name1@example.org'],
|
2016-06-18 14:54:26 +00:00
|
|
|
updated_by_id: 1,
|
|
|
|
created_by_id: 1,
|
|
|
|
)
|
|
|
|
|
|
|
|
assert(org1.member_ids.sort.include?(user1.id))
|
|
|
|
assert_not(org1.member_ids.sort.include?(user2.id))
|
|
|
|
|
|
|
|
org2 = Organization.create_or_update_with_ref(
|
2018-12-19 17:31:51 +00:00
|
|
|
name: 'some org update_with_ref member',
|
|
|
|
members: ['object_ref_name2@example.org'],
|
2016-06-18 14:54:26 +00:00
|
|
|
updated_by_id: 1,
|
|
|
|
created_by_id: 1,
|
|
|
|
)
|
|
|
|
|
|
|
|
assert_not(org2.member_ids.sort.include?(user1.id))
|
|
|
|
assert(org2.member_ids.sort.include?(user2.id))
|
|
|
|
assert_equal(org1.id, org2.id)
|
|
|
|
|
|
|
|
org3 = Organization.create_or_update_with_ref(
|
2018-12-19 17:31:51 +00:00
|
|
|
name: 'some org update_with_ref member2',
|
|
|
|
members: ['object_ref_name2@example.org'],
|
2016-06-18 14:54:26 +00:00
|
|
|
updated_by_id: 1,
|
|
|
|
created_by_id: 1,
|
|
|
|
)
|
|
|
|
|
|
|
|
assert_not(org3.member_ids.sort.include?(user1.id))
|
|
|
|
assert(org3.member_ids.sort.include?(user2.id))
|
|
|
|
assert_not_equal(org2.id, org3.id)
|
|
|
|
|
2021-07-16 13:38:01 +00:00
|
|
|
assert_raises(ActiveRecord::AssociationTypeMismatch) do
|
2016-06-18 14:54:26 +00:00
|
|
|
Organization.create_or_update_with_ref(
|
2018-12-19 17:31:51 +00:00
|
|
|
name: 'some org update_with_ref member2',
|
|
|
|
members: ['object_ref_name2@example.org'],
|
|
|
|
member_ids: [2],
|
2016-06-18 14:54:26 +00:00
|
|
|
updated_by_id: 1,
|
|
|
|
created_by_id: 1,
|
|
|
|
)
|
2017-10-01 12:25:52 +00:00
|
|
|
end
|
2016-06-18 14:54:26 +00:00
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
test 'user' do
|
2019-06-28 11:38:49 +00:00
|
|
|
Organization.create_if_not_exists_with_ref(
|
2018-12-19 17:31:51 +00:00
|
|
|
name: 'some org update_with_ref user',
|
2016-06-18 14:54:26 +00:00
|
|
|
updated_by_id: 1,
|
|
|
|
created_by_id: 1,
|
|
|
|
)
|
|
|
|
user1 = User.create_or_update_with_ref(
|
2018-12-19 17:31:51 +00:00
|
|
|
login: 'object_ref_name1@example.org',
|
|
|
|
firstname: 'object_ref_name1',
|
|
|
|
lastname: 'object_ref_name1',
|
|
|
|
email: 'object_ref_name1@example.org',
|
|
|
|
password: 'some_pass',
|
|
|
|
active: true,
|
|
|
|
organization: 'some org update_with_ref user',
|
2016-06-18 14:54:26 +00:00
|
|
|
updated_by_id: 1,
|
|
|
|
created_by_id: 1,
|
2018-12-19 17:31:51 +00:00
|
|
|
roles: %w[Agent Admin],
|
|
|
|
groups: ['Users'],
|
2016-06-18 14:54:26 +00:00
|
|
|
)
|
|
|
|
user2 = User.create_or_update_with_ref(
|
2018-12-19 17:31:51 +00:00
|
|
|
login: 'object_ref_name2@example.org',
|
|
|
|
firstname: 'object_ref_name2',
|
|
|
|
lastname: 'object_ref_name2',
|
|
|
|
email: 'object_ref_name2@example.org',
|
|
|
|
password: 'some_pass',
|
2016-06-18 14:54:26 +00:00
|
|
|
organization_id: nil,
|
2018-12-19 17:31:51 +00:00
|
|
|
active: true,
|
|
|
|
updated_by_id: 1,
|
|
|
|
created_by_id: 1,
|
|
|
|
roles: ['Customer'],
|
|
|
|
groups: [],
|
2016-06-18 14:54:26 +00:00
|
|
|
)
|
|
|
|
admin_role = Role.lookup(name: 'Admin')
|
|
|
|
agent_role = Role.lookup(name: 'Agent')
|
|
|
|
customer_role = Role.lookup(name: 'Customer')
|
|
|
|
|
|
|
|
users_group = Group.lookup(name: 'Users')
|
|
|
|
|
|
|
|
assert(user1.organization.name, 'some org update_with_ref user')
|
|
|
|
assert(user1.group_ids.include?(users_group.id))
|
|
|
|
assert(user1.role_ids.include?(admin_role.id))
|
|
|
|
assert(user1.role_ids.include?(agent_role.id))
|
|
|
|
assert_not(user1.role_ids.include?(customer_role.id))
|
|
|
|
|
2017-03-24 10:02:13 +00:00
|
|
|
assert_nil(user2.organization_id)
|
2016-06-18 14:54:26 +00:00
|
|
|
assert_not(user2.group_ids.include?(users_group.id))
|
|
|
|
assert_not(user2.role_ids.include?(admin_role.id))
|
|
|
|
assert_not(user2.role_ids.include?(agent_role.id))
|
|
|
|
assert(user2.role_ids.include?(customer_role.id))
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
test 'group' do
|
|
|
|
user1 = User.create_or_update_with_ref(
|
2018-12-19 17:31:51 +00:00
|
|
|
login: 'object_ref_name1@example.org',
|
|
|
|
firstname: 'object_ref_name1',
|
|
|
|
lastname: 'object_ref_name1',
|
|
|
|
email: 'object_ref_name1@example.org',
|
|
|
|
password: 'some_pass',
|
|
|
|
active: true,
|
2016-06-18 14:54:26 +00:00
|
|
|
organization_id: nil,
|
2018-12-19 17:31:51 +00:00
|
|
|
updated_by_id: 1,
|
|
|
|
created_by_id: 1,
|
|
|
|
roles: %w[Agent Admin],
|
|
|
|
groups: [],
|
2016-06-18 14:54:26 +00:00
|
|
|
)
|
|
|
|
user2 = User.create_or_update_with_ref(
|
2018-12-19 17:31:51 +00:00
|
|
|
login: 'object_ref_name2@example.org',
|
|
|
|
firstname: 'object_ref_name2',
|
|
|
|
lastname: 'object_ref_name2',
|
|
|
|
email: 'object_ref_name2@example.org',
|
|
|
|
password: 'some_pass',
|
2016-06-18 14:54:26 +00:00
|
|
|
organization_id: nil,
|
2018-12-19 17:31:51 +00:00
|
|
|
active: true,
|
|
|
|
updated_by_id: 1,
|
|
|
|
created_by_id: 1,
|
|
|
|
roles: ['Customer'],
|
|
|
|
groups: [],
|
2016-06-18 14:54:26 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
group1 = Group.create_if_not_exists_with_ref(
|
2018-12-19 17:31:51 +00:00
|
|
|
name: 'some group update_with_ref',
|
|
|
|
users: ['object_ref_name1@example.org'],
|
2016-06-18 14:54:26 +00:00
|
|
|
updated_by_id: 1,
|
|
|
|
created_by_id: 1,
|
|
|
|
)
|
|
|
|
|
|
|
|
assert(group1.name, 'some group update_with_ref')
|
|
|
|
assert(group1.user_ids.include?(user1.id))
|
|
|
|
assert_not(group1.user_ids.include?(user2.id))
|
|
|
|
|
|
|
|
end
|
|
|
|
end
|