omniauth-identity 3.0.3 → 3.0.8

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,6 +1,12 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- RSpec.describe(OmniAuth::Identity::Models::ActiveRecord, db: true) do
3
+ require 'sqlite3'
4
+ require 'active_record'
5
+ require 'anonymous_active_record'
6
+
7
+ class TestIdentity < OmniAuth::Identity::Models::ActiveRecord; end
8
+
9
+ RSpec.describe(OmniAuth::Identity::Models::ActiveRecord, sqlite3: true) do
4
10
  describe 'model', type: :model do
5
11
  subject(:model_klass) do
6
12
  AnonymousActiveRecord.generate(
@@ -14,17 +20,20 @@ RSpec.describe(OmniAuth::Identity::Models::ActiveRecord, db: true) do
14
20
  end
15
21
  end
16
22
 
17
- it 'delegates locate to the where query method' do
18
- allow(model_klass).to receive(:where).with('ham_sandwich' => 'open faced', 'category' => 'sandwiches',
19
- 'provider' => 'identity').and_return(['wakka'])
20
- expect(model_klass.locate('ham_sandwich' => 'open faced', 'category' => 'sandwiches')).to eq('wakka')
23
+ include_context 'persistable model'
24
+
25
+ describe '::table_name' do
26
+ it 'does not use STI rules for its table name' do
27
+ expect(TestIdentity.table_name).to eq('test_identities')
28
+ end
21
29
  end
22
- end
23
30
 
24
- describe '#table_name' do
25
- class TestIdentity < OmniAuth::Identity::Models::ActiveRecord; end
26
- it 'does not use STI rules for its table name' do
27
- expect(TestIdentity.table_name).to eq('test_identities')
31
+ describe '::locate' do
32
+ it 'delegates locate to the where query method' do
33
+ allow(model_klass).to receive(:where).with('email' => 'open faced', 'category' => 'sandwiches',
34
+ 'provider' => 'identity').and_return(['wakka'])
35
+ expect(model_klass.locate('email' => 'open faced', 'category' => 'sandwiches')).to eq('wakka')
36
+ end
28
37
  end
29
38
  end
30
39
  end
@@ -0,0 +1,38 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'sqlite3'
4
+ require 'sequel'
5
+
6
+ DB = Sequel.sqlite
7
+
8
+ RSpec.describe(OmniAuth::Identity::Models::Sequel, sqlite3: true) do
9
+ before(:all) do
10
+ # Connect to an in-memory sqlite3 database.
11
+ DB.create_table :sequel_test_identities do
12
+ primary_key :id
13
+ String :email, null: false
14
+ String :password_digest, null: false
15
+ end
16
+ end
17
+
18
+ before do
19
+ sequel_test_identity = Class.new(Sequel::Model(:sequel_test_identities)) do
20
+ include ::OmniAuth::Identity::Models::Sequel
21
+ end
22
+ stub_const('SequelTestIdentity', sequel_test_identity)
23
+ end
24
+
25
+ describe 'model', type: :model do
26
+ subject(:model_klass) { SequelTestIdentity }
27
+
28
+ include_context 'persistable model'
29
+
30
+ describe '::locate' do
31
+ it 'delegates to the where query method' do
32
+ allow(model_klass).to receive(:where).with('email' => 'open faced',
33
+ 'category' => 'sandwiches').and_return(['wakka'])
34
+ expect(model_klass.locate('email' => 'open faced', 'category' => 'sandwiches')).to eq('wakka')
35
+ end
36
+ end
37
+ end
38
+ end
@@ -1,10 +1,15 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- RSpec.describe OmniAuth::Strategies::Identity do
3
+ require 'sqlite3'
4
+ require 'active_record'
5
+ require 'anonymous_active_record'
6
+
7
+ RSpec.describe OmniAuth::Strategies::Identity, sqlite3: true do
4
8
  attr_accessor :app
5
9
 
6
- let(:auth_hash) { last_response.headers['env']['omniauth.auth'] }
7
- let(:identity_hash) { last_response.headers['env']['omniauth.identity'] }
10
+ let(:env_hash) { last_response.headers['env'] }
11
+ let(:auth_hash) { env_hash['omniauth.auth'] }
12
+ let(:identity_hash) { env_hash['omniauth.identity'] }
8
13
  let(:identity_options) { {} }
9
14
  let(:anon_ar) do
10
15
  AnonymousActiveRecord.generate(
@@ -193,7 +198,7 @@ RSpec.describe OmniAuth::Strategies::Identity do
193
198
  end
194
199
  end
195
200
 
196
- context 'with successful creation' do
201
+ context 'with good identity' do
197
202
  let(:properties) do
198
203
  {
199
204
  name: 'Awesome Dude',
@@ -209,9 +214,66 @@ RSpec.describe OmniAuth::Strategies::Identity do
209
214
  expect(auth_hash['uid']).to match(/\d+/)
210
215
  expect(auth_hash['provider']).to eq('identity')
211
216
  end
217
+
218
+ context 'with on_validation proc' do
219
+ let(:identity_options) do
220
+ { model: anon_ar, on_validation: on_validation_proc }
221
+ end
222
+ let(:on_validation_proc) do
223
+ lambda { |_env|
224
+ false
225
+ }
226
+ end
227
+
228
+ context 'when validation fails' do
229
+ it 'does not set the env hash' do
230
+ post '/auth/identity/register', properties
231
+ expect(env_hash).to eq(nil)
232
+ end
233
+
234
+ it 'renders registration form' do
235
+ post '/auth/identity/register', properties
236
+ expect(last_response.body).to be_include(described_class.default_options[:registration_form_title])
237
+ end
238
+
239
+ it 'displays validation failure message' do
240
+ post '/auth/identity/register', properties
241
+ expect(last_response.body).to be_include(described_class.default_options[:validation_failure_message])
242
+ end
243
+ end
244
+
245
+ context 'when validation succeeds' do
246
+ let(:on_validation_proc) do
247
+ lambda { |_env|
248
+ true
249
+ }
250
+ end
251
+
252
+ it 'sets the auth hash' do
253
+ post '/auth/identity/register', properties
254
+ expect(auth_hash['uid']).to match(/\d+/)
255
+ expect(auth_hash['provider']).to eq('identity')
256
+ end
257
+
258
+ it 'does not render registration form' do
259
+ post '/auth/identity/register', properties
260
+ expect(last_response.body).not_to be_include(described_class.default_options[:registration_form_title])
261
+ end
262
+
263
+ it 'does not display validation failure message' do
264
+ post '/auth/identity/register', properties
265
+ expect(last_response.body).not_to be_include(described_class.default_options[:validation_failure_message])
266
+ end
267
+
268
+ it 'does not display registration failure message' do
269
+ post '/auth/identity/register', properties
270
+ expect(last_response.body).not_to be_include(described_class.default_options[:registration_failure_message])
271
+ end
272
+ end
273
+ end
212
274
  end
213
275
 
214
- context 'with invalid identity' do
276
+ context 'with bad identity' do
215
277
  let(:properties) do
216
278
  {
217
279
  name: 'Awesome Dude',
@@ -249,6 +311,62 @@ RSpec.describe OmniAuth::Strategies::Identity do
249
311
  expect(last_response.body).not_to be_include('One or more fields were invalid')
250
312
  end
251
313
  end
314
+
315
+ context 'with on_validation proc' do
316
+ let(:identity_options) do
317
+ { model: anon_ar, on_validation: on_validation_proc }
318
+ end
319
+ let(:on_validation_proc) do
320
+ lambda { |_env|
321
+ false
322
+ }
323
+ end
324
+
325
+ context 'when validation fails' do
326
+ it 'does not set the env hash' do
327
+ post '/auth/identity/register', properties
328
+ expect(env_hash).to eq(nil)
329
+ end
330
+
331
+ it 'renders registration form' do
332
+ post '/auth/identity/register', properties
333
+ expect(last_response.body).to be_include(described_class.default_options[:registration_form_title])
334
+ end
335
+
336
+ it 'displays validation failure message' do
337
+ post '/auth/identity/register', properties
338
+ expect(last_response.body).to be_include(described_class.default_options[:validation_failure_message])
339
+ end
340
+ end
341
+
342
+ context 'when validation succeeds' do
343
+ let(:on_validation_proc) do
344
+ lambda { |_env|
345
+ true
346
+ }
347
+ end
348
+
349
+ it 'does not set the env hash' do
350
+ post '/auth/identity/register', properties
351
+ expect(env_hash).to eq(nil)
352
+ end
353
+
354
+ it 'renders registration form' do
355
+ post '/auth/identity/register', properties
356
+ expect(last_response.body).to be_include(described_class.default_options[:registration_form_title])
357
+ end
358
+
359
+ it 'does not display validation failure message' do
360
+ post '/auth/identity/register', properties
361
+ expect(last_response.body).not_to be_include(described_class.default_options[:validation_failure_message])
362
+ end
363
+
364
+ it 'display registration failure message' do
365
+ post '/auth/identity/register', properties
366
+ expect(last_response.body).to be_include(described_class.default_options[:registration_failure_message])
367
+ end
368
+ end
369
+ end
252
370
  end
253
371
  end
254
372
  end
data/spec/spec_helper.rb CHANGED
@@ -1,20 +1,32 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ # NOTE: mongoid and no_brainer can't be loaded at the same time.
4
+ # If you try it, one or both of them will not work.
5
+ # This is why the ORM specs are split into a separate directory and run in separate threads.
6
+
7
+ ENV['RUBY_ENV'] = 'test' # Used by NoBrainer
8
+ ENV['MONGOID_ENV'] = 'test' # Used by Mongoid
9
+
3
10
  ruby_version = Gem::Version.new(RUBY_VERSION)
4
11
  require 'simplecov' if ruby_version >= Gem::Version.new('2.7') && RUBY_ENGINE == 'ruby'
5
12
 
6
13
  require 'rack/test'
7
- require 'mongoid-rspec'
8
- require 'sqlite3'
9
- require 'anonymous_active_record'
14
+ require 'rspec/block_is_expected'
10
15
  require 'byebug' if RUBY_ENGINE == 'ruby'
11
16
 
12
17
  # This gem
13
18
  require 'omniauth/identity'
14
19
 
20
+ spec_root_matcher = %r{#{__dir__}/(.+)\.rb\Z}
21
+ Dir.glob(Pathname.new(__dir__).join('support/**/', '*.rb')).each { |f| require f.match(spec_root_matcher)[1] }
22
+
23
+ DEFAULT_PASSWORD = 'hang-a-left-at-the-diner'
24
+ DEFAULT_EMAIL = 'mojo@example.com'
25
+
15
26
  RSpec.configure do |config|
16
27
  config.include Rack::Test::Methods
17
- config.include Mongoid::Matchers, type: :model
28
+
29
+ # config.include ::Mongoid::Matchers, db: :mongodb
18
30
 
19
31
  # Enable flags like --only-failures and --next-failure
20
32
  config.example_status_persistence_file_path = '.rspec_status'
@@ -0,0 +1,89 @@
1
+ # frozen_string_literal: true
2
+
3
+ RSpec.shared_examples 'instance with instance methods' do
4
+ describe '#initialize' do
5
+ it 'does not raise an error' do
6
+ block_is_expected.not_to raise_error
7
+ end
8
+ end
9
+
10
+ describe '#uid' do
11
+ it 'defaults to #id' do
12
+ allow(instance).to receive(:respond_to?).with(:id).and_return(true)
13
+ allow(instance).to receive(:id).and_return 'wakka-do'
14
+ expect(instance.uid).to eq('wakka-do')
15
+ end
16
+
17
+ it 'stringifies it' do
18
+ allow(instance).to receive(:id).and_return 123
19
+ expect(instance.uid).to eq('123')
20
+ end
21
+
22
+ it 'raises NotImplementedError if #id is not defined' do
23
+ allow(instance).to receive(:respond_to?).with(:id).and_return(false)
24
+ expect { instance.uid }.to raise_error(NotImplementedError)
25
+ end
26
+ end
27
+
28
+ describe '#auth_key' do
29
+ it 'defaults to #email' do
30
+ allow(instance).to receive(:respond_to?).with(:email).and_return(true)
31
+ allow(instance).to receive(:email).and_return('bob@bob.com')
32
+ expect(instance.auth_key).to eq('bob@bob.com')
33
+ end
34
+
35
+ it 'uses the class .auth_key' do
36
+ instance.class.auth_key 'login'
37
+ allow(instance).to receive(:login).and_return 'bob'
38
+ expect(instance.auth_key).to eq('bob')
39
+ instance.class.auth_key nil
40
+ end
41
+ end
42
+
43
+ describe '#auth_key=' do
44
+ it 'defaults to setting email' do
45
+ allow(instance).to receive(:respond_to?).with(:email=).and_return(true)
46
+ expect(instance).to receive(:email=).with 'abc'
47
+
48
+ instance.auth_key = 'abc'
49
+ end
50
+
51
+ it 'uses a custom .auth_key if one is provided' do
52
+ instance.class.auth_key 'login'
53
+ allow(instance).to receive(:respond_to?).with(:login=).and_return(true)
54
+ expect(instance).to receive(:login=).with('abc')
55
+
56
+ instance.auth_key = 'abc'
57
+ end
58
+ end
59
+
60
+ describe '#info' do
61
+ it 'includes all attributes as they have been set' do
62
+ allow(instance).to receive(:name).and_return('Bob Bobson')
63
+ allow(instance).to receive(:nickname).and_return('bob')
64
+
65
+ expect(instance.info).to include({
66
+ 'name' => 'Bob Bobson',
67
+ 'nickname' => 'bob'
68
+ })
69
+ end
70
+
71
+ it 'uses firstname and lastname, over nickname, to set missing name' do
72
+ allow(instance).to receive(:first_name).and_return('shoeless')
73
+ allow(instance).to receive(:last_name).and_return('joe')
74
+ allow(instance).to receive(:nickname).and_return('george')
75
+ instance.info['name'] == 'shoeless joe'
76
+ end
77
+
78
+ it 'uses nickname to set missing name when first and last are not set' do
79
+ allow(instance).to receive(:nickname).and_return('bob')
80
+ instance.info['name'] == 'bob'
81
+ end
82
+
83
+ it 'does not overwrite a provided name' do
84
+ allow(instance).to receive(:name).and_return('Awesome Dude')
85
+ allow(instance).to receive(:first_name).and_return('Frank')
86
+ expect(instance.info['name']).to eq('Awesome Dude')
87
+ end
88
+ end
89
+ end
@@ -0,0 +1,29 @@
1
+ # frozen_string_literal: true
2
+
3
+ RSpec.shared_examples 'model with class methods' do
4
+ describe 'class definition' do
5
+ it 'does not raise an error' do
6
+ block_is_expected.not_to raise_error
7
+ end
8
+ end
9
+
10
+ describe '::authenticate' do
11
+ it 'calls locate and then authenticate' do
12
+ mocked_instance = double('ExampleModel', authenticate: 'abbadoo')
13
+ allow(model_klass).to receive(:locate).with('email' => 'example').and_return(mocked_instance)
14
+ expect(model_klass.authenticate({ 'email' => 'example' }, 'pass')).to eq('abbadoo')
15
+ end
16
+
17
+ it 'calls locate with additional scopes when provided' do
18
+ mocked_instance = double('ExampleModel', authenticate: 'abbadoo')
19
+ allow(model_klass).to receive(:locate).with('email' => 'example',
20
+ 'user_type' => 'admin').and_return(mocked_instance)
21
+ expect(model_klass.authenticate({ 'email' => 'example', 'user_type' => 'admin' }, 'pass')).to eq('abbadoo')
22
+ end
23
+
24
+ it 'recovers gracefully if locate is nil' do
25
+ allow(model_klass).to receive(:locate).and_return(nil)
26
+ expect(model_klass.authenticate('blah', 'foo')).to be false
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,24 @@
1
+ # frozen_string_literal: true
2
+
3
+ RSpec.shared_context 'persistable model' do
4
+ include_context 'model with class methods'
5
+
6
+ describe 'instance methods' do
7
+ subject(:instance) { model_klass.new }
8
+
9
+ include_context 'instance with instance methods'
10
+
11
+ describe '#save' do
12
+ subject(:save) do
13
+ instance.email = DEFAULT_EMAIL
14
+ instance.password = DEFAULT_PASSWORD
15
+ instance.password_confirmation = DEFAULT_PASSWORD
16
+ instance.save
17
+ end
18
+
19
+ it 'does not raise an error' do
20
+ save
21
+ end
22
+ end
23
+ end
24
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: omniauth-identity
3
3
  version: !ruby/object:Gem::Version
4
- version: 3.0.3
4
+ version: 3.0.8
5
5
  platform: ruby
6
6
  authors:
7
7
  - Peter Boling
@@ -10,7 +10,7 @@ authors:
10
10
  autorequire:
11
11
  bindir: exe
12
12
  cert_chain: []
13
- date: 2021-02-14 00:00:00.000000000 Z
13
+ date: 2021-03-24 00:00:00.000000000 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: bcrypt
@@ -41,89 +41,61 @@ dependencies:
41
41
  - !ruby/object:Gem::Version
42
42
  version: '0'
43
43
  - !ruby/object:Gem::Dependency
44
- name: anonymous_active_record
45
- requirement: !ruby/object:Gem::Requirement
46
- requirements:
47
- - - ">="
48
- - !ruby/object:Gem::Version
49
- version: 1.0.7
50
- type: :development
51
- prerelease: false
52
- version_requirements: !ruby/object:Gem::Requirement
53
- requirements:
54
- - - ">="
55
- - !ruby/object:Gem::Version
56
- version: 1.0.7
57
- - !ruby/object:Gem::Dependency
58
- name: mongoid
44
+ name: rack-test
59
45
  requirement: !ruby/object:Gem::Requirement
60
46
  requirements:
61
47
  - - "~>"
62
48
  - !ruby/object:Gem::Version
63
- version: '7'
49
+ version: '1'
64
50
  type: :development
65
51
  prerelease: false
66
52
  version_requirements: !ruby/object:Gem::Requirement
67
53
  requirements:
68
54
  - - "~>"
69
55
  - !ruby/object:Gem::Version
70
- version: '7'
71
- - !ruby/object:Gem::Dependency
72
- name: nobrainer
73
- requirement: !ruby/object:Gem::Requirement
74
- requirements:
75
- - - ">="
76
- - !ruby/object:Gem::Version
77
- version: '0'
78
- type: :development
79
- prerelease: false
80
- version_requirements: !ruby/object:Gem::Requirement
81
- requirements:
82
- - - ">="
83
- - !ruby/object:Gem::Version
84
- version: '0'
56
+ version: '1'
85
57
  - !ruby/object:Gem::Dependency
86
- name: rack-test
58
+ name: rake
87
59
  requirement: !ruby/object:Gem::Requirement
88
60
  requirements:
89
61
  - - "~>"
90
62
  - !ruby/object:Gem::Version
91
- version: '1'
63
+ version: '13'
92
64
  type: :development
93
65
  prerelease: false
94
66
  version_requirements: !ruby/object:Gem::Requirement
95
67
  requirements:
96
68
  - - "~>"
97
69
  - !ruby/object:Gem::Version
98
- version: '1'
70
+ version: '13'
99
71
  - !ruby/object:Gem::Dependency
100
- name: rake
72
+ name: rspec
101
73
  requirement: !ruby/object:Gem::Requirement
102
74
  requirements:
103
75
  - - "~>"
104
76
  - !ruby/object:Gem::Version
105
- version: '13'
77
+ version: '3'
106
78
  type: :development
107
79
  prerelease: false
108
80
  version_requirements: !ruby/object:Gem::Requirement
109
81
  requirements:
110
82
  - - "~>"
111
83
  - !ruby/object:Gem::Version
112
- version: '13'
84
+ version: '3'
113
85
  - !ruby/object:Gem::Dependency
114
- name: rspec
86
+ name: rspec-block_is_expected
115
87
  requirement: !ruby/object:Gem::Requirement
116
88
  requirements:
117
89
  - - "~>"
118
90
  - !ruby/object:Gem::Version
119
- version: '3'
91
+ version: '1.0'
120
92
  type: :development
121
93
  prerelease: false
122
94
  version_requirements: !ruby/object:Gem::Requirement
123
95
  requirements:
124
96
  - - "~>"
125
97
  - !ruby/object:Gem::Version
126
- version: '3'
98
+ version: '1.0'
127
99
  - !ruby/object:Gem::Dependency
128
100
  name: sqlite3
129
101
  requirement: !ruby/object:Gem::Requirement
@@ -138,7 +110,7 @@ dependencies:
138
110
  - - "~>"
139
111
  - !ruby/object:Gem::Version
140
112
  version: '1.4'
141
- description: Internal authentication handlers for OmniAuth.
113
+ description: Traditional username/password based authentication system for OmniAuth
142
114
  email:
143
115
  executables: []
144
116
  extensions: []
@@ -155,17 +127,19 @@ files:
155
127
  - lib/omniauth/identity/models/active_record.rb
156
128
  - lib/omniauth/identity/models/couch_potato.rb
157
129
  - lib/omniauth/identity/models/mongoid.rb
158
- - lib/omniauth/identity/models/no_brainer.rb
130
+ - lib/omniauth/identity/models/nobrainer.rb
131
+ - lib/omniauth/identity/models/sequel.rb
159
132
  - lib/omniauth/identity/secure_password.rb
160
133
  - lib/omniauth/strategies/identity.rb
161
134
  - spec/omniauth/identity/model_spec.rb
162
135
  - spec/omniauth/identity/models/active_record_spec.rb
163
- - spec/omniauth/identity/models/couch_potato_spec.rb
164
- - spec/omniauth/identity/models/mongoid_spec.rb
165
- - spec/omniauth/identity/models/no_brainer_spec.rb
136
+ - spec/omniauth/identity/models/sequel_spec.rb
166
137
  - spec/omniauth/identity/secure_password_spec.rb
167
138
  - spec/omniauth/strategies/identity_spec.rb
168
139
  - spec/spec_helper.rb
140
+ - spec/support/shared_contexts/instance_with_instance_methods.rb
141
+ - spec/support/shared_contexts/model_with_class_methods.rb
142
+ - spec/support/shared_contexts/persistable_model.rb
169
143
  homepage: http://github.com/omniauth/omniauth-identity
170
144
  licenses:
171
145
  - MIT
@@ -186,16 +160,17 @@ required_rubygems_version: !ruby/object:Gem::Requirement
186
160
  - !ruby/object:Gem::Version
187
161
  version: 1.3.6
188
162
  requirements: []
189
- rubygems_version: 3.2.3
163
+ rubygems_version: 3.2.9
190
164
  signing_key:
191
165
  specification_version: 4
192
- summary: Internal authentication handlers for OmniAuth.
166
+ summary: Traditional username/password based authentication system for OmniAuth
193
167
  test_files:
194
- - spec/omniauth/identity/model_spec.rb
168
+ - spec/spec_helper.rb
169
+ - spec/support/shared_contexts/persistable_model.rb
170
+ - spec/support/shared_contexts/model_with_class_methods.rb
171
+ - spec/support/shared_contexts/instance_with_instance_methods.rb
195
172
  - spec/omniauth/identity/models/active_record_spec.rb
196
- - spec/omniauth/identity/models/couch_potato_spec.rb
197
- - spec/omniauth/identity/models/mongoid_spec.rb
198
- - spec/omniauth/identity/models/no_brainer_spec.rb
173
+ - spec/omniauth/identity/models/sequel_spec.rb
199
174
  - spec/omniauth/identity/secure_password_spec.rb
175
+ - spec/omniauth/identity/model_spec.rb
200
176
  - spec/omniauth/strategies/identity_spec.rb
201
- - spec/spec_helper.rb