graphql_controller_spec.rb 1.5 KB
Newer Older
Nick Thomas's avatar
Nick Thomas committed
1 2 3 4
require 'spec_helper'

describe GraphqlController do
  describe 'execute' do
5 6
    let(:user) { nil }

Nick Thomas's avatar
Nick Thomas committed
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
    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
44 45 46 47 48 49 50 51 52

    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
Nick Thomas's avatar
Nick Thomas committed
53 54 55
  end

  # Chosen to exercise all the moving parts in GraphqlController#execute
56
  def run_test_query!(variables: { 'text' => 'test success' })
Nick Thomas's avatar
Nick Thomas committed
57 58 59 60 61 62
    query = <<~QUERY
      query Echo($text: String) {
        echo(text: $text)
      }
    QUERY

63
    post :execute, query: query, operationName: 'Echo', variables: variables
Nick Thomas's avatar
Nick Thomas committed
64 65 66 67 68 69
  end

  def query_response
    json_response['data']
  end
end