2019-10-28 20:06:10 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2016-08-12 17:16:12 -04:00
|
|
|
require 'spec_helper'
|
|
|
|
|
2017-04-25 08:28:55 -04:00
|
|
|
describe 'OAuth tokens' do
|
2016-08-12 17:16:12 -04:00
|
|
|
context 'Resource Owner Password Credentials' do
|
|
|
|
def request_oauth_token(user)
|
2018-12-17 17:52:17 -05:00
|
|
|
post '/oauth/token', params: { username: user.username, password: user.password, grant_type: 'password' }
|
2016-08-12 17:16:12 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
context 'when user has 2FA enabled' do
|
|
|
|
it 'does not create an access token' do
|
|
|
|
user = create(:user, :two_factor)
|
2016-08-17 18:39:20 -04:00
|
|
|
|
2016-08-12 17:16:12 -04:00
|
|
|
request_oauth_token(user)
|
|
|
|
|
2020-02-26 10:08:56 -05:00
|
|
|
expect(response).to have_gitlab_http_status(:unauthorized)
|
2016-08-12 17:16:12 -04:00
|
|
|
expect(json_response['error']).to eq('invalid_grant')
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when user does not have 2FA enabled' do
|
|
|
|
it 'creates an access token' do
|
|
|
|
user = create(:user)
|
2016-08-17 18:39:20 -04:00
|
|
|
|
2016-08-12 17:16:12 -04:00
|
|
|
request_oauth_token(user)
|
|
|
|
|
2020-02-26 10:08:56 -05:00
|
|
|
expect(response).to have_gitlab_http_status(:ok)
|
2016-08-12 17:16:12 -04:00
|
|
|
expect(json_response['access_token']).not_to be_nil
|
|
|
|
end
|
|
|
|
end
|
2017-01-18 05:23:25 -05:00
|
|
|
|
2020-01-30 16:08:47 -05:00
|
|
|
shared_examples 'does not create an access token' do
|
|
|
|
let(:user) { create(:user) }
|
|
|
|
|
2020-02-26 10:08:56 -05:00
|
|
|
it { expect(response).to have_gitlab_http_status(:unauthorized) }
|
2020-01-30 16:08:47 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
context 'when user is blocked' do
|
|
|
|
before do
|
2017-01-18 05:23:25 -05:00
|
|
|
user.block
|
|
|
|
|
|
|
|
request_oauth_token(user)
|
|
|
|
end
|
2020-01-30 16:08:47 -05:00
|
|
|
|
|
|
|
include_examples 'does not create an access token'
|
2017-01-18 05:23:25 -05:00
|
|
|
end
|
|
|
|
|
2020-01-30 16:08:47 -05:00
|
|
|
context 'when user is ldap_blocked' do
|
|
|
|
before do
|
2017-01-18 05:23:25 -05:00
|
|
|
user.ldap_block
|
|
|
|
|
|
|
|
request_oauth_token(user)
|
2020-01-30 16:08:47 -05:00
|
|
|
end
|
2017-01-18 05:23:25 -05:00
|
|
|
|
2020-01-30 16:08:47 -05:00
|
|
|
include_examples 'does not create an access token'
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when user account is not confirmed' do
|
|
|
|
before do
|
|
|
|
user.update!(confirmed_at: nil)
|
|
|
|
|
|
|
|
request_oauth_token(user)
|
2017-01-18 05:23:25 -05:00
|
|
|
end
|
2020-01-30 16:08:47 -05:00
|
|
|
|
|
|
|
include_examples 'does not create an access token'
|
2017-01-18 05:23:25 -05:00
|
|
|
end
|
2016-08-12 17:16:12 -04:00
|
|
|
end
|
2016-08-15 18:14:22 -04:00
|
|
|
end
|