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 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553
|
# frozen_string_literal: true
require 'spec_helper'
RSpec.describe API::UsageData, feature_category: :service_ping do
let_it_be(:user) { create(:user) }
shared_examples 'does not allow web request without CSRF token' do
it 'returns 401 response when CSRF check fails on web request' do
allow(Gitlab::RequestForgeryProtection).to receive(:verified?).and_return(false)
sign_in(user)
post api(endpoint), params: { event: known_event }
expect(response).to have_gitlab_http_status(:unauthorized)
end
end
describe 'GET /usage_data/service_ping' do
let(:endpoint) { '/usage_data/service_ping' }
context 'without authentication' do
it 'returns 401 response' do
get api(endpoint)
expect(response).to have_gitlab_http_status(:unauthorized)
end
end
context 'when authenticated as non-admin' do
let(:user) { create(:user) }
it 'returns 403' do
get api(endpoint, user)
expect(response).to have_gitlab_http_status(:forbidden)
end
end
context 'when authenticated as an admin using ai_workflows oauth access token' do
let(:oauth_access_token) { create(:oauth_access_token, user: user, scopes: [:ai_workflows]) }
before do
allow(Ability).to receive(:allowed?).and_return(true)
end
it 'returns 403' do
get api(endpoint, oauth_access_token: oauth_access_token)
expect(response).to have_gitlab_http_status(:forbidden)
end
end
context 'when authenticated as an admin using read_service_ping access token' do
let(:scopes) { [Gitlab::Auth::READ_SERVICE_PING_SCOPE] }
let(:personal_access_token) { create(:personal_access_token, user: user, scopes: scopes) }
before do
allow(Ability).to receive(:allowed?).and_return(true)
end
it 'returns 200' do
get api(endpoint, personal_access_token: personal_access_token)
expect(response).to have_gitlab_http_status(:ok)
end
it 'returns service ping payload' do
usage_data = { 'key' => 'value' }
allow(Rails.cache).to receive(:fetch).and_return(usage_data)
get api(endpoint, personal_access_token: personal_access_token)
expect(response.body).to eq(usage_data.to_json)
end
it 'tracks an internal event' do
expect(Gitlab::InternalEvents).to receive(:track_event)
.with('request_service_ping_via_rest', user: user)
get api(endpoint, personal_access_token: personal_access_token)
end
end
end
describe 'POST /usage_data/increment_counter' do
let(:endpoint) { '/usage_data/increment_counter' }
let(:known_event) { "diff_searches" }
let(:unknown_event) { 'unknown' }
context 'without authentication' do
it 'returns 401 response' do
post api(endpoint), params: { event: known_event }
expect(response).to have_gitlab_http_status(:unauthorized)
end
end
include_examples 'does not allow web request without CSRF token'
context 'with authentication' do
before do
stub_application_setting(usage_ping_enabled: true)
end
context 'when event is missing from params' do
it 'returns bad request' do
post api(endpoint, user), params: {}
expect(response).to have_gitlab_http_status(:bad_request)
end
end
context 'with correct params' do
it 'returns status :ok' do
expect(Gitlab::UsageDataCounters::BaseCounter).to receive(:count).with("searches")
post api(endpoint, user), params: { event: known_event }
expect(response).to have_gitlab_http_status(:ok)
end
end
context 'with unknown event' do
before do
skip_default_enabled_yaml_check
end
it 'returns status ok' do
expect(Gitlab::UsageDataCounters::BaseCounter).not_to receive(:count)
post api(endpoint, user), params: { event: unknown_event }
expect(response).to have_gitlab_http_status(:ok)
end
end
end
end
describe 'POST /usage_data/increment_unique_users' do
let(:endpoint) { '/usage_data/increment_unique_users' }
let(:known_event) { 'g_compliance_dashboard' }
let(:unknown_event) { 'unknown' }
context 'without authentication' do
it 'returns 401 response' do
post api(endpoint), params: { event: known_event }
expect(response).to have_gitlab_http_status(:unauthorized)
end
end
include_examples 'does not allow web request without CSRF token'
context 'with authentication' do
before do
stub_application_setting(usage_ping_enabled: true)
end
context 'with web authentication but without CSRF token' do
it 'returns 401 response' do
allow(Gitlab::RequestForgeryProtection).to receive(:verified?).and_return(false)
sign_in(user)
post api(endpoint), params: { event: known_event }
expect(response).to have_gitlab_http_status(:unauthorized)
end
end
context 'when event is missing from params' do
it 'returns bad request' do
post api(endpoint, user), params: {}
expect(response).to have_gitlab_http_status(:bad_request)
end
end
context 'with correct params' do
it 'returns status ok' do
expect(Gitlab::UsageDataCounters::HLLRedisCounter).to receive(:track).with(anything, known_event, anything)
# allow other events to also get triggered
allow(Gitlab::UsageDataCounters::HLLRedisCounter).to receive(:track)
post api(endpoint, user), params: { event: known_event }
expect(response).to have_gitlab_http_status(:ok)
end
end
context 'with unknown event' do
it 'returns status ok' do
expect(Gitlab::Redis::HLL).not_to receive(:add)
post api(endpoint, user), params: { event: unknown_event }
expect(response).to have_gitlab_http_status(:ok)
end
end
end
end
describe 'POST /usage_data/track_event' do
let(:endpoint) { '/usage_data/track_event' }
let(:known_event) { 'i_compliance_dashboard' }
let(:unknown_event) { 'unknown' }
let(:namespace_id) { 123 }
let(:project_id) { 123 }
context 'without authentication' do
it 'returns 401 response' do
post api(endpoint), params: { event: known_event, namespace_id: namespace_id, project_id: project_id }
expect(response).to have_gitlab_http_status(:unauthorized)
end
end
context 'with personal access token that has read_service_ping scope' do
let(:scopes) { [Gitlab::Auth::READ_SERVICE_PING_SCOPE] }
let(:personal_access_token) { create(:personal_access_token, user: user, scopes: scopes) }
let(:params) { { event: known_event } }
it 'returns 403' do
post api(endpoint, personal_access_token: personal_access_token), params: params
expect(response).to have_gitlab_http_status(:forbidden)
end
end
context 'with oauth token that has ai_workflows scope' do
let(:oauth_access_token) { create(:oauth_access_token, user: user, scopes: [:ai_workflows]) }
let(:params) { { event: known_event } }
it 'allows access' do
expect(Gitlab::InternalEvents).to receive(:track_event)
.with(
known_event,
send_snowplow_event: false,
user: user,
namespace: nil,
project: nil,
additional_properties: {}
)
post api(endpoint, oauth_access_token: oauth_access_token), params: params
expect(response).to have_gitlab_http_status(:ok)
end
end
context 'with usage ping enabled' do
let_it_be(:namespace) { create(:namespace) }
let_it_be(:project) { create(:project) }
let_it_be(:additional_properties) do
{
label: 'label3',
property: 'admin',
lang: 'ruby'
}
end
before do
stub_application_setting(usage_ping_enabled: true)
end
include_examples 'does not allow web request without CSRF token'
context 'with correct params' do
it 'returns status ok' do
expect(Gitlab::InternalEvents).to receive(:track_event)
.with(
known_event,
send_snowplow_event: false,
user: user,
namespace: namespace,
project: project,
additional_properties: additional_properties
)
params = {
event: known_event,
namespace_id: namespace.id,
project_id: project.id,
additional_properties: additional_properties
}
post api(endpoint, user), params: params
expect(response).to have_gitlab_http_status(:ok)
end
context 'with no additional_properties' do
it 'returns status ok' do
expect(Gitlab::InternalEvents).to receive(:track_event)
.with(
known_event,
send_snowplow_event: false,
user: user,
namespace: namespace,
project: project,
additional_properties: {}
)
post api(endpoint, user), params: { event: known_event, namespace_id: namespace.id, project_id: project.id }
expect(response).to have_gitlab_http_status(:ok)
end
end
end
describe 'send_to_snowplow param' do
it 'does not send the event to snowplow when send_to_snowplow is false' do
expect(Gitlab::InternalEvents).to receive(:track_event)
.with(
known_event,
send_snowplow_event: false,
user: user,
namespace: namespace,
project: project,
additional_properties: additional_properties
)
post api(endpoint, user), params: {
event: known_event,
namespace_id: namespace.id,
project_id: project.id,
additional_properties: additional_properties,
send_to_snowplow: false
}
expect(response).to have_gitlab_http_status(:ok)
end
it 'sends event to Snowplow when send_to_snowplow is true' do
expect(Gitlab::InternalEvents).to receive(:track_event)
.with(
known_event,
send_snowplow_event: true,
user: user,
namespace: namespace,
project: project,
additional_properties: additional_properties
)
post api(endpoint, user), params:
{
event: known_event,
namespace_id: namespace.id,
project_id: project.id,
additional_properties: additional_properties,
send_to_snowplow: true
}
expect(response).to have_gitlab_http_status(:ok)
end
it 'does not send event to Snowplow by default' do
expect(Gitlab::InternalEvents).to receive(:track_event)
.with(
known_event,
send_snowplow_event: false,
user: user,
namespace: namespace,
project: project,
additional_properties: additional_properties
)
post api(endpoint, user), params:
{
event: known_event,
namespace_id: namespace.id,
project_id: project.id,
additional_properties: additional_properties
}
expect(response).to have_gitlab_http_status(:ok)
end
end
end
end
describe 'POST /usage_data/track_events' do
let(:endpoint) { '/usage_data/track_events' }
let(:event) { 'web_ide_viewed' }
let(:namespace) { create(:namespace) }
let_it_be(:project) { create(:project) }
let(:additional_properties) do
{
label: 'label3',
property: 'admin',
lang: 'ruby'
}
end
context 'without authentication' do
it 'returns 401 response' do
post api(endpoint), params: { events: [{ event: event }] }
expect(response).to have_gitlab_http_status(:unauthorized)
end
end
context 'with the amount events greater than the limit' do
let(:params) { { events: Array.new(API::UsageData::MAXIMUM_TRACKED_EVENTS * 2) { { event: event } } } }
it 'returns bad request' do
expect(Gitlab::InternalEvents).not_to receive(:track_event)
post api(endpoint, user), params: params
expect(response).to have_gitlab_http_status(:bad_request)
end
end
context 'with correct params' do
let(:params) do
{
events: [
{
event: event,
namespace_id: namespace.id,
project_id: project.id,
additional_properties: additional_properties
}
]
}
end
it 'tracks the events' do
expect(Gitlab::InternalEvents).to receive(:track_event)
.with(
event,
send_snowplow_event: false,
namespace: namespace,
user: user,
project: project,
additional_properties: additional_properties
)
post api(endpoint, user), params: params
end
it 'triggers internal events and returns status ok' do
allow(Gitlab::InternalEvents).to receive(:track_event)
post api(endpoint, user), params: params
expect(response).to have_gitlab_http_status(:ok)
end
context 'with incorrect params' do
let(:params) do
{
events: [
{
event: 1
}
]
}
end
it 'returns bad request' do
expect { post(api(endpoint, user), params: params) }
.not_to trigger_internal_events(event)
end
end
end
end
describe 'GET /usage_data/metric_definitions' do
let(:endpoint) { '/usage_data/metric_definitions' }
let(:include_paths) { false }
let(:metrics) do
{
'counter.category.event' => metric1,
'counter.category.event2' => metric2
}
end
let(:metric1_attributes) do
{ 'key_path' => 'counter.category.event', 'description' => 'Metric description', 'tier' => ['free'] }
end
let(:metric2_attributes) do
{ 'key_path' => 'counter.category.event2', 'description' => 'Metric description2', 'tier' => ['free'] }
end
let(:metric1) do
Gitlab::Usage::MetricDefinition.new('/metrics/test_metric1.yml', metric1_attributes.dup.symbolize_keys)
end
let(:metric2) do
Gitlab::Usage::MetricDefinition.new('/metrics/test_metric2.yml', metric2_attributes.dup.symbolize_keys)
end
let(:metric_yaml) do
[metric1_attributes.merge('tiers' => ['free']), metric2_attributes.merge('tiers' => ['free'])].to_yaml
end
before do
allow(Gitlab::Usage::MetricDefinition).to receive(:definitions).and_return(metrics)
end
around do |example|
Gitlab::Usage::MetricDefinition.instance_variable_set(:@metrics_yaml, nil)
example.run
Gitlab::Usage::MetricDefinition.instance_variable_set(:@metrics_yaml, nil)
end
context 'without authentication' do
it 'returns a YAML file', :aggregate_failures do
get(api(endpoint), params: { include_paths: include_paths })
expect(response).to have_gitlab_http_status(:ok)
expect(response.media_type).to eq('application/yaml')
expect(response.body).to eq(metric_yaml)
end
end
context "without include_paths passed" do
it 'uses false include_paths by default', :aggregate_failures do
get(api(endpoint))
expect(response.body).to eq(metric_yaml)
end
end
it 'returns tiers in the metric attributes', :aggregate_failures do
get(api(endpoint))
payload = YAML.safe_load(response.body)
expect(payload.length).to be 2
expect(payload[0]).to include(metric1_attributes)
expect(payload[0]['tiers']).to eq(payload[0]['tier'])
expect(payload[1]).to include(metric2_attributes)
expect(payload[1]['tiers']).to eq(payload[1]['tier'])
end
context "with include_paths being true" do
let(:include_paths) { true }
it 'passes include_paths value', :aggregate_failures do
get(api(endpoint), params: { include_paths: include_paths })
payload = YAML.safe_load(response.body)
expect(payload.length).to be 2
expect(payload[0]).to include(metric1_attributes)
expect(payload[0]['file_path']).to end_with('metrics/test_metric1.yml')
expect(payload[1]).to include(metric2_attributes)
expect(payload[1]['file_path']).to end_with('metrics/test_metric2.yml')
end
end
end
end
|