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
164
165
166
|
# frozen_string_literal: true
require 'spec_helper'
RSpec.describe Ci::JobToken::Scope, feature_category: :continuous_integration, factory_default: :keep do
include Ci::JobTokenScopeHelpers
using RSpec::Parameterized::TableSyntax
let_it_be(:project) { create_default(:project) }
let_it_be(:user) { create_default(:user) }
let_it_be(:namespace) { create_default(:namespace) }
let_it_be(:source_project) do
create(:project,
ci_outbound_job_token_scope_enabled: true,
ci_inbound_job_token_scope_enabled: true
)
end
let(:current_project) { source_project }
let(:scope) { described_class.new(current_project) }
describe '#outbound_projects' do
subject { scope.outbound_projects }
context 'when no projects are added to the scope' do
it 'returns the project defining the scope' do
expect(subject).to contain_exactly(current_project)
end
end
context 'when projects are added to the scope' do
include_context 'with accessible and inaccessible projects'
it 'returns all projects that can be accessed from a given scope' do
expect(subject).to contain_exactly(current_project, outbound_allowlist_project, fully_accessible_project)
end
end
end
describe '#inbound_projects' do
subject { scope.inbound_projects }
context 'when no projects are added to the scope' do
it 'returns the project defining the scope' do
expect(subject).to contain_exactly(current_project)
end
end
context 'when projects are added to the scope' do
include_context 'with accessible and inaccessible projects'
it 'returns all projects that can be accessed from a given scope' do
expect(subject).to contain_exactly(current_project, inbound_allowlist_project)
end
end
end
describe 'add!' do
let_it_be(:new_project) { create(:project) }
subject { scope.add!(new_project, direction: direction, user: user) }
[:inbound, :outbound].each do |d|
context "with #{d}" do
let(:direction) { d }
it 'adds the project' do
subject
expect(scope.send("#{direction}_projects")).to contain_exactly(current_project, new_project)
end
end
end
# Context and before block can go away leaving just the example in 16.0
context 'with inbound only enabled' do
before do
project.ci_cd_settings.update!(job_token_scope_enabled: false)
end
it 'provides access' do
expect do
scope.add!(new_project, direction: :inbound, user: user)
end.to change { described_class.new(new_project).accessible?(current_project) }.from(false).to(true)
end
end
end
RSpec.shared_examples 'enforces outbound scope only' do
include_context 'with accessible and inaccessible projects'
where(:accessed_project, :result) do
ref(:current_project) | true
ref(:inbound_allowlist_project) | false
ref(:unscoped_project1) | false
ref(:unscoped_project2) | false
ref(:outbound_allowlist_project) | true
ref(:inbound_accessible_project) | false
ref(:fully_accessible_project) | true
end
with_them do
it { is_expected.to eq(result) }
end
end
describe 'accessible?' do
subject { scope.accessible?(accessed_project) }
context 'with inbound and outbound scopes enabled' do
context 'when inbound and outbound access setup' do
include_context 'with accessible and inaccessible projects'
where(:accessed_project, :result) do
ref(:current_project) | true
ref(:inbound_allowlist_project) | false
ref(:unscoped_project1) | false
ref(:unscoped_project2) | false
ref(:outbound_allowlist_project) | false
ref(:inbound_accessible_project) | false
ref(:fully_accessible_project) | true
end
with_them do
it 'allows self and projects allowed from both directions' do
is_expected.to eq(result)
end
end
end
end
context 'with inbound scope enabled and outbound scope disabled' do
before do
accessed_project.update!(ci_inbound_job_token_scope_enabled: true)
current_project.update!(ci_outbound_job_token_scope_enabled: false)
end
include_context 'with accessible and inaccessible projects'
where(:accessed_project, :result) do
ref(:current_project) | true
ref(:inbound_allowlist_project) | false
ref(:unscoped_project1) | false
ref(:unscoped_project2) | false
ref(:outbound_allowlist_project) | false
ref(:inbound_accessible_project) | true
ref(:fully_accessible_project) | true
end
with_them do
it { is_expected.to eq(result) }
end
end
context 'with inbound scope disabled and outbound scope enabled' do
before do
accessed_project.update!(ci_inbound_job_token_scope_enabled: false)
current_project.update!(ci_outbound_job_token_scope_enabled: true)
end
include_examples 'enforces outbound scope only'
end
end
end
|