debian-mirror-gitlab/spec/requests/oauth_tokens_spec.rb

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

110 lines
3.4 KiB
Ruby
Raw Normal View History

2019-12-26 22:10:19 +05:30
# frozen_string_literal: true
2018-11-08 19:23:39 +05:30
require 'spec_helper'
2020-07-28 23:09:34 +05:30
RSpec.describe 'OAuth Tokens requests' do
2018-11-08 19:23:39 +05:30
let(:user) { create :user }
let(:application) { create :oauth_application, scopes: 'api' }
2022-08-13 15:12:31 +05:30
let(:grant_type) { 'authorization_code' }
let(:refresh_token) { nil }
2018-11-08 19:23:39 +05:30
def request_access_token(user)
post '/oauth/token',
2019-02-15 15:39:39 +05:30
params: {
2022-08-13 15:12:31 +05:30
grant_type: grant_type,
2019-02-15 15:39:39 +05:30
code: generate_access_grant(user).token,
redirect_uri: application.redirect_uri,
client_id: application.uid,
2022-08-13 15:12:31 +05:30
client_secret: application.secret,
refresh_token: refresh_token
2019-02-15 15:39:39 +05:30
}
2018-11-08 19:23:39 +05:30
end
def generate_access_grant(user)
2022-08-13 15:12:31 +05:30
create(:oauth_access_grant, application: application, resource_owner_id: user.id)
2018-11-08 19:23:39 +05:30
end
context 'when there is already a token for the application' do
2022-08-13 15:12:31 +05:30
let!(:existing_token) { create(:oauth_access_token, application: application, resource_owner_id: user.id) }
2018-11-08 19:23:39 +05:30
2022-08-13 15:12:31 +05:30
shared_examples 'issues a new token' do
it 'issues a new token' do
2018-11-08 19:23:39 +05:30
expect do
request_access_token(user)
2022-08-13 15:12:31 +05:30
end.to change { Doorkeeper::AccessToken.count }.from(1).to(2)
expect(json_response['access_token']).not_to eq existing_token.token
expect(json_response['refresh_token']).not_to eq existing_token.refresh_token
end
end
2018-11-08 19:23:39 +05:30
2022-08-13 15:12:31 +05:30
shared_examples 'revokes previous token' do
it 'revokes previous token' do
expect { request_access_token(user) }.to(
change { existing_token.reload.revoked_at }.from(nil))
2018-11-08 19:23:39 +05:30
end
end
2022-08-13 15:12:31 +05:30
context 'and the request is done by the resource owner' do
context 'with authorization code grant type' do
include_examples 'issues a new token'
2018-11-08 19:23:39 +05:30
2022-08-13 15:12:31 +05:30
it 'does not revoke previous token' do
request_access_token(user)
expect(existing_token.reload.revoked_at).to be_nil
end
end
context 'with refresh token grant type' do
let(:grant_type) { 'refresh_token' }
let(:refresh_token) { existing_token.refresh_token }
include_examples 'issues a new token'
include_examples 'revokes previous token'
context 'expired refresh token' do
let!(:existing_token) do
create(:oauth_access_token, application: application,
resource_owner_id: user.id,
created_at: 10.minutes.ago,
expires_in: 5)
end
include_examples 'issues a new token'
include_examples 'revokes previous token'
end
context 'revoked refresh token' do
let!(:existing_token) do
2022-10-11 01:57:18 +05:30
create(:oauth_access_token,
application: application,
resource_owner_id: user.id,
created_at: 2.hours.ago,
revoked_at: 1.hour.ago,
expires_in: 5)
2022-08-13 15:12:31 +05:30
end
it 'does not issue a new token' do
request_access_token(user)
2018-11-08 19:23:39 +05:30
2022-08-13 15:12:31 +05:30
expect(json_response['error']).to eq('invalid_grant')
end
end
2018-11-08 19:23:39 +05:30
end
end
end
context 'when there is no token stored for the application' do
it 'generates and returns a new token' do
expect do
request_access_token(user)
end.to change { Doorkeeper::AccessToken.count }.by(1)
expect(json_response['access_token']).not_to be_nil
2022-07-16 23:28:13 +05:30
expect(json_response['expires_in']).not_to be_nil
2021-11-11 11:23:49 +05:30
end
2018-11-08 19:23:39 +05:30
end
end