Skip to content

Commit 9543b45

Browse files
committed
Increase GraphqlDevise::GraphqlController coverage
1 parent aa9ac82 commit 9543b45

3 files changed

Lines changed: 75 additions & 13 deletions

File tree

spec/rails_helper.rb

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,6 @@
3838
config.include(Requests::JsonHelpers, type: :request)
3939
config.include(Requests::AuthHelpers, type: :request)
4040
config.include(ActiveSupport::Testing::TimeHelpers)
41-
config.include(Generators::FileHelpers, type: :generator)
4241

4342
config.before(:suite) do
4443
ActionController::Base.allow_forgery_protection = true
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
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+
21+
context 'when variables is an empty string' do
22+
it 'returns an empty hash as variables' do
23+
post '/api/v1/graphql_auth',
24+
params: {
25+
query: "mutation { userLogin(email: \"#{user.email}\", password: \"#{password}\") { user { email name signInCount } } }",
26+
variables: ''
27+
28+
}
29+
30+
expect(json_response).to match(
31+
data: { userLogin: { user: { email: user.email, name: user.name, signInCount: 1 } } }
32+
)
33+
end
34+
end
35+
end
36+
37+
context 'when variables are not a string or hash' do
38+
it 'raises an error' do
39+
expect do
40+
post '/api/v1/graphql_auth',
41+
params: {
42+
query: "mutation($email: String!) { userLogin(email: $email, password: \"#{password}\") { user { email name signInCount } } }",
43+
variables: 1
44+
45+
}
46+
end.to raise_error(ArgumentError)
47+
end
48+
end
49+
50+
context 'when multiplexing queries' do
51+
it 'executes multiple queries in the same request' do
52+
post '/api/v1/graphql_auth',
53+
params: {
54+
_json: [
55+
{ query: "mutation { userLogin(email: \"#{user.email}\", password: \"#{password}\") { user { email name signInCount } } }" },
56+
{ query: "mutation { userLogin(email: \"#{user.email}\", password: \"wrong password\") { user { email name signInCount } } }" }
57+
]
58+
}
59+
60+
expect(json_response).to match(
61+
[
62+
{ data: { userLogin: { user: { email: user.email, name: user.name, signInCount: 1 } } } },
63+
{
64+
data: { userLogin: nil },
65+
errors: [
66+
hash_including(
67+
message: 'Invalid login credentials. Please try again.', extensions: { code: 'USER_ERROR' }
68+
)
69+
]
70+
}
71+
]
72+
)
73+
end
74+
end
75+
end

spec/support/generators/file_helpers.rb

Lines changed: 0 additions & 12 deletions
This file was deleted.

0 commit comments

Comments
 (0)