|
| 1 | +require 'rails_helper' |
| 2 | + |
| 3 | +RSpec.describe GraphqlDevise::GraphqlController do |
| 4 | + let(:password) { 'password123' } |
| 5 | + let(:user) { create(:user, :confirmed, password: password) } |
| 6 | + |
| 7 | + context 'when variables are a string' do |
| 8 | + it 'parses the string variables' do |
| 9 | + post '/api/v1/graphql_auth', |
| 10 | + params: { |
| 11 | + query: "mutation($email: String!) { userLogin(email: $email, password: \"#{password}\") { user { email name signInCount } } }", |
| 12 | + variables: "{\"email\": \"#{user.email}\"}" |
| 13 | + |
| 14 | + } |
| 15 | + |
| 16 | + expect(json_response).to match( |
| 17 | + data: { userLogin: { user: { email: user.email, name: user.name, signInCount: 1 } } } |
| 18 | + ) |
| 19 | + end |
| 20 | + end |
| 21 | + |
| 22 | + context 'when variables are not a string or hash' do |
| 23 | + it 'raises an error' do |
| 24 | + expect do |
| 25 | + post '/api/v1/graphql_auth', |
| 26 | + params: { |
| 27 | + query: "mutation($email: String!) { userLogin(email: $email, password: \"#{password}\") { user { email name signInCount } } }", |
| 28 | + variables: 1 |
| 29 | + |
| 30 | + } |
| 31 | + end.to raise_error(ArgumentError) |
| 32 | + end |
| 33 | + end |
| 34 | + |
| 35 | + context 'when multiplexing queries' do |
| 36 | + it 'executes multiple queries in the same request' do |
| 37 | + post '/api/v1/graphql_auth', |
| 38 | + params: { |
| 39 | + _json: [ |
| 40 | + { query: "mutation { userLogin(email: \"#{user.email}\", password: \"#{password}\") { user { email name signInCount } } }" }, |
| 41 | + { query: "mutation { userLogin(email: \"#{user.email}\", password: \"wrong password\") { user { email name signInCount } } }" } |
| 42 | + ] |
| 43 | + } |
| 44 | + |
| 45 | + expect(json_response).to match( |
| 46 | + [ |
| 47 | + { data: { userLogin: { user: { email: user.email, name: user.name, signInCount: 1 } } } }, |
| 48 | + { |
| 49 | + data: { userLogin: nil }, |
| 50 | + errors: [ |
| 51 | + hash_including( |
| 52 | + message: 'Invalid login credentials. Please try again.', extensions: { code: 'USER_ERROR' } |
| 53 | + ) |
| 54 | + ] |
| 55 | + } |
| 56 | + ] |
| 57 | + ) |
| 58 | + end |
| 59 | + end |
| 60 | +end |
0 commit comments