From 60b11db82509ea39f1845313e14f65ea9a4d7bdb Mon Sep 17 00:00:00 2001 From: Martin Edenhofer Date: Mon, 16 May 2016 19:49:06 +0200 Subject: [PATCH] Just push related user organisation to clients (improved tests). --- .gitignore | 2 +- lib/sessions.rb | 10 ++-- .../backend/collections/organization.rb | 5 +- script/websocket-server.rb | 3 +- test/unit/session_basic_test.rb | 37 ++++++++++---- test/unit/session_collections_test.rb | 49 ++++++++++++++++--- test/unit/session_enhanced_test.rb | 12 +++-- 7 files changed, 91 insertions(+), 27 deletions(-) diff --git a/.gitignore b/.gitignore index c6239efb2..d1e11b67a 100644 --- a/.gitignore +++ b/.gitignore @@ -12,7 +12,7 @@ # Ignore all logfiles and tempfiles. /log/*.log -/tmp/websocket/* +/tmp/websocket* /tmp/cache* /tmp/pids/* /tmp/*.png diff --git a/lib/sessions.rb b/lib/sessions.rb index 385d1909d..b175bf781 100644 --- a/lib/sessions.rb +++ b/lib/sessions.rb @@ -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, diff --git a/lib/sessions/backend/collections/organization.rb b/lib/sessions/backend/collections/organization.rb index 55a5390e2..a61388a12 100644 --- a/lib/sessions/backend/collections/organization.rb +++ b/lib/sessions/backend/collections/organization.rb @@ -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 diff --git a/script/websocket-server.rb b/script/websocket-server.rb index 485c1528d..b6fa8bd68 100755 --- a/script/websocket-server.rb +++ b/script/websocket-server.rb @@ -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, diff --git a/test/unit/session_basic_test.rb b/test/unit/session_basic_test.rb index 98f3b6e7e..a41415e4a 100644 --- a/test/unit/session_basic_test.rb +++ b/test/unit/session_basic_test.rb @@ -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, diff --git a/test/unit/session_collections_test.rb b/test/unit/session_collections_test.rb index 4f0e07c4a..770f8908f 100644 --- a/test/unit/session_collections_test.rb +++ b/test/unit/session_collections_test.rb @@ -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 diff --git a/test/unit/session_enhanced_test.rb b/test/unit/session_enhanced_test.rb index 1690cb310..68cbf92b6 100644 --- a/test/unit/session_enhanced_test.rb +++ b/test/unit/session_enhanced_test.rb @@ -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'