Skip to content

Commit 2d96400

Browse files
committed
Add missing logout specs
1 parent b5a1438 commit 2d96400

4 files changed

Lines changed: 56 additions & 12 deletions

File tree

app/graphql/graphql_devise/mutations/logout.rb

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,13 @@ class Logout < Base
77
def resolve
88
client = token.client if token.client
99

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

14-
yield user if block_given?
14+
yield current_resource if block_given?
1515

16-
{ success: true, errors: [] }
16+
{ success: true, errors: [], authenticable: current_resource }
1717
else
1818
{ success: false, errors: [I18n.t('graphql_devise.user_not_found')] }
1919
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)