omniauth-identity 1.1.1 → 3.0.3

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,122 +1,123 @@
1
- require 'spec_helper'
1
+ # frozen_string_literal: true
2
2
 
3
3
  class ExampleModel
4
4
  include OmniAuth::Identity::Model
5
5
  end
6
6
 
7
- describe OmniAuth::Identity::Model do
7
+ RSpec.describe OmniAuth::Identity::Model do
8
8
  context 'Class Methods' do
9
- subject{ ExampleModel }
9
+ subject { ExampleModel }
10
10
 
11
11
  describe '.locate' do
12
- it('should be abstract'){ lambda{ subject.locate('abc') }.should raise_error(NotImplementedError) }
12
+ it('is abstract') { expect { subject.locate('abc') }.to raise_error(NotImplementedError) }
13
13
  end
14
14
 
15
15
  describe '.authenticate' do
16
- it 'should call locate and then authenticate' do
17
- mocked_instance = mock('ExampleModel', :authenticate => 'abbadoo')
18
- subject.should_receive(:locate).with('email' => 'example').and_return(mocked_instance)
19
- subject.authenticate({'email' => 'example'},'pass').should == 'abbadoo'
16
+ it 'calls locate and then authenticate' do
17
+ mocked_instance = double('ExampleModel', authenticate: 'abbadoo')
18
+ allow(subject).to receive(:locate).with('email' => 'example').and_return(mocked_instance)
19
+ expect(subject.authenticate({ 'email' => 'example' }, 'pass')).to eq('abbadoo')
20
20
  end
21
21
 
22
- it 'should call locate with additional scopes when provided' do
23
- mocked_instance = mock('ExampleModel', :authenticate => 'abbadoo')
24
- subject.should_receive(:locate).with('email' => 'example', 'user_type' => 'admin').and_return(mocked_instance)
25
- subject.authenticate({'email' => 'example', 'user_type' => 'admin'}, 'pass').should == 'abbadoo'
22
+ it 'calls locate with additional scopes when provided' do
23
+ mocked_instance = double('ExampleModel', authenticate: 'abbadoo')
24
+ allow(subject).to receive(:locate).with('email' => 'example',
25
+ 'user_type' => 'admin').and_return(mocked_instance)
26
+ expect(subject.authenticate({ 'email' => 'example', 'user_type' => 'admin' }, 'pass')).to eq('abbadoo')
26
27
  end
27
28
 
28
- it 'should recover gracefully if locate is nil' do
29
- subject.stub!(:locate).and_return(nil)
30
- subject.authenticate('blah','foo').should be_false
29
+ it 'recovers gracefully if locate is nil' do
30
+ allow(subject).to receive(:locate).and_return(nil)
31
+ expect(subject.authenticate('blah', 'foo')).to be false
31
32
  end
32
33
  end
33
34
  end
34
35
 
35
36
  context 'Instance Methods' do
36
- subject{ ExampleModel.new }
37
+ subject { ExampleModel.new }
37
38
 
38
39
  describe '#authenticate' do
39
- it('should be abstract'){ lambda{ subject.authenticate('abc') }.should raise_error(NotImplementedError) }
40
+ it('is abstract') { expect { subject.authenticate('abc') }.to raise_error(NotImplementedError) }
40
41
  end
41
42
 
42
43
  describe '#uid' do
43
- it 'should default to #id' do
44
- subject.should_receive(:respond_to?).with('id').and_return(true)
45
- subject.stub!(:id).and_return 'wakka-do'
46
- subject.uid.should == 'wakka-do'
44
+ it 'defaults to #id' do
45
+ allow(subject).to receive(:respond_to?).with(:id).and_return(true)
46
+ allow(subject).to receive(:id).and_return 'wakka-do'
47
+ expect(subject.uid).to eq('wakka-do')
47
48
  end
48
49
 
49
- it 'should stringify it' do
50
- subject.stub!(:id).and_return 123
51
- subject.uid.should == '123'
50
+ it 'stringifies it' do
51
+ allow(subject).to receive(:id).and_return 123
52
+ expect(subject.uid).to eq('123')
52
53
  end
53
54
 
54
- it 'should raise NotImplementedError if #id is not defined' do
55
- subject.should_receive(:respond_to?).with('id').and_return(false)
56
- lambda{ subject.uid }.should raise_error(NotImplementedError)
55
+ it 'raises NotImplementedError if #id is not defined' do
56
+ allow(subject).to receive(:respond_to?).with(:id).and_return(false)
57
+ expect { subject.uid }.to raise_error(NotImplementedError)
57
58
  end
58
59
  end
59
60
 
60
61
  describe '#auth_key' do
61
- it 'should default to #email' do
62
- subject.should_receive(:respond_to?).with('email').and_return(true)
63
- subject.stub!(:email).and_return('bob@bob.com')
64
- subject.auth_key.should == 'bob@bob.com'
62
+ it 'defaults to #email' do
63
+ allow(subject).to receive(:respond_to?).with(:email).and_return(true)
64
+ allow(subject).to receive(:email).and_return('bob@bob.com')
65
+ expect(subject.auth_key).to eq('bob@bob.com')
65
66
  end
66
67
 
67
- it 'should use the class .auth_key' do
68
+ it 'uses the class .auth_key' do
68
69
  subject.class.auth_key 'login'
69
- subject.stub!(:login).and_return 'bob'
70
- subject.auth_key.should == 'bob'
70
+ allow(subject).to receive(:login).and_return 'bob'
71
+ expect(subject.auth_key).to eq('bob')
71
72
  subject.class.auth_key nil
72
73
  end
73
74
 
74
- it 'should raise a NotImplementedError if the auth_key method is not defined' do
75
- lambda{ subject.auth_key }.should raise_error(NotImplementedError)
75
+ it 'raises a NotImplementedError if the auth_key method is not defined' do
76
+ expect { subject.auth_key }.to raise_error(NotImplementedError)
76
77
  end
77
78
  end
78
79
 
79
80
  describe '#auth_key=' do
80
- it 'should default to setting email' do
81
- subject.should_receive(:respond_to?).with('email=').and_return(true)
82
- subject.should_receive(:email=).with 'abc'
83
-
81
+ it 'defaults to setting email' do
82
+ allow(subject).to receive(:respond_to?).with(:email=).and_return(true)
83
+ expect(subject).to receive(:email=).with 'abc'
84
+
84
85
  subject.auth_key = 'abc'
85
86
  end
86
87
 
87
- it 'should use a custom .auth_key if one is provided' do
88
+ it 'uses a custom .auth_key if one is provided' do
88
89
  subject.class.auth_key 'login'
89
- subject.should_receive(:respond_to?).with('login=').and_return(true)
90
- subject.should_receive('login=').with('abc')
90
+ allow(subject).to receive(:respond_to?).with(:login=).and_return(true)
91
+ expect(subject).to receive(:login=).with('abc')
91
92
 
92
93
  subject.auth_key = 'abc'
93
94
  end
94
95
 
95
- it 'should raise a NotImplementedError if the autH_key method is not defined' do
96
- lambda{ subject.auth_key = 'broken' }.should raise_error(NotImplementedError)
96
+ it 'raises a NotImplementedError if the autH_key method is not defined' do
97
+ expect { subject.auth_key = 'broken' }.to raise_error(NotImplementedError)
97
98
  end
98
99
  end
99
100
 
100
101
  describe '#info' do
101
- it 'should include attributes that are set' do
102
- subject.stub!(:name).and_return('Bob Bobson')
103
- subject.stub!(:nickname).and_return('bob')
104
-
105
- subject.info.should == {
106
- 'name' => 'Bob Bobson',
107
- 'nickname' => 'bob'
108
- }
102
+ it 'includes attributes that are set' do
103
+ allow(subject).to receive(:name).and_return('Bob Bobson')
104
+ allow(subject).to receive(:nickname).and_return('bob')
105
+
106
+ expect(subject.info).to eq({
107
+ 'name' => 'Bob Bobson',
108
+ 'nickname' => 'bob'
109
+ })
109
110
  end
110
111
 
111
- it 'should automatically set name off of nickname' do
112
- subject.stub!(:nickname).and_return('bob')
112
+ it 'automaticallies set name off of nickname' do
113
+ allow(subject).to receive(:nickname).and_return('bob')
113
114
  subject.info['name'] == 'bob'
114
115
  end
115
116
 
116
- it 'should not overwrite a provided name' do
117
- subject.stub!(:name).and_return('Awesome Dude')
118
- subject.stub!(:first_name).and_return('Frank')
119
- subject.info['name'].should == 'Awesome Dude'
117
+ it 'does not overwrite a provided name' do
118
+ allow(subject).to receive(:name).and_return('Awesome Dude')
119
+ allow(subject).to receive(:first_name).and_return('Frank')
120
+ expect(subject.info['name']).to eq('Awesome Dude')
120
121
  end
121
122
  end
122
123
  end
@@ -1,14 +1,30 @@
1
- require 'spec_helper'
1
+ # frozen_string_literal: true
2
2
 
3
- describe(OmniAuth::Identity::Models::ActiveRecord, :db => true) do
4
- class TestIdentity < OmniAuth::Identity::Models::ActiveRecord; end
3
+ RSpec.describe(OmniAuth::Identity::Models::ActiveRecord, db: true) do
4
+ describe 'model', type: :model do
5
+ subject(:model_klass) do
6
+ AnonymousActiveRecord.generate(
7
+ parent_klass: 'OmniAuth::Identity::Models::ActiveRecord',
8
+ columns: OmniAuth::Identity::Model::SCHEMA_ATTRIBUTES | %w[provider password_digest],
9
+ connection_params: { adapter: 'sqlite3', encoding: 'utf8', database: ':memory:' }
10
+ ) do
11
+ def flower
12
+ '🌸'
13
+ end
14
+ end
15
+ end
5
16
 
6
- it 'should delegate locate to the where query method' do
7
- TestIdentity.should_receive(:where).with('ham_sandwich' => 'open faced', 'category' => 'sandwiches').and_return(['wakka'])
8
- TestIdentity.locate('ham_sandwich' => 'open faced', 'category' => 'sandwiches').should == 'wakka'
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')
21
+ end
9
22
  end
10
23
 
11
- it 'should not use STI rules for its table name' do
12
- TestIdentity.table_name.should == 'test_identities'
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')
28
+ end
13
29
  end
14
30
  end
@@ -1,15 +1,21 @@
1
- require 'spec_helper'
1
+ # frozen_string_literal: true
2
2
 
3
- describe(OmniAuth::Identity::Models::CouchPotatoModule, :db => true) do
4
- class CouchPotatoTestIdentity
5
- include CouchPotato::Persistence
6
- include OmniAuth::Identity::Models::CouchPotatoModule
7
- auth_key :ham_sandwich
8
- end
3
+ require 'couch_potato'
9
4
 
10
- it 'should delegate locate to the where query method' do
11
- CouchPotatoTestIdentity.should_receive(:where).with('ham_sandwich' => 'open faced', 'category' => 'sandwiches').and_return(['wakka'])
12
- CouchPotatoTestIdentity.locate('ham_sandwich' => 'open faced', 'category' => 'sandwiches').should == 'wakka'
13
- end
5
+ class CouchPotatoTestIdentity
6
+ include CouchPotato::Persistence
7
+ include OmniAuth::Identity::Models::CouchPotatoModule
8
+ auth_key :ham_sandwich
9
+ end
14
10
 
11
+ RSpec.describe(OmniAuth::Identity::Models::CouchPotatoModule, db: true) do
12
+ describe 'model', type: :model do
13
+ subject { CouchPotatoTestIdentity }
14
+
15
+ it 'delegates locate to the where query method' do
16
+ allow(subject).to receive(:where).with('ham_sandwich' => 'open faced',
17
+ 'category' => 'sandwiches').and_return(['wakka'])
18
+ expect(subject.locate('ham_sandwich' => 'open faced', 'category' => 'sandwiches')).to eq('wakka')
19
+ end
20
+ end
15
21
  end
@@ -1,18 +1,28 @@
1
- require 'spec_helper'
1
+ # frozen_string_literal: true
2
2
 
3
- describe(OmniAuth::Identity::Models::Mongoid, :db => true) do
4
- class MongoidTestIdentity
5
- include Mongoid::Document
6
- include OmniAuth::Identity::Models::Mongoid
7
- auth_key :ham_sandwich
8
- end
3
+ require 'mongoid'
9
4
 
10
- it 'should delegate locate to the where query method' do
11
- MongoidTestIdentity.should_receive(:where).with('ham_sandwich' => 'open faced', 'category' => 'sandwiches').and_return(['wakka'])
12
- MongoidTestIdentity.locate('ham_sandwich' => 'open faced', 'category' => 'sandwiches').should == 'wakka'
13
- end
5
+ class MongoidTestIdentity
6
+ include Mongoid::Document
7
+ include OmniAuth::Identity::Models::Mongoid
8
+ auth_key :ham_sandwich
9
+ store_in database: 'db1', collection: 'mongoid_test_identities', client: 'secondary'
10
+ end
11
+
12
+ RSpec.describe(OmniAuth::Identity::Models::Mongoid, db: true) do
13
+ describe 'model', type: :model do
14
+ subject { MongoidTestIdentity }
15
+
16
+ it { is_expected.to be_mongoid_document }
17
+
18
+ it 'does not munge collection name' do
19
+ expect(subject).to be_stored_in(database: 'db1', collection: 'mongoid_test_identities', client: 'secondary')
20
+ end
14
21
 
15
- it 'should not use STI rules for its collection name' do
16
- MongoidTestIdentity.collection.name.should == 'mongoid_test_identities'
22
+ it 'delegates locate to the where query method' do
23
+ allow(subject).to receive(:where).with('ham_sandwich' => 'open faced',
24
+ 'category' => 'sandwiches').and_return(['wakka'])
25
+ expect(subject.locate('ham_sandwich' => 'open faced', 'category' => 'sandwiches')).to eq('wakka')
26
+ end
17
27
  end
18
28
  end
@@ -0,0 +1,17 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'nobrainer'
4
+
5
+ class NobrainerTestIdentity
6
+ include NoBrainer::Document
7
+ include OmniAuth::Identity::Models::NoBrainer
8
+ auth_key :ham_sandwich
9
+ end
10
+
11
+ RSpec.describe(OmniAuth::Identity::Models::NoBrainer, db: true) do
12
+ it 'delegates locate to the where query method' do
13
+ allow(NobrainerTestIdentity).to receive(:where).with('ham_sandwich' => 'open faced',
14
+ 'category' => 'sandwiches').and_return(['wakka'])
15
+ expect(NobrainerTestIdentity.locate('ham_sandwich' => 'open faced', 'category' => 'sandwiches')).to eq('wakka')
16
+ end
17
+ end
@@ -1,4 +1,4 @@
1
- require 'spec_helper'
1
+ # frozen_string_literal: true
2
2
 
3
3
  class HasTheMethod
4
4
  def self.has_secure_password; end
@@ -7,21 +7,21 @@ end
7
7
  class DoesNotHaveTheMethod
8
8
  end
9
9
 
10
- describe OmniAuth::Identity::SecurePassword do
11
- it 'should extend with the class methods if it does not have the method' do
12
- DoesNotHaveTheMethod.should_receive(:extend).with(OmniAuth::Identity::SecurePassword::ClassMethods)
13
- DoesNotHaveTheMethod.send(:include, OmniAuth::Identity::SecurePassword)
10
+ RSpec.describe OmniAuth::Identity::SecurePassword do
11
+ it 'extends with the class methods if it does not have the method' do
12
+ expect(DoesNotHaveTheMethod).to receive(:extend).with(OmniAuth::Identity::SecurePassword::ClassMethods)
13
+ DoesNotHaveTheMethod.include described_class
14
14
  end
15
15
 
16
- it 'should not extend if the method is already defined' do
17
- HasTheMethod.should_not_receive(:extend)
18
- HasTheMethod.send(:include, OmniAuth::Identity::SecurePassword)
16
+ it 'does not extend if the method is already defined' do
17
+ expect(HasTheMethod).not_to receive(:extend)
18
+ HasTheMethod.include described_class
19
19
  end
20
20
 
21
- it 'should respond to has_secure_password afterwards' do
22
- [HasTheMethod,DoesNotHaveTheMethod].each do |klass|
23
- klass.send(:include, OmniAuth::Identity::SecurePassword)
24
- klass.should be_respond_to(:has_secure_password)
21
+ it 'responds to has_secure_password afterwards' do
22
+ [HasTheMethod, DoesNotHaveTheMethod].each do |klass|
23
+ klass.send(:include, described_class)
24
+ expect(klass).to be_respond_to(:has_secure_password)
25
25
  end
26
26
  end
27
27
  end
@@ -1,141 +1,252 @@
1
- require 'spec_helper'
1
+ # frozen_string_literal: true
2
2
 
3
- class MockIdentity; end
4
-
5
- describe OmniAuth::Strategies::Identity do
3
+ RSpec.describe OmniAuth::Strategies::Identity do
6
4
  attr_accessor :app
7
5
 
8
- let(:auth_hash){ last_response.headers['env']['omniauth.auth'] }
9
- let(:identity_hash){ last_response.headers['env']['omniauth.identity'] }
6
+ let(:auth_hash) { last_response.headers['env']['omniauth.auth'] }
7
+ let(:identity_hash) { last_response.headers['env']['omniauth.identity'] }
8
+ let(:identity_options) { {} }
9
+ let(:anon_ar) do
10
+ AnonymousActiveRecord.generate(
11
+ parent_klass: 'OmniAuth::Identity::Models::ActiveRecord',
12
+ columns: OmniAuth::Identity::Model::SCHEMA_ATTRIBUTES | %w[provider password_digest],
13
+ connection_params: { adapter: 'sqlite3', encoding: 'utf8', database: ':memory:' }
14
+ ) do
15
+ def balloon
16
+ '🎈'
17
+ end
18
+ end
19
+ end
10
20
 
11
21
  # customize rack app for testing, if block is given, reverts to default
12
22
  # rack app after testing is done
13
23
  def set_app!(identity_options = {})
14
- identity_options = {:model => MockIdentity}.merge(identity_options)
15
- old_app = self.app
24
+ old_app = app
16
25
  self.app = Rack::Builder.app do
17
- use Rack::Session::Cookie
26
+ use Rack::Session::Cookie, secret: '1234567890qwertyuiop'
18
27
  use OmniAuth::Strategies::Identity, identity_options
19
- run lambda{|env| [404, {'env' => env}, ["HELLO!"]]}
28
+ run ->(env) { [404, { 'env' => env }, ['HELLO!']] }
20
29
  end
21
30
  if block_given?
22
31
  yield
23
32
  self.app = old_app
24
33
  end
25
- self.app
34
+ app
26
35
  end
27
36
 
28
- before(:all) do
29
- set_app!
37
+ before do
38
+ opts = identity_options.reverse_merge({ model: anon_ar })
39
+ set_app!(opts)
30
40
  end
31
41
 
32
42
  describe '#request_phase' do
33
- it 'should display a form' do
34
- get '/auth/identity'
35
- last_response.body.should be_include("<form")
43
+ context 'with default settings' do
44
+ let(:identity_options) { { model: anon_ar } }
45
+
46
+ it 'displays a form' do
47
+ get '/auth/identity'
48
+
49
+ expect(last_response.body).not_to eq('HELLO!')
50
+ expect(last_response.body).to be_include('<form')
51
+ end
52
+ end
53
+
54
+ context 'when login is enabled' do
55
+ context 'when registration is enabled' do
56
+ let(:identity_options) { { model: anon_ar, enable_registration: true, enable_login: true } }
57
+
58
+ it 'displays a form with a link to register' do
59
+ get '/auth/identity'
60
+
61
+ expect(last_response.body).not_to eq('HELLO!')
62
+ expect(last_response.body).to be_include('<form')
63
+ expect(last_response.body).to be_include('<a')
64
+ expect(last_response.body).to be_include('Create an Identity')
65
+ end
66
+ end
67
+
68
+ context 'when registration is disabled' do
69
+ let(:identity_options) { { model: anon_ar, enable_registration: false, enable_login: true } }
70
+
71
+ it 'displays a form without a link to register' do
72
+ get '/auth/identity'
73
+
74
+ expect(last_response.body).not_to eq('HELLO!')
75
+ expect(last_response.body).to be_include('<form')
76
+ expect(last_response.body).not_to be_include('<a')
77
+ expect(last_response.body).not_to be_include('Create an Identity')
78
+ end
79
+ end
80
+ end
81
+
82
+ context 'when login is disabled' do
83
+ context 'when registration is enabled' do
84
+ let(:identity_options) { { model: anon_ar, enable_registration: true, enable_login: false } }
85
+
86
+ it 'bypasses registration form' do
87
+ get '/auth/identity'
88
+
89
+ expect(last_response.body).to eq('HELLO!')
90
+ expect(last_response.body).not_to be_include('<form')
91
+ expect(last_response.body).not_to be_include('<a')
92
+ expect(last_response.body).not_to be_include('Create an Identity')
93
+ end
94
+ end
95
+
96
+ context 'when registration is disabled' do
97
+ let(:identity_options) { { model: anon_ar, enable_registration: false, enable_login: false } }
98
+
99
+ it 'displays a form without a link to register' do
100
+ get '/auth/identity'
101
+
102
+ expect(last_response.body).to eq('HELLO!')
103
+ expect(last_response.body).not_to be_include('<form')
104
+ expect(last_response.body).not_to be_include('<a')
105
+ expect(last_response.body).not_to be_include('Create an Identity')
106
+ end
107
+ end
36
108
  end
37
109
  end
38
110
 
39
111
  describe '#callback_phase' do
40
- let(:user){ mock(:uid => 'user1', :info => {'name' => 'Rockefeller'})}
112
+ let(:user) { double(uid: 'user1', info: { 'name' => 'Rockefeller' }) }
41
113
 
42
114
  context 'with valid credentials' do
43
115
  before do
44
- MockIdentity.stub('auth_key').and_return('email')
45
- MockIdentity.should_receive('authenticate').with({'email' => 'john'},'awesome').and_return(user)
46
- post '/auth/identity/callback', :auth_key => 'john', :password => 'awesome'
116
+ allow(anon_ar).to receive('auth_key').and_return('email')
117
+ expect(anon_ar).to receive('authenticate').with({ 'email' => 'john' }, 'awesome').and_return(user)
118
+ post '/auth/identity/callback', auth_key: 'john', password: 'awesome'
47
119
  end
48
120
 
49
- it 'should populate the auth hash' do
50
- auth_hash.should be_kind_of(Hash)
121
+ it 'populates the auth hash' do
122
+ expect(auth_hash).to be_kind_of(Hash)
51
123
  end
52
124
 
53
- it 'should populate the uid' do
54
- auth_hash['uid'].should == 'user1'
125
+ it 'populates the uid' do
126
+ expect(auth_hash['uid']).to eq('user1')
55
127
  end
56
128
 
57
- it 'should populate the info hash' do
58
- auth_hash['info'].should == {'name' => 'Rockefeller'}
129
+ it 'populates the info hash' do
130
+ expect(auth_hash['info']).to eq({ 'name' => 'Rockefeller' })
59
131
  end
60
132
  end
61
133
 
62
134
  context 'with invalid credentials' do
63
135
  before do
64
- MockIdentity.stub('auth_key').and_return('email')
65
- OmniAuth.config.on_failure = lambda{|env| [401, {}, [env['omniauth.error.type'].inspect]]}
66
- MockIdentity.should_receive(:authenticate).with({'email' => 'wrong'},'login').and_return(false)
67
- post '/auth/identity/callback', :auth_key => 'wrong', :password => 'login'
136
+ allow(anon_ar).to receive('auth_key').and_return('email')
137
+ OmniAuth.config.on_failure = ->(env) { [401, {}, [env['omniauth.error.type'].inspect]] }
138
+ expect(anon_ar).to receive(:authenticate).with({ 'email' => 'wrong' }, 'login').and_return(false)
139
+ post '/auth/identity/callback', auth_key: 'wrong', password: 'login'
68
140
  end
69
141
 
70
- it 'should fail with :invalid_credentials' do
71
- last_response.body.should == ':invalid_credentials'
142
+ it 'fails with :invalid_credentials' do
143
+ expect(last_response.body).to eq(':invalid_credentials')
72
144
  end
73
145
  end
74
146
 
75
147
  context 'with auth scopes' do
148
+ let(:identity_options) do
149
+ { model: anon_ar, locate_conditions: lambda { |req|
150
+ { model.auth_key => req['auth_key'], 'user_type' => 'admin' }
151
+ } }
152
+ end
76
153
 
77
- it 'should evaluate and pass through conditions proc' do
78
- MockIdentity.stub('auth_key').and_return('email')
79
- set_app!( :locate_conditions => lambda{|req| {model.auth_key => req['auth_key'], 'user_type' => 'admin'} } )
80
- MockIdentity.should_receive('authenticate').with( {'email' => 'john', 'user_type' => 'admin'}, 'awesome' ).and_return(user)
81
- post '/auth/identity/callback', :auth_key => 'john', :password => 'awesome'
154
+ it 'evaluates and pass through conditions proc' do
155
+ allow(anon_ar).to receive('auth_key').and_return('email')
156
+ expect(anon_ar).to receive('authenticate').with({ 'email' => 'john', 'user_type' => 'admin' },
157
+ 'awesome').and_return(user)
158
+ post '/auth/identity/callback', auth_key: 'john', password: 'awesome'
82
159
  end
83
160
  end
84
161
  end
85
162
 
86
163
  describe '#registration_form' do
87
- it 'should trigger from /auth/identity/register by default' do
88
- get '/auth/identity/register'
89
- last_response.body.should be_include("Register Identity")
164
+ context 'registration is enabled' do
165
+ it 'triggers from /auth/identity/register by default' do
166
+ get '/auth/identity/register'
167
+ expect(last_response.body).to be_include('Register Identity')
168
+ end
169
+ end
170
+
171
+ context 'registration is disabled' do
172
+ let(:identity_options) { { model: anon_ar, enable_registration: false } }
173
+
174
+ it 'calls app' do
175
+ get '/auth/identity/register'
176
+ expect(last_response.body).to be_include('HELLO!')
177
+ end
178
+ end
179
+
180
+ it 'supports methods other than GET and POST' do
181
+ head '/auth/identity/register'
182
+ expect(last_response.status).to eq(404)
90
183
  end
91
184
  end
92
185
 
93
186
  describe '#registration_phase' do
94
- context 'with successful creation' do
95
- let(:properties){ {
96
- :name => 'Awesome Dude',
97
- :email => 'awesome@example.com',
98
- :password => 'face',
99
- :password_confirmation => 'face'
100
- } }
187
+ context 'registration is disabled' do
188
+ let(:identity_options) { { model: anon_ar, enable_registration: false } }
101
189
 
102
- before do
103
- MockIdentity.stub('auth_key').and_return('email')
104
- m = mock(:uid => 'abc', :name => 'Awesome Dude', :email => 'awesome@example.com', :info => {:name => 'DUUUUDE!'}, :persisted? => true)
105
- MockIdentity.should_receive(:create).with(properties).and_return(m)
190
+ it 'calls app' do
191
+ post '/auth/identity/register'
192
+ expect(last_response.body).to eq('HELLO!')
106
193
  end
194
+ end
107
195
 
108
- it 'should set the auth hash' do
196
+ context 'with successful creation' do
197
+ let(:properties) do
198
+ {
199
+ name: 'Awesome Dude',
200
+ email: 'awesome@example.com',
201
+ password: 'face',
202
+ password_confirmation: 'face',
203
+ provider: 'identity'
204
+ }
205
+ end
206
+
207
+ it 'sets the auth hash' do
109
208
  post '/auth/identity/register', properties
110
- auth_hash['uid'].should == 'abc'
209
+ expect(auth_hash['uid']).to match(/\d+/)
210
+ expect(auth_hash['provider']).to eq('identity')
111
211
  end
112
212
  end
113
213
 
114
214
  context 'with invalid identity' do
115
- let(:properties) { {
116
- :name => 'Awesome Dude',
117
- :email => 'awesome@example.com',
118
- :password => 'NOT',
119
- :password_confirmation => 'MATCHING'
120
- } }
215
+ let(:properties) do
216
+ {
217
+ name: 'Awesome Dude',
218
+ email: 'awesome@example.com',
219
+ password: 'NOT',
220
+ password_confirmation: 'MATCHING',
221
+ provider: 'identity'
222
+ }
223
+ end
224
+ let(:invalid_identity) { double(persisted?: false, save: false) }
121
225
 
122
226
  before do
123
- MockIdentity.should_receive(:create).with(properties).and_return(mock(:persisted? => false))
227
+ expect(anon_ar).to receive(:new).with(properties).and_return(invalid_identity)
124
228
  end
125
229
 
126
230
  context 'default' do
127
- it 'should show registration form' do
231
+ it 'shows registration form' do
128
232
  post '/auth/identity/register', properties
129
- last_response.body.should be_include("Register Identity")
233
+ expect(last_response.body).to be_include('Register Identity')
234
+ expect(last_response.body).to be_include('One or more fields were invalid')
130
235
  end
131
236
  end
132
237
 
133
238
  context 'custom on_failed_registration endpoint' do
134
- it 'should set the identity hash' do
135
- set_app!(:on_failed_registration => lambda{|env| [404, {'env' => env}, ["HELLO!"]]}) do
136
- post '/auth/identity/register', properties
137
- identity_hash.should_not be_nil
138
- end
239
+ let(:identity_options) do
240
+ { model: anon_ar, on_failed_registration: lambda { |env|
241
+ [404, { 'env' => env }, ["FAIL'DOH!"]]
242
+ } }
243
+ end
244
+
245
+ it 'sets the identity hash' do
246
+ post '/auth/identity/register', properties
247
+ expect(identity_hash).to eq(invalid_identity)
248
+ expect(last_response.body).to be_include("FAIL'DOH!")
249
+ expect(last_response.body).not_to be_include('One or more fields were invalid')
139
250
  end
140
251
  end
141
252
  end