-
Notifications
You must be signed in to change notification settings - Fork 42
/
graphql_controller_spec.rb
83 lines (69 loc) · 2.61 KB
/
graphql_controller_spec.rb
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
# frozen_string_literal: true
require 'rails_helper'
RSpec.describe GraphqlDevise::GraphqlController do
let(:password) { 'password123' }
let(:user) { create(:user, :confirmed, password: password) }
let(:params) { { query: query, variables: variables } }
context 'when variables are a string' do
let(:variables) { "{\"email\": \"#{user.email}\"}" }
let(:query) { "mutation($email: String!) { userLogin(email: $email, password: \"#{password}\") { user { email name signInCount } } }" }
it 'parses the string variables' do
post_request('/api/v1/graphql_auth')
expect(json_response).to match(
data: { userLogin: { user: { email: user.email, name: user.name, signInCount: 1 } } }
)
end
context 'when variables is an empty string' do
let(:variables) { '' }
let(:query) { "mutation { userLogin(email: \"#{user.email}\", password: \"#{password}\") { user { email name signInCount } } }" }
it 'returns an empty hash as variables' do
post_request('/api/v1/graphql_auth')
expect(json_response).to match(
data: { userLogin: { user: { email: user.email, name: user.name, signInCount: 1 } } }
)
end
end
end
context 'when variables are not a string or hash' do
let(:variables) { 1 }
let(:query) { "mutation($email: String!) { userLogin(email: $email, password: \"#{password}\") { user { email name signInCount } } }" }
it 'raises an error' do
expect do
post_request('/api/v1/graphql_auth')
end.to raise_error(ArgumentError)
end
end
context 'when multiplexing queries' do
let(:params) do
{
_json: [
{ query: "mutation { userLogin(email: \"#{user.email}\", password: \"#{password}\") { user { email name signInCount } } }" },
{ query: "mutation { userLogin(email: \"#{user.email}\", password: \"wrong password\") { user { email name signInCount } } }" }
]
}
end
it 'executes multiple queries in the same request' do
post_request('/api/v1/graphql_auth')
expect(json_response).to match(
[
{ data: { userLogin: { user: { email: user.email, name: user.name, signInCount: 1 } } } },
{
data: { userLogin: nil },
errors: [
hash_including(
message: 'Invalid login credentials. Please try again.', extensions: { code: 'USER_ERROR' }
)
]
}
]
)
end
end
def post_request(path)
if Rails::VERSION::MAJOR >= 5
post(path, params: params)
else
post(path, params)
end
end
end