Skip to content

armgit / spree

Public Imported from GitHub
0

Open Source Platform for DTC, B2B Commerce, Marketplaces & Omnichannel. REST APIs, TypeScript SDKs, and production-ready Next.js storefront. Self-host it. Own your data. No vendor lock-in. Zero platform fees.

Fixes https://github.com/spree/spree/issues/10816

Damian Legawiec312b6a2c848a
diff --git a/api/config/initializers/doorkeeper.rb b/api/config/initializers/doorkeeper.rb
index 63d537c800..a8a0acbbf6 100644
--- a/api/config/initializers/doorkeeper.rb
+++ b/api/config/initializers/doorkeeper.rb
@@ -10,7 +10,7 @@ Doorkeeper.configure do
resource_owner_from_credentials do
user = Spree.user_class.find_for_database_authentication(email: params[:username])
- return nil if user.nil?
+ next if user.nil?
if defined?(Spree::Auth::Config) && Spree::Auth::Config[:confirmable] == true
user if user.active_for_authentication? && user.valid_for_authentication? { user.valid_password?(params[:password]) }
diff --git a/api/spec/requests/spree/oauth/token_spec.rb b/api/spec/requests/spree/oauth/token_spec.rb
index 537a034279..62f2da8c03 100644
--- a/api/spec/requests/spree/oauth/token_spec.rb
+++ b/api/spec/requests/spree/oauth/token_spec.rb
@@ -4,46 +4,61 @@ describe 'Spree OAuth', type: :request do
let!(:user) { create(:user, email: '[email protected]', password: 'secret', password_confirmation: 'secret') }
describe 'get token' do
+ shared_examples 'does not return a token' do
+ it do
+ expect(response.status).to eq(400)
+ expect(json_response[:error]).to eq('invalid_grant')
+ end
+ end
+
context 'by password' do
- before do
- module Spree
- module Auth
- class Config
- def self.[](key)
- {
- confirmable: true
- }[key]
- end
+ context 'with confirmation' do
+ before do
+ module Spree
+ module Auth
+ class Config
+ def self.[](key)
+ {
+ confirmable: true
+ }[key]
+ end
- def self.[]=(key)
+ def self.[]=(key)
+ end
end
end
end
+
+ allow(Spree.user_class).to receive(:find_for_database_authentication).with(hash_including(:email)) { user }
+ allow(user).to receive(:valid_for_authentication?).and_return(true)
+ allow(user).to receive(:active_for_authentication?).and_return(active_value)
+ post '/spree_oauth/token?grant_type=password&[email protected]&password=secret'
end
- allow(Spree.user_class).to receive(:find_for_database_authentication).with(hash_including(:email)) { user }
- allow(user).to receive(:valid_for_authentication?).and_return(true)
- allow(user).to receive(:active_for_authentication?).and_return(active_value)
- post '/spree_oauth/token?grant_type=password&[email protected]&password=secret'
- end
+ context 'when the user is confirmed' do
+ let(:active_value) { true }
- context 'when the user is confirmed' do
- let(:active_value) { true }
+ it 'returns new token' do
+ expect(response.status).to eq(200)
+ expect(json_response).to have_attributes([:access_token, :token_type, :expires_in, :refresh_token, :created_at])
+ expect(json_response['token_type']).to eq('Bearer')
+ end
+ end
+
+ context 'when the user is not confirmed' do
+ let(:active_value) { false }
- it 'returns new token' do
- expect(response.status).to eq(200)
- expect(json_response).to have_attributes([:access_token, :token_type, :expires_in, :refresh_token, :created_at])
- expect(json_response['token_type']).to eq('Bearer')
+ it_behaves_like 'does not return a token'
end
end
- context 'when the user is not confirmed' do
- let(:active_value) { false }
-
- it 'does not return new token' do
- expect(response.status).to eq(400)
- expect(json_response[:error]).to eq("invalid_grant")
+ context 'user does not exist' do
+ before do
+ allow(Spree.user_class).to receive(:find_for_database_authentication).with(hash_including(:email)) { nil }
+ post '/spree_oauth/token?grant_type=password&[email protected]&password=secret'
end
+
+ it_behaves_like 'does not return a token'
end
end
end