g5_authenticatable 0.9.1.pre.2 → 1.0.0.pre.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (107) hide show
  1. checksums.yaml +4 -4
  2. data/.gitignore +1 -0
  3. data/.ruby-version +1 -1
  4. data/.travis.yml +23 -9
  5. data/Appraisals +17 -0
  6. data/CHANGELOG.md +14 -0
  7. data/Gemfile +11 -14
  8. data/README.md +40 -13
  9. data/app/controllers/concerns/g5_authenticatable/authorization.rb +4 -1
  10. data/app/controllers/g5_authenticatable/application_controller.rb +3 -0
  11. data/app/controllers/g5_authenticatable/error_controller.rb +3 -2
  12. data/app/controllers/g5_authenticatable/failure_app.rb +35 -0
  13. data/app/controllers/g5_authenticatable/sessions_controller.rb +7 -3
  14. data/app/helpers/g5_authenticatable/application_helper.rb +3 -0
  15. data/app/models/g5_authenticatable/role.rb +5 -2
  16. data/app/models/g5_authenticatable/user.rb +12 -5
  17. data/app/policies/g5_authenticatable/base_policy.rb +75 -58
  18. data/app/policies/g5_updatable/client_policy.rb +5 -5
  19. data/app/policies/g5_updatable/location_policy.rb +6 -5
  20. data/app/policies/g5_updatable/selectable_client_policy.rb +6 -7
  21. data/app/services/g5_authenticatable/impersonate_sessionable.rb +12 -7
  22. data/config/initializers/devise.rb +4 -0
  23. data/config/initializers/impersonate_strategy.rb +5 -2
  24. data/config/initializers/rolify.rb +2 -0
  25. data/config/routes.rb +3 -1
  26. data/g5_authenticatable.gemspec +11 -7
  27. data/gemfiles/rails_4.1.gemfile +28 -0
  28. data/gemfiles/rails_4.2.gemfile +28 -0
  29. data/gemfiles/rails_5.0.gemfile +28 -0
  30. data/gemfiles/rails_5.1.gemfile +28 -0
  31. data/lib/g5_authenticatable.rb +3 -1
  32. data/lib/g5_authenticatable/engine.rb +5 -2
  33. data/lib/g5_authenticatable/rspec.rb +2 -0
  34. data/lib/g5_authenticatable/test/controller_helpers.rb +14 -9
  35. data/lib/g5_authenticatable/test/env_helpers.rb +3 -0
  36. data/lib/g5_authenticatable/test/factories/client_users.rb +45 -0
  37. data/lib/g5_authenticatable/test/factories/global_users.rb +43 -0
  38. data/lib/g5_authenticatable/test/factories/location_users.rb +45 -0
  39. data/lib/g5_authenticatable/test/factories/roles.rb +63 -0
  40. data/lib/g5_authenticatable/test/factory.rb +7 -59
  41. data/lib/g5_authenticatable/test/feature_helpers.rb +31 -17
  42. data/lib/g5_authenticatable/test/request_helpers.rb +5 -1
  43. data/lib/g5_authenticatable/test/token_validation_helpers.rb +15 -10
  44. data/lib/g5_authenticatable/version.rb +3 -1
  45. data/lib/generators/g5_authenticatable/install/install_generator.rb +49 -37
  46. data/lib/generators/g5_authenticatable/install/templates/application_policy.rb +2 -0
  47. data/lib/generators/g5_authenticatable/install/templates/initializer.rb +2 -0
  48. data/lib/generators/g5_authenticatable/install/templates/migrate/add_g5_authenticatable_users_contact_info.rb +3 -1
  49. data/lib/generators/g5_authenticatable/install/templates/migrate/create_g5_authenticatable_roles.rb +3 -1
  50. data/lib/generators/g5_authenticatable/install/templates/migrate/create_g5_authenticatable_users.rb +3 -1
  51. data/lib/tasks/g5_authenticatable/purge_users.rake +2 -0
  52. data/spec/config/application_spec.rb +7 -4
  53. data/spec/controllers/application_controller_spec.rb +10 -5
  54. data/spec/controllers/concerns/g5_authenticatable/{authorization.rb → authorization_spec.rb} +11 -6
  55. data/spec/dummy/app/controllers/home_controller.rb +5 -5
  56. data/spec/dummy/app/controllers/rails_api/secure_resources_controller.rb +6 -4
  57. data/spec/dummy/app/models/post.rb +1 -1
  58. data/spec/dummy/config/environments/test.rb +25 -4
  59. data/spec/dummy/config/initializers/rails_compatibility.rb +10 -0
  60. data/spec/dummy/db/migrate/20140206070137_create_g5_authenticatable_users.rb +3 -1
  61. data/spec/dummy/db/migrate/20150428182339_add_g5_authenticatable_users_contact_info.rb +3 -1
  62. data/spec/dummy/db/migrate/20150429212919_create_g5_authenticatable_roles.rb +2 -1
  63. data/spec/dummy/db/migrate/20150509061150_create_posts.rb +3 -1
  64. data/spec/dummy/db/migrate/20150603224032_create_g5_updatable_clients_and_locations.g5_updatable.rb +3 -1
  65. data/spec/dummy/db/migrate/20150603224033_create_integration_setting.g5_updatable.rb +3 -1
  66. data/spec/dummy/db/migrate/20150603224034_remove_integration_setting.g5_updatable.rb +3 -1
  67. data/spec/dummy/db/migrate/20150603224035_add_name_to_clients_and_locations.g5_updatable.rb +3 -1
  68. data/spec/dummy/db/migrate/20150603224036_update_names.g5_updatable.rb +3 -1
  69. data/spec/dummy/db/migrate/20170613201430_add_latitude_and_longitude_to_location.g5_updatable.rb +7 -0
  70. data/spec/dummy/db/migrate/20170613201431_copy_lat_long_props_to_lat_long_columns.g5_updatable.rb +20 -0
  71. data/spec/dummy/db/migrate/20170613201432_add_latitude_longitude_indexes_to_location.g5_updatable.rb +7 -0
  72. data/spec/dummy/db/migrate/{20161122070749_add_amenities.rb → 20170613201433_add_amenities.g5_updatable.rb} +2 -1
  73. data/spec/dummy/db/migrate/{20161209070749_add_client_urn_to_locations.rb → 20170613201434_add_client_urn_to_locations.g5_updatable.rb} +2 -1
  74. data/spec/dummy/db/migrate/20170613201435_add_points_of_interest.g5_updatable.rb +21 -0
  75. data/spec/dummy/db/migrate/20170613201436_add_unique_urn_indexes.g5_updatable.rb +11 -0
  76. data/spec/dummy/db/schema.rb +73 -61
  77. data/spec/factories/post.rb +2 -0
  78. data/spec/features/auth_error_path_spec.rb +3 -3
  79. data/spec/features/default_role_authorization_spec.rb +8 -4
  80. data/spec/features/sign_in_spec.rb +23 -13
  81. data/spec/features/token_validation_spec.rb +4 -2
  82. data/spec/g5_authenticatable/version_spec.rb +3 -1
  83. data/spec/lib/generators/g5_authenticatable/install_generator_spec.rb +73 -28
  84. data/spec/models/g5_authenticatable/role_spec.rb +8 -4
  85. data/spec/models/g5_authenticatable/user_spec.rb +119 -84
  86. data/spec/models/post_spec.rb +4 -2
  87. data/spec/policies/application_policy_spec.rb +80 -47
  88. data/spec/policies/client_policy_spec.rb +8 -5
  89. data/spec/policies/location_policy_spec.rb +21 -8
  90. data/spec/policies/selectable_client_policy_spec.rb +26 -15
  91. data/spec/rails_helper.rb +41 -0
  92. data/spec/requests/default_role_authorization_spec.rb +18 -14
  93. data/spec/requests/grape_api_spec.rb +7 -5
  94. data/spec/requests/rails_api_spec.rb +11 -9
  95. data/spec/requests/sign_out_spec.rb +10 -6
  96. data/spec/requests/token_validation_spec.rb +9 -5
  97. data/spec/routing/auth_error_routing_spec.rb +7 -6
  98. data/spec/routing/sign_out_routing_spec.rb +7 -5
  99. data/spec/services/g5_authenticatable/impersonate_sessionable_spec.rb +41 -18
  100. data/spec/spec_helper.rb +78 -45
  101. data/spec/support/devise.rb +3 -1
  102. data/spec/support/safe_request_helpers.rb +36 -0
  103. data/spec/support/shared_contexts/rake.rb +10 -4
  104. data/spec/support/shared_examples/super_admin_authorizer.rb +3 -1
  105. data/spec/tasks/purge_users_spec.rb +3 -1
  106. metadata +75 -39
  107. data/spec/controllers/.gitkeep +0 -0
@@ -1,6 +1,8 @@
1
- require 'spec_helper'
1
+ # frozen_string_literal: true
2
2
 
3
- describe 'UI Token validation' do
3
+ require 'rails_helper'
4
+
5
+ RSpec.describe 'UI Token validation' do
4
6
  context 'when token validation is enabled' do
5
7
  before { G5Authenticatable.strict_token_validation = true }
6
8
 
@@ -1,6 +1,8 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'spec_helper'
2
4
 
3
- describe G5Authenticatable do
5
+ RSpec.describe G5Authenticatable do
4
6
  it 'should have a version' do
5
7
  expect(G5Authenticatable::VERSION).to_not be_blank
6
8
  end
@@ -1,4 +1,6 @@
1
- require 'spec_helper'
1
+ # frozen_string_literal: true
2
+
3
+ require 'rails_helper'
2
4
 
3
5
  # For some reason, trying to load the generator from this spec
4
6
  # causes problems without an explicit require statement, even
@@ -6,7 +8,7 @@ require 'spec_helper'
6
8
  # when you execute it from the command line
7
9
  require 'generators/g5_authenticatable/install/install_generator'
8
10
 
9
- describe G5Authenticatable::InstallGenerator, type: :generator do
11
+ RSpec.describe G5Authenticatable::InstallGenerator, type: :generator do
10
12
  destination File.expand_path('../../../../tmp', __FILE__)
11
13
 
12
14
  before do
@@ -16,40 +18,83 @@ describe G5Authenticatable::InstallGenerator, type: :generator do
16
18
  run_generator
17
19
  end
18
20
 
19
- it 'should copy the create user migration' do
20
- expect(destination_root).to have_structure {
21
- directory 'db' do
22
- directory 'migrate' do
23
- migration 'create_g5_authenticatable_users' do
24
- contains 'class CreateG5AuthenticatableUsers < ActiveRecord::Migration'
21
+ context 'under rails 4.x', skip: !Rails.version.starts_with?('4') do
22
+ it 'should copy the unversioned create user migration' do
23
+ expect(destination_root).to have_structure {
24
+ directory 'db' do
25
+ directory 'migrate' do
26
+ migration 'create_g5_authenticatable_users' do
27
+ contains "class CreateG5AuthenticatableUsers < ActiveRecord::Migration\n"
28
+ end
25
29
  end
26
30
  end
27
- end
28
- }
29
- end
30
-
31
- it 'should copy the migration to add user contact info' do
32
- expect(destination_root).to have_structure {
33
- directory 'db' do
34
- directory 'migrate' do
35
- migration 'add_g5_authenticatable_users_contact_info' do
36
- contains 'class AddG5AuthenticatableUsersContactInfo < ActiveRecord::Migration'
31
+ }
32
+ end
33
+
34
+ it 'should copy the unversioned migration to add user contact info' do
35
+ expect(destination_root).to have_structure {
36
+ directory 'db' do
37
+ directory 'migrate' do
38
+ migration 'add_g5_authenticatable_users_contact_info' do
39
+ contains "class AddG5AuthenticatableUsersContactInfo < ActiveRecord::Migration\n"
40
+ end
37
41
  end
38
42
  end
39
- end
40
- }
43
+ }
44
+ end
45
+
46
+ it 'should copy the unversioned migration to add user roles' do
47
+ expect(destination_root).to have_structure {
48
+ directory 'db' do
49
+ directory 'migrate' do
50
+ migration 'create_g5_authenticatable_roles' do
51
+ contains "class CreateG5AuthenticatableRoles < ActiveRecord::Migration\n"
52
+ end
53
+ end
54
+ end
55
+ }
56
+ end
41
57
  end
42
58
 
43
- it 'should copy the migration to add user roles' do
44
- expect(destination_root).to have_structure {
45
- directory 'db' do
46
- directory 'migrate' do
47
- migration 'create_g5_authenticatable_roles' do
48
- contains 'class CreateG5AuthenticatableRoles < ActiveRecord::Migration'
59
+ context 'under rails 5.x', skip: !Rails.version.starts_with?('5') do
60
+ it 'should copy the versioned create user migration' do
61
+ expect(destination_root).to have_structure {
62
+ directory 'db' do
63
+ directory 'migrate' do
64
+ migration 'create_g5_authenticatable_users' do
65
+ migration_version = "[#{Rails::VERSION::MAJOR}.#{Rails::VERSION::MINOR}]"
66
+ contains "class CreateG5AuthenticatableUsers < ActiveRecord::Migration#{migration_version}\n"
67
+ end
49
68
  end
50
69
  end
51
- end
52
- }
70
+ }
71
+ end
72
+
73
+ it 'should copy the unversioned migration to add user contact info' do
74
+ expect(destination_root).to have_structure {
75
+ directory 'db' do
76
+ directory 'migrate' do
77
+ migration 'add_g5_authenticatable_users_contact_info' do
78
+ migration_version = "[#{Rails::VERSION::MAJOR}.#{Rails::VERSION::MINOR}]"
79
+ contains "class AddG5AuthenticatableUsersContactInfo < ActiveRecord::Migration#{migration_version}\n"
80
+ end
81
+ end
82
+ end
83
+ }
84
+ end
85
+
86
+ it 'should copy the unversioned migration to add user roles' do
87
+ expect(destination_root).to have_structure {
88
+ directory 'db' do
89
+ directory 'migrate' do
90
+ migration 'create_g5_authenticatable_roles' do
91
+ migration_version = "[#{Rails::VERSION::MAJOR}.#{Rails::VERSION::MINOR}]"
92
+ contains "class CreateG5AuthenticatableRoles < ActiveRecord::Migration#{migration_version}\n"
93
+ end
94
+ end
95
+ end
96
+ }
97
+ end
53
98
  end
54
99
 
55
100
  it 'should copy the initializer' do
@@ -1,6 +1,8 @@
1
- require 'spec_helper'
1
+ # frozen_string_literal: true
2
2
 
3
- describe G5Authenticatable::Role do
3
+ require 'rails_helper'
4
+
5
+ RSpec.describe G5Authenticatable::Role do
4
6
  subject { role }
5
7
  let(:role) { G5Authenticatable::Role.new(role_attributes) }
6
8
  let(:role_attributes) { FactoryGirl.attributes_for(:g5_authenticatable_role) }
@@ -15,7 +17,7 @@ describe G5Authenticatable::Role do
15
17
  describe '.global' do
16
18
  subject(:global) { G5Authenticatable::Role.global }
17
19
  let!(:global_role) { user.roles.first }
18
- let(:user) { FactoryGirl.create(:g5_authenticatable_user) }
20
+ let(:user) { FactoryGirl.create(:g5_authenticatable_viewer) }
19
21
 
20
22
  let!(:scoped_role) do
21
23
  FactoryGirl.create(:g5_authenticatable_role,
@@ -32,7 +34,9 @@ describe G5Authenticatable::Role do
32
34
  end
33
35
 
34
36
  describe '.class_scoped' do
35
- subject(:class_scoped) { G5Authenticatable::Role.class_scoped(G5Authenticatable::User) }
37
+ subject(:class_scoped) do
38
+ G5Authenticatable::Role.class_scoped(G5Authenticatable::User)
39
+ end
36
40
  let!(:global_role) { FactoryGirl.create(:g5_authenticatable_role) }
37
41
  let!(:class_scoped_role) do
38
42
  FactoryGirl.create(:g5_authenticatable_role,
@@ -1,6 +1,8 @@
1
- require 'spec_helper'
1
+ # frozen_string_literal: true
2
2
 
3
- describe G5Authenticatable::User do
3
+ require 'rails_helper'
4
+
5
+ RSpec.describe G5Authenticatable::User do
4
6
  subject { user }
5
7
  let(:user) { G5Authenticatable::User.create(user_attributes) }
6
8
  let(:user_attributes) { FactoryGirl.attributes_for(:g5_authenticatable_user) }
@@ -32,7 +34,7 @@ describe G5Authenticatable::User do
32
34
  end
33
35
 
34
36
  it { is_expected.to validate_presence_of(:email) }
35
- it { is_expected.to validate_uniqueness_of(:email) }
37
+ it { is_expected.to validate_uniqueness_of(:email).case_insensitive }
36
38
  it { is_expected.to validate_uniqueness_of(:uid).scoped_to(:provider) }
37
39
 
38
40
  it 'should support timeouts' do
@@ -63,12 +65,14 @@ describe G5Authenticatable::User do
63
65
  subject(:attributes_from_auth) { user.attributes_from_auth(auth_data) }
64
66
 
65
67
  let(:auth_data) do
68
+ full_name = [new_user_attributes[:first_name],
69
+ new_user_attributes[:last_name]].join(' ')
66
70
  OmniAuth::AuthHash.new(
67
71
  'uid' => new_user_attributes[:uid],
68
72
  'provider' => new_user_attributes[:provider],
69
73
  'info' => {
70
74
  'email' => new_user_attributes[:email],
71
- 'name' => "#{new_user_attributes[:first_name]} #{new_user_attributes[:last_name]}",
75
+ 'name' => full_name,
72
76
  'first_name' => new_user_attributes[:first_name],
73
77
  'last_name' => new_user_attributes[:last_name],
74
78
  'phone' => new_user_attributes[:phone_number]
@@ -82,29 +86,39 @@ describe G5Authenticatable::User do
82
86
  'title' => new_user_attributes[:title],
83
87
  'organization_name' => new_user_attributes[:organization_name],
84
88
  'roles' => [
85
- { 'name' => new_role_attributes[:name], 'type' => 'GLOBAL', 'urn' => nil }
89
+ { 'name' => new_role_attributes[:name],
90
+ 'type' => 'GLOBAL',
91
+ 'urn' => nil }
86
92
  ],
87
93
  'raw_info' => {}
88
- })
94
+ }
95
+ )
89
96
  end
90
97
 
91
- let(:new_user_attributes) { FactoryGirl.attributes_for(:g5_authenticatable_user) }
92
- let(:new_role_attributes) { FactoryGirl.attributes_for(:g5_authenticatable_role) }
98
+ let(:new_user_attributes) do
99
+ FactoryGirl.attributes_for(:g5_authenticatable_user)
100
+ end
101
+ let(:new_role_attributes) do
102
+ FactoryGirl.attributes_for(:g5_authenticatable_role)
103
+ end
93
104
 
94
105
  it 'has the correct uid' do
95
106
  expect(attributes_from_auth[:uid]).to eq(new_user_attributes[:uid])
96
107
  end
97
108
 
98
109
  it 'has the correct provider' do
99
- expect(attributes_from_auth[:provider]).to eq(new_user_attributes[:provider])
110
+ expect(attributes_from_auth[:provider])
111
+ .to eq(new_user_attributes[:provider])
100
112
  end
101
113
 
102
114
  it 'has the correct first_name' do
103
- expect(attributes_from_auth[:first_name]).to eq(new_user_attributes[:first_name])
115
+ expect(attributes_from_auth[:first_name])
116
+ .to eq(new_user_attributes[:first_name])
104
117
  end
105
118
 
106
119
  it 'has the correct last_name' do
107
- expect(attributes_from_auth[:last_name]).to eq(new_user_attributes[:last_name])
120
+ expect(attributes_from_auth[:last_name])
121
+ .to eq(new_user_attributes[:last_name])
108
122
  end
109
123
 
110
124
  it 'has the correct email' do
@@ -112,7 +126,8 @@ describe G5Authenticatable::User do
112
126
  end
113
127
 
114
128
  it 'has the correct phone_number' do
115
- expect(attributes_from_auth[:phone_number]).to eq(new_user_attributes[:phone_number])
129
+ expect(attributes_from_auth[:phone_number])
130
+ .to eq(new_user_attributes[:phone_number])
116
131
  end
117
132
 
118
133
  it 'has the correct title' do
@@ -120,20 +135,25 @@ describe G5Authenticatable::User do
120
135
  end
121
136
 
122
137
  it 'has the correct organization_name' do
123
- expect(attributes_from_auth[:organization_name]).to eq(new_user_attributes[:organization_name])
138
+ expect(attributes_from_auth[:organization_name])
139
+ .to eq(new_user_attributes[:organization_name])
124
140
  end
125
141
  end
126
142
 
127
143
  describe '.new_with_session' do
128
- subject(:new_user) { G5Authenticatable::User.new_with_session(params, session) }
144
+ subject(:new_user) do
145
+ G5Authenticatable::User.new_with_session(params, session)
146
+ end
129
147
 
130
- let(:params) { Hash.new }
148
+ let(:params) { {} }
131
149
  let(:auth_data) do
150
+ full_name = [new_user_attributes[:first_name],
151
+ new_user_attributes[:last_name]].join(' ')
132
152
  OmniAuth::AuthHash.new(
133
153
  'provider' => new_user_attributes[:provider],
134
154
  'info' => {
135
155
  'email' => new_user_attributes[:email],
136
- 'name' => "#{new_user_attributes[:first_name]} #{new_user_attributes[:last_name]}",
156
+ 'name' => full_name,
137
157
  'first_name' => new_user_attributes[:first_name],
138
158
  'last_name' => new_user_attributes[:last_name],
139
159
  'phone' => new_user_attributes[:phone_number]
@@ -147,14 +167,21 @@ describe G5Authenticatable::User do
147
167
  'title' => new_user_attributes[:title],
148
168
  'organization_name' => new_user_attributes[:organization_name],
149
169
  'roles' => [
150
- { 'name' => new_role_attributes[:name], 'type' => 'GLOBAL', 'urn' => nil }
170
+ { 'name' => new_role_attributes[:name],
171
+ 'type' => 'GLOBAL',
172
+ 'urn' => nil }
151
173
  ],
152
174
  'raw_info' => {}
153
- })
175
+ }
176
+ )
154
177
  end
155
178
 
156
- let(:new_user_attributes) { FactoryGirl.attributes_for(:g5_authenticatable_user) }
157
- let(:new_role_attributes) { FactoryGirl.attributes_for(:g5_authenticatable_role) }
179
+ let(:new_user_attributes) do
180
+ FactoryGirl.attributes_for(:g5_authenticatable_user)
181
+ end
182
+ let(:new_role_attributes) do
183
+ FactoryGirl.attributes_for(:g5_authenticatable_role)
184
+ end
158
185
 
159
186
  context 'when there is auth data in the session' do
160
187
  let(:session) do
@@ -209,7 +236,8 @@ describe G5Authenticatable::User do
209
236
  end
210
237
 
211
238
  it 'should set the organization_name from the session data' do
212
- expect(new_user.organization_name).to eq(new_user_attributes[:organization_name])
239
+ expect(new_user.organization_name)
240
+ .to eq(new_user_attributes[:organization_name])
213
241
  end
214
242
 
215
243
  it 'should assign the role from the session data' do
@@ -218,7 +246,7 @@ describe G5Authenticatable::User do
218
246
  end
219
247
 
220
248
  context 'when there is no auth data in the session' do
221
- let(:session) { Hash.new }
249
+ let(:session) { {} }
222
250
 
223
251
  it 'should initialize a new user' do
224
252
  expect(new_user).to be_a_new_record
@@ -247,7 +275,9 @@ describe G5Authenticatable::User do
247
275
  end
248
276
 
249
277
  describe '.find_and_update_for_g5_oauth' do
250
- subject(:updated_user) { G5Authenticatable::User.find_and_update_for_g5_oauth(auth_data) }
278
+ subject(:updated_user) do
279
+ G5Authenticatable::User.find_and_update_for_g5_oauth(auth_data)
280
+ end
251
281
 
252
282
  let(:user_attributes) do
253
283
  FactoryGirl.attributes_for(:g5_authenticatable_user,
@@ -255,8 +285,7 @@ describe G5Authenticatable::User do
255
285
  last_name: nil,
256
286
  phone_number: nil,
257
287
  title: nil,
258
- organization_name: nil
259
- )
288
+ organization_name: nil)
260
289
  end
261
290
  let(:role_name) { :my_role }
262
291
 
@@ -267,29 +296,28 @@ describe G5Authenticatable::User do
267
296
 
268
297
  let(:auth_data) do
269
298
  OmniAuth::AuthHash.new(
270
- {
271
- 'provider' => user_attributes[:provider],
272
- 'uid' => user_attributes[:uid],
273
- 'info' => {
274
- 'email' => updated_attributes[:email],
275
- 'first_name' => updated_attributes[:first_name],
276
- 'last_name' => updated_attributes[:last_name],
277
- 'phone' => updated_attributes[:phone_number]
278
- },
279
- 'credentials' => {
280
- 'token' => updated_attributes[:g5_access_token],
281
- 'expires' => true,
282
- 'expires_at' => Time.now + 1000
283
- },
284
- 'extra' => {
285
- 'title' => updated_attributes[:title],
286
- 'organization_name' => updated_attributes[:organization_name],
287
- 'roles' => [
288
- { name: updated_role_name, type: 'GLOBAL', urn: nil }
289
- ],
290
- 'raw_info' => {}
291
- }
292
- })
299
+ 'provider' => user_attributes[:provider],
300
+ 'uid' => user_attributes[:uid],
301
+ 'info' => {
302
+ 'email' => updated_attributes[:email],
303
+ 'first_name' => updated_attributes[:first_name],
304
+ 'last_name' => updated_attributes[:last_name],
305
+ 'phone' => updated_attributes[:phone_number]
306
+ },
307
+ 'credentials' => {
308
+ 'token' => updated_attributes[:g5_access_token],
309
+ 'expires' => true,
310
+ 'expires_at' => Time.now + 1000
311
+ },
312
+ 'extra' => {
313
+ 'title' => updated_attributes[:title],
314
+ 'organization_name' => updated_attributes[:organization_name],
315
+ 'roles' => [
316
+ { name: updated_role_name, type: 'GLOBAL', urn: nil }
317
+ ],
318
+ 'raw_info' => {}
319
+ }
320
+ )
293
321
  end
294
322
 
295
323
  context 'when user info is the same' do
@@ -299,7 +327,8 @@ describe G5Authenticatable::User do
299
327
  let(:updated_role_name) { role_name }
300
328
 
301
329
  it 'should update the access token' do
302
- expect { updated_user }.to change { user.reload.g5_access_token }.to(updated_attributes[:g5_access_token])
330
+ expect { updated_user }.to change { user.reload.g5_access_token }
331
+ .to(updated_attributes[:g5_access_token])
303
332
  end
304
333
 
305
334
  it 'should return the updated user' do
@@ -353,7 +382,8 @@ describe G5Authenticatable::User do
353
382
  let(:updated_role_name) { 'super_admin' }
354
383
 
355
384
  it 'should update the access token' do
356
- expect { updated_user }.to change { user.reload.g5_access_token }.to(updated_attributes[:g5_access_token])
385
+ expect { updated_user }.to change { user.reload.g5_access_token }
386
+ .to(updated_attributes[:g5_access_token])
357
387
  end
358
388
 
359
389
  it 'should return the updated user' do
@@ -369,27 +399,33 @@ describe G5Authenticatable::User do
369
399
  end
370
400
 
371
401
  it 'should update the email' do
372
- expect { updated_user }.to change { user.reload.email }.to(updated_attributes[:email])
402
+ expect { updated_user }.to change { user.reload.email }
403
+ .to(updated_attributes[:email])
373
404
  end
374
405
 
375
406
  it 'should update the first name' do
376
- expect { updated_user }.to change { user.reload.first_name }.to(updated_attributes[:first_name])
407
+ expect { updated_user }.to change { user.reload.first_name }
408
+ .to(updated_attributes[:first_name])
377
409
  end
378
410
 
379
411
  it 'should update the last name' do
380
- expect { updated_user }.to change { user.reload.last_name }.to(updated_attributes[:last_name])
412
+ expect { updated_user }.to change { user.reload.last_name }
413
+ .to(updated_attributes[:last_name])
381
414
  end
382
415
 
383
416
  it 'should update the phone number' do
384
- expect { updated_user }.to change { user.reload.phone_number }.to(updated_attributes[:phone_number])
417
+ expect { updated_user }.to change { user.reload.phone_number }
418
+ .to(updated_attributes[:phone_number])
385
419
  end
386
420
 
387
421
  it 'should update the title' do
388
- expect { updated_user }.to change { user.reload.title }.to(updated_attributes[:title])
422
+ expect { updated_user }.to change { user.reload.title }
423
+ .to(updated_attributes[:title])
389
424
  end
390
425
 
391
426
  it 'should update the organization_name' do
392
- expect { updated_user }.to change { user.reload.organization_name }.to(updated_attributes[:organization_name])
427
+ expect { updated_user }.to change { user.reload.organization_name }
428
+ .to(updated_attributes[:organization_name])
393
429
  end
394
430
 
395
431
  it 'should unassign the old role' do
@@ -459,17 +495,12 @@ describe G5Authenticatable::User do
459
495
  end
460
496
 
461
497
  let(:user2) { G5Authenticatable::User.create(user_attributes) }
462
- let(:user_attributes2) { FactoryGirl.attributes_for(:g5_authenticatable_user) }
463
- let(:mock_urn) { 'mock_urn' }
464
-
465
- let(:mock_resource_class) { Class.new }
466
- before { stub_const('MockResource', mock_resource_class) }
467
-
468
- let(:mock_resource) { stub_model(mock_resource_class, urn: mock_urn) }
469
- before do
470
- allow(mock_resource_class).to receive(:where).with(urn: mock_urn).and_return([mock_resource])
498
+ let(:user_attributes2) do
499
+ FactoryGirl.attributes_for(:g5_authenticatable_user)
471
500
  end
472
501
 
502
+ let!(:resource) { FactoryGirl.create(:g5_updatable_client) }
503
+
473
504
  let(:auth_data) do
474
505
  OmniAuth::AuthHash.new(
475
506
  'provider' => user_attributes[:provider],
@@ -490,7 +521,8 @@ describe G5Authenticatable::User do
490
521
  'organization_name' => user_attributes[:organization_name],
491
522
  'roles' => roles,
492
523
  'raw_info' => {}
493
- })
524
+ }
525
+ )
494
526
  end
495
527
 
496
528
  context 'with global role' do
@@ -499,7 +531,8 @@ describe G5Authenticatable::User do
499
531
  end
500
532
 
501
533
  it 'will add a global role' do
502
- expect { user.update_roles_from_auth(auth_data) }.to change { user.roles.length }.from(0).to(1)
534
+ expect { user.update_roles_from_auth(auth_data) }
535
+ .to change { user.roles.length }.from(0).to(1)
503
536
  expect(user.roles.first.name).to eq('admin')
504
537
  expect(user.roles.first.resource).to be_nil
505
538
  end
@@ -507,42 +540,42 @@ describe G5Authenticatable::User do
507
540
 
508
541
  context 'with a scoped role' do
509
542
  let(:roles) do
510
- [{ name: 'viewer', type: 'MockResource', urn: mock_urn }]
543
+ [{ name: 'viewer', type: resource.class.name, urn: resource.urn }]
511
544
  end
512
545
 
513
546
  it 'will add a scoped role' do
514
- expect { user.update_roles_from_auth(auth_data) }.to change { user.roles.length }.from(0).to(1)
547
+ expect { user.update_roles_from_auth(auth_data) }
548
+ .to change { user.roles.length }.from(0).to(1)
515
549
  expect(user.roles.first.name).to eq('viewer')
516
- expect(user.roles.first.resource_id).to eq(mock_resource.id)
517
- expect(user.roles.first.resource_type).to eq('MockResource')
550
+ expect(user.roles.first.resource_id).to eq(resource.id)
551
+ expect(user.roles.first.resource_type).to eq(resource.class.name)
518
552
  end
519
553
  end
520
554
 
521
555
  context 'with a more than 1 role' do
522
556
  let(:roles) do
523
557
  [
524
- { name: 'viewer', type: 'MockResource', urn: mock_urn },
558
+ { name: 'viewer', type: resource.class.name, urn: resource.urn },
525
559
  { name: 'admin', type: 'GLOBAL', urn: nil }
526
560
  ]
527
561
  end
528
562
 
529
563
  it 'will add a scoped role' do
530
- expect { user.update_roles_from_auth(auth_data) }.to change { user.roles.length }.from(0).to(2)
564
+ expect { user.update_roles_from_auth(auth_data) }
565
+ .to change { user.roles.length }.from(0).to(2)
531
566
  end
532
567
  end
533
568
 
534
569
  context 'with an un-existing scoped role URL' do
535
570
  let(:non_existing_urn) { 'some-non-existing-urn' }
536
- before do
537
- allow(mock_resource_class).to receive(:where).with(urn: non_existing_urn).and_return([])
538
- end
539
571
 
540
572
  let(:roles) do
541
- [{ name: 'viewer', type: 'MockResource', urn: non_existing_urn }]
573
+ [{ name: 'viewer', type: 'G5Updatable::Client', urn: non_existing_urn }]
542
574
  end
543
575
 
544
576
  it 'will add a scoped role' do
545
- expect { user.update_roles_from_auth(auth_data) }.to_not change { user.roles.length }
577
+ expect { user.update_roles_from_auth(auth_data) }
578
+ .to_not change { user.roles.length }
546
579
  end
547
580
  end
548
581
 
@@ -550,23 +583,25 @@ describe G5Authenticatable::User do
550
583
  let(:roles) { [] }
551
584
 
552
585
  it 'will add a scoped role' do
553
- expect { user.update_roles_from_auth(auth_data) }.to_not change { user.roles.length }.from(0)
586
+ expect { user.update_roles_from_auth(auth_data) }
587
+ .to_not change { user.roles.length }.from(0)
554
588
  end
555
589
  end
556
590
 
557
591
  context 'with a bad role type' do
558
592
  let(:roles) do
559
593
  [
560
- { name: 'viewer', type: 'MockResource', urn: mock_urn },
561
- { name: 'viewer', type: 'BadResource', urn: mock_urn }
594
+ { name: 'viewer', type: resource.class.name, urn: resource.urn },
595
+ { name: 'viewer', type: 'BadResource', urn: resource.urn }
562
596
  ]
563
597
  end
564
598
 
565
599
  it 'will skip the bad role' do
566
- expect { user.update_roles_from_auth(auth_data) }.to change { user.roles.length }.from(0).to(1)
600
+ expect { user.update_roles_from_auth(auth_data) }
601
+ .to change { user.roles.length }.from(0).to(1)
567
602
  expect(user.roles.first.name).to eq('viewer')
568
- expect(user.roles.first.resource_id).to eq(mock_resource.id)
569
- expect(user.roles.first.resource_type).to eq('MockResource')
603
+ expect(user.roles.first.resource_id).to eq(resource.id)
604
+ expect(user.roles.first.resource_type).to eq(resource.class.name)
570
605
  end
571
606
  end
572
607
  end