Skip to content

Commit

Permalink
Merge pull request #94 from graphql-devise/increase-controller-coverage
Browse files Browse the repository at this point in the history
Increase GraphqlDevise::GraphqlController coverage
  • Loading branch information
mcelicalderon committed May 25, 2020
2 parents aa9ac82 + db84826 commit 49d85a1
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 13 deletions.
1 change: 0 additions & 1 deletion spec/rails_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@
config.include(Requests::JsonHelpers, type: :request)
config.include(Requests::AuthHelpers, type: :request)
config.include(ActiveSupport::Testing::TimeHelpers)
config.include(Generators::FileHelpers, type: :generator)

config.before(:suite) do
ActionController::Base.allow_forgery_protection = true
Expand Down
80 changes: 80 additions & 0 deletions spec/requests/graphql_controller_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
require 'rails_helper'

RSpec.describe GraphqlDevise::GraphqlController do
let(:password) { 'password123' }
let(:user) { create(:user, :confirmed, password: password) }
let(:params) { { query: query, variables: variables } }
let(:request_params) do
if Rails::VERSION::MAJOR >= 5
{ params: params }
else
params
end
end

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 '/api/v1/graphql_auth', request_params

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 '/api/v1/graphql_auth', request_params

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 '/api/v1/graphql_auth', request_params
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 '/api/v1/graphql_auth', request_params

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
end
12 changes: 0 additions & 12 deletions spec/support/generators/file_helpers.rb

This file was deleted.

0 comments on commit 49d85a1

Please sign in to comment.