simple_crowd 1.0.5 → 1.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,13 +1,13 @@
1
- module SimpleCrowd
2
- module Mappers
3
- class SoapAttributes
4
- def self.produce hash
5
- {"int:SOAPAttribute" => hash.inject([]) {|attrs, (key, val)| attrs << {"int:name" => key, "int:values" => {"wsdl:string" => val}}}}
6
- end
7
- def self.parse attributes
8
- soap = attributes[:soap_attribute]
9
- (soap && soap.inject({}) {|hash, attr| hash[attr[:name].to_sym] = attr[:values][:string]; hash }) || {}
10
- end
11
- end
12
- end
13
- end
1
+ module SimpleCrowd
2
+ module Mappers
3
+ class SoapAttributes
4
+ def self.produce hash
5
+ {"int:SOAPAttribute" => hash.inject([]) {|attrs, (key, val)| attrs << {"int:name" => key, "int:values" => {"wsdl:string" => val}}}}
6
+ end
7
+ def self.parse attributes
8
+ soap = attributes[:soap_attribute]
9
+ (soap && soap.inject({}) {|hash, attr| hash[attr[:name].to_sym] = attr[:values][:string]; hash }) || {}
10
+ end
11
+ end
12
+ end
13
+ end
@@ -9,6 +9,8 @@ module SimpleCrowd
9
9
  end
10
10
  end
11
11
 
12
+ attr_accessor :app_token
13
+
12
14
  @users = []
13
15
  @tokens = []
14
16
  @groups = []
@@ -19,7 +21,6 @@ module SimpleCrowd
19
21
  def authenticate_application(name = nil, password = nil)
20
22
  "MOCKAPPTOKEN"
21
23
  end
22
- alias_method :app_token, :authenticate_application
23
24
 
24
25
  def authenticate_user name, password, factors = nil
25
26
  (user = find_user_by_name(name)) && user.password && user.password == password ? new_user_token(name) : nil
@@ -112,4 +113,4 @@ module SimpleCrowd
112
113
  def groups; self.class.groups; end
113
114
 
114
115
  end
115
- end
116
+ end
@@ -1,17 +1,17 @@
1
- module SimpleCrowd
2
- class User < CrowdEntity
3
-
4
- # Immutable properties
5
- property :id, :immutable => true
6
- property :username, :immutable => true, :map_soap => :name
7
- property :description, :immutable => true
8
- property :active, :immutable => true, :default => true
9
- property :directory_id, :immutable => true
10
-
11
- # Assumed available attributes (with soap aliases)
12
- property :first_name, :attribute => true, :map_soap => :givenName
13
- property :last_name, :attribute => true, :map_soap => :sn
14
- property :display_name, :attribute => true, :map_soap => :displayName
15
- property :email, :attribute => true, :map_soap => :mail
16
- end
17
- end
1
+ module SimpleCrowd
2
+ class User < CrowdEntity
3
+
4
+ # Immutable properties
5
+ property :id, :immutable => true
6
+ property :username, :immutable => true, :map_soap => :name
7
+ property :description, :immutable => true
8
+ property :active, :immutable => true, :default => true
9
+ property :directory_id, :immutable => true
10
+
11
+ # Assumed available attributes (with soap aliases)
12
+ property :first_name, :attribute => true, :map_soap => :givenName
13
+ property :last_name, :attribute => true, :map_soap => :sn
14
+ property :display_name, :attribute => true, :map_soap => :displayName
15
+ property :email, :attribute => true, :map_soap => :mail
16
+ end
17
+ end
@@ -1,3 +1,3 @@
1
1
  module SimpleCrowd
2
- VERSION = "1.0.5"
2
+ VERSION = "1.1.0"
3
3
  end
data/lib/simple_crowd.rb CHANGED
@@ -1,51 +1,53 @@
1
- require 'savon'
2
- require 'hashie'
3
- require 'yaml'
4
- require 'forwardable'
5
- require 'simple_crowd/crowd_entity'
6
- require 'simple_crowd/crowd_error'
7
- require 'simple_crowd/user'
8
- require 'simple_crowd/group'
9
- require 'simple_crowd/client'
10
- require 'simple_crowd/mappers/soap_attributes'
11
- Dir['simple_crowd/mappers/*.rb'].each {|file| require File.basename(file, File.extname(file)) }
12
-
13
- module SimpleCrowd
14
- class << self
15
- def config &config_block
16
- config_block.call(options)
17
- end
18
-
19
- def options app_options = {}
20
- c = soap_options.merge(default_crowd_options).merge(app_options) and
21
- c.merge(:service_url => c[:service_url] + 'services/SecurityServer')
22
- end
23
- def soap_options
24
- @soap_options ||= {
25
- :service_ns => "urn:SecurityServer",
26
- :service_namespaces => {
27
- 'xmlns:auth' => 'http://authentication.integration.crowd.atlassian.com',
28
- 'xmlns:ex' => 'http://exception.integration.crowd.atlassian.com',
29
- 'xmlns:int' => 'http://soap.integration.crowd.atlassian.com',
30
- 'xmlns:xsd' => 'http://www.w3.org/2001/XMLSchema',
31
- 'xmlns:xsi' => 'http://www.w3.org/2001/XMLSchema-instance'
32
- }
33
- }
34
- end
35
- def default_crowd_options
36
- @default_crowd_options ||= {
37
- :service_url => "http://localhost:8095/crowd/",
38
- :app_name => "crowd",
39
- :app_password => ""
40
- }
41
- defined?(IRB) ? @default_crowd_options.merge(config_file_options) : @default_crowd_options
42
- end
43
- def config_file_options
44
- @config_file_options ||= begin
45
- (File.exists?('config/crowd.yml') &&
46
- yml = (YAML.load_file('config/crowd.yml')[ENV["RAILS_ENV"] || "development"] || {}) and
47
- yml.symbolize_keys!) || {}
48
- end
49
- end
50
- end
51
- end
1
+ require 'savon'
2
+ require 'hashie'
3
+ require 'yaml'
4
+ require 'forwardable'
5
+ require 'simple_crowd/crowd_entity'
6
+ require 'simple_crowd/crowd_error'
7
+ require 'simple_crowd/user'
8
+ require 'simple_crowd/group'
9
+ require 'simple_crowd/client'
10
+ require 'simple_crowd/mappers/soap_attributes'
11
+ require 'simple_crowd/cache/null_store'
12
+ Dir['simple_crowd/mappers/*.rb'].each {|file| require File.basename(file, File.extname(file)) }
13
+
14
+ module SimpleCrowd
15
+ class << self
16
+ def config &config_block
17
+ config_block.call(options)
18
+ end
19
+
20
+ def options app_options = {}
21
+ c = soap_options.merge(default_crowd_options).merge(app_options) and
22
+ c.merge(:service_url => c[:service_url] + 'services/SecurityServer')
23
+ end
24
+ def soap_options
25
+ @soap_options ||= {
26
+ :service_ns => "urn:SecurityServer",
27
+ :service_namespaces => {
28
+ 'xmlns:auth' => 'http://authentication.integration.crowd.atlassian.com',
29
+ 'xmlns:ex' => 'http://exception.integration.crowd.atlassian.com',
30
+ 'xmlns:int' => 'http://soap.integration.crowd.atlassian.com',
31
+ 'xmlns:xsd' => 'http://www.w3.org/2001/XMLSchema',
32
+ 'xmlns:xsi' => 'http://www.w3.org/2001/XMLSchema-instance'
33
+ }
34
+ }
35
+ end
36
+ def default_crowd_options
37
+ @default_crowd_options ||= {
38
+ :service_url => "http://localhost:8095/crowd/",
39
+ :app_name => "crowd",
40
+ :app_password => "",
41
+ :cache_prefix => "simple_crowd.",
42
+ }
43
+ defined?(IRB) ? @default_crowd_options.merge(config_file_options) : @default_crowd_options
44
+ end
45
+ def config_file_options
46
+ @config_file_options ||= begin
47
+ (File.exists?('config/crowd.yml') &&
48
+ yml = (YAML.load_file('config/crowd.yml')[ENV["RAILS_ENV"] || "development"] || {}) and
49
+ yml.symbolize_keys!) || {}
50
+ end
51
+ end
52
+ end
53
+ end
data/simple_crowd.gemspec CHANGED
@@ -1,58 +1,27 @@
1
- $:.push File.expand_path("../lib",__FILE__)
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
2
3
  require 'simple_crowd/version'
3
4
 
4
5
  Gem::Specification.new do |s|
5
- s.name = %q{simple_crowd}
6
- s.version = SimpleCrowd::VERSION.dup
7
- s.platform = Gem::Platform::RUBY
8
- s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
9
- s.authors = ["Paul Strong"]
10
- s.description = %q{Simple Atlassian Crowd client using REST and SOAP APIs where needed.
11
- Doesn't do any fancy object mapping, etc.}
12
- s.email = %q{paul@thestrongfamily.org}
13
- s.files = %w(
14
- .gitignore
15
- Gemfile
16
- README.md
17
- Rakefile
18
- lib/simple_crowd.rb
19
- lib/simple_crowd/client.rb
20
- lib/simple_crowd/crowd_entity.rb
21
- lib/simple_crowd/crowd_error.rb
22
- lib/simple_crowd/group.rb
23
- lib/simple_crowd/mappers/soap_attributes.rb
24
- lib/simple_crowd/mock_client.rb
25
- lib/simple_crowd/user.rb
26
- lib/simple_crowd/version.rb
27
- simple_crowd.gemspec
28
- test/crowd_config.yml.example
29
- test/factories.rb
30
- test/helper.rb
31
- test/test_client.rb
32
- test/test_simple_crowd.rb
33
- test/test_user.rb
34
- )
35
- s.test_files = %w(
36
- test/crowd_config.yml.example
37
- test/factories.rb
38
- test/helper.rb
39
- test/test_client.rb
40
- test/test_simple_crowd.rb
41
- test/test_user.rb
42
- )
43
- s.homepage = %q{http://github.com/thinkwell/simple_crowd}
6
+ s.name = %q{simple_crowd}
7
+ s.version = SimpleCrowd::VERSION.dup
8
+ s.authors = ["Paul Strong"]
9
+ s.email = %q{paul@thestrongfamily.org}
10
+ s.homepage = %q{http://github.com/thinkwell/simple_crowd}
11
+ s.summary = %q{Simple Atlassian Crowd client using REST and SOAP APIs where needed.}
12
+ s.description = %q{Simple Atlassian Crowd client using REST and SOAP APIs where needed. Doesn't do any fancy object mapping, etc.}
13
+
14
+ s.rubyforge_project = "simple_crowd"
15
+
16
+ s.files = `git ls-files`.split("\n")
17
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
18
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
44
19
  s.require_paths = ["lib"]
45
- s.rubygems_version = %q{1.5.0}
46
- s.summary = %q{Simple Atlassian Crowd client using REST and SOAP APIs where needed.}
47
20
 
48
- s.add_development_dependency(%q<shoulda>, [">= 0"])
49
- s.add_development_dependency(%q<fcoury-matchy>, [">= 0"])
50
- s.add_development_dependency(%q<factory_girl>, [">= 0"])
51
- s.add_development_dependency(%q<forgery>, [">= 0"])
52
- s.add_development_dependency(%q<webmock>, [">= 0"])
53
- s.add_development_dependency(%q<rr>, [">= 0"])
21
+ s.add_runtime_dependency(%q<savon>, ["~> 0.9.9"])
22
+ s.add_runtime_dependency(%q<hashie>, [">= 1.0.0"])
23
+
24
+ s.add_development_dependency(%q<bundler>, [">= 1.0.21"])
54
25
  s.add_development_dependency(%q<rake>, [">= 0"])
55
- s.add_runtime_dependency(%q<savon>, ["= 0.7.9"])
56
- s.add_runtime_dependency(%q<hashie>, ["= 1.0.0"])
57
26
  end
58
27
 
@@ -1,6 +1,6 @@
1
- # Configure Crowd Test server for unit tests
2
- # Replace default values with crowd server params
3
- crowd:
4
- service_url: http://www.crowdserver.com:8095/crowd/
5
- app_name: crowdapp
6
- app_password: testpass
1
+ # Configure Crowd Test server for unit tests
2
+ # Replace default values with crowd server params
3
+ crowd:
4
+ service_url: http://www.crowdserver.com:8095/crowd/
5
+ app_name: crowdapp
6
+ app_password: testpass
data/test/factories.rb CHANGED
@@ -1,9 +1,11 @@
1
- Factory.define :user, :class => SimpleCrowd::User do |u|
2
- u.first_name 'Test'
3
- u.last_name 'User'
4
- u.display_name {|a| "#{a.first_name} #{a.last_name}"}
5
- u.email {|a| "#{a.first_name}.#{a.last_name}@example.com".downcase }
6
- u.sequence(:username) {|n| "test#{n}" }
7
- # Clear dirty properties
8
- u.after_build { |user| user.dirty_properties.clear }
9
- end
1
+ FactoryGirl.define do
2
+ factory :user, :class => SimpleCrowd::User do
3
+ first_name 'Test'
4
+ last_name 'User'
5
+ display_name {|a| "#{a.first_name} #{a.last_name}"}
6
+ email {|a| "#{a.first_name}.#{a.last_name}@example.com".downcase }
7
+ sequence(:username) {|n| "test#{n}" }
8
+ # Clear dirty properties
9
+ after_build { |user| user.dirty_properties.clear }
10
+ end
11
+ end
data/test/helper.rb CHANGED
@@ -1,28 +1,32 @@
1
- require 'rubygems'
2
-
3
- $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
4
- $LOAD_PATH.unshift(File.dirname(__FILE__))
5
-
6
- $CROWD_CONFIG_PATH = File.join(File.dirname(__FILE__), 'crowd_config.yml')
7
-
8
- require 'simple_crowd'
9
- require 'test/unit'
10
- require 'shoulda'
11
- require 'matchy'
12
- require 'webmock/test_unit'
13
- require 'rr'
14
- require 'factory_girl'
15
- require 'forgery'
16
-
17
- # Load factories
18
- require File.dirname(__FILE__) + "/factories"
19
-
20
- WebMock.allow_net_connect!
21
-
22
- class Test::Unit::TestCase
23
- include WebMock
24
- include RR::Adapters::TestUnit
25
- def setup
26
- WebMock.allow_net_connect!
27
- end
28
- end
1
+ require 'rubygems'
2
+
3
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
4
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
5
+
6
+ $CROWD_CONFIG_PATH = File.join(File.dirname(__FILE__), 'crowd_config.yml')
7
+
8
+ require 'simple_crowd'
9
+ require 'test/unit'
10
+ require 'shoulda'
11
+ require 'matchy'
12
+ require 'webmock/test_unit'
13
+ require 'rr'
14
+ require 'factory_girl'
15
+ require 'forgery'
16
+
17
+ # Load factories
18
+ require File.dirname(__FILE__) + "/factories"
19
+
20
+ WebMock.allow_net_connect!
21
+
22
+ class Test::Unit::TestCase
23
+ include RR::Adapters::TestUnit
24
+ def setup
25
+ WebMock.allow_net_connect!
26
+ end
27
+ end
28
+
29
+ Savon.configure do |config|
30
+ config.log = false
31
+ end
32
+ HTTPI.log = false
data/test/test_client.rb CHANGED
@@ -1,331 +1,329 @@
1
- require 'helper'
2
-
3
- class TestClient < Test::Unit::TestCase
4
- CROWD_CONFIG = YAML.load_file($CROWD_CONFIG_PATH)['crowd']
5
- context "A Client" do
6
- setup do
7
- @client = SimpleCrowd::Client.new({:service_url => CROWD_CONFIG['service_url'],
8
- :app_name => CROWD_CONFIG['app_name'],
9
- :app_password => CROWD_CONFIG['app_password']})
10
- @service_url = @client.options[:service_url]
11
- reset_webmock
12
- end
13
- should "initialize" do
14
- @client.should_not be nil
15
- assert_instance_of SimpleCrowd::Client, @client
16
- end
17
- should "get app token" do
18
- token = @client.app_token
19
- token.should_not be nil
20
- token.length.should == 24
21
-
22
- assert_requested :post, @service_url
23
- end
24
- should "refresh app token if invalid" do
25
- # Get initial valid token
26
- token = @client.app_token
27
- info = @client.get_cookie_info
28
- info.should_not be nil
29
- @client.app_token = token + "invalid"
30
- @client.app_token.should == token + "invalid"
31
- # making the token invalid should cause the client to refresh it
32
- # and get the cookie info successfully
33
- @client.get_cookie_info.should == info
34
- # Validate refreshed token is same as original token
35
- @client.app_token.should == token
36
- assert_requested :post, @service_url, :times => 5
37
- end
38
- should "get cookie info" do
39
- info = @client.get_cookie_info
40
- info.should_not be nil
41
- info[:domain].should_not be nil
42
- info[:domain].length.should > 0
43
- info[:secure].should_not be nil
44
- assert_requested :post, @service_url, :times => 2
45
- end
46
- should "authenticate user" do
47
- token = @client.authenticate_user "test", "test"
48
- token.should_not be nil
49
- token.length.should == 24
50
-
51
- assert_requested :post, @service_url, :times => 2
52
- end
53
- should "authenticate user with validation factors" do
54
- token = @client.authenticate_user "test", "test", {:test_factor => "test1234"}
55
- token.should_not be nil
56
- token.length.should == 24
57
-
58
- assert_requested :post, @service_url, :times => 2
59
- end
60
- should "create user token without password" do
61
- token = @client.create_user_token "test"
62
- token.should_not be nil
63
- token.length.should == 24
64
-
65
- assert_requested :post, @service_url, :times => 2
66
- end
67
- should "return same user token with or without password" do
68
- token_with_pass = @client.authenticate_user "test", "test"
69
- token_with_pass.should_not be nil
70
- token_with_pass.length.should == 24
71
- token_without_pass = @client.create_user_token "test"
72
- token_without_pass.should_not be nil
73
- token_with_pass.length.should == 24
74
-
75
- token_with_pass.should == token_without_pass
76
-
77
- assert_requested :post, @service_url, :times => 3
78
- end
79
- should "validate user token" do
80
- token = @client.authenticate_user "test", "test"
81
- valid = @client.is_valid_user_token? token
82
- valid.should be true
83
- invalid = @client.is_valid_user_token?(token + "void")
84
- invalid.should be false
85
- assert_requested :post, @service_url, :times => 4
86
- end
87
- should "validate user token with factors" do
88
- token = @client.authenticate_user "test", "test", {"Random-Number" => 6375}
89
- @client.is_valid_user_token?(token).should be false
90
- @client.is_valid_user_token?(token, {"Random-Number" => 6375}).should be true
91
- token2 = @client.authenticate_user "test", "test"
92
- @client.is_valid_user_token?(token2, {"Random-Number" => 48289}).should be false
93
- assert_requested :post, @service_url, :times => 6
94
- end
95
- should "invalidate user token (logout)" do
96
- token = @client.authenticate_user "test", "test"
97
- @client.is_valid_user_token?(token).should be true
98
-
99
- # Invalidate nonexistant token
100
- @client.invalidate_user_token(token + "void").should be true
101
- @client.is_valid_user_token?(token).should be true
102
-
103
- # Invalidate token
104
- @client.invalidate_user_token(token).should be true
105
- @client.is_valid_user_token?(token).should be false
106
-
107
- assert_requested :post, @service_url, :times => 7
108
- end
109
- should "reset user password" do
110
- # Get real app token before mocking reset call
111
- @client.app_token
112
- WebMock.disable_net_connect!
113
-
114
- response = %Q{<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"
115
- xmlns:xsd="http://www.w3.org/2001/XMLSchema"
116
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
117
- <soap:Body><ns1:resetPrincipalCredentialResponse xmlns:ns1="urn:SecurityServer" />
118
- </soap:Body></soap:Envelope>
119
- }
120
- stub_request(:post, @service_url).to_return(:body => response, :status => 200)
121
-
122
- @client.reset_user_password("test").should be true
123
- end
124
- should "find all user names" do
125
- names = @client.find_all_user_names
126
- names.should_not be nil
127
- names.is_a?(Array).should be true
128
- names.empty?.should be false
129
- names.include?("test").should be true
130
- end
131
- should "find user by name" do
132
- user = @client.find_user_by_name "test"
133
- user.should_not be nil
134
- [:id, :username, :description, :active, :directory_id, :first_name, :last_name, :email].each {|v| user.key?(v).should be true}
135
- [:id, :username, :active, :directory_id].each {|v| user[v].should_not be nil}
136
- assert_requested :post, @service_url, :times => 2
137
- end
138
- should "find user by token" do
139
- token = @client.authenticate_user "test", "test"
140
- user = @client.find_user_by_token token
141
- user.should_not be nil
142
- user.first_name.should == "Test"
143
-
144
- assert_requested :post, @service_url, :times => 3
145
- end
146
- should "find username by token" do
147
- token = @client.authenticate_user "test", "test"
148
- user = @client.find_username_by_token token
149
- user.should_not be nil
150
- user.length.should > 0
151
- user.should == "test"
152
-
153
- assert_requested :post, @service_url, :times => 3
154
- end
155
- should "find user by email" do
156
- user = @client.find_user_by_email "test@testing.com"
157
- user.should_not be nil
158
- user.first_name.should == "Test"
159
- user.last_name.should == "User"
160
-
161
- # partial searches should return nothing
162
- user = @client.find_user_by_email "test"
163
- user.should be nil
164
-
165
- assert_requested :post, @service_url, :times => 3
166
- end
167
- should "search for users by email" do
168
- users = @client.search_users_by_email "test"
169
- users.empty?.should_not be true
170
- users.all?{|u| u.email =~ /test/ }.should be true
171
-
172
- assert_requested :post, @service_url, :times => 2
173
- end
174
- should "search users" do
175
- users = @client.search_users({'principal.email' => "test@testing.com"})
176
- users.should_not be nil
177
- users.empty?.should_not be true
178
- users.all?{|u| u.email == "test@testing.com" }.should be true
179
-
180
- users = @client.search_users({'principal.fullname' => "Test"})
181
- users.should_not be nil
182
- users.empty?.should_not be true
183
- users.each{|u| u.display_name.downcase.should =~ /test/ }
184
-
185
- assert_requested :post, @service_url, :times => 3
186
- end
187
- should "return nil for nonexistant user" do
188
- user = @client.find_user_by_name "nonexistant"
189
- user.should be nil
190
- assert_requested :post, @service_url, :times => 2
191
- end
192
- should "update user credential" do
193
- @client.authenticate_user("test", "test").should_not be nil
194
- @client.update_user_credential("test", "testupdate").should be true
195
- lambda {@client.authenticate_user("test", "test")}.should raise_error
196
- @client.authenticate_user("test", "testupdate").should_not be nil
197
- @client.update_user_credential("test", "test").should be true
198
- end
199
- should "add/remove user" do
200
- localuser = Factory.build(:user)
201
- user = @client.add_user(localuser, "newuserpass")
202
- user.should_not be nil
203
- user.username.should == localuser.username
204
- user.first_name.should == localuser.first_name
205
- user.last_name.should == localuser.last_name
206
- @client.authenticate_user(localuser.username, "newuserpass").should_not be nil
207
- @client.remove_user(localuser.username).should be true
208
- lambda {@client.authenticate_user(localuser.username, "newuserpass")}.should raise_error
209
-
210
- assert_requested :post, @service_url, :times => 5
211
- end
212
-
213
- should "update user attribute" do
214
- username = "test_update"
215
- localuser = Factory.build(:user, :username => username)
216
- remoteuser = @client.add_user(localuser, "updatepass")
217
- @client.update_user_attribute(username, 'givenName', 'UpdatedFirst').should be true
218
- updateduser = @client.find_user_by_name(username)
219
- updateduser.last_name.should == localuser.last_name
220
- updateduser.first_name.should == 'UpdatedFirst'
221
- @client.remove_user "test_update"
222
- end
223
- should "update user custom attribute" do
224
- username = "test_update"
225
- localuser = Factory.build(:user, :username => username)
226
- remoteuser = @client.add_user(localuser, "updatepass")
227
- @client.update_user_attribute(username, 'customAttr', 'customVal').should be true
228
- remoteuser = @client.find_user_with_attributes_by_name username
229
- remoteuser.last_name.should == localuser.last_name
230
-
231
- remoteuser[:customAttr].should == 'customVal'
232
- @client.remove_user "test_update"
233
- end
234
- should "update user attribute array" do
235
- username = "test_update"
236
- localuser = Factory.build(:user, :username => username)
237
- remoteuser = @client.add_user(localuser, "updatepass")
238
- test_array = ["one", "two", "4"]
239
- @client.update_user_attribute(username, 'arrayTest', test_array).should be true
240
- remoteuser = @client.find_user_with_attributes_by_name username
241
- remoteuser.last_name.should == localuser.last_name
242
- remoteuser[:arrayTest].sort.should == test_array.sort
243
- test_array.delete "two"
244
- @client.update_user_attribute(username, 'arrayTest', test_array).should be true
245
- remoteuser = @client.find_user_with_attributes_by_name username
246
- remoteuser[:arrayTest].sort.should == test_array.sort
247
- remoteuser[:arrayTest].include?("two").should be false
248
- @client.remove_user "test_update"
249
- end
250
- should "update user" do
251
- username = "test_update"
252
- localuser = Factory.build(:user, :username => username)
253
- remoteuser = @client.add_user(localuser, "updatepass")
254
- remoteuser.should_not be nil
255
- remoteuser.username.should == localuser.username
256
- remoteuser.first_name.should == localuser.first_name
257
- remoteuser.last_name.should == localuser.last_name
258
-
259
- remoteuser.dirty?.should be false
260
- # Should be ignored
261
- remoteuser.active = false
262
- remoteuser.first_name = "UpdatedFirst"
263
- remoteuser.last_name = "UpdatedLast"
264
- remoteuser.dirty?.should be true
265
-
266
- remoteuser.dirty_attributes.should == [:first_name, :last_name]
267
-
268
- @client.update_user(remoteuser)
269
-
270
- remoteuser = @client.find_user_with_attributes_by_name username
271
- remoteuser.first_name.should == "UpdatedFirst"
272
- remoteuser.last_name.should == "UpdatedLast"
273
- remoteuser.email.should == localuser.email
274
- @client.remove_user "test_update"
275
- end
276
- should "check if cache enabled" do
277
- enabled = @client.is_cache_enabled?
278
- is_true = enabled.class == TrueClass
279
- is_false = enabled.class == FalseClass
280
- (is_true || is_false).should be true
281
- end
282
- should "get granted authorities" do
283
- granted = @client.get_granted_authorities
284
- (granted.nil? || (granted.is_a?(Array) && !granted.empty? && granted[0].is_a?(String))).should be true
285
-
286
- assert_requested :post, @service_url, :times => 2
287
- end
288
- should "find group by name" do
289
- group = @client.find_group_by_name("Testing")
290
- group.should_not be nil
291
- assert_requested :post, @service_url, :times => 2
292
- end
293
- should "find all group names" do
294
- names = @client.find_all_group_names
295
- names.should_not be nil
296
- names.is_a?(Array).should be true
297
- names.empty?.should be false
298
- names.include?("Testing").should be true
299
- end
300
- should "add/remove user from group" do
301
- @client.add_user_to_group("test", "Testing").should be true
302
- @client.is_group_member?("Testing", "test").should be true
303
- @client.remove_user_from_group("test", "Testing").should be true
304
- @client.is_group_member?("Testing", "test").should be false
305
- assert_requested :post, @service_url, :times => 5
306
- end
307
- # should "add/remove attribute from group" do
308
- # @client.add_attribute_to_group("test", "tmpattribute", "Hello World").should be true
309
- # @client.remove_attribute_from_group("test", "tmpattribute").should be true
310
- # end
311
- should "update group" do
312
- @client.find_group_by_name("Testing").active.should be true
313
- @client.update_group("Testing", "Test Description", false).should be true
314
- updated_group = @client.find_group_by_name("Testing")
315
- updated_group.active.should be false
316
- updated_group.description.should == "Test Description"
317
- @client.update_group("Testing", "", true).should be true
318
- end
319
- should "check if user is group member" do
320
- @client.add_user_to_group("test", "Testing").should be true
321
- @client.is_group_member?("Testing", "test").should be true
322
- @client.is_group_member?("nonexistantgroup", "test").should be false
323
- assert_requested :post, @service_url, :times => 4
324
- end
325
- should "accept cached app token" do
326
- @client.app_token = "cachedtoken"
327
- @client.app_token.should == "cachedtoken"
328
- assert_not_requested :post, @service_url
329
- end
330
- end
331
- end
1
+ require 'helper'
2
+
3
+ class TestClient < Test::Unit::TestCase
4
+ CROWD_CONFIG = YAML.load_file($CROWD_CONFIG_PATH)['crowd']
5
+ context "A Client" do
6
+ setup do
7
+ @client = SimpleCrowd::Client.new({:service_url => CROWD_CONFIG['service_url'],
8
+ :app_name => CROWD_CONFIG['app_name'],
9
+ :app_password => CROWD_CONFIG['app_password']})
10
+ @service_url = @client.options[:service_url]
11
+ WebMock.reset!
12
+ end
13
+ should "initialize" do
14
+ @client.should_not be nil
15
+ assert_instance_of SimpleCrowd::Client, @client
16
+ end
17
+ should "get app token" do
18
+ @client.app_token.should be nil
19
+ @client.get_cookie_info
20
+ token = @client.app_token
21
+ token.should_not be nil
22
+ token.length.should == 24
23
+
24
+ assert_requested :post, @service_url, :times => 2
25
+ end
26
+ should "refresh app token if invalid" do
27
+ @client.app_token = "invalid"
28
+ @client.app_token.should == "invalid"
29
+ # making the token invalid should cause the client to refresh it
30
+ # and get the cookie info successfully
31
+ @client.get_cookie_info
32
+ # Validate refreshed token is same as original token
33
+ @client.app_token.should_not == "invalid"
34
+ assert_requested :post, @service_url, :times => 3
35
+ end
36
+ should "get cookie info" do
37
+ info = @client.get_cookie_info
38
+ info.should_not be nil
39
+ info[:domain].should_not be nil
40
+ info[:domain].length.should > 0
41
+ info[:secure].should_not be nil
42
+ assert_requested :post, @service_url, :times => 2
43
+ end
44
+ should "authenticate user" do
45
+ token = @client.authenticate_user "test", "test"
46
+ token.should_not be nil
47
+ token.length.should == 24
48
+
49
+ assert_requested :post, @service_url, :times => 2
50
+ end
51
+ should "authenticate user with validation factors" do
52
+ token = @client.authenticate_user "test", "test", {:test_factor => "test1234"}
53
+ token.should_not be nil
54
+ token.length.should == 24
55
+
56
+ assert_requested :post, @service_url, :times => 2
57
+ end
58
+ should "create user token without password" do
59
+ token = @client.create_user_token "test"
60
+ token.should_not be nil
61
+ token.length.should == 24
62
+
63
+ assert_requested :post, @service_url, :times => 2
64
+ end
65
+ should "return same user token with or without password" do
66
+ token_with_pass = @client.authenticate_user "test", "test"
67
+ token_with_pass.should_not be nil
68
+ token_with_pass.length.should == 24
69
+ token_without_pass = @client.create_user_token "test"
70
+ token_without_pass.should_not be nil
71
+ token_with_pass.length.should == 24
72
+
73
+ token_with_pass.should == token_without_pass
74
+
75
+ assert_requested :post, @service_url, :times => 3
76
+ end
77
+ should "validate user token" do
78
+ token = @client.authenticate_user "test", "test"
79
+ valid = @client.is_valid_user_token? token
80
+ valid.should be true
81
+ invalid = @client.is_valid_user_token?(token + "void")
82
+ invalid.should be false
83
+ assert_requested :post, @service_url, :times => 4
84
+ end
85
+ should "validate user token with factors" do
86
+ token = @client.authenticate_user "test", "test", {"Random-Number" => 6375}
87
+ @client.is_valid_user_token?(token).should be false
88
+ @client.is_valid_user_token?(token, {"Random-Number" => 6375}).should be true
89
+ token2 = @client.authenticate_user "test", "test"
90
+ @client.is_valid_user_token?(token2, {"Random-Number" => 48289}).should be false
91
+ assert_requested :post, @service_url, :times => 6
92
+ end
93
+ should "invalidate user token (logout)" do
94
+ token = @client.authenticate_user "test", "test"
95
+ @client.is_valid_user_token?(token).should be true
96
+
97
+ # Invalidate nonexistant token
98
+ @client.invalidate_user_token(token + "void").should be true
99
+ @client.is_valid_user_token?(token).should be true
100
+
101
+ # Invalidate token
102
+ @client.invalidate_user_token(token).should be true
103
+ @client.is_valid_user_token?(token).should be false
104
+
105
+ assert_requested :post, @service_url, :times => 7
106
+ end
107
+ should "reset user password" do
108
+ # Get real app token before mocking reset call
109
+ @client.app_token = @client.authenticate_application
110
+ WebMock.disable_net_connect!
111
+
112
+ response = %Q{<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"
113
+ xmlns:xsd="http://www.w3.org/2001/XMLSchema"
114
+ xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
115
+ <soap:Body><ns1:resetPrincipalCredentialResponse xmlns:ns1="urn:SecurityServer" />
116
+ </soap:Body></soap:Envelope>
117
+ }
118
+ stub_request(:post, @service_url).to_return(:body => response, :status => 200)
119
+
120
+ @client.reset_user_password("test").should be true
121
+ end
122
+ should "find all user names" do
123
+ names = @client.find_all_user_names
124
+ names.should_not be nil
125
+ names.is_a?(Array).should be true
126
+ names.empty?.should be false
127
+ names.include?("test").should be true
128
+ end
129
+ should "find user by name" do
130
+ user = @client.find_user_by_name "test"
131
+ user.should_not be nil
132
+ [:id, :username, :description, :active, :directory_id, :first_name, :last_name, :email].each {|v| user.key?(v).should be true}
133
+ [:id, :username, :active, :directory_id].each {|v| user[v].should_not be nil}
134
+ assert_requested :post, @service_url, :times => 2
135
+ end
136
+ should "find user by token" do
137
+ token = @client.authenticate_user "test", "test"
138
+ user = @client.find_user_by_token token
139
+ user.should_not be nil
140
+ user.first_name.should == "Test"
141
+
142
+ assert_requested :post, @service_url, :times => 3
143
+ end
144
+ should "find username by token" do
145
+ token = @client.authenticate_user "test", "test"
146
+ user = @client.find_username_by_token token
147
+ user.should_not be nil
148
+ user.length.should > 0
149
+ user.should == "test"
150
+
151
+ assert_requested :post, @service_url, :times => 3
152
+ end
153
+ should "find user by email" do
154
+ user = @client.find_user_by_email "test@testing.com"
155
+ user.should_not be nil
156
+ user.first_name.should == "Test"
157
+ user.last_name.should == "User"
158
+
159
+ # partial searches should return nothing
160
+ user = @client.find_user_by_email "test"
161
+ user.should be nil
162
+
163
+ assert_requested :post, @service_url, :times => 3
164
+ end
165
+ should "search for users by email" do
166
+ users = @client.search_users_by_email "test"
167
+ users.empty?.should_not be true
168
+ users.all?{|u| u.email =~ /test/ }.should be true
169
+
170
+ assert_requested :post, @service_url, :times => 2
171
+ end
172
+ should "search users" do
173
+ users = @client.search_users({'principal.email' => "test@testing.com"})
174
+ users.should_not be nil
175
+ users.empty?.should_not be true
176
+ users.all?{|u| u.email == "test@testing.com" }.should be true
177
+
178
+ users = @client.search_users({'principal.fullname' => "Test"})
179
+ users.should_not be nil
180
+ users.empty?.should_not be true
181
+ users.each{|u| u.display_name.downcase.should =~ /test/ }
182
+
183
+ assert_requested :post, @service_url, :times => 3
184
+ end
185
+ should "return nil for nonexistant user" do
186
+ user = @client.find_user_by_name "nonexistant"
187
+ user.should be nil
188
+ assert_requested :post, @service_url, :times => 2
189
+ end
190
+ should "update user credential" do
191
+ @client.authenticate_user("test", "test").should_not be nil
192
+ @client.update_user_credential("test", "testupdate").should be true
193
+ lambda {@client.authenticate_user("test", "test")}.should raise_error
194
+ @client.authenticate_user("test", "testupdate").should_not be nil
195
+ @client.update_user_credential("test", "test").should be true
196
+ end
197
+ should "add/remove user" do
198
+ localuser = FactoryGirl.build(:user)
199
+ user = @client.add_user(localuser, "newuserpass")
200
+ user.should_not be nil
201
+ user.username.should == localuser.username
202
+ user.first_name.should == localuser.first_name
203
+ user.last_name.should == localuser.last_name
204
+ @client.authenticate_user(localuser.username, "newuserpass").should_not be nil
205
+ @client.remove_user(localuser.username).should be true
206
+ lambda {@client.authenticate_user(localuser.username, "newuserpass")}.should raise_error
207
+
208
+ assert_requested :post, @service_url, :times => 5
209
+ end
210
+
211
+ should "update user attribute" do
212
+ username = "test_update"
213
+ localuser = FactoryGirl.build(:user, :username => username)
214
+ remoteuser = @client.add_user(localuser, "updatepass")
215
+ @client.update_user_attribute(username, 'givenName', 'UpdatedFirst').should be true
216
+ updateduser = @client.find_user_by_name(username)
217
+ updateduser.last_name.should == localuser.last_name
218
+ updateduser.first_name.should == 'UpdatedFirst'
219
+ @client.remove_user username
220
+ end
221
+ should "update user custom attribute" do
222
+ username = "test_update"
223
+ localuser = FactoryGirl.build(:user, :username => username)
224
+ remoteuser = @client.add_user(localuser, "updatepass")
225
+ @client.update_user_attribute(username, 'customAttr', 'customVal').should be true
226
+ remoteuser = @client.find_user_with_attributes_by_name username
227
+ remoteuser.last_name.should == localuser.last_name
228
+
229
+ remoteuser[:customAttr].should == 'customVal'
230
+ @client.remove_user username
231
+ end
232
+ should "update user attribute array" do
233
+ username = "test_update"
234
+ localuser = FactoryGirl.build(:user, :username => username)
235
+ remoteuser = @client.add_user(localuser, "updatepass")
236
+ test_array = ["one", "two", "4"]
237
+ @client.update_user_attribute(username, 'arrayTest', test_array).should be true
238
+ remoteuser = @client.find_user_with_attributes_by_name username
239
+ remoteuser.last_name.should == localuser.last_name
240
+ remoteuser[:arrayTest].sort.should == test_array.sort
241
+ test_array.delete "two"
242
+ @client.update_user_attribute(username, 'arrayTest', test_array).should be true
243
+ remoteuser = @client.find_user_with_attributes_by_name username
244
+ remoteuser[:arrayTest].sort.should == test_array.sort
245
+ remoteuser[:arrayTest].include?("two").should be false
246
+ @client.remove_user username
247
+ end
248
+ should "update user" do
249
+ username = "test_update"
250
+ localuser = FactoryGirl.build(:user, :username => username)
251
+ remoteuser = @client.add_user(localuser, "updatepass")
252
+ remoteuser.should_not be nil
253
+ remoteuser.username.should == localuser.username
254
+ remoteuser.first_name.should == localuser.first_name
255
+ remoteuser.last_name.should == localuser.last_name
256
+
257
+ remoteuser.dirty?.should be false
258
+ # Should be ignored
259
+ remoteuser.active = false
260
+ remoteuser.first_name = "UpdatedFirst"
261
+ remoteuser.last_name = "UpdatedLast"
262
+ remoteuser.dirty?.should be true
263
+
264
+ remoteuser.dirty_attributes.should == [:first_name, :last_name]
265
+
266
+ @client.update_user(remoteuser)
267
+
268
+ remoteuser = @client.find_user_with_attributes_by_name username
269
+ remoteuser.first_name.should == "UpdatedFirst"
270
+ remoteuser.last_name.should == "UpdatedLast"
271
+ remoteuser.email.should == localuser.email
272
+ @client.remove_user username
273
+ end
274
+ should "check if cache enabled" do
275
+ enabled = @client.is_cache_enabled?
276
+ is_true = enabled.class == TrueClass
277
+ is_false = enabled.class == FalseClass
278
+ (is_true || is_false).should be true
279
+ end
280
+ should "get granted authorities" do
281
+ granted = @client.get_granted_authorities
282
+ (granted.nil? || (granted.is_a?(Array) && !granted.empty? && granted[0].is_a?(String))).should be true
283
+
284
+ assert_requested :post, @service_url, :times => 2
285
+ end
286
+ should "find group by name" do
287
+ group = @client.find_group_by_name("Testing")
288
+ group.should_not be nil
289
+ assert_requested :post, @service_url, :times => 2
290
+ end
291
+ should "find all group names" do
292
+ names = @client.find_all_group_names
293
+ names.should_not be nil
294
+ names.is_a?(Array).should be true
295
+ names.empty?.should be false
296
+ names.include?("Testing").should be true
297
+ end
298
+ should "add/remove user from group" do
299
+ @client.add_user_to_group("test", "Testing").should be true
300
+ @client.is_group_member?("Testing", "test").should be true
301
+ @client.remove_user_from_group("test", "Testing").should be true
302
+ @client.is_group_member?("Testing", "test").should be false
303
+ assert_requested :post, @service_url, :times => 5
304
+ end
305
+ # should "add/remove attribute from group" do
306
+ # @client.add_attribute_to_group("test", "tmpattribute", "Hello World").should be true
307
+ # @client.remove_attribute_from_group("test", "tmpattribute").should be true
308
+ # end
309
+ should "update group" do
310
+ @client.find_group_by_name("Testing").active.should be true
311
+ @client.update_group("Testing", "Test Description", false).should be true
312
+ updated_group = @client.find_group_by_name("Testing")
313
+ updated_group.active.should be false
314
+ updated_group.description.should == "Test Description"
315
+ @client.update_group("Testing", "", true).should be true
316
+ end
317
+ should "check if user is group member" do
318
+ @client.add_user_to_group("test", "Testing").should be true
319
+ @client.is_group_member?("Testing", "test").should be true
320
+ @client.is_group_member?("nonexistantgroup", "test").should be false
321
+ assert_requested :post, @service_url, :times => 4
322
+ end
323
+ should "accept cached app token" do
324
+ @client.app_token = "cachedtoken"
325
+ @client.app_token.should == "cachedtoken"
326
+ assert_not_requested :post, @service_url
327
+ end
328
+ end
329
+ end