trabajo-afectivo/test/unit/session_basic_test.rb

333 lines
10 KiB
Ruby
Raw Normal View History

2014-07-13 18:52:32 +00:00
# encoding: utf-8
require 'test_helper'
class SessionBasicTest < ActiveSupport::TestCase
test 'a cache' do
Sessions::CacheIn.set( 'last_run_test', true, { expires_in: 2.seconds } )
2014-07-13 18:52:32 +00:00
result = Sessions::CacheIn.get( 'last_run_test' )
assert_equal( true, result, 'check 1' )
2014-07-13 18:52:32 +00:00
# should not be expired
result = Sessions::CacheIn.expired( 'last_run_test' )
assert_equal( false, result, 'check 1 - expired' )
2014-07-13 18:52:32 +00:00
# should be expired
sleep 3
result = Sessions::CacheIn.expired( 'last_run_test' )
assert_equal( true, result, 'check 1 - expired' )
2014-07-13 18:52:32 +00:00
# renew expire
result = Sessions::CacheIn.get( 'last_run_test', re_expire: true )
assert_equal( true, result, 'check 1 - re_expire' )
2014-07-13 18:52:32 +00:00
# should not be expired
result = Sessions::CacheIn.expired( 'last_run_test' )
assert_equal( false, result, 'check 1 - expired' )
2014-07-13 18:52:32 +00:00
# ignore expired
sleep 3
result = Sessions::CacheIn.get( 'last_run_test', ignore_expire: true )
assert_equal( true, result, 'check 1 - ignore_expire' )
2014-07-13 18:52:32 +00:00
# should be expired
result = Sessions::CacheIn.expired( 'last_run_test' )
assert_equal( true, result, 'check 2' )
2014-07-13 18:52:32 +00:00
result = Sessions::CacheIn.get( 'last_run_test' )
assert_equal( nil, result, 'check 2' )
2014-07-13 18:52:32 +00:00
# check delete cache
Sessions::CacheIn.set( 'last_run_delete', true, { expires_in: 5.seconds } )
2014-07-13 18:52:32 +00:00
result = Sessions::CacheIn.get( 'last_run_delete' )
assert_equal( true, result, 'check 1' )
2014-07-13 18:52:32 +00:00
Sessions::CacheIn.delete( 'last_run_delete' )
result = Sessions::CacheIn.get( 'last_run_delete' )
assert_equal( nil, nil, 'check delete' )
2014-07-13 18:52:32 +00:00
end
2015-05-03 12:11:47 +00:00
test 'c session create / update' do
# create users
roles = Role.where( name: [ 'Agent'] )
groups = Group.all
UserInfo.current_user_id = 1
agent1 = User.create_or_update(
login: 'session-agent-1',
firstname: 'Session',
lastname: 'Agent 1',
email: 'session-agent1@example.com',
password: 'agentpw',
active: true,
roles: roles,
groups: groups,
)
agent1.roles = roles
agent1.save
# create sessions
client_id1 = '123456789'
Sessions.create( client_id1, {}, { type: 'websocket' } )
# check if session exists
assert( Sessions.session_exists?(client_id1), 'check if session exists' )
# check session data
data = Sessions.get(client_id1)
assert( data[:meta], 'check if meta exists' )
assert( data[:user], 'check if user exists' )
assert_equal( data[:user]['id'], nil, 'check if user id is correct' )
# recreate session
Sessions.create( client_id1, agent1.attributes, { type: 'websocket' } )
# check if session exists
assert( Sessions.session_exists?(client_id1), 'check if session exists' )
# check session data
data = Sessions.get(client_id1)
assert( data[:meta], 'check if meta exists' )
assert( data[:user], 'check if user exists' )
assert_equal( data[:user]['id'], agent1.id, 'check if user id is correct' )
# destroy session
Sessions.destory(client_id1)
# check if session exists
assert( !Sessions.session_exists?(client_id1), 'check if session exists' )
end
test 'c collections group' do
2014-07-13 18:52:32 +00:00
require 'sessions/backend/collections/group.rb'
2014-11-10 19:14:48 +00:00
UserInfo.current_user_id = 2
user = User.lookup(id: 1)
2015-02-25 22:35:00 +00:00
collection_client1 = Sessions::Backend::Collections::Group.new(user, false, '123-1', 3)
collection_client2 = Sessions::Backend::Collections::Group.new(user, false, '234-2', 3)
2014-07-13 18:52:32 +00:00
# get whole collections
result1 = collection_client1.push
assert( !result1.empty?, 'check collections' )
2015-02-25 22:35:00 +00:00
sleep 0.6
2014-07-13 18:52:32 +00:00
result2 = collection_client2.push
assert( !result2.empty?, 'check collections' )
assert_equal( result1, result2, 'check collections' )
2014-07-13 18:52:32 +00:00
# next check should be empty
result1 = collection_client1.push
assert( !result1, 'check collections - recall' )
2014-07-13 18:52:32 +00:00
sleep 1
result2 = collection_client2.push
assert( !result2, 'check collections - recall' )
2014-07-13 18:52:32 +00:00
# change collection
group = Group.first
group.touch
2015-02-25 22:35:00 +00:00
sleep 4
2014-07-13 18:52:32 +00:00
# get whole collections
result1 = collection_client1.push
assert( !result1.empty?, 'check collections - after touch' )
2014-07-13 18:52:32 +00:00
result2 = collection_client2.push
assert( !result2.empty?, 'check collections - after touch' )
assert_equal( result1, result2, 'check collections' )
2014-07-13 18:52:32 +00:00
# check again after touch
result1 = collection_client1.push
assert( !result1, 'check collections - after touch - recall' )
2014-07-13 18:52:32 +00:00
result2 = collection_client2.push
assert( !result2, 'check collections - after touch - recall' )
assert_equal( result1, result2, 'check collections' )
2014-07-13 18:52:32 +00:00
# change collection
group = Group.create( name: 'SomeGroup::' + rand(999_999).to_s, active: true )
2015-02-25 22:35:00 +00:00
sleep 4
2014-07-13 18:52:32 +00:00
# get whole collections
result1 = collection_client1.push
assert( !result1.empty?, 'check collections - after create' )
2014-07-13 18:52:32 +00:00
result2 = collection_client2.push
assert( !result2.empty?, 'check collections - after create' )
assert_equal( result1, result2, 'check collections' )
2014-07-13 18:52:32 +00:00
# check again after create
2015-02-25 22:35:00 +00:00
sleep 4
2014-07-13 18:52:32 +00:00
result1 = collection_client1.push
assert( !result1, 'check collections - after create - recall' )
2014-07-13 18:52:32 +00:00
result2 = collection_client2.push
assert( !result2, 'check collections - after create - recall' )
assert_equal( result1, result2, 'check collections' )
2014-07-13 18:52:32 +00:00
# change collection
group.destroy
2015-02-25 22:35:00 +00:00
sleep 4
2014-07-13 18:52:32 +00:00
# get whole collections
result1 = collection_client1.push
assert( !result1.empty?, 'check collections - after destroy' )
2014-07-13 18:52:32 +00:00
result2 = collection_client2.push
assert( !result2.empty?, 'check collections - after destroy' )
assert_equal( result1, result2, 'check collections' )
2014-07-13 18:52:32 +00:00
# check again after destroy
2015-02-25 22:35:00 +00:00
sleep 4
2014-07-13 18:52:32 +00:00
result1 = collection_client1.push
assert( !result1, 'check collections - after destroy - recall' )
2014-07-13 18:52:32 +00:00
result2 = collection_client2.push
assert( !result2, 'check collections - after destroy - recall' )
assert_equal( result1, result2, 'check collections' )
2014-07-13 18:52:32 +00:00
end
user = User.lookup(id: 1)
roles = Role.where( name: %w(Agent Admin) )
2014-07-13 18:52:32 +00:00
user.roles = roles
user.save
2015-05-03 12:11:47 +00:00
test 'c collections organization' do
2014-07-13 18:52:32 +00:00
require 'sessions/backend/collections/organization.rb'
2014-11-10 19:14:48 +00:00
UserInfo.current_user_id = 2
user = User.lookup(id: 1)
org = Organization.create( name: 'SomeOrg1::' + rand(999_999).to_s, active: true )
2014-07-13 18:52:32 +00:00
2015-02-25 22:35:00 +00:00
collection_client1 = Sessions::Backend::Collections::Organization.new(user, false, '123-1', 3)
collection_client2 = Sessions::Backend::Collections::Organization.new(user, false, '234-2', 3)
2014-07-13 18:52:32 +00:00
# get whole collections - should be nil, no org exists!
result1 = collection_client1.push
assert( !result1.empty?, 'check collections' )
2014-07-13 18:52:32 +00:00
result2 = collection_client2.push
assert( !result2.empty?, 'check collections' )
assert_equal( result1, result2, 'check collections' )
2014-07-13 18:52:32 +00:00
# next check - should still be nil, no org exists!
result1 = collection_client1.push
assert( !result1, 'check collections - recall' )
2015-02-25 22:35:00 +00:00
sleep 0.6
2014-07-13 18:52:32 +00:00
result2 = collection_client2.push
assert( !result2, 'check collections - recall' )
2014-07-13 18:52:32 +00:00
# change collection
org = Organization.create( name: 'SomeOrg2::' + rand(999_999).to_s, active: true )
2015-02-25 22:35:00 +00:00
sleep 4
2014-07-13 18:52:32 +00:00
# get whole collections
result1 = collection_client1.push
assert( !result1.empty?, 'check collections - after create' )
2014-07-13 18:52:32 +00:00
result2 = collection_client2.push
assert( !result2.empty?, 'check collections - after create' )
assert_equal( result1, result2, 'check collections' )
2014-07-13 18:52:32 +00:00
2015-02-25 22:35:00 +00:00
sleep 4
2014-07-13 18:52:32 +00:00
# next check should be empty
result1 = collection_client1.push
assert( !result1, 'check collections - after create recall' )
2014-07-13 18:52:32 +00:00
result2 = collection_client2.push
assert( !result2, 'check collections - after create recall' )
2014-07-13 18:52:32 +00:00
organization = Organization.first
organization.touch
2015-02-25 22:35:00 +00:00
sleep 4
2014-07-13 18:52:32 +00:00
# get whole collections
result1 = collection_client1.push
assert( !result1.empty?, 'check collections - after touch' )
2014-07-13 18:52:32 +00:00
result2 = collection_client2.push
assert( !result1.empty?, 'check collections - after touch' )
assert_equal( result1, result2, 'check collections' )
2014-07-13 18:52:32 +00:00
end
2015-05-03 12:11:47 +00:00
test 'c rss' do
user = User.lookup(id: 1)
2014-07-13 18:52:32 +00:00
collection_client1 = Sessions::Backend::Rss.new(user, false, '123-1')
# get whole collections
result1 = collection_client1.push
#puts "RSS1: #{result1.inspect}"
assert( !result1.empty?, 'check rss' )
2014-07-13 18:52:32 +00:00
sleep 1
# next check should be empty
result1 = collection_client1.push
#puts "R1: #{result1.inspect}"
assert( !result1, 'check rss - recall' )
2014-07-13 18:52:32 +00:00
end
2015-05-03 12:11:47 +00:00
test 'c activity stream' do
2014-07-13 18:52:32 +00:00
# create users
roles = Role.where( name: %w(Agent Admin) )
2014-07-13 18:52:32 +00:00
groups = Group.all
2014-11-10 19:14:48 +00:00
UserInfo.current_user_id = 2
2014-07-13 18:52:32 +00:00
agent1 = User.create_or_update(
login: 'activity-stream-agent-1',
firstname: 'Session',
lastname: 'activity stream ' + rand(99_999).to_s,
email: 'activity-stream-agent1@example.com',
password: 'agentpw',
active: true,
roles: roles,
groups: groups,
2014-07-13 18:52:32 +00:00
)
agent1.roles = roles
assert( agent1.save, 'create/update agent1' )
2014-07-13 18:52:32 +00:00
2015-02-25 22:35:00 +00:00
as_client1 = Sessions::Backend::ActivityStream.new(agent1, false, '123-1', 3)
2014-07-13 18:52:32 +00:00
# get as stream
result1 = as_client1.push
assert( result1, 'check as agent1' )
2014-07-13 18:52:32 +00:00
sleep 1
# next check should be empty
result1 = as_client1.push
assert( !result1, 'check as agent1 - recall' )
2014-07-13 18:52:32 +00:00
# next check should be empty
2015-02-25 22:35:00 +00:00
sleep 4
2014-07-13 18:52:32 +00:00
result1 = as_client1.push
assert( !result1, 'check as agent1 - recall 2' )
2014-07-13 18:52:32 +00:00
agent1.update_attribute( :email, 'activity-stream-agent11@example.com' )
ticket = Ticket.create(title: '12323', group_id: 1, priority_id: 1, state_id: 1, customer_id: 1 )
2014-07-13 18:52:32 +00:00
2015-02-25 22:35:00 +00:00
sleep 4
2014-07-13 18:52:32 +00:00
# get as stream
result1 = as_client1.push
assert( result1, 'check as agent1 - recall 3' )
2014-07-13 18:52:32 +00:00
end
2015-05-03 12:11:47 +00:00
test 'c ticket_create' do
2014-07-13 18:52:32 +00:00
2014-11-10 19:14:48 +00:00
UserInfo.current_user_id = 2
user = User.lookup(id: 1)
2015-02-25 22:35:00 +00:00
ticket_create_client1 = Sessions::Backend::TicketCreate.new(user, false, '123-1', 3)
2014-07-13 18:52:32 +00:00
# get as stream
result1 = ticket_create_client1.push
assert( result1, 'check ticket_create' )
2015-02-25 22:35:00 +00:00
sleep 0.6
2014-07-13 18:52:32 +00:00
# next check should be empty
result1 = ticket_create_client1.push
assert( !result1, 'check ticket_create - recall' )
2014-07-13 18:52:32 +00:00
# next check should be empty
2015-02-25 22:35:00 +00:00
sleep 0.6
2014-07-13 18:52:32 +00:00
result1 = ticket_create_client1.push
assert( !result1, 'check ticket_create - recall 2' )
2014-07-13 18:52:32 +00:00
Group.create( name: 'SomeTicketCreateGroup::' + rand(999_999).to_s, active: true )
2014-07-13 18:52:32 +00:00
2015-02-25 22:35:00 +00:00
sleep 4
2014-07-13 18:52:32 +00:00
# get as stream
result1 = ticket_create_client1.push
assert( result1, 'check ticket_create - recall 3' )
2014-07-13 18:52:32 +00:00
end
end