Skip to content

Commit af0af51

Browse files
author
David Revelo
committed
Avoid returning user information on password reset mutation
1 parent 56c7e58 commit af0af51

12 files changed

Lines changed: 70 additions & 31 deletions

File tree

lib/graphql_devise/default_operations/mutations.rb

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,13 @@
99
module GraphqlDevise
1010
module DefaultOperations
1111
MUTATIONS = {
12-
login: GraphqlDevise::Mutations::Login,
13-
logout: GraphqlDevise::Mutations::Logout,
14-
sign_up: GraphqlDevise::Mutations::SignUp,
15-
update_password: GraphqlDevise::Mutations::UpdatePassword,
16-
send_password_reset: GraphqlDevise::Mutations::SendPasswordReset,
17-
resend_confirmation: GraphqlDevise::Mutations::ResendConfirmation
12+
login: { klass: GraphqlDevise::Mutations::Login, authenticable: true },
13+
logout: { klass: GraphqlDevise::Mutations::Logout, authenticable: true },
14+
sign_up: { klass: GraphqlDevise::Mutations::SignUp, authenticable: true },
15+
update_password: { klass: GraphqlDevise::Mutations::UpdatePassword, authenticable: true },
16+
send_password_reset: { klass: GraphqlDevise::Mutations::SendPasswordReset, authenticable: true },
17+
resend_confirmation: { klass: GraphqlDevise::Mutations::ResendConfirmation, authenticable: true }
18+
1819
}.freeze
1920
end
2021
end

lib/graphql_devise/default_operations/resolvers.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55
module GraphqlDevise
66
module DefaultOperations
77
QUERIES = {
8-
confirm_account: GraphqlDevise::Resolvers::ConfirmAccount,
9-
check_password_token: GraphqlDevise::Resolvers::CheckPasswordToken
8+
confirm_account: { klass: GraphqlDevise::Resolvers::ConfirmAccount },
9+
check_password_token: { klass: GraphqlDevise::Resolvers::CheckPasswordToken }
1010
}.freeze
1111
end
1212
end

lib/graphql_devise/mount_method/operation_preparers/default_operation_preparer.rb

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,18 @@ def initialize(selected_operations:, custom_keys:, mapping_name:, preparer:)
1010
end
1111

1212
def call
13-
@selected_operations.except(*@custom_keys).each_with_object({}) do |(action, operation), result|
13+
@selected_operations.except(*@custom_keys).each_with_object({}) do |(action, operation_info), result|
1414
mapped_action = "#{@mapping_name}_#{action}"
15+
operation = operation_info[:klass]
16+
options = operation_info.except(:klass)
1517

1618
result[mapped_action.to_sym] = [
1719
OperationPreparers::GqlNameSetter.new(mapped_action),
1820
@preparer,
1921
OperationPreparers::ResourceNameSetter.new(@mapping_name)
20-
].reduce(child_class(operation)) { |prepared_operation, preparer| preparer.call(prepared_operation) }
22+
].reduce(child_class(operation)) do |prepared_operation, preparer|
23+
preparer.call(prepared_operation, **options)
24+
end
2125
end
2226
end
2327

lib/graphql_devise/mount_method/operation_preparers/gql_name_setter.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ def initialize(mapping_name)
66
@mapping_name = mapping_name
77
end
88

9-
def call(operation)
9+
def call(operation, **)
1010
operation.graphql_name(graphql_name)
1111

1212
operation

lib/graphql_devise/mount_method/operation_preparers/mutation_field_setter.rb

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,10 @@ def initialize(authenticatable_type)
66
@authenticatable_type = authenticatable_type
77
end
88

9-
def call(mutation)
10-
mutation.field(:authenticatable, @authenticatable_type, null: false)
9+
def call(mutation, authenticable: true)
10+
return unless authenticable
1111

12+
mutation.field(:authenticatable, @authenticatable_type, null: false)
1213
mutation
1314
end
1415
end

lib/graphql_devise/mount_method/operation_preparers/resolver_type_setter.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ def initialize(authenticatable_type)
66
@authenticatable_type = authenticatable_type
77
end
88

9-
def call(resolver)
9+
def call(resolver, **)
1010
resolver.type(@authenticatable_type, null: false)
1111

1212
resolver

lib/graphql_devise/mount_method/operation_preparers/resource_name_setter.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ def initialize(name)
66
@name = name
77
end
88

9-
def call(operation)
9+
def call(operation, **)
1010
operation.instance_variable_set(:@resource_name, @name)
1111

1212
operation

lib/graphql_devise/mutations/send_password_reset.rb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ class SendPasswordReset < Base
44
argument :email, String, required: true
55
argument :redirect_url, String, required: true
66

7+
field :message, String, null: false
8+
79
def resolve(email:, redirect_url:)
810
resource = find_resource(:email, get_case_insensitive_field(:email, email))
911

@@ -18,7 +20,7 @@ def resolve(email:, redirect_url:)
1820
)
1921

2022
if resource.errors.empty?
21-
{ authenticatable: resource }
23+
{ message: I18n.t('devise.passwords.send_instructions') }
2224
else
2325
raise_user_error_list(I18n.t('graphql_devise.invalid_resource'), errors: resource.errors.full_messages)
2426
end

spec/requests/mutations/send_password_reset_spec.rb

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,7 @@
1313
email: "#{email}",
1414
redirectUrl: "#{redirect_url}"
1515
) {
16-
authenticatable {
17-
email
18-
}
16+
message
1917
}
2018
}
2119
GRAPHQL
@@ -25,6 +23,10 @@
2523
it 'sends password reset email' do
2624
expect { post_request }.to change(ActionMailer::Base.deliveries, :count).by(1)
2725

26+
expect(json_response.dig(:data, :userSendPasswordReset)).to include(
27+
message: 'You will receive an email with instructions on how to reset your password in a few minutes.'
28+
)
29+
2830
email = Nokogiri::HTML(ActionMailer::Base.deliveries.last.body.encoded)
2931
link = email.css('a').first
3032

@@ -41,6 +43,9 @@
4143

4244
it 'honors devise configuration for case insensitive fields' do
4345
expect { post_request }.to change(ActionMailer::Base.deliveries, :count).by(1)
46+
expect(json_response.dig(:data, :userSendPasswordReset)).to include(
47+
message: 'You will receive an email with instructions on how to reset your password in a few minutes.'
48+
)
4449
end
4550
end
4651

spec/services/mount_method/operation_preparer_spec.rb

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,15 @@
1414

1515
let(:logout_class) { Class.new(GraphQL::Schema::Resolver) }
1616
let(:mapping) { :user }
17-
let(:selected) { { login: double(:login_default), logout: logout_class } }
1817
let(:preparer) { double(:preparer, call: logout_class) }
1918
let(:custom) { { login: double(:custom_login, graphql_name: nil) } }
2019
let(:additional) { { user_additional: double(:user_additional) } }
20+
let(:selected) do
21+
{
22+
login: { klass: double(:login_default) },
23+
logout:{ klass: logout_class }
24+
}
25+
end
2126

2227
it 'is expected to return all provided operation keys' do
2328
expect(prepared_operations.keys).to contain_exactly(

0 commit comments

Comments
 (0)