g5_authenticatable 0.5.1 → 0.6.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 5cad812409d3b2240214bf1984fc0ac285011072
4
- data.tar.gz: 7116d3828bcc927ffc1aa7cef4e0e79ec52da2e8
3
+ metadata.gz: 93238b9dd28980b2722ffc334c98c7c29aed9952
4
+ data.tar.gz: cd9eaa8d01343dd9463a0a6a2cc082331ec89f49
5
5
  SHA512:
6
- metadata.gz: c1ea849772f6d6bfb1948c172f6c5e991a0e6160e924a0b0e843024aa80b308329478c8ef10494482c35d68a4c1377527df244d49549cc290e9ca4c389f7b5ac
7
- data.tar.gz: 50fbcc29a23cbb1673f6eb3baed96ff36dbbc3926e65e7c3f379f1e2d19b5ffff027b65b328394e57588e024546d5abb6d5fb85098771f1843f4ce9edc2aa788
6
+ metadata.gz: 39a183780b26fb0bb1c730797504f549bd7510d10e76bf6946b52d818de6b5215649eebb48717150cffa551e337e0cea7c85e3891c9ee3a1196b6437aa4c080f
7
+ data.tar.gz: 036629738e9fd3c4f1888f9bd53b82a1bf61485bf01305cc97da8ebb32c9a342d792ec2e3b68abc5db9eb91ecf6202f8f03079d5f7dcaf66d981ca1423eed60d
data/.gitignore CHANGED
@@ -23,3 +23,4 @@ spec/dummy/tmp/
23
23
  spec/dummy/.sass-cache
24
24
  spec/dummy/config/database.yml
25
25
  .env.*
26
+ .idea
data/Gemfile CHANGED
@@ -27,6 +27,7 @@ group :test do
27
27
  gem 'shoulda-matchers', '~> 2.6'
28
28
  gem 'generator_spec'
29
29
  gem 'rspec-http', require: 'rspec/http'
30
+ gem 'rspec-activemodel-mocks'
30
31
  end
31
32
 
32
33
  # Declare any dependencies that are still in development here instead of in
@@ -32,7 +32,19 @@ module G5Authenticatable
32
32
 
33
33
  def update_roles_from_auth(auth_data)
34
34
  roles.clear
35
- auth_data.extra.roles.each { |role| add_role(role.name) }
35
+ auth_data.extra.roles.each do |role|
36
+ if(role.type == 'GLOBAL')
37
+ add_role(role.name)
38
+ else
39
+ begin
40
+ the_class = Object.const_get(role.type)
41
+ resource = the_class.where(urn: role.urn).first
42
+ add_role(role.name, resource)
43
+ rescue => e
44
+ Rails.logger.error(e)
45
+ end
46
+ end
47
+ end
36
48
  end
37
49
 
38
50
  private
@@ -45,5 +57,7 @@ module G5Authenticatable
45
57
  organization_name: auth_data.extra.organization_name
46
58
  }
47
59
  end
60
+
61
+
48
62
  end
49
63
  end
@@ -20,9 +20,9 @@ Gem::Specification.new do |spec|
20
20
  spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
21
21
  spec.require_paths = ['lib']
22
22
 
23
- spec.add_dependency 'devise_g5_authenticatable', '~> 0.2', '>= 0.2.1'
24
- spec.add_dependency 'omniauth-g5', '~> 0.2'
25
- spec.add_dependency 'g5_authenticatable_api', '~> 0.3.1'
23
+ spec.add_dependency 'devise_g5_authenticatable', '~> 0.2.2'
24
+ spec.add_dependency 'omniauth-g5', '~> 0.3'
25
+ spec.add_dependency 'g5_authenticatable_api', '~> 0.4.1'
26
26
  spec.add_dependency 'rolify', '~> 4.0'
27
27
  spec.add_dependency 'pundit', '~> 1.0'
28
28
  end
@@ -16,7 +16,7 @@ module G5Authenticatable
16
16
  title: user.title,
17
17
  organization_name: user.organization_name,
18
18
  roles: user.roles.collect do |role|
19
- {name: role.name}
19
+ {name: role.name, type: 'GLOBAL', urn: nil}
20
20
  end,
21
21
  raw_info: {}
22
22
  }
@@ -1,3 +1,3 @@
1
1
  module G5Authenticatable
2
- VERSION = '0.5.1'
2
+ VERSION = '0.6.0'
3
3
  end
@@ -28,7 +28,7 @@ describe 'Signing in' do
28
28
  extra: {
29
29
  title: updated_title,
30
30
  organization_name: updated_organization_name,
31
- roles: [{name: updated_role.name}],
31
+ roles: [{name: updated_role.name, type: 'GLOBAL', urn: nil}],
32
32
  raw_info: {}
33
33
  }
34
34
  })
@@ -123,7 +123,7 @@ describe 'Signing in' do
123
123
  extra: {
124
124
  title: user_attributes[:title],
125
125
  organization_name: user_attributes[:organization_name],
126
- roles: [{name: role_attributes[:name]}],
126
+ roles: [{name: role_attributes[:name], type: 'GLOBAL', urn: nil}],
127
127
  raw_info: {}
128
128
  }
129
129
  })
@@ -64,30 +64,31 @@ describe G5Authenticatable::User do
64
64
 
65
65
  let(:params) { Hash.new }
66
66
  let(:auth_data) do
67
- OmniAuth::AuthHash.new({
68
- 'provider' => new_user_attributes[:provider],
69
- 'uid' => new_user_attributes[:uid],
70
- 'info' => {
71
- 'email' => new_user_attributes[:email],
72
- 'name' => "#{new_user_attributes[:first_name]} #{new_user_attributes[:last_name]}",
73
- 'first_name' => new_user_attributes[:first_name],
74
- 'last_name' => new_user_attributes[:last_name],
75
- 'phone' => new_user_attributes[:phone_number]
76
- },
77
- 'credentials' => {
78
- 'token' => new_user_attributes[:g5_access_token],
79
- 'expires' => true,
80
- 'expires_at' => Time.now + 1000
81
- },
82
- 'extra' => {
83
- 'title' => new_user_attributes[:title],
84
- 'organization_name' => new_user_attributes[:organization_name],
85
- 'roles' => [
86
- {'name' => new_role_attributes[:name]}
87
- ],
88
- 'raw_info' => {}
89
- }
90
- })
67
+ OmniAuth::AuthHash.new(
68
+ {
69
+ 'provider' => new_user_attributes[:provider],
70
+ 'uid' => new_user_attributes[:uid],
71
+ 'info' => {
72
+ 'email' => new_user_attributes[:email],
73
+ 'name' => "#{new_user_attributes[:first_name]} #{new_user_attributes[:last_name]}",
74
+ 'first_name' => new_user_attributes[:first_name],
75
+ 'last_name' => new_user_attributes[:last_name],
76
+ 'phone' => new_user_attributes[:phone_number]
77
+ },
78
+ 'credentials' => {
79
+ 'token' => new_user_attributes[:g5_access_token],
80
+ 'expires' => true,
81
+ 'expires_at' => Time.now + 1000
82
+ },
83
+ 'extra' => {
84
+ 'title' => new_user_attributes[:title],
85
+ 'organization_name' => new_user_attributes[:organization_name],
86
+ 'roles' => [
87
+ {'name' => new_role_attributes[:name], 'type' => 'GLOBAL', 'urn' => nil}
88
+ ],
89
+ 'raw_info' => {}
90
+ }
91
+ })
91
92
  end
92
93
 
93
94
  let(:new_user_attributes) { FactoryGirl.attributes_for(:g5_authenticatable_user) }
@@ -180,7 +181,7 @@ describe G5Authenticatable::User do
180
181
  phone_number: nil,
181
182
  title: nil,
182
183
  organization_name: nil
183
- )
184
+ )
184
185
  end
185
186
  let(:role_name) { :my_role }
186
187
 
@@ -190,29 +191,30 @@ describe G5Authenticatable::User do
190
191
  end
191
192
 
192
193
  let(:auth_data) do
193
- OmniAuth::AuthHash.new({
194
- 'provider' => user_attributes[:provider],
195
- 'uid' => user_attributes[:uid],
196
- 'info' => {
197
- 'email' => updated_attributes[:email],
198
- 'first_name' => updated_attributes[:first_name],
199
- 'last_name' => updated_attributes[:last_name],
200
- 'phone' => updated_attributes[:phone_number]
201
- },
202
- 'credentials' => {
203
- 'token' => updated_attributes[:g5_access_token],
204
- 'expires' => true,
205
- 'expires_at' => Time.now + 1000
206
- },
207
- 'extra' => {
208
- 'title' => updated_attributes[:title],
209
- 'organization_name' => updated_attributes[:organization_name],
210
- 'roles' => [
211
- {name: updated_role_name}
212
- ],
213
- 'raw_info' => {}
214
- }
215
- })
194
+ OmniAuth::AuthHash.new(
195
+ {
196
+ 'provider' => user_attributes[:provider],
197
+ 'uid' => user_attributes[:uid],
198
+ 'info' => {
199
+ 'email' => updated_attributes[:email],
200
+ 'first_name' => updated_attributes[:first_name],
201
+ 'last_name' => updated_attributes[:last_name],
202
+ 'phone' => updated_attributes[:phone_number]
203
+ },
204
+ 'credentials' => {
205
+ 'token' => updated_attributes[:g5_access_token],
206
+ 'expires' => true,
207
+ 'expires_at' => Time.now + 1000
208
+ },
209
+ 'extra' => {
210
+ 'title' => updated_attributes[:title],
211
+ 'organization_name' => updated_attributes[:organization_name],
212
+ 'roles' => [
213
+ {name: updated_role_name, type: 'GLOBAL', urn: nil}
214
+ ],
215
+ 'raw_info' => {}
216
+ }
217
+ })
216
218
  end
217
219
 
218
220
  context 'when user info is the same' do
@@ -373,4 +375,106 @@ describe G5Authenticatable::User do
373
375
  end
374
376
  end
375
377
  end
378
+
379
+ describe '#update_roles_from_auth' do
380
+ before do
381
+ user.roles = []
382
+ user.save!
383
+ end
384
+
385
+ let(:user2) { G5Authenticatable::User.create(user_attributes) }
386
+ let(:user_attributes2) { FactoryGirl.attributes_for(:g5_authenticatable_user) }
387
+ let(:mock_urn) { 'mock_urn' }
388
+
389
+
390
+ let(:mock_resource_class) { Class.new }
391
+ before { stub_const('MockResource', mock_resource_class) }
392
+
393
+ let(:mock_resource) { stub_model(mock_resource_class, urn: mock_urn) }
394
+ before do
395
+ allow(mock_resource_class).to receive(:where).with(urn: mock_urn).and_return([mock_resource])
396
+ end
397
+
398
+ let(:auth_data) do
399
+ OmniAuth::AuthHash.new(
400
+ {
401
+ 'provider' => user_attributes[:provider],
402
+ 'uid' => user_attributes[:uid],
403
+ 'info' => {
404
+ 'email' => user_attributes[:email],
405
+ 'first_name' => user_attributes[:first_name],
406
+ 'last_name' => user_attributes[:last_name],
407
+ 'phone' => user_attributes[:phone_number]
408
+ },
409
+ 'credentials' => {
410
+ 'token' => user_attributes[:g5_access_token],
411
+ 'expires' => true,
412
+ 'expires_at' => Time.now + 1000
413
+ },
414
+ 'extra' => {
415
+ 'title' => user_attributes[:title],
416
+ 'organization_name' => user_attributes[:organization_name],
417
+ 'roles' => roles,
418
+ 'raw_info' => {}
419
+ }
420
+ })
421
+ end
422
+
423
+ context 'with global role' do
424
+ let(:roles) { [
425
+ {name: 'admin', type: 'GLOBAL', urn: nil}
426
+ ] }
427
+
428
+ it 'will add a global role' do
429
+ expect{ user.update_roles_from_auth(auth_data) }.to change{ user.roles.length }.from(0).to(1)
430
+ expect(user.roles.first.name).to eq('admin')
431
+ expect(user.roles.first.resource).to be_nil
432
+ end
433
+ end
434
+
435
+ context 'with a scoped role' do
436
+ let(:roles) { [
437
+ {name: 'viewer', type: 'MockResource', urn: mock_urn}
438
+ ] }
439
+
440
+ it 'will add a scoped role' do
441
+ expect{ user.update_roles_from_auth(auth_data) }.to change{ user.roles.length }.from(0).to(1)
442
+ expect(user.roles.first.name).to eq('viewer')
443
+ expect(user.roles.first.resource_id).to eq(mock_resource.id)
444
+ expect(user.roles.first.resource_type).to eq('MockResource')
445
+ end
446
+ end
447
+
448
+ context 'with a more than 1 role' do
449
+ let(:roles) { [
450
+ {name: 'viewer', type: 'MockResource', urn: mock_urn},
451
+ {name: 'admin', type: 'GLOBAL', urn: nil}
452
+ ] }
453
+
454
+ it 'will add a scoped role' do
455
+ expect{ user.update_roles_from_auth(auth_data) }.to change{ user.roles.length }.from(0).to(2)
456
+ end
457
+ end
458
+ context 'with 0 roles' do
459
+ let(:roles) { [] }
460
+
461
+ it 'will add a scoped role' do
462
+ expect{ user.update_roles_from_auth(auth_data) }.to_not change{ user.roles.length }.from(0)
463
+ end
464
+ end
465
+
466
+ context 'with a bad role type' do
467
+ let(:roles) { [
468
+ {name: 'viewer', type: 'MockResource', urn: mock_urn},
469
+ {name: 'viewer', type: 'BadResource', urn: mock_urn}
470
+ ] }
471
+
472
+ it 'will skip the bad role' do
473
+ expect{ user.update_roles_from_auth(auth_data) }.to change{ user.roles.length }.from(0).to(1)
474
+ expect(user.roles.first.name).to eq('viewer')
475
+ expect(user.roles.first.resource_id).to eq(mock_resource.id)
476
+ expect(user.roles.first.resource_type).to eq('MockResource')
477
+ end
478
+ end
479
+ end
376
480
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: g5_authenticatable
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.5.1
4
+ version: 0.6.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - maeve
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-06-01 00:00:00.000000000 Z
11
+ date: 2015-06-02 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: devise_g5_authenticatable
@@ -16,48 +16,42 @@ dependencies:
16
16
  requirements:
17
17
  - - "~>"
18
18
  - !ruby/object:Gem::Version
19
- version: '0.2'
20
- - - ">="
21
- - !ruby/object:Gem::Version
22
- version: 0.2.1
19
+ version: 0.2.2
23
20
  type: :runtime
24
21
  prerelease: false
25
22
  version_requirements: !ruby/object:Gem::Requirement
26
23
  requirements:
27
24
  - - "~>"
28
25
  - !ruby/object:Gem::Version
29
- version: '0.2'
30
- - - ">="
31
- - !ruby/object:Gem::Version
32
- version: 0.2.1
26
+ version: 0.2.2
33
27
  - !ruby/object:Gem::Dependency
34
28
  name: omniauth-g5
35
29
  requirement: !ruby/object:Gem::Requirement
36
30
  requirements:
37
31
  - - "~>"
38
32
  - !ruby/object:Gem::Version
39
- version: '0.2'
33
+ version: '0.3'
40
34
  type: :runtime
41
35
  prerelease: false
42
36
  version_requirements: !ruby/object:Gem::Requirement
43
37
  requirements:
44
38
  - - "~>"
45
39
  - !ruby/object:Gem::Version
46
- version: '0.2'
40
+ version: '0.3'
47
41
  - !ruby/object:Gem::Dependency
48
42
  name: g5_authenticatable_api
49
43
  requirement: !ruby/object:Gem::Requirement
50
44
  requirements:
51
45
  - - "~>"
52
46
  - !ruby/object:Gem::Version
53
- version: 0.3.1
47
+ version: 0.4.1
54
48
  type: :runtime
55
49
  prerelease: false
56
50
  version_requirements: !ruby/object:Gem::Requirement
57
51
  requirements:
58
52
  - - "~>"
59
53
  - !ruby/object:Gem::Version
60
- version: 0.3.1
54
+ version: 0.4.1
61
55
  - !ruby/object:Gem::Dependency
62
56
  name: rolify
63
57
  requirement: !ruby/object:Gem::Requirement