2019-10-28 20:06:10 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2016-08-12 17:16:12 -04:00
|
|
|
require 'spec_helper'
|
|
|
|
|
2020-06-24 02:09:01 -04:00
|
|
|
RSpec.describe 'OAuth tokens' do
|
2020-06-16 11:08:32 -04:00
|
|
|
include HttpBasicAuthHelpers
|
2020-05-28 17:08:22 -04:00
|
|
|
|
2020-06-16 11:08:32 -04:00
|
|
|
context 'Resource Owner Password Credentials' do
|
2020-05-28 17:08:22 -04:00
|
|
|
def request_oauth_token(user, headers = {})
|
|
|
|
post '/oauth/token',
|
|
|
|
params: { username: user.username, password: user.password, grant_type: 'password' },
|
|
|
|
headers: headers
|
|
|
|
end
|
|
|
|
|
|
|
|
let_it_be(:client) { create(:oauth_application) }
|
|
|
|
|
2016-08-12 17:16:12 -04:00
|
|
|
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
|
|
|
|
2020-05-28 17:08:22 -04:00
|
|
|
request_oauth_token(user, client_basic_auth_header(client))
|
2016-08-12 17:16:12 -04:00
|
|
|
|
2020-08-31 23:10:22 -04:00
|
|
|
expect(response).to have_gitlab_http_status(:bad_request)
|
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
|
2020-05-28 17:08:22 -04:00
|
|
|
# NOTE: using ROPS grant flow without client credentials will be deprecated
|
|
|
|
# and removed in the next version of Doorkeeper.
|
|
|
|
# See https://gitlab.com/gitlab-org/gitlab/-/issues/219137
|
|
|
|
context 'when no client credentials provided' do
|
|
|
|
it 'creates an access token' do
|
|
|
|
user = create(:user)
|
|
|
|
|
|
|
|
request_oauth_token(user)
|
|
|
|
|
|
|
|
expect(response).to have_gitlab_http_status(:ok)
|
|
|
|
expect(json_response['access_token']).not_to be_nil
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when client credentials provided' do
|
|
|
|
context 'with valid credentials' do
|
|
|
|
it 'creates an access token' do
|
|
|
|
user = create(:user)
|
|
|
|
|
|
|
|
request_oauth_token(user, client_basic_auth_header(client))
|
|
|
|
|
|
|
|
expect(response).to have_gitlab_http_status(:ok)
|
|
|
|
expect(json_response['access_token']).not_to be_nil
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'with invalid credentials' do
|
|
|
|
it 'does not create an access token' do
|
|
|
|
# NOTE: remove this after update to Doorkeeper 5.5 or newer, see
|
|
|
|
# https://gitlab.com/gitlab-org/gitlab/-/issues/219137
|
|
|
|
pending 'Enable this example after upgrading Doorkeeper to 5.5 or newer'
|
|
|
|
|
|
|
|
user = create(:user)
|
2016-08-17 18:39:20 -04:00
|
|
|
|
2020-05-28 17:08:22 -04:00
|
|
|
request_oauth_token(user, basic_auth_header(client.uid, 'invalid secret'))
|
2016-08-12 17:16:12 -04:00
|
|
|
|
2020-08-31 23:10:22 -04:00
|
|
|
expect(response).to have_gitlab_http_status(:bad_request)
|
2020-05-28 17:08:22 -04:00
|
|
|
expect(json_response['error']).to eq('invalid_client')
|
|
|
|
end
|
|
|
|
end
|
2016-08-12 17:16:12 -04:00
|
|
|
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-08-31 23:10:22 -04:00
|
|
|
it { expect(response).to have_gitlab_http_status(:bad_request) }
|
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
|
|
|
|
|
2020-05-28 17:08:22 -04:00
|
|
|
request_oauth_token(user, client_basic_auth_header(client))
|
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
|
|
|
|
|
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
|
|
|
|
|
2020-05-28 17:08:22 -04:00
|
|
|
request_oauth_token(user, client_basic_auth_header(client))
|
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)
|
|
|
|
|
2020-05-28 17:08:22 -04:00
|
|
|
request_oauth_token(user, client_basic_auth_header(client))
|
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
|