Commit 15205430 authored by Shinya Maeda's avatar Shinya Maeda

Merge branch '220388-project-access-tokens-bot-not-deleted-when-token-expires' into 'master'

Remove project bot user membership when project access token expires

See merge request gitlab-org/gitlab!43605
parents 58156648 7392bc7f
...@@ -94,7 +94,7 @@ module ResourceAccessTokens ...@@ -94,7 +94,7 @@ module ResourceAccessTokens
end end
def provision_access(resource, user) def provision_access(resource, user)
resource.add_maintainer(user) resource.add_user(user, :maintainer, expires_at: params[:expires_at])
end end
def error(message) def error(message)
......
---
title: Remove project bot user membership when project access token expires
merge_request: 43605
author:
type: fixed
...@@ -24,6 +24,7 @@ RSpec.describe ResourceAccessTokens::CreateService do ...@@ -24,6 +24,7 @@ RSpec.describe ResourceAccessTokens::CreateService do
end end
end end
# Remove this shared example when https://gitlab.com/gitlab-org/gitlab/-/merge_requests/43190 merges
shared_examples 'fails on gitlab.com' do shared_examples 'fails on gitlab.com' do
before do before do
allow(Gitlab).to receive(:com?) { true } allow(Gitlab).to receive(:com?) { true }
...@@ -68,8 +69,8 @@ RSpec.describe ResourceAccessTokens::CreateService do ...@@ -68,8 +69,8 @@ RSpec.describe ResourceAccessTokens::CreateService do
end end
context 'bot name' do context 'bot name' do
context 'when no value is passed' do context 'when no name is passed' do
it 'uses default value' do it 'uses default name' do
response = subject response = subject
access_token = response.payload[:access_token] access_token = response.payload[:access_token]
...@@ -77,10 +78,10 @@ RSpec.describe ResourceAccessTokens::CreateService do ...@@ -77,10 +78,10 @@ RSpec.describe ResourceAccessTokens::CreateService do
end end
end end
context 'when user provides value' do context 'when user provides name' do
let_it_be(:params) { { name: 'Random bot' } } let_it_be(:params) { { name: 'Random bot' } }
it 'overrides the default value' do it 'overrides the default name value' do
response = subject response = subject
access_token = response.payload[:access_token] access_token = response.payload[:access_token]
...@@ -112,7 +113,7 @@ RSpec.describe ResourceAccessTokens::CreateService do ...@@ -112,7 +113,7 @@ RSpec.describe ResourceAccessTokens::CreateService do
context 'when user provides scope explicitly' do context 'when user provides scope explicitly' do
let_it_be(:params) { { scopes: Gitlab::Auth::REPOSITORY_SCOPES } } let_it_be(:params) { { scopes: Gitlab::Auth::REPOSITORY_SCOPES } }
it 'overrides the default value' do it 'overrides the default scope value' do
response = subject response = subject
access_token = response.payload[:access_token] access_token = response.payload[:access_token]
...@@ -121,24 +122,44 @@ RSpec.describe ResourceAccessTokens::CreateService do ...@@ -121,24 +122,44 @@ RSpec.describe ResourceAccessTokens::CreateService do
end end
context 'expires_at' do context 'expires_at' do
context 'when no value is passed' do context 'when no expiration value is passed' do
it 'uses default value' do it 'uses nil expiration value' do
response = subject response = subject
access_token = response.payload[:access_token] access_token = response.payload[:access_token]
expect(access_token.expires_at).to eq(nil) expect(access_token.expires_at).to eq(nil)
end end
context 'expiry of the project bot member' do
it 'project bot membership does not expire' do
response = subject
access_token = response.payload[:access_token]
project_bot = access_token.user
expect(project.members.find_by(user_id: project_bot.id).expires_at).to eq(nil)
end
end
end end
context 'when user provides value' do context 'when user provides expiration value' do
let_it_be(:params) { { expires_at: Date.today + 1.month } } let_it_be(:params) { { expires_at: Date.today + 1.month } }
it 'overrides the default value' do it 'overrides the default expiration value' do
response = subject response = subject
access_token = response.payload[:access_token] access_token = response.payload[:access_token]
expect(access_token.expires_at).to eq(params[:expires_at]) expect(access_token.expires_at).to eq(params[:expires_at])
end end
context 'expiry of the project bot member' do
it 'sets the project bot to expire on the same day as the token' do
response = subject
access_token = response.payload[:access_token]
project_bot = access_token.user
expect(project.members.find_by(user_id: project_bot.id).expires_at).to eq(params[:expires_at])
end
end
end end
context 'when invalid scope is passed' do context 'when invalid scope is passed' do
...@@ -155,7 +176,7 @@ RSpec.describe ResourceAccessTokens::CreateService do ...@@ -155,7 +176,7 @@ RSpec.describe ResourceAccessTokens::CreateService do
context 'when access provisioning fails' do context 'when access provisioning fails' do
before do before do
allow(resource).to receive(:add_maintainer).and_return(nil) allow(resource).to receive(:add_user).and_return(nil)
end end
it 'returns error' do it 'returns error' do
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment