disiid_user 3.0.1 → 3.0.2

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/TODO ADDED
@@ -0,0 +1 @@
1
+ - should be PUTting remote_user on @user.save() so that remote attributes get updated too (if permitted)
@@ -1,3 +1,3 @@
1
1
  module DisiidUser
2
- VERSION = "3.0.1"
2
+ VERSION = "3.0.2"
3
3
  end
data/lib/disiid_user.rb CHANGED
@@ -74,16 +74,26 @@ module DisiidUser
74
74
  # Method for interaction with RemoteUser
75
75
  #
76
76
 
77
- # Method for requesting data of remote user
77
+ # Method for requesting or updating data of remote user
78
78
  REMOTE_ATTRIBUTES.each do |remote_attr|
79
+ # getters
79
80
  define_method remote_attr do
80
- remote_user.try(remote_attr)
81
+ remote_user.public_send(remote_attr) if remote_user
82
+ end
83
+ # setters
84
+ meth = "#{remote_attr}="
85
+ define_method meth do |val|
86
+ # works for 1.9.2, not sure public_send() is available for 1.8.7
87
+ remote_user.public_send(meth, val) if remote_user
81
88
  end
82
89
  end
83
90
 
84
91
  # Get the remote user via get ActiveResource
85
92
  def remote_user
86
- @remote_user ||= DisiidUser::RemoteUser.find uuid, :params => { :auth_token => DisiidUser::RemoteUser.auth_token }
93
+ return nil unless uuid
94
+ @remote_user ||= Rails.cache.fetch(uuid, :expires_in => DisiidUser::RemoteUser.cache_expiry) do
95
+ DisiidUser::RemoteUser.find uuid, :params => { :auth_token => DisiidUser::RemoteUser.auth_token }
96
+ end
87
97
  rescue; nil; end
88
98
 
89
99
  # Return the uuid of a user
@@ -105,7 +115,24 @@ module DisiidUser
105
115
  class RemoteUser < ActiveResource::Base
106
116
  self.site = 'http://localhost:3001'
107
117
  self.element_name = 'user'
118
+
119
+ # authentication token that will be appended at the end of the request URL,
120
+ # e.g. http://id.provider.com/:collection_name/:uuid?auth_token=:auth_token
108
121
  cattr_accessor :auth_token
122
+
123
+ # remote_user cache expiration time, in seconds
124
+ # defaults to 1 hour
125
+ cattr_accessor :cache_expiry
126
+ self.cache_expiry = 3600
127
+
128
+ protected
129
+
130
+ # Redefined method from Base class: we need to add auth_token suffix
131
+ # to the request params
132
+ def element_path(options = nil)
133
+ auth = { :auth_token => self.class.auth_token }
134
+ self.class.element_path(to_param, (options || prefix_options || {}).merge(auth))
135
+ end
109
136
  end
110
137
 
111
138
  end
@@ -1,5 +1,5 @@
1
1
  require 'spec_helper'
2
- require 'user'
2
+ require 'stub_user'
3
3
 
4
4
  # admin@example.org
5
5
  # 6a002a40-bc63-4095-9bb0-f31bf386bf55
@@ -95,11 +95,28 @@ describe DisiidUser do
95
95
  before { DisiidUser::RemoteUser.auth_token = 'sometoken' }
96
96
 
97
97
  it 'should fetch it using find()' do
98
+ @user.identity_url = "http://host/users/some-uuid"
98
99
  DisiidUser::RemoteUser.should_receive(:find).with('some-uuid', :params => hash_including(:auth_token => 'sometoken'))
100
+
101
+ @user.remote_user
102
+ end
103
+
104
+ it 'should be cached' do
99
105
  @user.identity_url = "http://host/users/some-uuid"
106
+
107
+ cache_expiry = DisiidUser::RemoteUser.cache_expiry
108
+ Rails.cache.should_receive(:fetch).with('some-uuid', hash_including(:expires_in => cache_expiry))
109
+
100
110
  @user.remote_user
101
111
  end
102
112
 
113
+ it 'should not search for it if uuid is missing' do
114
+ @user.identity_url = nil
115
+ DisiidUser::RemoteUser.should_not_receive(:find)
116
+
117
+ @user.remote_user.should be_nil
118
+ end
119
+
103
120
  context 'when does not exist' do
104
121
  before { @user.identity_url = "http://openid.provider.local/users/does-no-exist" }
105
122
  it 'should just be nil' do
@@ -112,6 +129,14 @@ describe DisiidUser do
112
129
  @user.first_name.should be_nil
113
130
  @user.last_name.should be_nil
114
131
  end
132
+
133
+ it 'should not raise an exception on attributes assignment' do
134
+ @user.first_name = 'whatever'
135
+ # this will basically check the part '... if remote_user'
136
+ # in e.g. define_method('first_name=') { |val| first_name = val if remote_user }
137
+ @user.first_name.should be_nil
138
+ # assuming other attributes work the same way
139
+ end
115
140
  end
116
141
 
117
142
  context 'when exists' do
@@ -125,6 +150,15 @@ describe DisiidUser do
125
150
  @user.last_name.should == 'Smith'
126
151
  @user.homepage.should == 'http://john.smith.example.org'
127
152
  end
153
+
154
+ it 'should have assign methods like email=(...)' do
155
+ @user.email = 'new@email.com'
156
+ @user.email.should == 'new@email.com'
157
+
158
+ @user.homepage = 'http://some.page.com'
159
+ @user.homepage.should == 'http://some.page.com'
160
+ # assuming other attributes work too
161
+ end
128
162
 
129
163
  it 'should get all roles, including remote' do
130
164
  @user.local_roles = 'manager'
@@ -1,5 +1,5 @@
1
1
  require 'spec_helper'
2
- require 'user'
2
+ require 'stub_user'
3
3
 
4
4
  # stub methods check
5
5
  describe Array do
@@ -24,14 +24,41 @@ module ActiveResource
24
24
  # taps into ActiveRecord to read data from stub files
25
25
  # instead of doing a real HTTP request
26
26
  def get(path, *args)
27
- record = /\/[^\/]+\/([^\/]+)/.match(path)[1]
27
+ read_from_file(path)
28
+ rescue
29
+ raise ActiveResource::ResourceNotFound.new("dummy remote user didn't find that resource: #{path} #{args.inspect}")
30
+ end
31
+
32
+ # stubs PUT request so to not do it for real
33
+ def put(path, payload, headers)
34
+ read_from_file(path)
35
+ rescue
36
+ raise ActiveResource::ResourceNotFound.new("couldn't update dummy remote user: #{path} #{args.inspect}")
37
+ end
38
+
39
+
40
+ private
41
+
42
+ def read_from_file(fake_url_path)
43
+ # /collection_name/uuid?query
44
+ record = /\/[^\/]+\/([^\/\?]+)/.match(fake_url_path)[1]
28
45
  # record string includes .xml or .json too
29
46
  filename = "#{File.dirname(__FILE__)}/#{record}".sub(/\?.*$/, '')
30
47
  # format is either ActiveResource::Formats::XmlFormat or JsonFormat
31
48
  # 3.0.x defaults to XML, 3.1.x defaults to JSON
32
49
  format.decode open(filename).read
33
- rescue
34
- raise ActiveResource::ResourceNotFound.new("dummy remote user didn't find that resource: #{path} #{args.inspect}")
35
50
  end
36
51
  end
37
52
  end
53
+
54
+ # stub implementation for Rails.cache
55
+ # it will never really cache results
56
+ # so it is safe to Rails.cache.fetch(...) { ... }
57
+ class StubCache
58
+ def self.fetch(*args); yield; end
59
+ end
60
+
61
+ module Rails
62
+ def self.cache; StubCache; end
63
+ end
64
+
metadata CHANGED
@@ -2,7 +2,7 @@
2
2
  name: disiid_user
3
3
  version: !ruby/object:Gem::Version
4
4
  prerelease:
5
- version: 3.0.1
5
+ version: 3.0.2
6
6
  platform: ruby
7
7
  authors:
8
8
  - Danilo Severina
@@ -11,7 +11,7 @@ autorequire:
11
11
  bindir: bin
12
12
  cert_chain: []
13
13
 
14
- date: 2011-11-16 00:00:00 Z
14
+ date: 2011-11-30 00:00:00 Z
15
15
  dependencies:
16
16
  - !ruby/object:Gem::Dependency
17
17
  name: rspec
@@ -60,6 +60,7 @@ files:
60
60
  - .rvmrc-template
61
61
  - Gemfile
62
62
  - Rakefile
63
+ - TODO
63
64
  - disiid_user.gemspec
64
65
  - lib/disiid_user.rb
65
66
  - lib/disiid_user/version.rb
@@ -69,7 +70,7 @@ files:
69
70
  - spec/remote_user_spec.rb
70
71
  - spec/spec_helper.rb
71
72
  - spec/stub_methods_spec.rb
72
- - spec/user.rb
73
+ - spec/stub_user.rb
73
74
  homepage: http://disi.unitn.it
74
75
  licenses: []
75
76
 
@@ -104,4 +105,4 @@ test_files:
104
105
  - spec/remote_user_spec.rb
105
106
  - spec/spec_helper.rb
106
107
  - spec/stub_methods_spec.rb
107
- - spec/user.rb
108
+ - spec/stub_user.rb