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 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385
|
# frozen_string_literal: true
require 'spec_helper'
RSpec.describe API::Unleash, feature_category: :feature_flags do
include FeatureFlagHelpers
let_it_be(:project, refind: true) { create(:project) }
let(:project_id) { project.id }
let(:params) {}
let(:headers) {}
shared_examples 'authenticated request' do
context 'when using instance id' do
let(:client) { create(:operations_feature_flags_client, project: project) }
let(:params) { { instance_id: client.token } }
it 'responds with OK' do
subject
expect(response).to have_gitlab_http_status(:ok)
end
context 'when repository is disabled' do
before do
project.project_feature.update!(
repository_access_level: ::ProjectFeature::DISABLED,
merge_requests_access_level: ::ProjectFeature::DISABLED,
builds_access_level: ::ProjectFeature::DISABLED
)
end
it 'responds with forbidden' do
subject
expect(response).to have_gitlab_http_status(:ok)
end
end
context 'when repository is private' do
before do
project.project_feature.update!(
repository_access_level: ::ProjectFeature::PRIVATE,
merge_requests_access_level: ::ProjectFeature::DISABLED,
builds_access_level: ::ProjectFeature::DISABLED
)
end
it 'responds with OK' do
subject
expect(response).to have_gitlab_http_status(:ok)
end
end
end
context 'when using header' do
let(:client) { create(:operations_feature_flags_client, project: project) }
let(:headers) { { "UNLEASH-INSTANCEID" => client.token } }
it 'responds with OK' do
subject
expect(response).to have_gitlab_http_status(:ok)
end
end
context 'when using bogus instance id' do
let(:params) { { instance_id: 'token' } }
it 'responds with unauthorized' do
subject
expect(response).to have_gitlab_http_status(:unauthorized)
end
end
context 'when using not existing project' do
let(:project_id) { -5000 }
let(:params) { { instance_id: 'token' } }
it 'responds with unauthorized' do
subject
expect(response).to have_gitlab_http_status(:unauthorized)
end
end
end
describe 'GET /feature_flags/unleash/:project_id/client/features', :use_clean_rails_redis_caching do
specify do
get api("/feature_flags/unleash/#{project_id}/client/features"), params: params, headers: headers
is_expected.to have_request_urgency(:medium)
end
end
%w[/feature_flags/unleash/:project_id/features /feature_flags/unleash/:project_id/client/features].each do |features_endpoint|
describe "GET #{features_endpoint}", :use_clean_rails_redis_caching do
let(:features_url) { features_endpoint.sub(':project_id', project_id.to_s) }
let(:client) { create(:operations_feature_flags_client, project: project) }
subject { get api(features_url), params: params, headers: headers }
it_behaves_like 'authenticated request'
context 'when a client fetches feature flags several times' do
let(:headers) { { 'UNLEASH-INSTANCEID' => client.token, 'UNLEASH-APPNAME' => 'production' } }
before do
create_list(:operations_feature_flag, 3, project: project)
end
it 'serializes feature flags for the first time and read cached data from the second time' do
expect(API::Entities::Unleash::ClientFeatureFlags)
.to receive(:represent).with(instance_of(Operations::FeatureFlagsClient), any_args)
.once
5.times { get api(features_url), params: params, headers: headers }
end
it 'increments the cache key when feature flags are modified' do
expect(API::Entities::Unleash::ClientFeatureFlags)
.to receive(:represent).with(instance_of(Operations::FeatureFlagsClient), any_args)
.twice
2.times { get api(features_url), params: params, headers: headers }
::FeatureFlags::CreateService.new(project, project.owner, name: 'feature_flag').execute
3.times { get api(features_url), params: params, headers: headers }
end
end
context 'with version 2 feature flags' do
it 'does not return a flag without any strategies' do
create(:operations_feature_flag, project: project, name: 'feature1', active: true, version: 2)
get api(features_url), headers: { 'UNLEASH-INSTANCEID' => client.token, 'UNLEASH-APPNAME' => 'production' }
expect(response).to have_gitlab_http_status(:ok)
expect(json_response['features']).to be_empty
end
it 'returns a flag with a default strategy' do
feature_flag = create(:operations_feature_flag, project: project, name: 'feature1', active: true, version: 2)
strategy = create(:operations_strategy, feature_flag: feature_flag, name: 'default', parameters: {})
create(:operations_scope, strategy: strategy, environment_scope: 'production')
get api(features_url), headers: { 'UNLEASH-INSTANCEID' => client.token, 'UNLEASH-APPNAME' => 'production' }
expect(response).to have_gitlab_http_status(:ok)
expect(json_response['features']).to eq([{
'name' => 'feature1',
'enabled' => true,
'strategies' => [{
'name' => 'default',
'parameters' => {}
}]
}])
end
it 'returns a flag with a userWithId strategy' do
feature_flag = create(:operations_feature_flag, project: project, name: 'feature1', active: true, version: 2)
strategy = create(:operations_strategy,
feature_flag: feature_flag, name: 'userWithId', parameters: { userIds: 'user123,user456' })
create(:operations_scope, strategy: strategy, environment_scope: 'production')
get api(features_url), headers: { 'UNLEASH-INSTANCEID' => client.token, 'UNLEASH-APPNAME' => 'production' }
expect(response).to have_gitlab_http_status(:ok)
expect(json_response['features']).to eq([{
'name' => 'feature1',
'enabled' => true,
'strategies' => [{
'name' => 'userWithId',
'parameters' => { 'userIds' => 'user123,user456' }
}]
}])
end
it 'returns a flag with multiple strategies' do
feature_flag = create(:operations_feature_flag, project: project, name: 'feature1', active: true, version: 2)
strategy_a = create(:operations_strategy,
feature_flag: feature_flag, name: 'userWithId', parameters: { userIds: 'user_a,user_b' })
strategy_b = create(:operations_strategy,
feature_flag: feature_flag,
name: 'gradualRolloutUserId',
parameters: { groupId: 'default', percentage: '45' })
create(:operations_scope, strategy: strategy_a, environment_scope: 'production')
create(:operations_scope, strategy: strategy_b, environment_scope: 'production')
get api(features_url), headers: { 'UNLEASH-INSTANCEID' => client.token, 'UNLEASH-APPNAME' => 'production' }
expect(response).to have_gitlab_http_status(:ok)
expect(json_response['features'].map { |f| f['name'] }.sort).to eq(['feature1'])
features_json = json_response['features'].map do |feature|
feature.merge(feature.slice('strategies').transform_values { |v| v.sort_by { |s| s['name'] } })
end
expect(features_json).to eq([{
'name' => 'feature1',
'enabled' => true,
'strategies' => [{
'name' => 'gradualRolloutUserId',
'parameters' => { 'groupId' => 'default', 'percentage' => '45' }
}, {
'name' => 'userWithId',
'parameters' => { 'userIds' => 'user_a,user_b' }
}]
}])
end
it 'returns only flags matching the environment scope' do
feature_flag_a = create(:operations_feature_flag,
project: project, name: 'feature1', active: true, version: 2)
strategy_a = create(:operations_strategy, feature_flag: feature_flag_a)
create(:operations_scope, strategy: strategy_a, environment_scope: 'production')
feature_flag_b = create(:operations_feature_flag,
project: project, name: 'feature2', active: true, version: 2)
strategy_b = create(:operations_strategy, feature_flag: feature_flag_b)
create(:operations_scope, strategy: strategy_b, environment_scope: 'staging')
get api(features_url), headers: { 'UNLEASH-INSTANCEID' => client.token, 'UNLEASH-APPNAME' => 'staging' }
expect(response).to have_gitlab_http_status(:ok)
expect(json_response['features'].map { |f| f['name'] }.sort).to eq(['feature2'])
expect(json_response['features']).to eq([{
'name' => 'feature2',
'enabled' => true,
'strategies' => [{
'name' => 'default',
'parameters' => {}
}]
}])
end
it 'returns only strategies matching the environment scope' do
feature_flag = create(:operations_feature_flag, project: project, name: 'feature1', active: true, version: 2)
strategy_a = create(:operations_strategy,
feature_flag: feature_flag, name: 'userWithId', parameters: { userIds: 'user2,user8,user4' })
create(:operations_scope, strategy: strategy_a, environment_scope: 'production')
strategy_b = create(:operations_strategy, feature_flag: feature_flag, name: 'default', parameters: {})
create(:operations_scope, strategy: strategy_b, environment_scope: 'staging')
get api(features_url), headers: { 'UNLEASH-INSTANCEID' => client.token, 'UNLEASH-APPNAME' => 'production' }
expect(response).to have_gitlab_http_status(:ok)
expect(json_response['features']).to eq([{
'name' => 'feature1',
'enabled' => true,
'strategies' => [{
'name' => 'userWithId',
'parameters' => { 'userIds' => 'user2,user8,user4' }
}]
}])
end
it 'returns only flags for the given project' do
project_b = create(:project)
feature_flag_a = create(:operations_feature_flag,
project: project, name: 'feature_a', active: true, version: 2)
strategy_a = create(:operations_strategy, feature_flag: feature_flag_a)
create(:operations_scope, strategy: strategy_a, environment_scope: 'sandbox')
feature_flag_b = create(:operations_feature_flag,
project: project_b, name: 'feature_b', active: true, version: 2)
strategy_b = create(:operations_strategy, feature_flag: feature_flag_b)
create(:operations_scope, strategy: strategy_b, environment_scope: 'sandbox')
get api(features_url), headers: { 'UNLEASH-INSTANCEID' => client.token, 'UNLEASH-APPNAME' => 'sandbox' }
expect(response).to have_gitlab_http_status(:ok)
expect(json_response['features']).to eq([{
'name' => 'feature_a',
'enabled' => true,
'strategies' => [{
'name' => 'default',
'parameters' => {}
}]
}])
end
it 'returns all strategies with a matching scope' do
feature_flag = create(:operations_feature_flag, project: project, name: 'feature1', active: true, version: 2)
strategy_a = create(:operations_strategy,
feature_flag: feature_flag, name: 'userWithId', parameters: { userIds: 'user2,user8,user4' })
create(:operations_scope, strategy: strategy_a, environment_scope: '*')
strategy_b = create(:operations_strategy, feature_flag: feature_flag, name: 'default', parameters: {})
create(:operations_scope, strategy: strategy_b, environment_scope: 'review/*')
strategy_c = create(:operations_strategy,
feature_flag: feature_flag,
name: 'gradualRolloutUserId',
parameters: { groupId: 'default', percentage: '15' })
create(:operations_scope, strategy: strategy_c, environment_scope: 'review/patch-1')
get api(features_url), headers: { 'UNLEASH-INSTANCEID' => client.token, 'UNLEASH-APPNAME' => 'review/patch-1' }
expect(response).to have_gitlab_http_status(:ok)
expect(json_response['features'].first['strategies'].sort_by { |s| s['name'] }).to eq([{
'name' => 'default',
'parameters' => {}
}, {
'name' => 'gradualRolloutUserId',
'parameters' => { 'groupId' => 'default', 'percentage' => '15' }
}, {
'name' => 'userWithId',
'parameters' => { 'userIds' => 'user2,user8,user4' }
}])
end
it 'returns a strategy with more than one matching scope' do
feature_flag = create(:operations_feature_flag, project: project, name: 'feature1', active: true, version: 2)
strategy = create(:operations_strategy, feature_flag: feature_flag, name: 'default', parameters: {})
create(:operations_scope, strategy: strategy, environment_scope: 'production')
create(:operations_scope, strategy: strategy, environment_scope: '*')
get api(features_url), headers: { 'UNLEASH-INSTANCEID' => client.token, 'UNLEASH-APPNAME' => 'production' }
expect(response).to have_gitlab_http_status(:ok)
expect(json_response['features']).to eq([{
'name' => 'feature1',
'enabled' => true,
'strategies' => [{
'name' => 'default',
'parameters' => {}
}]
}])
end
it 'returns a disabled flag with a matching scope' do
feature_flag = create(:operations_feature_flag,
project: project, name: 'myfeature', active: false, version: 2)
strategy = create(:operations_strategy, feature_flag: feature_flag, name: 'default', parameters: {})
create(:operations_scope, strategy: strategy, environment_scope: 'production')
get api(features_url), headers: { 'UNLEASH-INSTANCEID' => client.token, 'UNLEASH-APPNAME' => 'production' }
expect(response).to have_gitlab_http_status(:ok)
expect(json_response['features']).to eq([{
'name' => 'myfeature',
'enabled' => false,
'strategies' => [{
'name' => 'default',
'parameters' => {}
}]
}])
end
it 'returns a userWithId strategy for a gitlabUserList strategy' do
feature_flag = create(:operations_feature_flag, :new_version_flag,
project: project, name: 'myfeature', active: true)
user_list = create(:operations_feature_flag_user_list,
project: project, name: 'My List', user_xids: 'user1,user2')
strategy = create(:operations_strategy,
feature_flag: feature_flag, name: 'gitlabUserList', parameters: {}, user_list: user_list)
create(:operations_scope, strategy: strategy, environment_scope: 'production')
get api(features_url), headers: { 'UNLEASH-INSTANCEID' => client.token, 'UNLEASH-APPNAME' => 'production' }
expect(response).to have_gitlab_http_status(:ok)
expect(json_response['features']).to eq([{
'name' => 'myfeature',
'enabled' => true,
'strategies' => [{
'name' => 'userWithId',
'parameters' => { 'userIds' => 'user1,user2' }
}]
}])
end
end
end
end
describe 'POST /feature_flags/unleash/:project_id/client/register' do
subject { post api("/feature_flags/unleash/#{project_id}/client/register"), params: params, headers: headers }
it_behaves_like 'authenticated request'
end
describe 'POST /feature_flags/unleash/:project_id/client/metrics' do
subject { post api("/feature_flags/unleash/#{project_id}/client/metrics"), params: params, headers: headers }
it_behaves_like 'authenticated request'
end
end
|