2012-10-25 22:12:16 +00:00
|
|
|
# encoding: utf-8
|
|
|
|
require 'test_helper'
|
2014-07-27 11:40:42 +00:00
|
|
|
|
2012-10-25 22:12:16 +00:00
|
|
|
class UserTest < ActiveSupport::TestCase
|
|
|
|
test 'user' do
|
|
|
|
tests = [
|
|
|
|
{
|
2015-04-27 13:42:53 +00:00
|
|
|
name: '#1 - simple create',
|
|
|
|
create: {
|
|
|
|
firstname: 'Firstname',
|
|
|
|
lastname: 'Lastname',
|
|
|
|
email: 'some@example.com',
|
|
|
|
login: 'some@example.com',
|
|
|
|
updated_by_id: 1,
|
|
|
|
created_by_id: 1,
|
2012-10-25 22:12:16 +00:00
|
|
|
},
|
2015-04-27 13:42:53 +00:00
|
|
|
create_verify: {
|
|
|
|
firstname: 'Firstname',
|
|
|
|
lastname: 'Lastname',
|
|
|
|
image: nil,
|
|
|
|
fullname: 'Firstname Lastname',
|
|
|
|
email: 'some@example.com',
|
|
|
|
login: 'some@example.com',
|
2012-10-25 22:12:16 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2015-04-27 13:42:53 +00:00
|
|
|
name: '#2 - simple create - no lastname',
|
|
|
|
create: {
|
|
|
|
firstname: 'Firstname Lastname',
|
|
|
|
lastname: '',
|
|
|
|
email: 'some@example.com',
|
|
|
|
login: 'some@example.com',
|
|
|
|
updated_by_id: 1,
|
|
|
|
created_by_id: 1,
|
2012-10-25 22:12:16 +00:00
|
|
|
},
|
2015-04-27 13:42:53 +00:00
|
|
|
create_verify: {
|
|
|
|
firstname: 'Firstname',
|
|
|
|
lastname: 'Lastname',
|
|
|
|
image: nil,
|
|
|
|
email: 'some@example.com',
|
|
|
|
login: 'some@example.com',
|
2012-10-25 22:12:16 +00:00
|
|
|
},
|
|
|
|
},
|
2012-11-02 07:14:59 +00:00
|
|
|
{
|
2015-04-27 13:42:53 +00:00
|
|
|
name: '#3 - simple create - nil as lastname',
|
|
|
|
create: {
|
|
|
|
firstname: 'Firstname Lastname',
|
|
|
|
lastname: '',
|
|
|
|
email: 'some@example.com',
|
|
|
|
login: 'some@example.com',
|
|
|
|
updated_by_id: 1,
|
|
|
|
created_by_id: 1,
|
2012-11-02 07:14:59 +00:00
|
|
|
},
|
2015-04-27 13:42:53 +00:00
|
|
|
create_verify: {
|
|
|
|
firstname: 'Firstname',
|
|
|
|
lastname: 'Lastname',
|
|
|
|
image: nil,
|
|
|
|
email: 'some@example.com',
|
|
|
|
login: 'some@example.com',
|
2012-11-02 07:14:59 +00:00
|
|
|
},
|
|
|
|
},
|
2012-10-25 22:12:16 +00:00
|
|
|
{
|
2015-04-27 13:42:53 +00:00
|
|
|
name: '#4 - simple create - no lastname, firstname with ","',
|
|
|
|
create: {
|
|
|
|
firstname: 'Lastname, Firstname',
|
|
|
|
lastname: '',
|
|
|
|
email: 'some@example.com',
|
|
|
|
login: 'some@example.com',
|
|
|
|
updated_by_id: 1,
|
|
|
|
created_by_id: 1,
|
2012-10-25 22:12:16 +00:00
|
|
|
},
|
2015-04-27 13:42:53 +00:00
|
|
|
create_verify: {
|
|
|
|
firstname: 'Firstname',
|
|
|
|
lastname: 'Lastname',
|
|
|
|
email: 'some@example.com',
|
|
|
|
login: 'some@example.com',
|
2012-10-25 22:12:16 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2015-04-27 13:42:53 +00:00
|
|
|
name: '#5 - simple create - no lastname/firstname',
|
|
|
|
create: {
|
|
|
|
firstname: '',
|
|
|
|
lastname: '',
|
|
|
|
email: 'firstname.lastname@example.com',
|
|
|
|
login: 'login-1',
|
|
|
|
updated_by_id: 1,
|
|
|
|
created_by_id: 1,
|
2012-10-25 22:12:16 +00:00
|
|
|
},
|
2015-04-27 13:42:53 +00:00
|
|
|
create_verify: {
|
|
|
|
firstname: 'Firstname',
|
|
|
|
lastname: 'Lastname',
|
|
|
|
fullname: 'Firstname Lastname',
|
|
|
|
email: 'firstname.lastname@example.com',
|
|
|
|
login: 'login-1',
|
2012-10-25 22:12:16 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2015-04-27 13:42:53 +00:00
|
|
|
name: '#6 - simple create - no lastname/firstnam',
|
|
|
|
create: {
|
|
|
|
firstname: '',
|
|
|
|
lastname: '',
|
|
|
|
email: 'FIRSTNAME.lastname@example.com',
|
|
|
|
login: 'login-2',
|
|
|
|
updated_by_id: 1,
|
|
|
|
created_by_id: 1,
|
2012-10-25 22:12:16 +00:00
|
|
|
},
|
2015-04-27 13:42:53 +00:00
|
|
|
create_verify: {
|
|
|
|
firstname: 'Firstname',
|
|
|
|
lastname: 'Lastname',
|
|
|
|
email: 'firstname.lastname@example.com',
|
|
|
|
login: 'login-2',
|
2012-10-25 22:12:16 +00:00
|
|
|
},
|
|
|
|
},
|
2012-11-02 07:14:59 +00:00
|
|
|
{
|
2015-04-27 13:42:53 +00:00
|
|
|
name: '#7 - simple create - nill as fristname and lastname',
|
|
|
|
create: {
|
|
|
|
firstname: '',
|
|
|
|
lastname: '',
|
|
|
|
email: 'FIRSTNAME.lastname@example.com',
|
|
|
|
login: 'login-3',
|
|
|
|
updated_by_id: 1,
|
|
|
|
created_by_id: 1,
|
2012-11-02 07:14:59 +00:00
|
|
|
},
|
2015-04-27 13:42:53 +00:00
|
|
|
create_verify: {
|
|
|
|
firstname: 'Firstname',
|
|
|
|
lastname: 'Lastname',
|
|
|
|
email: 'firstname.lastname@example.com',
|
|
|
|
login: 'login-3',
|
2014-07-27 11:40:42 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2015-04-27 13:42:53 +00:00
|
|
|
name: '#8 - update with avatar check',
|
|
|
|
create: {
|
|
|
|
firstname: 'Bob',
|
|
|
|
lastname: 'Smith',
|
|
|
|
email: 'bob.smith@example.com',
|
|
|
|
login: 'login-4',
|
|
|
|
updated_by_id: 1,
|
|
|
|
created_by_id: 1,
|
2014-07-27 11:40:42 +00:00
|
|
|
},
|
2015-04-27 13:42:53 +00:00
|
|
|
create_verify: {
|
|
|
|
firstname: 'Bob',
|
|
|
|
lastname: 'Smith',
|
|
|
|
image: nil,
|
|
|
|
email: 'bob.smith@example.com',
|
|
|
|
login: 'login-4',
|
2014-07-27 11:40:42 +00:00
|
|
|
},
|
2015-04-27 13:42:53 +00:00
|
|
|
update: {
|
|
|
|
email: 'unit-test1@znuny.com',
|
2014-07-27 11:40:42 +00:00
|
|
|
},
|
2015-04-27 13:42:53 +00:00
|
|
|
update_verify: {
|
|
|
|
firstname: 'Bob',
|
|
|
|
lastname: 'Smith',
|
|
|
|
image: 'a6f7f7f9dac25b2c023d403ef998801c',
|
|
|
|
image_md5: 'a6f7f7f9dac25b2c023d403ef998801c',
|
|
|
|
email: 'unit-test1@znuny.com',
|
|
|
|
login: 'login-4',
|
2014-07-27 11:40:42 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
{
|
2015-04-27 13:42:53 +00:00
|
|
|
name: '#9 - update create with avatar check',
|
|
|
|
create: {
|
|
|
|
firstname: 'Bob',
|
|
|
|
lastname: 'Smith',
|
|
|
|
email: 'unit-test2@znuny.com',
|
|
|
|
login: 'login-5',
|
|
|
|
updated_by_id: 1,
|
|
|
|
created_by_id: 1,
|
2012-11-02 07:14:59 +00:00
|
|
|
},
|
2015-04-27 13:42:53 +00:00
|
|
|
create_verify: {
|
|
|
|
firstname: 'Bob',
|
|
|
|
lastname: 'Smith',
|
|
|
|
image: '8765a1ac93f54405d8dfdd856c48c31f',
|
|
|
|
image_md5: '8765a1ac93f54405d8dfdd856c48c31f',
|
|
|
|
email: 'unit-test2@znuny.com',
|
|
|
|
login: 'login-5',
|
2014-07-27 11:40:42 +00:00
|
|
|
},
|
2015-04-27 13:42:53 +00:00
|
|
|
update: {
|
|
|
|
email: 'unit-test1@znuny.com',
|
2014-07-27 11:40:42 +00:00
|
|
|
},
|
2015-04-27 13:42:53 +00:00
|
|
|
update_verify: {
|
|
|
|
firstname: 'Bob',
|
|
|
|
lastname: 'Smith',
|
|
|
|
image: 'a6f7f7f9dac25b2c023d403ef998801c',
|
|
|
|
image_md5: 'a6f7f7f9dac25b2c023d403ef998801c',
|
|
|
|
email: 'unit-test1@znuny.com',
|
|
|
|
login: 'login-5',
|
2014-07-27 11:40:42 +00:00
|
|
|
}
|
2012-11-02 07:14:59 +00:00
|
|
|
},
|
2015-01-07 20:42:12 +00:00
|
|
|
{
|
2015-04-27 13:42:53 +00:00
|
|
|
name: '#10 - update create with login/email check',
|
|
|
|
create: {
|
|
|
|
firstname: '',
|
|
|
|
lastname: '',
|
|
|
|
email: 'caoyaoewfzfw@21222cn.com',
|
|
|
|
updated_by_id: 1,
|
|
|
|
created_by_id: 1,
|
2015-01-07 20:42:12 +00:00
|
|
|
},
|
2015-04-27 13:42:53 +00:00
|
|
|
create_verify: {
|
|
|
|
firstname: '',
|
|
|
|
lastname: '',
|
|
|
|
fullname: 'caoyaoewfzfw@21222cn.com',
|
|
|
|
email: 'caoyaoewfzfw@21222cn.com',
|
|
|
|
login: 'caoyaoewfzfw@21222cn.com',
|
2015-01-07 20:42:12 +00:00
|
|
|
},
|
2015-04-27 13:42:53 +00:00
|
|
|
update: {
|
|
|
|
email: 'caoyaoewfzfw@212224cn.com',
|
2015-01-07 20:42:12 +00:00
|
|
|
},
|
2015-04-27 13:42:53 +00:00
|
|
|
update_verify: {
|
|
|
|
firstname: '',
|
|
|
|
lastname: '',
|
|
|
|
email: 'caoyaoewfzfw@212224cn.com',
|
|
|
|
fullname: 'caoyaoewfzfw@212224cn.com',
|
|
|
|
login: 'caoyaoewfzfw@212224cn.com',
|
2015-01-07 20:42:12 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
{
|
2015-04-27 13:42:53 +00:00
|
|
|
name: '#11 - update create with login/email check',
|
|
|
|
create: {
|
|
|
|
firstname: 'Firstname',
|
|
|
|
lastname: 'Lastname',
|
|
|
|
email: 'some_tEst11@example.com',
|
|
|
|
updated_by_id: 1,
|
|
|
|
created_by_id: 1,
|
2015-01-07 20:42:12 +00:00
|
|
|
},
|
2015-04-27 13:42:53 +00:00
|
|
|
create_verify: {
|
|
|
|
firstname: 'Firstname',
|
|
|
|
lastname: 'Lastname',
|
|
|
|
fullname: 'Firstname Lastname',
|
|
|
|
email: 'some_test11@example.com',
|
2015-01-07 20:42:12 +00:00
|
|
|
},
|
2015-04-27 13:42:53 +00:00
|
|
|
update: {
|
|
|
|
email: 'some_Test11-1@example.com',
|
2015-01-07 20:42:12 +00:00
|
|
|
},
|
2015-04-27 13:42:53 +00:00
|
|
|
update_verify: {
|
|
|
|
firstname: 'Firstname',
|
|
|
|
lastname: 'Lastname',
|
|
|
|
email: 'some_test11-1@example.com',
|
|
|
|
fullname: 'Firstname Lastname',
|
|
|
|
login: 'some_test11-1@example.com',
|
2015-01-07 20:42:12 +00:00
|
|
|
}
|
|
|
|
},
|
2012-10-25 22:12:16 +00:00
|
|
|
]
|
|
|
|
|
|
|
|
tests.each { |test|
|
|
|
|
|
2014-07-27 11:40:42 +00:00
|
|
|
# check if user exists
|
2015-04-27 13:42:53 +00:00
|
|
|
user = User.where( login: test[:create][:login] ).first
|
2014-07-27 11:40:42 +00:00
|
|
|
if user
|
|
|
|
user.destroy
|
|
|
|
end
|
|
|
|
|
2012-10-25 22:12:16 +00:00
|
|
|
user = User.create( test[:create] )
|
|
|
|
|
|
|
|
test[:create_verify].each { |key, value|
|
2014-07-27 11:40:42 +00:00
|
|
|
next if key == :image_md5
|
2015-01-07 20:42:12 +00:00
|
|
|
if user.respond_to?( key )
|
2015-07-03 15:18:01 +00:00
|
|
|
assert_equal( value, user.send(key), "create check #{key} in (#{test[:name]})" )
|
2015-01-07 20:42:12 +00:00
|
|
|
else
|
2015-07-03 15:18:01 +00:00
|
|
|
assert_equal( value, user[key], "create check #{key} in (#{test[:name]})" )
|
2015-01-07 20:42:12 +00:00
|
|
|
end
|
2012-10-25 22:12:16 +00:00
|
|
|
}
|
2014-07-27 11:40:42 +00:00
|
|
|
if test[:create_verify][:image_md5]
|
2014-12-01 07:32:35 +00:00
|
|
|
file = Avatar.get_by_hash( user.image )
|
|
|
|
file_md5 = Digest::MD5.hexdigest( file.content )
|
2015-07-03 15:18:01 +00:00
|
|
|
assert_equal( test[:create_verify][:image_md5], file_md5, "create avatar md5 check in (#{test[:name]})" )
|
2014-07-27 11:40:42 +00:00
|
|
|
end
|
|
|
|
if test[:update]
|
|
|
|
user.update_attributes( test[:update] )
|
|
|
|
|
|
|
|
test[:update_verify].each { |key, value|
|
|
|
|
next if key == :image_md5
|
2015-01-07 20:42:12 +00:00
|
|
|
if user.respond_to?( key )
|
2015-07-03 15:18:01 +00:00
|
|
|
assert_equal( value, user.send(key), "update check #{key} in (#{test[:name]})" )
|
2015-01-07 20:42:12 +00:00
|
|
|
else
|
2015-07-03 15:18:01 +00:00
|
|
|
assert_equal( value, user[key], "update check #{key} in (#{test[:name]})" )
|
2015-01-07 20:42:12 +00:00
|
|
|
end
|
2014-07-27 11:40:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if test[:update_verify][:image_md5]
|
2014-12-01 07:32:35 +00:00
|
|
|
file = Avatar.get_by_hash( user.image )
|
|
|
|
file_md5 = Digest::MD5.hexdigest( file.content )
|
2015-07-03 15:18:01 +00:00
|
|
|
assert_equal( test[:update_verify][:image_md5], file_md5, "update avatar md5 check in (#{test[:name]})" )
|
2014-07-27 11:40:42 +00:00
|
|
|
end
|
|
|
|
end
|
2012-10-25 22:12:16 +00:00
|
|
|
|
|
|
|
user.destroy
|
2014-07-27 11:40:42 +00:00
|
|
|
}
|
2012-10-25 22:12:16 +00:00
|
|
|
end
|
2016-02-07 13:00:29 +00:00
|
|
|
|
|
|
|
test 'user default preferences' do
|
2016-09-29 11:25:20 +00:00
|
|
|
name = rand(999_999_999)
|
2016-02-07 13:00:29 +00:00
|
|
|
groups = Group.where(name: 'Users')
|
|
|
|
roles = Role.where(name: 'Agent')
|
|
|
|
agent1 = User.create_or_update(
|
2016-09-29 11:25:20 +00:00
|
|
|
login: "agent-default-preferences#{name}@example.com",
|
2016-02-07 13:00:29 +00:00
|
|
|
firstname: 'Preferences',
|
2016-09-29 11:25:20 +00:00
|
|
|
lastname: "Agent#{name}",
|
|
|
|
email: "agent-default-preferences#{name}@example.com",
|
2016-02-07 13:00:29 +00:00
|
|
|
password: 'agentpw',
|
|
|
|
active: true,
|
|
|
|
roles: roles,
|
|
|
|
groups: groups,
|
|
|
|
preferences: {
|
|
|
|
locale: 'de-de',
|
|
|
|
},
|
|
|
|
updated_by_id: 1,
|
|
|
|
created_by_id: 1,
|
|
|
|
)
|
2016-09-29 11:25:20 +00:00
|
|
|
agent1 = User.find(agent1.id)
|
2016-02-07 13:00:29 +00:00
|
|
|
assert(agent1.preferences)
|
|
|
|
assert(agent1.preferences['locale'])
|
|
|
|
assert_equal(agent1.preferences['locale'], 'de-de')
|
|
|
|
assert(agent1.preferences['notification_config'])
|
|
|
|
assert(agent1.preferences['notification_config']['matrix'])
|
|
|
|
assert(agent1.preferences['notification_config']['matrix']['create'])
|
|
|
|
assert(agent1.preferences['notification_config']['matrix']['update'])
|
|
|
|
|
|
|
|
roles = Role.where(name: 'Customer')
|
|
|
|
customer1 = User.create_or_update(
|
2016-09-29 11:25:20 +00:00
|
|
|
login: "customer-default-preferences#{name}@example.com",
|
2016-02-07 13:00:29 +00:00
|
|
|
firstname: 'Preferences',
|
2016-09-29 11:25:20 +00:00
|
|
|
lastname: "Customer#{name}",
|
|
|
|
email: "customer-default-preferences#{name}@example.com",
|
2016-02-07 13:00:29 +00:00
|
|
|
password: 'customerpw',
|
|
|
|
active: true,
|
|
|
|
roles: roles,
|
|
|
|
preferences: {
|
|
|
|
locale: 'de-de',
|
|
|
|
},
|
|
|
|
updated_by_id: 1,
|
|
|
|
created_by_id: 1,
|
|
|
|
)
|
2016-09-29 11:25:20 +00:00
|
|
|
customer1 = User.find(customer1.id)
|
2016-02-07 13:00:29 +00:00
|
|
|
assert(customer1.preferences)
|
|
|
|
assert(customer1.preferences['locale'])
|
|
|
|
assert_equal(customer1.preferences['locale'], 'de-de')
|
|
|
|
assert_not(customer1.preferences['notification_config'])
|
|
|
|
|
2016-09-29 11:25:20 +00:00
|
|
|
customer1 = User.find(customer1.id)
|
2016-02-07 13:00:29 +00:00
|
|
|
customer1.roles = Role.where(name: 'Agent')
|
2016-09-29 11:25:20 +00:00
|
|
|
customer1 = User.find(customer1.id)
|
|
|
|
|
2016-02-07 13:00:29 +00:00
|
|
|
assert(customer1.preferences)
|
|
|
|
assert(customer1.preferences['locale'])
|
|
|
|
assert_equal(customer1.preferences['locale'], 'de-de')
|
|
|
|
assert(customer1.preferences['notification_config'])
|
|
|
|
assert(customer1.preferences['notification_config']['matrix']['create'])
|
|
|
|
assert(customer1.preferences['notification_config']['matrix']['update'])
|
|
|
|
|
|
|
|
end
|
|
|
|
|
2016-08-12 16:39:09 +00:00
|
|
|
test 'permission' do
|
|
|
|
test_role_1 = Role.create_or_update(
|
|
|
|
name: 'Test1',
|
|
|
|
note: 'To configure your system.',
|
|
|
|
preferences: {
|
|
|
|
not: ['Test3'],
|
|
|
|
},
|
|
|
|
updated_by_id: 1,
|
|
|
|
created_by_id: 1
|
|
|
|
)
|
|
|
|
test_role_2 = Role.create_or_update(
|
|
|
|
name: 'Test2',
|
|
|
|
note: 'To work on Tickets.',
|
|
|
|
preferences: {
|
|
|
|
not: ['Test3'],
|
|
|
|
},
|
|
|
|
updated_by_id: 1,
|
|
|
|
created_by_id: 1
|
|
|
|
)
|
|
|
|
test_role_3 = Role.create_or_update(
|
|
|
|
name: 'Test3',
|
|
|
|
note: 'People who create Tickets ask for help.',
|
|
|
|
preferences: {
|
|
|
|
not: %w(Test1 Test2),
|
|
|
|
},
|
|
|
|
updated_by_id: 1,
|
|
|
|
created_by_id: 1
|
|
|
|
)
|
|
|
|
test_role_4 = Role.create_or_update(
|
|
|
|
name: 'Test4',
|
|
|
|
note: 'Access the report area.',
|
|
|
|
preferences: {},
|
|
|
|
created_by_id: 1,
|
|
|
|
updated_by_id: 1,
|
|
|
|
)
|
2016-09-29 11:25:20 +00:00
|
|
|
name = rand(999_999_999)
|
2016-08-12 16:39:09 +00:00
|
|
|
assert_raises(RuntimeError) {
|
|
|
|
User.create_or_update(
|
2016-09-29 11:25:20 +00:00
|
|
|
login: "customer-role#{name}@example.com",
|
2016-08-12 16:39:09 +00:00
|
|
|
firstname: 'Role',
|
2016-09-29 11:25:20 +00:00
|
|
|
lastname: "Customer#{name}",
|
|
|
|
email: "customer-role#{name}@example.com",
|
2016-08-12 16:39:09 +00:00
|
|
|
password: 'customerpw',
|
|
|
|
active: true,
|
|
|
|
roles: [test_role_1, test_role_3],
|
|
|
|
updated_by_id: 1,
|
|
|
|
created_by_id: 1,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
assert_raises(RuntimeError) {
|
|
|
|
User.create_or_update(
|
2016-09-29 11:25:20 +00:00
|
|
|
login: "customer-role#{name}@example.com",
|
2016-08-12 16:39:09 +00:00
|
|
|
firstname: 'Role',
|
2016-09-29 11:25:20 +00:00
|
|
|
lastname: "Customer#{name}",
|
|
|
|
email: "customer-role#{name}@example.com",
|
2016-08-12 16:39:09 +00:00
|
|
|
password: 'customerpw',
|
|
|
|
active: true,
|
|
|
|
roles: [test_role_2, test_role_3],
|
|
|
|
updated_by_id: 1,
|
|
|
|
created_by_id: 1,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
user1 = User.create_or_update(
|
2016-09-29 11:25:20 +00:00
|
|
|
login: "customer-role#{name}@example.com",
|
2016-08-12 16:39:09 +00:00
|
|
|
firstname: 'Role',
|
2016-09-29 11:25:20 +00:00
|
|
|
lastname: "Customer#{name}",
|
|
|
|
email: "customer-role#{name}@example.com",
|
2016-08-12 16:39:09 +00:00
|
|
|
password: 'customerpw',
|
|
|
|
active: true,
|
|
|
|
roles: [test_role_1, test_role_2],
|
|
|
|
updated_by_id: 1,
|
|
|
|
created_by_id: 1,
|
|
|
|
)
|
|
|
|
assert(user1.role_ids.include?(test_role_1.id))
|
|
|
|
assert(user1.role_ids.include?(test_role_2.id))
|
|
|
|
assert_not(user1.role_ids.include?(test_role_3.id))
|
|
|
|
assert_not(user1.role_ids.include?(test_role_4.id))
|
|
|
|
user1 = User.create_or_update(
|
2016-09-29 11:25:20 +00:00
|
|
|
login: "customer-role#{name}@example.com",
|
2016-08-12 16:39:09 +00:00
|
|
|
firstname: 'Role',
|
2016-09-29 11:25:20 +00:00
|
|
|
lastname: "Customer#{name}",
|
|
|
|
email: "customer-role#{name}@example.com",
|
2016-08-12 16:39:09 +00:00
|
|
|
password: 'customerpw',
|
|
|
|
active: true,
|
|
|
|
roles: [test_role_1, test_role_4],
|
|
|
|
updated_by_id: 1,
|
|
|
|
created_by_id: 1,
|
|
|
|
)
|
|
|
|
assert(user1.role_ids.include?(test_role_1.id))
|
|
|
|
assert_not(user1.role_ids.include?(test_role_2.id))
|
|
|
|
assert_not(user1.role_ids.include?(test_role_3.id))
|
|
|
|
assert(user1.role_ids.include?(test_role_4.id))
|
|
|
|
assert_raises(RuntimeError) {
|
|
|
|
User.create_or_update(
|
2016-09-29 11:25:20 +00:00
|
|
|
login: "customer-role#{name}@example.com",
|
2016-08-12 16:39:09 +00:00
|
|
|
firstname: 'Role',
|
2016-09-29 11:25:20 +00:00
|
|
|
lastname: "Customer#{name}",
|
|
|
|
email: "customer-role#{name}@example.com",
|
2016-08-12 16:39:09 +00:00
|
|
|
password: 'customerpw',
|
|
|
|
active: true,
|
|
|
|
roles: [test_role_1, test_role_3],
|
|
|
|
updated_by_id: 1,
|
|
|
|
created_by_id: 1,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
assert_raises(RuntimeError) {
|
|
|
|
User.create_or_update(
|
2016-09-29 11:25:20 +00:00
|
|
|
login: "customer-role#{name}@example.com",
|
2016-08-12 16:39:09 +00:00
|
|
|
firstname: 'Role',
|
2016-09-29 11:25:20 +00:00
|
|
|
lastname: "Customer#{name}",
|
|
|
|
email: "customer-role#{name}@example.com",
|
2016-08-12 16:39:09 +00:00
|
|
|
password: 'customerpw',
|
|
|
|
active: true,
|
|
|
|
roles: [test_role_2, test_role_3],
|
|
|
|
updated_by_id: 1,
|
|
|
|
created_by_id: 1,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
assert(user1.role_ids.include?(test_role_1.id))
|
|
|
|
assert_not(user1.role_ids.include?(test_role_2.id))
|
|
|
|
assert_not(user1.role_ids.include?(test_role_3.id))
|
|
|
|
assert(user1.role_ids.include?(test_role_4.id))
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
test 'permission default' do
|
2016-09-29 11:25:20 +00:00
|
|
|
name = rand(999_999_999)
|
|
|
|
admin_count = User.with_permissions('admin').count
|
2016-08-12 16:39:09 +00:00
|
|
|
admin = User.create_or_update(
|
2016-09-29 11:25:20 +00:00
|
|
|
login: "admin-role#{name}@example.com",
|
2016-08-12 16:39:09 +00:00
|
|
|
firstname: 'Role',
|
2016-09-29 11:25:20 +00:00
|
|
|
lastname: "Admin#{name}",
|
|
|
|
email: "admin-role#{name}@example.com",
|
2016-08-12 16:39:09 +00:00
|
|
|
password: 'adminpw',
|
|
|
|
active: true,
|
|
|
|
roles: Role.where(name: %w(Admin Agent)),
|
|
|
|
updated_by_id: 1,
|
|
|
|
created_by_id: 1,
|
|
|
|
)
|
2016-09-29 11:25:20 +00:00
|
|
|
agent_count = User.with_permissions('ticket.agent').count
|
2016-08-12 16:39:09 +00:00
|
|
|
agent = User.create_or_update(
|
2016-09-29 11:25:20 +00:00
|
|
|
login: "agent-role#{name}@example.com",
|
2016-08-12 16:39:09 +00:00
|
|
|
firstname: 'Role',
|
2016-09-29 11:25:20 +00:00
|
|
|
lastname: "Agent#{name}",
|
|
|
|
email: "agent-role#{name}@example.com",
|
2016-08-12 16:39:09 +00:00
|
|
|
password: 'agentpw',
|
|
|
|
active: true,
|
|
|
|
roles: Role.where(name: 'Agent'),
|
|
|
|
updated_by_id: 1,
|
|
|
|
created_by_id: 1,
|
|
|
|
)
|
2016-09-29 11:25:20 +00:00
|
|
|
customer_count = User.with_permissions('ticket.customer').count
|
2016-08-12 16:39:09 +00:00
|
|
|
customer = User.create_or_update(
|
2016-09-29 11:25:20 +00:00
|
|
|
login: "customer-role#{name}@example.com",
|
2016-08-12 16:39:09 +00:00
|
|
|
firstname: 'Role',
|
2016-09-29 11:25:20 +00:00
|
|
|
lastname: "Customer#{name}",
|
|
|
|
email: "customer-role#{name}@example.com",
|
2016-08-12 16:39:09 +00:00
|
|
|
password: 'customerpw',
|
|
|
|
active: true,
|
|
|
|
roles: Role.where(name: 'Customer'),
|
|
|
|
updated_by_id: 1,
|
|
|
|
created_by_id: 1,
|
|
|
|
)
|
|
|
|
users = User.with_permissions('not_existing')
|
|
|
|
assert(users.empty?)
|
|
|
|
|
|
|
|
users = User.with_permissions('admin')
|
|
|
|
assert_equal(admin_count + 1, users.count)
|
|
|
|
assert_equal(admin.login, users.last.login)
|
|
|
|
|
|
|
|
users = User.with_permissions('admin.session')
|
|
|
|
assert_equal(admin_count + 1, users.count)
|
|
|
|
assert_equal(admin.login, users.last.login)
|
|
|
|
|
|
|
|
users = User.with_permissions(['admin.session', 'not_existing'])
|
|
|
|
assert_equal(admin_count + 1, users.count)
|
|
|
|
assert_equal(admin.login, users.last.login)
|
|
|
|
|
|
|
|
users = User.with_permissions('ticket.agent')
|
|
|
|
assert_equal(agent_count + 1, users.count)
|
|
|
|
assert_equal(agent.login, users.last.login)
|
|
|
|
users = User.with_permissions(['ticket.agent', 'not_existing'])
|
|
|
|
assert_equal(agent_count + 1, users.count)
|
|
|
|
assert_equal(agent.login, users.last.login)
|
|
|
|
|
|
|
|
users = User.with_permissions('ticket.customer')
|
|
|
|
assert_equal(customer_count + 1, users.count)
|
|
|
|
assert_equal(customer.login, users.last.login)
|
|
|
|
users = User.with_permissions(['ticket.customer', 'not_existing'])
|
|
|
|
assert_equal(customer_count + 1, users.count)
|
|
|
|
assert_equal(customer.login, users.last.login)
|
|
|
|
|
|
|
|
end
|
|
|
|
|
2012-10-25 22:12:16 +00:00
|
|
|
end
|