disiid_user 3.1.0 → 3.1.1

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.
data/disiid_user.gemspec CHANGED
@@ -25,4 +25,5 @@ Gem::Specification.new do |s|
25
25
  s.add_development_dependency "bundler"
26
26
  # runtime dependencies
27
27
  s.add_runtime_dependency "activeresource", '~> 3'
28
+ s.add_runtime_dependency "activerecord", '~> 3'
28
29
  end
@@ -1,3 +1,3 @@
1
1
  module DisiidUser
2
- VERSION = "3.1.0"
2
+ VERSION = "3.1.1"
3
3
  end
data/lib/disiid_user.rb CHANGED
@@ -32,12 +32,41 @@ module DisiidUser
32
32
  # Class Methods
33
33
  #
34
34
  module ClassMethods
35
- # Search
36
- # Return list of users that match the query search params
35
+ # Returns a list of users as ActiveResource objects that match given query params, e.g.
36
+ # User.search(:q => "dude") # => [{name: 'dude', ...}, {...}]
37
+ # User.search(:position => 'whatever) # => []
37
38
  def search(args = {})
38
39
  args.merge! :auth_token => DisiidUser::RemoteUser.auth_token
39
40
  DisiidUser::RemoteUser.find :all, :params => args
40
41
  end
42
+
43
+ # Creates a new user object with the identity attribute assigned
44
+ # and returns a not presisted record.
45
+ # You'll need to call .save or .save! on the returned result
46
+ # if you want to persist it in a local database.
47
+ #
48
+ # Note: your local :users table should :identity_url string attribute for this to work.
49
+ def build_from_identity(url)
50
+ user = new
51
+ user.identity_url = url
52
+ user
53
+ end
54
+
55
+ # Looks for a local user record with a given UUID by matching its local attribute :identity_url.
56
+ # Creates a new user if it doesn't exist locally but is present remotely using #build_from_identity method.
57
+ # This method will throw ActiveRecord::RecordNotFound exception if persistance process didn't go well.
58
+ def find_local_or_remote_by_uuid!(id)
59
+ user = first(conditions: ['identity_url LIKE ?', "%#{id}"])
60
+ if !user && DisiidUser::RemoteUser.exists?(id, params: {auth_token: DisiidUser::RemoteUser.auth_token})
61
+ user_url = "#{DisiidUser::RemoteUser.site}/#{DisiidUser::RemoteUser.collection_name}/#{id}"
62
+ user = build_from_identity(user_url)
63
+ user.save!
64
+ end
65
+ user
66
+ rescue
67
+ raise ::ActiveRecord::RecordNotFound
68
+ end
69
+
41
70
  end
42
71
 
43
72
  def self.version_string
@@ -118,8 +147,7 @@ module DisiidUser
118
147
  # Last part of identity_url, after path 'user' (element_name of RemoteUser params)
119
148
  def uuid
120
149
  @uuid ||= begin
121
- collection_name = DisiidUser::RemoteUser.collection_name
122
- /.*\/#{collection_name}\/([a-z0-9\-]+)$/.match(identity_url.to_s)[1]
150
+ /.*\/#{DisiidUser::RemoteUser.collection_name}\/([a-z0-9\-]+)$/.match(identity_url.to_s)[1]
123
151
  rescue; nil; end
124
152
  end
125
153
 
@@ -145,6 +173,7 @@ module DisiidUser
145
173
  # defaults to 1 hour
146
174
  cattr_accessor :cache_expiry
147
175
  self.cache_expiry = 3600
176
+
148
177
 
149
178
  protected
150
179
 
@@ -36,6 +36,87 @@ describe DisiidUser do
36
36
  @user.uuid.should be_nil
37
37
  end
38
38
 
39
+ it "should build a new user with correct identity url" do
40
+ u = User.build_from_identity 'http://openid.provider.local/users/6a002a40-bc63-4095-9bb0-f31bf386bf55'
41
+
42
+ u.should be_instance_of(User)
43
+ u.should be_new_record
44
+ u.identity_url.should == 'http://openid.provider.local/users/6a002a40-bc63-4095-9bb0-f31bf386bf55'
45
+ u.uuid.should == '6a002a40-bc63-4095-9bb0-f31bf386bf55'
46
+ end
47
+
48
+ describe "#find_local_or_remote_by_uuid!" do
49
+ let(:user) { User }
50
+ subject { user }
51
+
52
+ it { should respond_to :find_local_or_remote_by_uuid! }
53
+ it "should look for a local user" do
54
+ user.should_receive(:first).with(hash_including(conditions: ['identity_url LIKE ?', '%some-uuid'])).and_return(true)
55
+ user.find_local_or_remote_by_uuid!('some-uuid')
56
+ end
57
+
58
+ context "when local user doesn't exist" do
59
+ let(:new_user) { mock('new user', :save! => true) }
60
+ before {
61
+ user.stub(:first).and_return(nil)
62
+ user.stub(:build_from_identity).and_return(new_user)
63
+ }
64
+
65
+ it "should try and see if remote user exists" do
66
+ DisiidUser::RemoteUser.should_receive(:exists?).with('some-uuid', params: hash_including(
67
+ auth_token: DisiidUser::RemoteUser.auth_token
68
+ ))
69
+ user.find_local_or_remote_by_uuid!('some-uuid')
70
+ end
71
+
72
+ context "but the remote does" do
73
+ before { DisiidUser::RemoteUser.stub(:exists?).and_return(true) }
74
+ it "should build a new user" do
75
+ user.should_receive(:build_from_identity).with('http://localhost:3001/users/a-uuid')
76
+ user.find_local_or_remote_by_uuid!('a-uuid')
77
+ end
78
+
79
+ it "should store user with .save!" do
80
+ new_user.should_receive(:save!)
81
+ user.find_local_or_remote_by_uuid!('a-uuid')
82
+ end
83
+
84
+ it "should return newly created user" do
85
+ user.find_local_or_remote_by_uuid!('a-uuid').should == new_user
86
+ end
87
+
88
+ it "should raise 'record not found' if .save! fails" do
89
+ new_user.stub(:save!).and_raise('some exception')
90
+ expect { user.find_local_or_remote_by_uuid!('some-uuid') }.to raise_error(ActiveRecord::RecordNotFound)
91
+ end
92
+ end
93
+
94
+ context "and niether does remote" do
95
+ before { DisiidUser::RemoteUser.stub(:exists?).and_return(false) }
96
+ subject { user.find_local_or_remote_by_uuid!('some-uuid') }
97
+ it { should be_nil }
98
+ end
99
+
100
+ context "and an exception's been raised" do
101
+ before { DisiidUser::RemoteUser.stub(:exists?).and_raise('some error') }
102
+ it "should throw 'record not found'" do
103
+ expect { user.find_local_or_remote_by_uuid!('some-uuid') }.to raise_error(ActiveRecord::RecordNotFound)
104
+ end
105
+ end
106
+ end
107
+
108
+ context "when local user exists" do
109
+ let(:existing_user) { mock('existing user') }
110
+ before { user.stub(:first).and_return(existing_user) }
111
+
112
+ it "should not fetch from remote" do
113
+ DisiidUser::RemoteUser.should_not_receive(:exists?)
114
+ user.should_not_receive(:build_from_identity)
115
+ user.find_local_or_remote_by_uuid!('some-uuid').should == existing_user
116
+ end
117
+ end
118
+ end
119
+
39
120
  context "with local roles" do
40
121
 
41
122
  it 'should correctly parse :role attribute' do
data/spec/spec_helper.rb CHANGED
@@ -1,5 +1,6 @@
1
1
  require 'rspec'
2
2
  require 'disiid_user'
3
+ require 'active_record/errors'
3
4
 
4
5
  RSpec.configure do |config|
5
6
  config.color_enabled = true
data/spec/stub_user.rb CHANGED
@@ -11,11 +11,18 @@ end
11
11
  # - role (Integer)
12
12
  # - identity_url (String)
13
13
  class User
14
+ attr_accessor :new_record, :role, :identity_url
15
+ alias_method :new_record?, :new_record
16
+
17
+ def initialize(*args)
18
+ super(*args)
19
+ @new_record = true
20
+ end
21
+
14
22
  # stub methods and attributes
15
23
  def self.scope(*args); end
16
- attr_accessor :role, :identity_url
17
24
 
18
- include DisiidUser
25
+ include DisiidUser
19
26
  end
20
27
 
21
28
  class FakeResponse
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: disiid_user
3
3
  version: !ruby/object:Gem::Version
4
- version: 3.1.0
4
+ version: 3.1.1
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -10,11 +10,11 @@ authors:
10
10
  autorequire:
11
11
  bindir: bin
12
12
  cert_chain: []
13
- date: 2011-12-15 00:00:00.000000000Z
13
+ date: 2011-12-21 00:00:00.000000000Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: rspec
17
- requirement: &70317348018360 !ruby/object:Gem::Requirement
17
+ requirement: &70154939590940 !ruby/object:Gem::Requirement
18
18
  none: false
19
19
  requirements:
20
20
  - - ! '>='
@@ -22,10 +22,10 @@ dependencies:
22
22
  version: '0'
23
23
  type: :development
24
24
  prerelease: false
25
- version_requirements: *70317348018360
25
+ version_requirements: *70154939590940
26
26
  - !ruby/object:Gem::Dependency
27
27
  name: bundler
28
- requirement: &70317348017780 !ruby/object:Gem::Requirement
28
+ requirement: &70154939648160 !ruby/object:Gem::Requirement
29
29
  none: false
30
30
  requirements:
31
31
  - - ! '>='
@@ -33,10 +33,10 @@ dependencies:
33
33
  version: '0'
34
34
  type: :development
35
35
  prerelease: false
36
- version_requirements: *70317348017780
36
+ version_requirements: *70154939648160
37
37
  - !ruby/object:Gem::Dependency
38
38
  name: activeresource
39
- requirement: &70317348017160 !ruby/object:Gem::Requirement
39
+ requirement: &70154939656120 !ruby/object:Gem::Requirement
40
40
  none: false
41
41
  requirements:
42
42
  - - ~>
@@ -44,7 +44,18 @@ dependencies:
44
44
  version: '3'
45
45
  type: :runtime
46
46
  prerelease: false
47
- version_requirements: *70317348017160
47
+ version_requirements: *70154939656120
48
+ - !ruby/object:Gem::Dependency
49
+ name: activerecord
50
+ requirement: &70154939665280 !ruby/object:Gem::Requirement
51
+ none: false
52
+ requirements:
53
+ - - ~>
54
+ - !ruby/object:Gem::Version
55
+ version: '3'
56
+ type: :runtime
57
+ prerelease: false
58
+ version_requirements: *70154939665280
48
59
  description: DISI - UniTN internal development
49
60
  email:
50
61
  - tech@disi.unitn.it