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.
/log/*.log
/tmp/websocket/*
/tmp/websocket*
/tmp/cache*
/tmp/pids/*
/tmp/*.png

View file

@ -10,7 +10,7 @@ module Sessions
end
# get working directories
@path = "#{@root}/tmp/websocket"
@path = "#{@root}/tmp/websocket_#{Rails.env}"
# create global vars for threads
@@client_threads = {} # rubocop:disable Style/ClassVars
@ -19,7 +19,7 @@ module Sessions
start new session
Sessions.create( client_id, session_data, { type: 'websocket' } )
Sessions.create(client_id, session_data, { type: 'websocket' })
returns
@ -215,7 +215,7 @@ returns
path = "#{@path}/#{client_id}"
data[:meta][:last_ping] = Time.now.utc.to_i
content = data.to_json
File.open( path + '/session', 'wb' ) { |file|
File.open("#{path}/session", 'wb' ) { |file|
file.write content
}
true
@ -394,7 +394,7 @@ returns
path = "#{@path}/#{client_id}/"
data = []
files = []
Dir.foreach( path ) {|entry|
Dir.foreach(path) {|entry|
next if entry == '.'
next if entry == '..'
files.push entry
@ -432,7 +432,7 @@ returns
path = "#{@path}/spool/"
FileUtils.mkpath path
file_path = "#{path}/#{Time.now.utc.to_f}-#{rand(99_999)}"
File.open( file_path, 'wb' ) { |file|
File.open(file_path, 'wb') { |file|
data = {
msg: msg,
timestamp: Time.now.utc.to_i,

View file

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

View file

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

View file

@ -187,13 +187,30 @@ class SessionBasicTest < ActiveSupport::TestCase
test 'c collections organization' do
require 'sessions/backend/collections/organization.rb'
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)
collection_client2 = Sessions::Backend::Collections::Organization.new(user, {}, false, '234-2', 3)
# create users
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
assert(!result1.empty?, 'check collections')
result2 = collection_client2.push
@ -208,10 +225,12 @@ class SessionBasicTest < ActiveSupport::TestCase
assert(!result2, 'check collections - recall')
# 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
# get whole collections
# get collection
result1 = collection_client1.push
assert(!result1.empty?, 'check collections - after create')
result2 = collection_client2.push
@ -230,7 +249,7 @@ class SessionBasicTest < ActiveSupport::TestCase
organization.touch
sleep 4
# get whole collections
# get collection
result1 = collection_client1.push
assert(!result1.empty?, 'check collections - after touch')
result2 = collection_client2.push
@ -264,7 +283,7 @@ class SessionBasicTest < ActiveSupport::TestCase
agent1 = User.create_or_update(
login: 'activity-stream-agent-1',
firstname: 'Session',
lastname: 'activity stream ' + rand(99_999).to_s,
lastname: "activity stream #{rand(99_999)}",
email: 'activity-stream-agent1@example.com',
password: 'agentpw',
active: true,

View file

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

View file

@ -176,8 +176,13 @@ class SessionEnhancedTest < ActiveSupport::TestCase
test 'b check client and backends' do
# create users
roles = Role.where(name: ['Agent'])
groups = Group.all
roles = Role.where(name: ['Agent'])
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
agent1 = User.create_or_update(
@ -187,6 +192,7 @@ class SessionEnhancedTest < ActiveSupport::TestCase
email: 'session-agent1@example.com',
password: 'agentpw',
active: true,
organization: organization,
roles: roles,
groups: groups,
)
@ -199,12 +205,12 @@ class SessionEnhancedTest < ActiveSupport::TestCase
email: 'session-agent2@example.com',
password: 'agentpw',
active: true,
organization: organization,
roles: roles,
groups: groups,
)
agent2.roles = roles
agent2.save
org = Organization.create(name: 'SomeOrg::' + rand(999_999).to_s, active: true)
# create sessions
client_id1_0 = '1234-1'