2017-08-16 09:04:41 -04:00
|
|
|
require 'spec_helper'
|
|
|
|
|
|
|
|
describe GraphqlController do
|
|
|
|
describe 'execute' do
|
2018-05-21 07:42:07 -04:00
|
|
|
let(:user) { nil }
|
|
|
|
|
2017-08-16 09:04:41 -04:00
|
|
|
before do
|
|
|
|
sign_in(user) if user
|
|
|
|
|
|
|
|
run_test_query!
|
|
|
|
end
|
|
|
|
|
|
|
|
subject { query_response }
|
|
|
|
|
|
|
|
context 'graphql is disabled by feature flag' do
|
|
|
|
let(:user) { nil }
|
|
|
|
|
|
|
|
before do
|
|
|
|
stub_feature_flags(graphql: false)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns 404' do
|
|
|
|
run_test_query!
|
|
|
|
|
|
|
|
expect(response).to have_gitlab_http_status(404)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'signed out' do
|
|
|
|
let(:user) { nil }
|
|
|
|
|
|
|
|
it 'runs the query with current_user: nil' do
|
|
|
|
is_expected.to eq('echo' => 'nil says: test success')
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'signed in' do
|
|
|
|
let(:user) { create(:user, username: 'Simon') }
|
|
|
|
|
|
|
|
it 'runs the query with current_user set' do
|
|
|
|
is_expected.to eq('echo' => '"Simon" says: test success')
|
|
|
|
end
|
|
|
|
end
|
2018-05-21 07:42:07 -04:00
|
|
|
|
|
|
|
context 'invalid variables' do
|
|
|
|
it 'returns an error' do
|
|
|
|
run_test_query!(variables: "This is not JSON")
|
|
|
|
|
|
|
|
expect(response).to have_gitlab_http_status(422)
|
|
|
|
expect(json_response['errors'].first['message']).not_to be_nil
|
|
|
|
end
|
|
|
|
end
|
2017-08-16 09:04:41 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
# Chosen to exercise all the moving parts in GraphqlController#execute
|
2018-05-21 07:42:07 -04:00
|
|
|
def run_test_query!(variables: { 'text' => 'test success' })
|
2017-08-16 09:04:41 -04:00
|
|
|
query = <<~QUERY
|
|
|
|
query Echo($text: String) {
|
|
|
|
echo(text: $text)
|
|
|
|
}
|
|
|
|
QUERY
|
|
|
|
|
2018-05-21 07:42:07 -04:00
|
|
|
post :execute, query: query, operationName: 'Echo', variables: variables
|
2017-08-16 09:04:41 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def query_response
|
|
|
|
json_response['data']
|
|
|
|
end
|
|
|
|
end
|