Skip to content

Commit 302f367

Browse files
author
David Revelo
committed
Remove comments, apply suggestions and DRY up specs
1 parent aeb71bf commit 302f367

3 files changed

Lines changed: 35 additions & 54 deletions

File tree

lib/graphql_devise/schema_plugin.rb

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -43,18 +43,16 @@ def set_current_resource(context)
4343
controller = context[:controller]
4444
resource_names = Array(context[:resource_name])
4545

46-
if context[:current_resource].blank?
47-
context[:current_resource] = resource_names.find do |resource_name|
48-
unless Devise.mappings.key?(resource_name)
49-
raise(
50-
GraphqlDevise::Error,
51-
"Invalid resource_name `#{resource_name}` provided to `graphql_context`. Possible values are: #{Devise.mappings.keys}."
52-
)
53-
end
54-
55-
found = controller.set_resource_by_token(resource_name)
56-
break found if found
46+
context[:current_resource] ||= resource_names.find do |resource_name|
47+
unless Devise.mappings.key?(resource_name)
48+
raise(
49+
GraphqlDevise::Error,
50+
"Invalid resource_name `#{resource_name}` provided to `graphql_context`. Possible values are: #{Devise.mappings.keys}."
51+
)
5752
end
53+
54+
found = controller.set_resource_by_token(resource_name)
55+
break found if found
5856
end
5957

6058
context

spec/graphql/user_queries_spec.rb

Lines changed: 25 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,21 @@
1+
# frozen_string_literal: true
12

23
require 'rails_helper'
34

45
RSpec.describe 'Sign Up process' do
56
include_context 'with graphql schema test'
67

7-
let(:schema) { DummySchema }
8-
let(:user) { create(:user, :confirmed) }
8+
let(:schema) { DummySchema }
9+
let(:user) { create(:user, :confirmed) }
10+
let(:field) { 'privateField' }
11+
let(:public_message) { 'Field does not require authentication' }
12+
let(:private_message) { 'Field will always require authentication' }
13+
let(:private_error) do
14+
{
15+
message: "#{field} field requires authentication",
16+
extensions: { code: 'AUTHENTICATION_ERROR' }
17+
}
18+
end
919

1020
describe 'publicField' do
1121
let(:query) do
@@ -18,7 +28,7 @@
1828

1929
context 'when using a regular schema' do
2030
it 'does not require authentication' do
21-
expect(response[:data][:publicField]).to eq('Field does not require authentication')
31+
expect(response[:data][:publicField]).to eq(public_message)
2232
end
2333
end
2434
end
@@ -37,26 +47,21 @@
3747
let(:resource) { user }
3848

3949
it 'allows to perform the query' do
40-
expect(response[:data][:privateField]).to eq('Field will always require authentication')
50+
expect(response[:data][:privateField]).to eq(private_message)
4151
end
4252

4353
context 'when using a SchemaUser' do
4454
let(:resource) { create(:schema_user, :confirmed) }
4555

4656
it 'allows to perform the query' do
47-
expect(response[:data][:privateField]).to eq('Field will always require authentication')
57+
expect(response[:data][:privateField]).to eq(private_message)
4858
end
4959
end
5060
end
5161

5262
context 'when user is not authenticated' do
5363
it 'returns a must sign in error' do
54-
expect(response[:errors]).to contain_exactly(
55-
hash_including(
56-
message: 'privateField field requires authentication',
57-
extensions: { code: 'AUTHENTICATION_ERROR' }
58-
)
59-
)
64+
expect(response[:errors]).to contain_exactly(hash_including(**private_error))
6065
end
6166
end
6267
end
@@ -68,24 +73,20 @@
6873
let(:resource) { user }
6974

7075
it 'allows to perform the query' do
71-
expect(response[:data][:privateField]).to eq('Field will always require authentication')
76+
expect(response[:data][:privateField]).to eq(private_message)
7277
end
7378
end
7479

7580
context 'when user is not authenticated' do
7681
it 'returns a must sign in error' do
77-
expect(response[:errors]).to contain_exactly(
78-
hash_including(
79-
message: 'privateField field requires authentication',
80-
extensions: { code: 'AUTHENTICATION_ERROR' }
81-
)
82-
)
82+
expect(response[:errors]).to contain_exactly(hash_including(**private_error))
8383
end
8484
end
8585
end
8686
end
8787

8888
describe 'user' do
89+
let(:user_data) { { email: user.email, id: user.id } }
8990
let(:query) do
9091
<<-GRAPHQL
9192
query {
@@ -102,21 +103,15 @@
102103
let(:resource) { user }
103104

104105
it 'allows to perform the query' do
105-
expect(response[:data][:user]).to match(
106-
email: user.email,
107-
id: user.id
108-
)
106+
expect(response[:data][:user]).to match(**user_data)
109107
end
110108
end
111109

112110
context 'when user is not authenticated' do
111+
let(:field) { 'user' }
112+
113113
it 'returns a must sign in error' do
114-
expect(response[:errors]).to contain_exactly(
115-
hash_including(
116-
message: 'user field requires authentication',
117-
extensions: { code: 'AUTHENTICATION_ERROR' }
118-
)
119-
)
114+
expect(response[:errors]).to contain_exactly(hash_including(**private_error))
120115
end
121116
end
122117
end
@@ -128,20 +123,14 @@
128123
let(:resource) { user }
129124

130125
it 'allows to perform the query' do
131-
expect(response[:data][:user]).to match(
132-
email: user.email,
133-
id: user.id
134-
)
126+
expect(response[:data][:user]).to match(**user_data)
135127
end
136128
end
137129

138130
context 'when user is not authenticated' do
139131
# Interpreter schema fields are public unless specified otherwise (plugin setting)
140132
it 'allows to perform the query' do
141-
expect(response[:data][:user]).to match(
142-
email: user.email,
143-
id: user.id
144-
)
133+
expect(response[:data][:user]).to match(**user_data)
145134
end
146135
end
147136
end
Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,9 @@
11
RSpec.shared_context 'with graphql schema test' do
22
let(:variables) { {} }
33
let(:resource) { nil }
4-
let(:controller) { instance_double(Api::V1::GraphqlController) }
4+
let(:controller) { instance_double(GraphqlDevise::GraphqlController) }
55
let(:context) { { current_resource: resource, controller: controller } }
66
let(:response) do
77
schema.execute(query, context: context, variables: variables).deep_symbolize_keys
88
end
9-
10-
# before do
11-
# allow_any_instance_of(GraphqlDevise::Mutations::Login).to receive(:set_auth_headers)
12-
# allow(controller).to receive(:request).and_return(request)
13-
# # allow(controller).to receive(:response).and_return(response)
14-
# end
159
end

0 commit comments

Comments
 (0)