slayer-rpx_now 0.6.24

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,414 @@
1
+ require 'spec/spec_helper'
2
+
3
+ describe RPXNow do
4
+ def fake_response(replace={})
5
+ body = {'stat' => 'ok'}.merge(replace)
6
+ mock({:code => "200", :body => body.to_json})
7
+ end
8
+
9
+ describe :domain do
10
+ it 'defaults to rpxnow.com' do
11
+ RPXNow.domain.should == 'rpxnow.com'
12
+ end
13
+ end
14
+
15
+ describe :domain= do
16
+ it "is stored" do
17
+ RPXNow.domain = 'domain.com'
18
+ RPXNow.domain.should == 'domain.com'
19
+ end
20
+ end
21
+
22
+ describe :api_key= do
23
+ before do
24
+ RPXNow.api_key='XX'
25
+ end
26
+
27
+ it "is stored" do
28
+ RPXNow.api_key.should == 'XX'
29
+ end
30
+
31
+ it "stores the api key, so i do not have to supply everytime" do
32
+ RPXNow::Api.should_receive(:request).
33
+ with(anything, hash_including(:apiKey => 'XX')).
34
+ and_return fake_response
35
+ RPXNow.mappings(1)
36
+ end
37
+
38
+ it "is not overwritten when overwriting for a single request" do
39
+ RPXNow::Api.should_receive(:request).
40
+ with(anything, hash_including(:apiKey => 'YY')).
41
+ and_return fake_response
42
+ RPXNow.mappings(1, :apiKey => 'YY')
43
+ RPXNow.api_key.should == 'XX'
44
+ end
45
+ end
46
+
47
+ describe :api_version= do
48
+ it "is 2 by default" do
49
+ RPXNow.api_version.should == 2
50
+ end
51
+
52
+ it "is stored" do
53
+ RPXNow.api_version='XX'
54
+ RPXNow.api_version.should == 'XX'
55
+ end
56
+
57
+ it "used for every request" do
58
+ RPXNow.api_version='XX'
59
+ RPXNow::Api.should_receive(:request).
60
+ with('/api/vXX/mappings', anything).
61
+ and_return fake_response
62
+ RPXNow.mappings(1)
63
+ end
64
+
65
+ it "is not overwritten when overwriting for a single request" do
66
+ RPXNow.api_version='XX'
67
+ RPXNow::Api.should_receive(:request).
68
+ with('/api/vYY/mappings', anything).
69
+ and_return fake_response
70
+ RPXNow.mappings(1, :api_version => 'YY')
71
+ RPXNow.api_version.should == 'XX'
72
+ end
73
+
74
+ it "is not passed in data for request" do
75
+ RPXNow.api_version='XX'
76
+ RPXNow::Api.should_receive(:request).
77
+ with(anything, hash_not_including(:api_version => 'YY')).
78
+ and_return fake_response
79
+ RPXNow.mappings(1, :api_version => 'YY')
80
+ end
81
+ end
82
+
83
+ describe :embed_code do
84
+ it "contains the subdomain" do
85
+ RPXNow.embed_code('xxx','my_url').should =~ /xxx/
86
+ end
87
+
88
+ it "contains the url" do
89
+ RPXNow.embed_code('xxx','my_url').should =~ /token_url=my_url/
90
+ end
91
+
92
+ it "defaults to no language" do
93
+ RPXNow.embed_code('xxx', 'my_url').should_not =~ /language_preference/
94
+ end
95
+
96
+ it "has a changeable language" do
97
+ RPXNow.embed_code('xxx', 'my_url', :language => 'es').should =~ /language_preference=es/
98
+ end
99
+
100
+ it "defaults to 400px width" do
101
+ RPXNow.embed_code('xxx', 'my_url').should =~ /width:400px;/
102
+ end
103
+
104
+ it "has a changeable width" do
105
+ RPXNow.embed_code('xxx', 'my_url', :width => '300').should =~ /width:300px;/
106
+ end
107
+
108
+ it "defaults to 240px height" do
109
+ RPXNow.embed_code('xxx', 'my_url').should =~ /height:240px;/
110
+ end
111
+
112
+ it "has a changeable height" do
113
+ RPXNow.embed_code('xxx', 'my_url', :height => '500').should =~ /height:500px;/
114
+ end
115
+
116
+ it "has id on iframe" do
117
+ RPXNow.embed_code('xxx','my_url').should =~ /id=\"rpx_now_embed\"/
118
+ end
119
+ end
120
+
121
+ describe :popup_code do
122
+ it "defaults to obtrusive output" do
123
+ RPXNow.popup_code('sign on', 'subdomain', 'http://fake.domain.com/').should =~ /script src=/
124
+ end
125
+
126
+ it "does not change supplied options" do
127
+ options = {:xxx => 1}
128
+ RPXNow.popup_code('a','b','c', options)
129
+ options.should == {:xxx => 1}
130
+ end
131
+
132
+ it "adds html params to link" do
133
+ options = {:html => {:id => "xxxx"}}
134
+ RPXNow.popup_code('a','b','c', options).should =~ /id="xxxx"/
135
+ end
136
+
137
+ it "adds rpxnow to given html class" do
138
+ options = {:html => {:class => "c1 c2"}}
139
+ RPXNow.popup_code('a','b','c', options).should =~ /class="rpxnow c1 c2"/
140
+ end
141
+
142
+ describe 'obstrusive' do
143
+ it "does not encode token_url for popup" do
144
+ expected = %Q(RPXNOW.token_url = 'http://fake.domain.com/')
145
+ RPXNow.popup_code('sign on', 'subdomain', 'http://fake.domain.com/').should include(expected)
146
+ end
147
+ it "encodes token_url for unobtrusive fallback link" do
148
+ expected = %Q(<a class="rpxnow" href="https://subdomain.rpxnow.com/openid/v2/signin?token_url=http%3A%2F%2Ffake.domain.com%2F">sign on</a>)
149
+ RPXNow.popup_code('sign on', 'subdomain', 'http://fake.domain.com/').should include(expected)
150
+ end
151
+ end
152
+
153
+ describe 'unobstrusive' do
154
+ it "can build an unobtrusive widget with encoded token_url" do
155
+ expected = %Q(<a class="rpxnow" href="https://subdomain.rpxnow.com/openid/v2/signin?token_url=http%3A%2F%2Ffake.domain.com%2F">sign on</a>)
156
+ actual = RPXNow.popup_code('sign on', 'subdomain', 'http://fake.domain.com/', :unobtrusive => true)
157
+ actual.should == expected
158
+ end
159
+
160
+ it "can change api version" do
161
+ RPXNow.popup_code('x', 'y', 'z', :unobtrusive => true, :api_version => 'XX').should include("openid/vXX/signin?")
162
+ end
163
+
164
+ it "can change language" do
165
+ RPXNow.popup_code('x', 'y', 'z', :unobtrusive => true, :language => 'XX').should include("language_preference=XX")
166
+ end
167
+
168
+ it "can add flags" do
169
+ RPXNow.popup_code('x', 'y', 'z', :unobtrusive => true, :flags => 'test').should include("flags=test")
170
+ end
171
+
172
+ it "can add default_provider" do
173
+ RPXNow.popup_code('x', 'y', 'z', :unobtrusive => true, :default_provider => 'test').should include("default_provider=test")
174
+ end
175
+ end
176
+
177
+ it "allows to specify the version of the widget" do
178
+ RPXNow.popup_code('x','y','z', :api_version => 300).should =~ %r(/openid/v300/signin)
179
+ end
180
+
181
+ it "defaults to widget version 2" do
182
+ RPXNow.popup_code('x','y','z').should =~ %r(/openid/v2/signin)
183
+ end
184
+
185
+ describe 'language' do
186
+ it "defaults to no language" do
187
+ RPXNow.popup_code('x','y','z').should_not =~ /RPXNOW.language_preference/
188
+ end
189
+
190
+ it "has a changeable language" do
191
+ RPXNow.popup_code('x','y','z', :language=>'de').should =~ /RPXNOW.language_preference = 'de'/
192
+ end
193
+ end
194
+
195
+ describe 'flags' do
196
+ it "defaults to no language" do
197
+ RPXNow.popup_code('x','y','z').should_not =~ /RPXNOW.flags/
198
+ end
199
+
200
+ it "can have flags" do
201
+ RPXNow.popup_code('x','y','z', :flags=>'test').should =~ /RPXNOW.flags = 'test'/
202
+ end
203
+ end
204
+
205
+ describe 'default_provider' do
206
+ it "defaults to no provider" do
207
+ RPXNow.popup_code('x','y','z').should_not =~ /RPXNOW.default_provider/
208
+ end
209
+
210
+ it "can have default_provider" do
211
+ RPXNow.popup_code('x','y','z', :default_provider=>'test').should =~ /RPXNOW.default_provider = 'test'/
212
+ end
213
+ end
214
+ end
215
+
216
+ describe :user_data do
217
+ before do
218
+ @response_body = {
219
+ "profile" => {
220
+ "verifiedEmail" => "grosser.michael@googlemail.com",
221
+ "displayName" => "Michael Grosser",
222
+ "preferredUsername" => "grosser.michael",
223
+ "identifier" => "https:\/\/www.google.com\/accounts\/o8\/id?id=AItOawmaOlyYezg_WfbgP_qjaUyHjmqZD9qNIVM",
224
+ "email" => "grosser.michael@gmail.com"
225
+ }
226
+ }
227
+ @response = fake_response(@response_body)
228
+ @fake_user_data = {'profile'=>{}}
229
+ end
230
+
231
+ it "raises ApiError when used with an invalid token" do
232
+ lambda{
233
+ RPXNow.user_data('xxxx')
234
+ }.should raise_error(RPXNow::ApiError)
235
+ end
236
+
237
+ it "is empty when used with an unknown token" do
238
+ RPXNow.user_data('60d8c6374f4e9d290a7b55f39da7cc6435aef3d3').should == nil
239
+ end
240
+
241
+ it "parses JSON response to user data" do
242
+ expected = {
243
+ :name => 'Michael Grosser',
244
+ :email => 'grosser.michael@googlemail.com',
245
+ :identifier => 'https://www.google.com/accounts/o8/id?id=AItOawmaOlyYezg_WfbgP_qjaUyHjmqZD9qNIVM',
246
+ :username => 'grosser.michael',
247
+ }
248
+ RPXNow::Api.should_receive(:request).and_return @response
249
+ RPXNow.user_data('').should == expected
250
+ end
251
+
252
+ it "deprecated: adds raw profile data if i want it" do
253
+ RPXNow::Api.should_receive(:request).and_return @response
254
+ RPXNow.should_receive(:warn)
255
+ RPXNow.user_data('',:additional => [:raw])[:raw]["verifiedEmail"].should == "grosser.michael@googlemail.com"
256
+ end
257
+
258
+ it "adds raw data if i want it" do
259
+ RPXNow::Api.should_receive(:request).and_return @response
260
+ RPXNow.user_data('',:additional => [:raw_response])[:raw_response]['profile']["verifiedEmail"].should == "grosser.michael@googlemail.com"
261
+ end
262
+
263
+ it "adds a :id when primaryKey was returned" do
264
+ @response_body['profile']['primaryKey'] = "2"
265
+ response = fake_response(@response_body)
266
+ RPXNow::Api.should_receive(:request).and_return response
267
+ RPXNow.user_data('')[:id].should == '2'
268
+ end
269
+
270
+ it "handles primaryKeys that are not numeric" do
271
+ @response_body['profile']['primaryKey'] = "dbalatero"
272
+ response = fake_response(@response_body)
273
+ RPXNow::Api.should_receive(:request).and_return response
274
+ RPXNow.user_data('')[:id].should == 'dbalatero'
275
+ end
276
+
277
+ it "can fetch additional fields" do
278
+ @response_body['profile']['xxxy'] = "test"
279
+ response = fake_response(@response_body)
280
+ RPXNow::Api.should_receive(:request).and_return response
281
+ RPXNow.user_data('', :additional => [:xxxy])[:xxxy].should == 'test'
282
+ end
283
+
284
+ it "hands JSON response to supplied block" do
285
+ RPXNow::Api.should_receive(:request).and_return @response
286
+ response = nil
287
+ RPXNow.user_data(''){|data| response = data}
288
+ response.delete('stat') # dunno why it happens, but is not important...
289
+ response.should == @response_body
290
+ end
291
+
292
+ it "returns what the supplied block returned" do
293
+ RPXNow::Api.should_receive(:request).and_return @response
294
+ RPXNow.user_data(''){|data| "x"}.should == 'x'
295
+ end
296
+
297
+ it "can request extended data" do
298
+ RPXNow::Api.should_receive(:request).
299
+ with(anything, hash_including(:extended => true)).
300
+ and_return @response
301
+ RPXNow.user_data('', :extended=>true)
302
+ end
303
+
304
+ it "returns extended data as an additional field" do
305
+ @response_body['friends'] = {'x' => 1}
306
+ @response = fake_response(@response_body)
307
+
308
+ RPXNow::Api.should_receive(:request).and_return @response
309
+ RPXNow.user_data('', :extended=>true)[:extended].should == {'friends' => {'x' => 1}}
310
+ end
311
+
312
+ it "does not pass raw_response to RPX" do
313
+ RPXNow::Api.should_receive(:request).
314
+ with(anything, hash_not_including(:raw_response => true)).
315
+ and_return @response
316
+ RPXNow.user_data('', :raw_response=>true)
317
+ end
318
+
319
+ it "can return a raw_response" do
320
+ RPXNow::Api.should_receive(:request).and_return @response
321
+ RPXNow.user_data('', :raw_response=>true).should == @response_body.merge('stat' => 'ok')
322
+ end
323
+
324
+ # these 2 tests are kind of duplicates of the api_version/key tests,
325
+ # but i want to be extra-sure user_data works
326
+ it "works with api version as option" do
327
+ RPXNow::Api.should_receive(:request).
328
+ with('/api/v123/auth_info', anything).
329
+ and_return @response
330
+ RPXNow.user_data('id', :extended=>'abc', :api_version=>123)
331
+ RPXNow.api_version.should == API_VERSION
332
+ end
333
+
334
+ it "works with apiKey as option" do
335
+ RPXNow::Api.should_receive(:request).
336
+ with('/api/v2/auth_info', hash_including(:apiKey=>'THE KEY')).
337
+ and_return @response
338
+ RPXNow.user_data('id', :extended=>'abc', :apiKey=>'THE KEY')
339
+ RPXNow.api_key.should == API_KEY
340
+ end
341
+ end
342
+
343
+ describe :set_status do
344
+ it "sets the status" do
345
+ RPXNow::Api.should_receive(:request).
346
+ with("/api/v2/set_status", :identifier=>"identifier", :status=>"Chillen...", :apiKey=>API_KEY).
347
+ and_return fake_response
348
+ RPXNow.set_status('identifier', 'Chillen...')
349
+ end
350
+ end
351
+
352
+ describe :activity do
353
+ it "does a api call with the right arguments" do
354
+ RPXNow::Api.should_receive(:request).with("/api/v2/activity", :identifier=>"identifier", :activity=>'{"test":"something"}', :apiKey=>API_KEY).and_return fake_response
355
+ RPXNow.activity('identifier', :test => 'something')
356
+ end
357
+
358
+ it "can pass identifier/apiKey" do
359
+ RPXNow::Api.should_receive(:request).with("/api/v66666/activity", hash_including(:apiKey=>'MYKEY')).and_return fake_response
360
+ RPXNow.activity('identifier', {:test => 'something'}, :apiKey => 'MYKEY', :api_version => '66666')
361
+ end
362
+ end
363
+
364
+ describe :parse_user_data do
365
+ it "reads secondary names" do
366
+ RPXNow.send(:parse_user_data,{'profile'=>{'preferredUsername'=>'1'}}, {})[:name].should == '1'
367
+ end
368
+
369
+ it "parses email when no name is found" do
370
+ RPXNow.send(:parse_user_data,{'profile'=>{'email'=>'1@xxx.com'}}, {})[:name].should == '1'
371
+ end
372
+ end
373
+
374
+ describe :contacts do
375
+ it "finds all contacts" do
376
+ response = fake_response(JSON.parse(File.read('spec/fixtures/get_contacts_response.json')))
377
+ RPXNow::Api.should_receive(:request).
378
+ with('/api/v2/get_contacts',:identifier=>'xx', :apiKey=>API_KEY).
379
+ and_return response
380
+ RPXNow.contacts('xx').size.should == 5
381
+ end
382
+ end
383
+
384
+ describe :mappings do
385
+ it "shows all mappings" do
386
+ RPXNow::Api.should_receive(:request).
387
+ with("/api/v2/mappings", :apiKey=>API_KEY, :primaryKey=>1).
388
+ and_return fake_response("identifiers" => ["http://test.myopenid.com/"])
389
+ RPXNow.mappings(1).should == ["http://test.myopenid.com/"]
390
+ end
391
+ end
392
+
393
+ describe :map do
394
+ it "maps a identifier" do
395
+ RPXNow::Api.should_receive(:request).
396
+ with("/api/v2/map", :apiKey=>API_KEY, :primaryKey=>1, :identifier=>"http://test.myopenid.com").
397
+ and_return fake_response
398
+ RPXNow.map('http://test.myopenid.com',1)
399
+ end
400
+ end
401
+
402
+ describe :unmap do
403
+ it "unmaps a indentifier" do
404
+ RPXNow::Api.should_receive(:request).
405
+ with("/api/v2/unmap", :apiKey=>API_KEY, :primaryKey=>1, :identifier=>"http://test.myopenid.com").
406
+ and_return fake_response
407
+ RPXNow.unmap('http://test.myopenid.com', 1)
408
+ end
409
+ end
410
+
411
+ it "has a VERSION" do
412
+ RPXNow::VERSION.should =~ /^\d+\.\d+\.\d+$/
413
+ end
414
+ end
@@ -0,0 +1,16 @@
1
+ $LOAD_PATH << File.expand_path("../lib", File.dirname(__FILE__))
2
+
3
+ # ---- setup environment/plugin
4
+ require File.expand_path("../init", File.dirname(__FILE__))
5
+ API_KEY = '4b339169026742245b754fa338b9b0aebbd0a733'
6
+ API_VERSION = RPXNow.api_version
7
+ DOMAIN = RPXNow.domain
8
+
9
+ # ---- rspec
10
+ RSpec.configure do |config|
11
+ config.before do
12
+ RPXNow.api_key = API_KEY
13
+ RPXNow.api_version = API_VERSION
14
+ RPXNow.domain = DOMAIN
15
+ end
16
+ end
metadata ADDED
@@ -0,0 +1,107 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: slayer-rpx_now
3
+ version: !ruby/object:Gem::Version
4
+ hash: 55
5
+ prerelease:
6
+ segments:
7
+ - 0
8
+ - 6
9
+ - 24
10
+ version: 0.6.24
11
+ platform: ruby
12
+ authors:
13
+ - Michael Grosser
14
+ - Vlad Moskovets
15
+ autorequire:
16
+ bindir: bin
17
+ cert_chain: []
18
+
19
+ date: 2011-08-09 00:00:00 Z
20
+ dependencies:
21
+ - !ruby/object:Gem::Dependency
22
+ name: json_pure
23
+ prerelease: false
24
+ requirement: &id001 !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ">="
28
+ - !ruby/object:Gem::Version
29
+ hash: 3
30
+ segments:
31
+ - 0
32
+ version: "0"
33
+ type: :runtime
34
+ version_requirements: *id001
35
+ description:
36
+ email: devvlad@gmail.com
37
+ executables: []
38
+
39
+ extensions: []
40
+
41
+ extra_rdoc_files: []
42
+
43
+ files:
44
+ - .gitignore
45
+ - CHANGELOG
46
+ - Gemfile
47
+ - Gemfile.lock
48
+ - MIGRATION
49
+ - Rakefile
50
+ - Readme.md
51
+ - VERSION
52
+ - certs/ssl_cert.pem
53
+ - init.rb
54
+ - lib/rpx_now.rb
55
+ - lib/rpx_now/api.rb
56
+ - lib/rpx_now/contacts_collection.rb
57
+ - lib/rpx_now/user_integration.rb
58
+ - lib/rpx_now/user_proxy.rb
59
+ - rpx_now.gemspec
60
+ - spec/fixtures/get_contacts_response.json
61
+ - spec/integration/mapping_spec.rb
62
+ - spec/rpx_now/api_spec.rb
63
+ - spec/rpx_now/contacts_collection_spec.rb
64
+ - spec/rpx_now/user_proxy_spec.rb
65
+ - spec/rpx_now_spec.rb
66
+ - spec/spec_helper.rb
67
+ homepage: http://github.com/slayer/slayer-rpx_now
68
+ licenses: []
69
+
70
+ post_install_message:
71
+ rdoc_options:
72
+ - --charset=UTF-8
73
+ require_paths:
74
+ - lib
75
+ required_ruby_version: !ruby/object:Gem::Requirement
76
+ none: false
77
+ requirements:
78
+ - - ">="
79
+ - !ruby/object:Gem::Version
80
+ hash: 3
81
+ segments:
82
+ - 0
83
+ version: "0"
84
+ required_rubygems_version: !ruby/object:Gem::Requirement
85
+ none: false
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ hash: 3
90
+ segments:
91
+ - 0
92
+ version: "0"
93
+ requirements: []
94
+
95
+ rubyforge_project:
96
+ rubygems_version: 1.7.2
97
+ signing_key:
98
+ specification_version: 3
99
+ summary: Helper to simplify RPX Now user login/creation
100
+ test_files:
101
+ - spec/rpx_now/user_proxy_spec.rb
102
+ - spec/rpx_now/api_spec.rb
103
+ - spec/rpx_now/contacts_collection_spec.rb
104
+ - spec/rpx_now_spec.rb
105
+ - spec/spec_helper.rb
106
+ - spec/integration/mapping_spec.rb
107
+ has_rdoc: