Skip to content

Commit 8716767

Browse files
committed
Add missing logout specs
1 parent b5a1438 commit 8716767

6 files changed

Lines changed: 66 additions & 17 deletions

File tree

app/controllers/graphql_devise/graphql_controller.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ def auth
1818
render json: result
1919
end
2020

21+
attr_reader :client_id, :token
22+
2123
private
2224

2325
def execute_params(item)
@@ -26,7 +28,6 @@ def execute_params(item)
2628
variables: ensure_hash(item[:variables]),
2729
context: {
2830
current_resource: @resource,
29-
token: @token,
3031
controller: self,
3132
resource_class: resource_class
3233
}

app/graphql/graphql_devise/mutations/base.rb

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
require 'devise_token_auth/version'
2+
13
module GraphqlDevise
24
module Mutations
35
class Base < GraphQL::Schema::Mutation
@@ -27,8 +29,12 @@ def current_resource
2729
context[:current_resource]
2830
end
2931

30-
def token
31-
context[:token]
32+
def client
33+
if DeviseTokenAuth::VERSION <= '1'
34+
controller.client_id
35+
else
36+
controller.token.client if controller.token.present?
37+
end
3238
end
3339
end
3440
end

app/graphql/graphql_devise/mutations/logout.rb

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,13 @@ class Logout < Base
55
field :errors, [String], null: false
66

77
def resolve
8-
client = token.client if token.client
8+
if current_resource && client && current_resource.tokens[client]
9+
current_resource.tokens.delete(client)
10+
current_resource.save!
911

10-
if current_user && token.client && current_user.tokens[client]
11-
user.tokens.delete(client)
12-
user.save!
12+
yield current_resource if block_given?
1313

14-
yield user if block_given?
15-
16-
{ success: true, errors: [] }
14+
{ success: true, errors: [], authenticable: current_resource }
1715
else
1816
{ success: false, errors: [I18n.t('graphql_devise.user_not_found')] }
1917
end

lib/graphql_devise/rails/routes.rb

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,8 @@ def mount_graphql_devise_for(resource, opts = {})
2929
else
3030
new_mutation = Class.new(mutation)
3131
new_mutation.graphql_name("#{resource}#{action.to_s.titleize}")
32-
new_mutation.field(
33-
:authenticable,
34-
authenticable_type,
35-
null: true
36-
)
32+
new_mutation.field(:authenticable, authenticable_type, null: true)
33+
3734
new_mutation
3835
end
3936

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
require 'rails_helper'
2+
3+
RSpec.describe 'Logout Requests' do
4+
include_context 'with graphql query request'
5+
6+
let(:user) { create(:user, :confirmed) }
7+
let(:query) do
8+
<<-GRAPHQL
9+
mutation {
10+
userLogout{
11+
authenticable { email }
12+
success
13+
errors
14+
}
15+
}
16+
GRAPHQL
17+
end
18+
19+
before { post '/api/v1/graphql_auth', *graphql_params }
20+
21+
context 'when user is logged in' do
22+
let(:headers) { user.create_new_auth_token }
23+
24+
it 'logs out the user' do
25+
expect(response).not_to include_auth_headers
26+
expect(user.reload.tokens.keys).to be_empty
27+
expect(json_response[:data][:userLogout]).to match(
28+
success: true,
29+
errors: [],
30+
authenticable: { email: user.email }
31+
)
32+
end
33+
end
34+
35+
context 'when user is not logged in' do
36+
it 'returns an error' do
37+
expect(response).not_to include_auth_headers
38+
expect(user.reload.tokens.keys).to be_empty
39+
expect(json_response[:data][:userLogout]).to match(
40+
success: false,
41+
errors: ['User was not found or was not logged in.'],
42+
authenticable: nil
43+
)
44+
end
45+
end
46+
end
Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
RSpec.shared_context 'with graphql query request' do
2+
let(:headers) { {} }
23
let(:variables) { {} }
34
let(:graphql_params) do
45
if Rails::VERSION::MAJOR >= 5
5-
[{ params: { query: query, variables: variables } }]
6+
[{ params: { query: query, variables: variables }, headers: headers }]
67
else
7-
[{ query: query, variables: variables }]
8+
[{ query: query, variables: variables }, headers]
89
end
910
end
1011
end

0 commit comments

Comments
 (0)