2019-07-07 11:18:12 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2018-11-08 19:23:39 +05:30
|
|
|
require 'spec_helper'
|
|
|
|
|
2023-04-23 21:23:45 +05:30
|
|
|
RSpec.describe GraphqlController, feature_category: :integrations do
|
2020-05-24 23:13:21 +05:30
|
|
|
include GraphqlHelpers
|
|
|
|
|
2022-07-23 23:45:48 +05:30
|
|
|
# two days is enough to make timezones irrelevant
|
|
|
|
let_it_be(:last_activity_on) { 2.days.ago.to_date }
|
|
|
|
|
2022-07-16 23:28:13 +05:30
|
|
|
describe 'rescue_from' do
|
|
|
|
let_it_be(:message) { 'green ideas sleep furiously' }
|
2019-10-12 21:52:04 +05:30
|
|
|
|
2022-07-16 23:28:13 +05:30
|
|
|
it 'handles ArgumentError' do
|
2019-10-12 21:52:04 +05:30
|
|
|
allow(subject).to receive(:execute) do
|
|
|
|
raise Gitlab::Graphql::Errors::ArgumentError, message
|
|
|
|
end
|
|
|
|
|
|
|
|
post :execute
|
|
|
|
|
|
|
|
expect(json_response).to include(
|
|
|
|
'errors' => include(a_hash_including('message' => message))
|
|
|
|
)
|
|
|
|
end
|
2022-07-16 23:28:13 +05:30
|
|
|
|
2022-08-13 15:12:31 +05:30
|
|
|
it 'handles a timeout nicely' do
|
|
|
|
allow(subject).to receive(:execute) do
|
|
|
|
raise ActiveRecord::QueryCanceled, '**taps wristwatch**'
|
|
|
|
end
|
|
|
|
|
|
|
|
post :execute
|
|
|
|
|
|
|
|
expect(json_response).to include(
|
|
|
|
'errors' => include(a_hash_including('message' => /Request timed out/))
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
2022-07-16 23:28:13 +05:30
|
|
|
it 'handles StandardError' do
|
|
|
|
allow(subject).to receive(:execute) do
|
|
|
|
raise StandardError, message
|
|
|
|
end
|
|
|
|
|
|
|
|
post :execute
|
|
|
|
|
|
|
|
expect(json_response).to include(
|
2023-05-27 22:25:52 +05:30
|
|
|
'errors' => include(
|
|
|
|
a_hash_including('message' => /Internal server error/, 'raisedAt' => /graphql_controller_spec.rb/)
|
|
|
|
)
|
2022-07-16 23:28:13 +05:30
|
|
|
)
|
|
|
|
end
|
2023-03-04 22:38:38 +05:30
|
|
|
|
|
|
|
it 'handles Gitlab::Auth::TooManyIps', :aggregate_failures do
|
|
|
|
allow(controller).to receive(:execute) do
|
|
|
|
raise Gitlab::Auth::TooManyIps.new(150, '123.123.123.123', 10)
|
|
|
|
end
|
|
|
|
|
|
|
|
expect(controller).to receive(:log_exception).and_call_original
|
|
|
|
|
|
|
|
post :execute
|
|
|
|
|
|
|
|
expect(json_response).to include(
|
|
|
|
'errors' => include(
|
|
|
|
a_hash_including('message' => 'User 150 from IP: 123.123.123.123 tried logging from too many ips: 10')
|
|
|
|
)
|
|
|
|
)
|
|
|
|
expect(response).to have_gitlab_http_status(:forbidden)
|
|
|
|
end
|
2019-10-12 21:52:04 +05:30
|
|
|
end
|
|
|
|
|
2019-07-07 11:18:12 +05:30
|
|
|
describe 'POST #execute' do
|
|
|
|
context 'when user is logged in' do
|
2022-07-23 23:45:48 +05:30
|
|
|
let(:user) { create(:user, last_activity_on: last_activity_on) }
|
2018-11-08 19:23:39 +05:30
|
|
|
|
|
|
|
before do
|
2019-07-07 11:18:12 +05:30
|
|
|
sign_in(user)
|
2018-11-29 20:51:05 +05:30
|
|
|
end
|
|
|
|
|
2021-11-18 22:05:49 +05:30
|
|
|
it 'sets feature category in ApplicationContext from request' do
|
|
|
|
request.headers["HTTP_X_GITLAB_FEATURE_CATEGORY"] = "web_ide"
|
|
|
|
|
|
|
|
post :execute
|
|
|
|
|
|
|
|
expect(::Gitlab::ApplicationContext.current_context_attribute(:feature_category)).to eq('web_ide')
|
|
|
|
end
|
|
|
|
|
2019-07-07 11:18:12 +05:30
|
|
|
it 'returns 200 when user can access API' do
|
|
|
|
post :execute
|
2019-05-30 16:15:17 +05:30
|
|
|
|
2020-03-13 15:44:24 +05:30
|
|
|
expect(response).to have_gitlab_http_status(:ok)
|
2018-11-29 20:51:05 +05:30
|
|
|
end
|
2019-05-30 16:15:17 +05:30
|
|
|
|
2021-12-07 22:27:20 +05:30
|
|
|
it 'executes a simple query with no errors' do
|
|
|
|
post :execute, params: { query: '{ __typename }' }
|
|
|
|
|
|
|
|
expect(response).to have_gitlab_http_status(:ok)
|
|
|
|
expect(json_response).to eq({ 'data' => { '__typename' => 'Query' } })
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'executes a simple multiplexed query with no errors' do
|
|
|
|
multiplex = [{ query: '{ __typename }' }] * 2
|
|
|
|
|
|
|
|
post :execute, params: { _json: multiplex }
|
|
|
|
|
|
|
|
expect(response).to have_gitlab_http_status(:ok)
|
2022-10-11 01:57:18 +05:30
|
|
|
expect(json_response).to eq(
|
|
|
|
[
|
|
|
|
{ 'data' => { '__typename' => 'Query' } },
|
|
|
|
{ 'data' => { '__typename' => 'Query' } }
|
|
|
|
])
|
2021-12-07 22:27:20 +05:30
|
|
|
end
|
|
|
|
|
2023-05-27 22:25:52 +05:30
|
|
|
it 'executes a multiplexed queries with variables with no errors' do
|
|
|
|
query = <<~GQL
|
|
|
|
mutation($a: String!, $b: String!) {
|
|
|
|
echoCreate(input: { messages: [$a, $b] }) { echoes }
|
|
|
|
}
|
|
|
|
GQL
|
|
|
|
multiplex = [
|
|
|
|
{ query: query, variables: { a: 'A', b: 'B' } },
|
|
|
|
{ query: query, variables: { a: 'a', b: 'b' } }
|
|
|
|
]
|
|
|
|
|
|
|
|
post :execute, params: { _json: multiplex }
|
|
|
|
|
|
|
|
expect(response).to have_gitlab_http_status(:ok)
|
|
|
|
expect(json_response).to eq(
|
|
|
|
[
|
|
|
|
{ 'data' => { 'echoCreate' => { 'echoes' => %w[A B] } } },
|
|
|
|
{ 'data' => { 'echoCreate' => { 'echoes' => %w[a b] } } }
|
|
|
|
])
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'does not allow string as _json parameter' do
|
|
|
|
post :execute, params: { _json: 'bad' }
|
|
|
|
|
|
|
|
expect(response).to have_gitlab_http_status(:ok)
|
|
|
|
expect(json_response).to eq({
|
|
|
|
"errors" => [
|
|
|
|
{
|
|
|
|
"message" => "Unexpected end of document",
|
|
|
|
"locations" => []
|
|
|
|
}
|
|
|
|
]
|
|
|
|
})
|
|
|
|
end
|
|
|
|
|
2021-12-07 22:27:20 +05:30
|
|
|
it 'sets a limit on the total query size' do
|
|
|
|
graphql_query = "{#{(['__typename'] * 1000).join(' ')}}"
|
|
|
|
|
|
|
|
post :execute, params: { query: graphql_query }
|
|
|
|
|
|
|
|
expect(response).to have_gitlab_http_status(:unprocessable_entity)
|
|
|
|
expect(json_response).to eq({ 'errors' => [{ 'message' => 'Query too large' }] })
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'sets a limit on the total query size for multiplex queries' do
|
|
|
|
graphql_query = "{#{(['__typename'] * 200).join(' ')}}"
|
|
|
|
multiplex = [{ query: graphql_query }] * 5
|
|
|
|
|
|
|
|
post :execute, params: { _json: multiplex }
|
|
|
|
|
|
|
|
expect(response).to have_gitlab_http_status(:unprocessable_entity)
|
|
|
|
expect(json_response).to eq({ 'errors' => [{ 'message' => 'Query too large' }] })
|
|
|
|
end
|
|
|
|
|
2021-07-02 01:05:55 +05:30
|
|
|
it 'returns forbidden when user cannot access API' do
|
2019-07-07 11:18:12 +05:30
|
|
|
# User cannot access API in a couple of cases
|
|
|
|
# * When user is internal(like ghost users)
|
|
|
|
# * When user is blocked
|
2020-04-22 19:07:51 +05:30
|
|
|
expect(Ability).to receive(:allowed?).with(user, :log_in, :global).and_call_original
|
2019-07-07 11:18:12 +05:30
|
|
|
expect(Ability).to receive(:allowed?).with(user, :access_api, :global).and_return(false)
|
2018-11-29 20:51:05 +05:30
|
|
|
|
2019-07-07 11:18:12 +05:30
|
|
|
post :execute
|
2019-05-30 16:15:17 +05:30
|
|
|
|
2020-04-22 19:07:51 +05:30
|
|
|
expect(response).to have_gitlab_http_status(:forbidden)
|
2021-07-02 01:05:55 +05:30
|
|
|
expect(json_response).to include(
|
|
|
|
'errors' => include(a_hash_including('message' => /API not accessible/))
|
|
|
|
)
|
2018-11-29 20:51:05 +05:30
|
|
|
end
|
2020-05-24 23:13:21 +05:30
|
|
|
|
|
|
|
it 'updates the users last_activity_on field' do
|
|
|
|
expect { post :execute }.to change { user.reload.last_activity_on }
|
|
|
|
end
|
2020-11-24 15:15:51 +05:30
|
|
|
|
|
|
|
it "sets context's sessionless value as false" do
|
|
|
|
post :execute
|
|
|
|
|
|
|
|
expect(assigns(:context)[:is_sessionless_user]).to be false
|
|
|
|
end
|
2021-03-11 19:13:27 +05:30
|
|
|
|
|
|
|
it 'calls the track api when trackable method' do
|
|
|
|
agent = 'vs-code-gitlab-workflow/3.11.1 VSCode/1.52.1 Node.js/12.14.1 (darwin; x64)'
|
|
|
|
request.env['HTTP_USER_AGENT'] = agent
|
|
|
|
|
|
|
|
expect(Gitlab::UsageDataCounters::VSCodeExtensionActivityUniqueCounter)
|
|
|
|
.to receive(:track_api_request_when_trackable).with(user_agent: agent, user: user)
|
|
|
|
|
|
|
|
post :execute
|
|
|
|
end
|
2022-04-04 11:22:00 +05:30
|
|
|
|
|
|
|
it 'calls the track jetbrains api when trackable method' do
|
|
|
|
agent = 'gitlab-jetbrains-plugin/0.0.1 intellij-idea/2021.2.4 java/11.0.13 mac-os-x/aarch64/12.1'
|
|
|
|
request.env['HTTP_USER_AGENT'] = agent
|
|
|
|
|
|
|
|
expect(Gitlab::UsageDataCounters::JetBrainsPluginActivityUniqueCounter)
|
|
|
|
.to receive(:track_api_request_when_trackable).with(user_agent: agent, user: user)
|
|
|
|
|
|
|
|
post :execute
|
|
|
|
end
|
2022-06-21 17:19:12 +05:30
|
|
|
|
2023-05-27 22:25:52 +05:30
|
|
|
context 'if using the GitLab CLI' do
|
|
|
|
it 'call trackable for the old UserAgent' do
|
|
|
|
agent = 'GLab - GitLab CLI'
|
2022-06-21 17:19:12 +05:30
|
|
|
|
2023-05-27 22:25:52 +05:30
|
|
|
request.env['HTTP_USER_AGENT'] = agent
|
2022-06-21 17:19:12 +05:30
|
|
|
|
2023-05-27 22:25:52 +05:30
|
|
|
expect(Gitlab::UsageDataCounters::GitLabCliActivityUniqueCounter)
|
|
|
|
.to receive(:track_api_request_when_trackable).with(user_agent: agent, user: user)
|
|
|
|
|
|
|
|
post :execute
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'call trackable for the current UserAgent' do
|
|
|
|
agent = 'glab/v1.25.3-27-g7ec258fb (built 2023-02-16), darwin'
|
|
|
|
|
|
|
|
request.env['HTTP_USER_AGENT'] = agent
|
|
|
|
|
|
|
|
expect(Gitlab::UsageDataCounters::GitLabCliActivityUniqueCounter)
|
|
|
|
.to receive(:track_api_request_when_trackable).with(user_agent: agent, user: user)
|
|
|
|
|
|
|
|
post :execute
|
|
|
|
end
|
2022-06-21 17:19:12 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
it "assigns username in ApplicationContext" do
|
|
|
|
post :execute
|
|
|
|
|
|
|
|
expect(Gitlab::ApplicationContext.current).to include('meta.user' => user.username)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when 2FA is required for the user' do
|
2022-07-23 23:45:48 +05:30
|
|
|
let(:user) { create(:user, last_activity_on: last_activity_on) }
|
2022-06-21 17:19:12 +05:30
|
|
|
|
|
|
|
before do
|
|
|
|
group = create(:group, require_two_factor_authentication: true)
|
|
|
|
group.add_developer(user)
|
|
|
|
|
|
|
|
sign_in(user)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'does not redirect if 2FA is enabled' do
|
|
|
|
expect(controller).not_to receive(:redirect_to)
|
|
|
|
|
|
|
|
post :execute
|
|
|
|
|
|
|
|
expect(response).to have_gitlab_http_status(:unauthorized)
|
|
|
|
|
|
|
|
expected_message = "Authentication error: " \
|
|
|
|
"enable 2FA in your profile settings to continue using GitLab: %{mfa_help_page}" %
|
2023-03-04 22:38:38 +05:30
|
|
|
{ mfa_help_page: controller.mfa_help_page_url }
|
2022-06-21 17:19:12 +05:30
|
|
|
|
|
|
|
expect(json_response).to eq({ 'errors' => [{ 'message' => expected_message }] })
|
|
|
|
end
|
2020-05-24 23:13:21 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
context 'when user uses an API token' do
|
2022-07-23 23:45:48 +05:30
|
|
|
let(:user) { create(:user, last_activity_on: last_activity_on) }
|
2020-05-24 23:13:21 +05:30
|
|
|
let(:token) { create(:personal_access_token, user: user, scopes: [:api]) }
|
2022-05-07 20:08:51 +05:30
|
|
|
let(:query) { '{ __typename }' }
|
2020-05-24 23:13:21 +05:30
|
|
|
|
2022-05-07 20:08:51 +05:30
|
|
|
subject { post :execute, params: { query: query, access_token: token.token } }
|
|
|
|
|
|
|
|
context 'when the user is a project bot' do
|
2022-07-23 23:45:48 +05:30
|
|
|
let(:user) { create(:user, :project_bot, last_activity_on: last_activity_on) }
|
2022-05-07 20:08:51 +05:30
|
|
|
|
|
|
|
it 'updates the users last_activity_on field' do
|
|
|
|
expect { subject }.to change { user.reload.last_activity_on }
|
|
|
|
end
|
|
|
|
|
|
|
|
it "sets context's sessionless value as true" do
|
|
|
|
subject
|
|
|
|
|
|
|
|
expect(assigns(:context)[:is_sessionless_user]).to be true
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'executes a simple query with no errors' do
|
|
|
|
subject
|
|
|
|
|
|
|
|
expect(response).to have_gitlab_http_status(:ok)
|
|
|
|
expect(json_response).to eq({ 'data' => { '__typename' => 'Query' } })
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'can access resources the project_bot has access to' do
|
|
|
|
project_a, project_b = create_list(:project, 2, :private)
|
|
|
|
project_a.add_developer(user)
|
|
|
|
|
|
|
|
post :execute, params: { query: <<~GQL, access_token: token.token }
|
|
|
|
query {
|
|
|
|
a: project(fullPath: "#{project_a.full_path}") { name }
|
|
|
|
b: project(fullPath: "#{project_b.full_path}") { name }
|
|
|
|
}
|
|
|
|
GQL
|
|
|
|
|
|
|
|
expect(response).to have_gitlab_http_status(:ok)
|
|
|
|
expect(json_response).to eq({ 'data' => { 'a' => { 'name' => project_a.name }, 'b' => nil } })
|
|
|
|
end
|
|
|
|
end
|
2020-11-24 15:15:51 +05:30
|
|
|
|
2020-05-24 23:13:21 +05:30
|
|
|
it 'updates the users last_activity_on field' do
|
2020-11-24 15:15:51 +05:30
|
|
|
expect { subject }.to change { user.reload.last_activity_on }
|
|
|
|
end
|
|
|
|
|
|
|
|
it "sets context's sessionless value as true" do
|
|
|
|
subject
|
|
|
|
|
|
|
|
expect(assigns(:context)[:is_sessionless_user]).to be true
|
2020-05-24 23:13:21 +05:30
|
|
|
end
|
2021-03-11 19:13:27 +05:30
|
|
|
|
2022-06-21 17:19:12 +05:30
|
|
|
it "assigns username in ApplicationContext" do
|
|
|
|
subject
|
|
|
|
|
|
|
|
expect(Gitlab::ApplicationContext.current).to include('meta.user' => user.username)
|
|
|
|
end
|
|
|
|
|
2021-03-11 19:13:27 +05:30
|
|
|
it 'calls the track api when trackable method' do
|
|
|
|
agent = 'vs-code-gitlab-workflow/3.11.1 VSCode/1.52.1 Node.js/12.14.1 (darwin; x64)'
|
|
|
|
request.env['HTTP_USER_AGENT'] = agent
|
|
|
|
|
|
|
|
expect(Gitlab::UsageDataCounters::VSCodeExtensionActivityUniqueCounter)
|
|
|
|
.to receive(:track_api_request_when_trackable).with(user_agent: agent, user: user)
|
|
|
|
|
|
|
|
subject
|
|
|
|
end
|
2022-04-04 11:22:00 +05:30
|
|
|
|
|
|
|
it 'calls the track jetbrains api when trackable method' do
|
|
|
|
agent = 'gitlab-jetbrains-plugin/0.0.1 intellij-idea/2021.2.4 java/11.0.13 mac-os-x/aarch64/12.1'
|
|
|
|
request.env['HTTP_USER_AGENT'] = agent
|
|
|
|
|
|
|
|
expect(Gitlab::UsageDataCounters::JetBrainsPluginActivityUniqueCounter)
|
|
|
|
.to receive(:track_api_request_when_trackable).with(user_agent: agent, user: user)
|
|
|
|
|
|
|
|
subject
|
|
|
|
end
|
2022-06-21 17:19:12 +05:30
|
|
|
|
|
|
|
it 'calls the track gitlab cli when trackable method' do
|
|
|
|
agent = 'GLab - GitLab CLI'
|
|
|
|
request.env['HTTP_USER_AGENT'] = agent
|
|
|
|
|
|
|
|
expect(Gitlab::UsageDataCounters::GitLabCliActivityUniqueCounter)
|
|
|
|
.to receive(:track_api_request_when_trackable).with(user_agent: agent, user: user)
|
|
|
|
|
|
|
|
subject
|
|
|
|
end
|
2018-11-29 20:51:05 +05:30
|
|
|
end
|
2019-05-30 16:15:17 +05:30
|
|
|
|
2019-07-07 11:18:12 +05:30
|
|
|
context 'when user is not logged in' do
|
|
|
|
it 'returns 200' do
|
|
|
|
post :execute
|
2019-05-30 16:15:17 +05:30
|
|
|
|
2020-03-13 15:44:24 +05:30
|
|
|
expect(response).to have_gitlab_http_status(:ok)
|
2019-05-30 16:15:17 +05:30
|
|
|
end
|
2020-11-24 15:15:51 +05:30
|
|
|
|
|
|
|
it "sets context's sessionless value as false" do
|
|
|
|
post :execute
|
|
|
|
|
|
|
|
expect(assigns(:context)[:is_sessionless_user]).to be false
|
|
|
|
end
|
2022-06-21 17:19:12 +05:30
|
|
|
|
|
|
|
it "does not assign a username in ApplicationContext" do
|
|
|
|
subject
|
|
|
|
|
|
|
|
expect(Gitlab::ApplicationContext.current.key?('meta.user')).to be false
|
|
|
|
end
|
2019-05-30 16:15:17 +05:30
|
|
|
end
|
2021-01-03 14:25:43 +05:30
|
|
|
|
|
|
|
it 'includes request object in context' do
|
|
|
|
post :execute
|
|
|
|
|
|
|
|
expect(assigns(:context)[:request]).to eq request
|
|
|
|
end
|
2023-04-23 21:23:45 +05:30
|
|
|
|
|
|
|
it 'sets `context[:remove_deprecated]` to false by default' do
|
|
|
|
post :execute
|
|
|
|
|
|
|
|
expect(assigns(:context)[:remove_deprecated]).to be false
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'sets `context[:remove_deprecated]` to true when `remove_deprecated` param is truthy' do
|
|
|
|
post :execute, params: { remove_deprecated: '1' }
|
|
|
|
|
|
|
|
expect(assigns(:context)[:remove_deprecated]).to be true
|
|
|
|
end
|
2019-05-30 16:15:17 +05:30
|
|
|
end
|
2020-05-24 23:13:21 +05:30
|
|
|
|
|
|
|
describe 'Admin Mode' do
|
2023-04-23 21:23:45 +05:30
|
|
|
let_it_be(:admin) { create(:admin) }
|
|
|
|
let_it_be(:project) { create(:project) }
|
|
|
|
|
2020-05-24 23:13:21 +05:30
|
|
|
let(:graphql_query) { graphql_query_for('project', { 'fullPath' => project.full_path }, %w(id name)) }
|
|
|
|
|
|
|
|
before do
|
|
|
|
sign_in(admin)
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when admin mode enabled' do
|
|
|
|
before do
|
|
|
|
Gitlab::Session.with_session(controller.session) do
|
|
|
|
controller.current_user_mode.request_admin_mode!
|
|
|
|
controller.current_user_mode.enable_admin_mode!(password: admin.password)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'can query project data' do
|
|
|
|
post :execute, params: { query: graphql_query }
|
|
|
|
|
|
|
|
expect(controller.current_user_mode.admin_mode?).to be(true)
|
|
|
|
expect(json_response['data']['project']['name']).to eq(project.name)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when admin mode disabled' do
|
|
|
|
it 'cannot query project data' do
|
|
|
|
post :execute, params: { query: graphql_query }
|
|
|
|
|
|
|
|
expect(controller.current_user_mode.admin_mode?).to be(false)
|
|
|
|
expect(json_response['data']['project']).to be_nil
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when admin is member of the project' do
|
|
|
|
before do
|
|
|
|
project.add_developer(admin)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'can query project data' do
|
|
|
|
post :execute, params: { query: graphql_query }
|
|
|
|
|
|
|
|
expect(controller.current_user_mode.admin_mode?).to be(false)
|
|
|
|
expect(json_response['data']['project']['name']).to eq(project.name)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2020-11-24 15:15:51 +05:30
|
|
|
|
|
|
|
describe '#append_info_to_payload' do
|
2021-04-29 21:17:54 +05:30
|
|
|
let(:query_1) { { query: graphql_query_for('project', { 'fullPath' => 'foo' }, %w(id name), 'getProject_1') } }
|
|
|
|
let(:query_2) { { query: graphql_query_for('project', { 'fullPath' => 'bar' }, %w(id), 'getProject_2') } }
|
|
|
|
let(:graphql_queries) { [query_1, query_2] }
|
2020-11-24 15:15:51 +05:30
|
|
|
let(:log_payload) { {} }
|
2021-04-29 21:17:54 +05:30
|
|
|
let(:expected_logs) do
|
|
|
|
[
|
|
|
|
{
|
|
|
|
operation_name: 'getProject_1',
|
|
|
|
complexity: 3,
|
|
|
|
depth: 2,
|
|
|
|
used_deprecated_fields: [],
|
|
|
|
used_fields: ['Project.id', 'Project.name', 'Query.project'],
|
|
|
|
variables: '{}'
|
|
|
|
},
|
|
|
|
{
|
|
|
|
operation_name: 'getProject_2',
|
|
|
|
complexity: 2,
|
|
|
|
depth: 2,
|
|
|
|
used_deprecated_fields: [],
|
|
|
|
used_fields: ['Project.id', 'Query.project'],
|
|
|
|
variables: '{}'
|
|
|
|
}
|
|
|
|
]
|
|
|
|
end
|
2020-11-24 15:15:51 +05:30
|
|
|
|
|
|
|
before do
|
2021-04-29 21:17:54 +05:30
|
|
|
RequestStore.clear!
|
|
|
|
|
2020-11-24 15:15:51 +05:30
|
|
|
allow(controller).to receive(:append_info_to_payload).and_wrap_original do |method, *|
|
|
|
|
method.call(log_payload)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'appends metadata for logging' do
|
2021-04-29 21:17:54 +05:30
|
|
|
post :execute, params: { _json: graphql_queries }
|
2020-11-24 15:15:51 +05:30
|
|
|
|
|
|
|
expect(controller).to have_received(:append_info_to_payload)
|
2021-04-29 21:17:54 +05:30
|
|
|
expect(log_payload.dig(:metadata, :graphql)).to match_array(expected_logs)
|
2020-11-24 15:15:51 +05:30
|
|
|
end
|
2022-01-26 12:08:38 +05:30
|
|
|
|
|
|
|
it 'appends the exception in case of errors' do
|
|
|
|
exception = StandardError.new('boom')
|
|
|
|
|
|
|
|
expect(controller).to receive(:execute).and_raise(exception)
|
|
|
|
|
|
|
|
post :execute, params: { _json: graphql_queries }
|
|
|
|
|
|
|
|
expect(controller).to have_received(:append_info_to_payload)
|
|
|
|
expect(log_payload.dig(:exception_object)).to eq(exception)
|
|
|
|
end
|
2020-11-24 15:15:51 +05:30
|
|
|
end
|
2018-11-08 19:23:39 +05:30
|
|
|
end
|