blob: 8c108d9937ab47d901f4b4916626b9a77f189bf0 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
|
# frozen_string_literal: true
require 'spec_helper'
describe Resources::CreateAccessTokenService do
subject { described_class.new(resource_type, resource, user, params).execute }
let_it_be(:user) { create(:user) }
let_it_be(:project) { create(:project, :private) }
let_it_be(:params) { {} }
describe '#execute' do
# Created shared_examples as it will easy to include specs for group bots in https://gitlab.com/gitlab-org/gitlab/-/issues/214046
shared_examples 'fails when user does not have the permission to create a Resource Bot' do
before do
resource.add_developer(user)
end
it 'returns error' do
response = subject
expect(response.error?).to be true
expect(response.message).to eq("User does not have permission to create #{resource_type} Access Token")
end
end
shared_examples 'fails when flag is disabled' do
before do
stub_feature_flags(resource_access_token: false)
end
it 'returns nil' do
expect(subject).to be nil
end
end
shared_examples 'allows creation of bot with valid params' do
it { expect { subject }.to change { User.count }.by(1) }
it 'creates resource bot user' do
response = subject
access_token = response.payload[:access_token]
expect(access_token.user.reload.user_type).to eq("#{resource_type}_bot")
end
context 'bot name' do
context 'when no value is passed' do
it 'uses default value' do
response = subject
access_token = response.payload[:access_token]
expect(access_token.user.name).to eq("#{resource.name.to_s.humanize} bot")
end
end
context 'when user provides value' do
let(:params) { { name: 'Random bot' } }
it 'overrides the default value' do
response = subject
access_token = response.payload[:access_token]
expect(access_token.user.name).to eq(params[:name])
end
end
end
it 'adds the bot user as a maintainer in the resource' do
response = subject
access_token = response.payload[:access_token]
bot_user = access_token.user
expect(resource.members.maintainers.map(&:user_id)).to include(bot_user.id)
end
context 'personal access token' do
it { expect { subject }.to change { PersonalAccessToken.count }.by(1) }
context 'when user does not provide scope' do
it 'has default scopes' do
response = subject
access_token = response.payload[:access_token]
expect(access_token.scopes).to eq(Gitlab::Auth::API_SCOPES + Gitlab::Auth::REPOSITORY_SCOPES + Gitlab::Auth.registry_scopes - [:read_user])
end
end
context 'when user provides scope explicitly' do
let(:params) { { scopes: Gitlab::Auth::REPOSITORY_SCOPES } }
it 'overrides the default value' do
response = subject
access_token = response.payload[:access_token]
expect(access_token.scopes).to eq(Gitlab::Auth::REPOSITORY_SCOPES)
end
end
context 'expires_at' do
context 'when no value is passed' do
it 'uses default value' do
response = subject
access_token = response.payload[:access_token]
expect(access_token.expires_at).to eq(nil)
end
end
context 'when user provides value' do
let(:params) { { expires_at: Date.today + 1.month } }
it 'overrides the default value' do
response = subject
access_token = response.payload[:access_token]
expect(access_token.expires_at).to eq(params[:expires_at])
end
end
context 'when invalid scope is passed' do
let(:params) { { scopes: [:invalid_scope] } }
it 'returns error' do
response = subject
expect(response.error?).to be true
end
end
end
end
context 'when access provisioning fails' do
before do
allow(resource).to receive(:add_maintainer).and_return(nil)
end
it 'returns error' do
response = subject
expect(response.error?).to be true
end
end
end
context 'when resource is a project' do
let(:resource_type) { 'project' }
let(:resource) { project }
it_behaves_like 'fails when user does not have the permission to create a Resource Bot'
it_behaves_like 'fails when flag is disabled'
context 'user with valid permission' do
before do
resource.add_maintainer(user)
end
it_behaves_like 'allows creation of bot with valid params'
end
end
end
end
|