Just push related user organisation to clients (improved tests).

This commit is contained in:
Martin Edenhofer 2016-05-16 19:49:06 +02:00
parent f513062d40
commit 60b11db825
7 changed files with 91 additions and 27 deletions

2
.gitignore vendored
View file

@ -12,7 +12,7 @@
# Ignore all logfiles and tempfiles. # Ignore all logfiles and tempfiles.
/log/*.log /log/*.log
/tmp/websocket/* /tmp/websocket*
/tmp/cache* /tmp/cache*
/tmp/pids/* /tmp/pids/*
/tmp/*.png /tmp/*.png

View file

@ -10,7 +10,7 @@ module Sessions
end end
# get working directories # get working directories
@path = "#{@root}/tmp/websocket" @path = "#{@root}/tmp/websocket_#{Rails.env}"
# create global vars for threads # create global vars for threads
@@client_threads = {} # rubocop:disable Style/ClassVars @@client_threads = {} # rubocop:disable Style/ClassVars
@ -215,7 +215,7 @@ returns
path = "#{@path}/#{client_id}" path = "#{@path}/#{client_id}"
data[:meta][:last_ping] = Time.now.utc.to_i data[:meta][:last_ping] = Time.now.utc.to_i
content = data.to_json content = data.to_json
File.open( path + '/session', 'wb' ) { |file| File.open("#{path}/session", 'wb' ) { |file|
file.write content file.write content
} }
true true

View file

@ -7,7 +7,10 @@ class Sessions::Backend::Collections::Organization < Sessions::Backend::Collecti
all = [] all = []
if @user.organization_id if @user.organization_id
all = Organization.lookup(id: @user.organization_id) organization = Organization.lookup(id: @user.organization_id)
if organization
all = [organization]
end
end end
all all

View file

@ -7,7 +7,6 @@ require 'eventmachine'
require 'em-websocket' require 'em-websocket'
require 'json' require 'json'
require 'fileutils' require 'fileutils'
require 'sessions'
require 'optparse' require 'optparse'
require 'daemons' require 'daemons'
@ -17,6 +16,8 @@ Dir.chdir dir
RAILS_ENV = ENV['RAILS_ENV'] || 'development' RAILS_ENV = ENV['RAILS_ENV'] || 'development'
require File.join(dir, 'config', 'environment') require File.join(dir, 'config', 'environment')
require 'sessions'
# Look for -o with argument, and -I and -D boolean arguments # Look for -o with argument, and -I and -D boolean arguments
@options = { @options = {
p: 6042, p: 6042,

View file

@ -187,13 +187,30 @@ class SessionBasicTest < ActiveSupport::TestCase
test 'c collections organization' do test 'c collections organization' do
require 'sessions/backend/collections/organization.rb' require 'sessions/backend/collections/organization.rb'
UserInfo.current_user_id = 2 UserInfo.current_user_id = 2
user = User.lookup(id: 1)
org = Organization.create( name: 'SomeOrg1::' + rand(999_999).to_s, active: true )
collection_client1 = Sessions::Backend::Collections::Organization.new(user, {}, false, '123-1', 3) # create users
collection_client2 = Sessions::Backend::Collections::Organization.new(user, {}, false, '234-2', 3) roles = Role.where(name: ['Agent'])
groups = Group.all
organization = Organization.create( name: "SomeOrg1::#{rand(999_999)}", active: true )
# get whole collections - should be nil, no org exists! agent2 = User.create_or_update(
login: 'session-agent-2',
firstname: 'Session',
lastname: 'Agent 2',
email: 'session-agent2@example.com',
password: 'agentpw',
active: true,
roles: roles,
groups: groups,
organization: organization,
)
agent2.roles = roles
agent2.save
collection_client1 = Sessions::Backend::Collections::Organization.new(agent2, {}, false, '123-1', 3)
collection_client2 = Sessions::Backend::Collections::Organization.new(agent2, {}, false, '234-2', 3)
# get collection - should be nil, no chnaged org exists!
result1 = collection_client1.push result1 = collection_client1.push
assert(!result1.empty?, 'check collections') assert(!result1.empty?, 'check collections')
result2 = collection_client2.push result2 = collection_client2.push
@ -208,10 +225,12 @@ class SessionBasicTest < ActiveSupport::TestCase
assert(!result2, 'check collections - recall') assert(!result2, 'check collections - recall')
# change collection # change collection
org = Organization.create(name: 'SomeOrg2::' + rand(999_999).to_s, active: true) organization.name = "#{organization.name}-2"
organization.save
#organization = Organization.create(name: "SomeOrg2::#{rand(999_999)}", active: true)
sleep 4 sleep 4
# get whole collections # get collection
result1 = collection_client1.push result1 = collection_client1.push
assert(!result1.empty?, 'check collections - after create') assert(!result1.empty?, 'check collections - after create')
result2 = collection_client2.push result2 = collection_client2.push
@ -230,7 +249,7 @@ class SessionBasicTest < ActiveSupport::TestCase
organization.touch organization.touch
sleep 4 sleep 4
# get whole collections # get collection
result1 = collection_client1.push result1 = collection_client1.push
assert(!result1.empty?, 'check collections - after touch') assert(!result1.empty?, 'check collections - after touch')
result2 = collection_client2.push result2 = collection_client2.push
@ -264,7 +283,7 @@ class SessionBasicTest < ActiveSupport::TestCase
agent1 = User.create_or_update( agent1 = User.create_or_update(
login: 'activity-stream-agent-1', login: 'activity-stream-agent-1',
firstname: 'Session', firstname: 'Session',
lastname: 'activity stream ' + rand(99_999).to_s, lastname: "activity stream #{rand(99_999)}",
email: 'activity-stream-agent1@example.com', email: 'activity-stream-agent1@example.com',
password: 'agentpw', password: 'agentpw',
active: true, active: true,

View file

@ -17,6 +17,7 @@ class SessionCollectionsTest < ActiveSupport::TestCase
lastname: 'collections 1', lastname: 'collections 1',
email: 'session-collections-agent-1@example.com', email: 'session-collections-agent-1@example.com',
password: 'agentpw', password: 'agentpw',
organization_id: nil,
active: true, active: true,
roles: roles, roles: roles,
groups: groups, groups: groups,
@ -33,6 +34,7 @@ class SessionCollectionsTest < ActiveSupport::TestCase
lastname: 'collections 2', lastname: 'collections 2',
email: 'session-collections-agent-2@example.com', email: 'session-collections-agent-2@example.com',
password: 'agentpw', password: 'agentpw',
organization_id: nil,
active: true, active: true,
roles: roles, roles: roles,
groups: groups, groups: groups,
@ -108,23 +110,56 @@ class SessionCollectionsTest < ActiveSupport::TestCase
assert(check_if_collection_exists(result3, :Group), 'check collections - after touch') assert(check_if_collection_exists(result3, :Group), 'check collections - after touch')
# change collection # change collection
org = Organization.create(name: 'SomeOrg::' + rand(999_999).to_s, active: true, member_ids: [customer1.id]) organization = Organization.create( name: "SomeSessionOrg1::#{rand(999_999)}", active: true )
sleep 4
# get whole collections # get whole collections
result1 = collection_client1.push result1 = collection_client1.push
assert(result1, 'check collections - after create') assert(result1, 'check collections - after create')
assert(check_if_collection_exists(result1, :Organization, { id: org.id, member_ids: [customer1.id] }), 'check collections - after create with attributes') assert(!check_if_collection_exists(result1, :Organization), 'check collections - after create')
sleep 0.3
result2 = collection_client2.push result2 = collection_client2.push
assert(result2, 'check collections - after create') assert(result2, 'check collections - after create')
assert(check_if_collection_exists(result2, :Organization), 'check collections - after create') assert(!check_if_collection_exists(result2, :Organization), 'check collections - after create')
# user has no organization, so collection should be empty
result3 = collection_client3.push result3 = collection_client3.push
assert(result3, 'check collections - after create') assert(result3, 'check collections - after create')
assert(!check_if_collection_exists(result3, :Organization), 'check collections - after create') assert(!check_if_collection_exists(result3, :Organization), 'check collections - after create')
# assigne new org to agent1
agent1.organization = organization
agent1.save
sleep 4
# user has new organization
result1 = collection_client1.push
assert(result1, 'check collections - after create')
assert(check_if_collection_exists(result1, :Organization, { id: organization.id, member_ids: [agent1.id] }), 'check collections - after create with attributes')
sleep 0.3
# users have no organization, so collection should be empty
result2 = collection_client2.push
assert(result2, 'check collections - after create')
assert(!check_if_collection_exists(result2, :Organization), 'check collections - after create')
result3 = collection_client3.push
assert(result3, 'check collections - after create')
assert(!check_if_collection_exists(result3, :Organization), 'check collections - after create')
# assigne new org to customer1
customer1.organization = organization
customer1.save
sleep 4
# users have no organization, so collection should be empty
result1 = collection_client1.push
assert(result1, 'check collections - after create')
assert(check_if_collection_exists(result1, :Organization, { id: organization.id, member_ids: [agent1.id, customer1.id] }), 'check collections - after create with attributes')
result2 = collection_client2.push
assert(result2, 'check collections - after create')
assert(!check_if_collection_exists(result2, :Organization), 'check collections - after create')
# user has new organization
result3 = collection_client3.push
assert(result3, 'check collections - after create')
assert(check_if_collection_exists(result3, :Organization, { id: organization.id, member_ids: [agent1.id, customer1.id] }), 'check collections - after create with attributes')
# next check should be empty # next check should be empty
sleep 1 sleep 1
result1 = collection_client1.push result1 = collection_client1.push

View file

@ -178,6 +178,11 @@ class SessionEnhancedTest < ActiveSupport::TestCase
# create users # create users
roles = Role.where(name: ['Agent']) roles = Role.where(name: ['Agent'])
groups = Group.all groups = Group.all
organization = Organization.create(
name: 'SomeOrg::' + rand(999_999).to_s, active: true,
updated_by_id: 1,
created_by_id: 1,
)
UserInfo.current_user_id = 1 UserInfo.current_user_id = 1
agent1 = User.create_or_update( agent1 = User.create_or_update(
@ -187,6 +192,7 @@ class SessionEnhancedTest < ActiveSupport::TestCase
email: 'session-agent1@example.com', email: 'session-agent1@example.com',
password: 'agentpw', password: 'agentpw',
active: true, active: true,
organization: organization,
roles: roles, roles: roles,
groups: groups, groups: groups,
) )
@ -199,12 +205,12 @@ class SessionEnhancedTest < ActiveSupport::TestCase
email: 'session-agent2@example.com', email: 'session-agent2@example.com',
password: 'agentpw', password: 'agentpw',
active: true, active: true,
organization: organization,
roles: roles, roles: roles,
groups: groups, groups: groups,
) )
agent2.roles = roles agent2.roles = roles
agent2.save agent2.save
org = Organization.create(name: 'SomeOrg::' + rand(999_999).to_s, active: true)
# create sessions # create sessions
client_id1_0 = '1234-1' client_id1_0 = '1234-1'