Skip to content

Commit

Permalink
Merge e6865a5 into 766a7d2
Browse files Browse the repository at this point in the history
  • Loading branch information
00dav00 committed Jun 17, 2020
2 parents 766a7d2 + e6865a5 commit 8129eaf
Show file tree
Hide file tree
Showing 11 changed files with 133 additions and 37 deletions.
Expand Up @@ -2,4 +2,4 @@

<p><%= t('.confirm_link_msg') %></p>

<p><%= link_to t('.confirm_account_link'), url_for(controller: 'graphql_devise/graphql', action: :auth, **confirmation_query(resource_name: @resource.class.to_s, redirect_url: message['redirect-url'], token: @token)) %></p>
<p><%= link_to t('.confirm_account_link'), url_for(controller: message['controller'], action: message['action'], **confirmation_query(resource_name: @resource.class.to_s, redirect_url: message['redirect-url'], token: @token)) %></p>
Expand Up @@ -2,7 +2,7 @@

<p><%= t('.request_reset_link_msg') %></p>

<p><%= link_to t('.password_change_link'), url_for(controller: 'graphql_devise/graphql', action: :auth, **password_reset_query(token: @token, redirect_url: message['redirect-url'], resource_name: @resource.class.to_s)) %></p>
<p><%= link_to t('.password_change_link'), url_for(controller: message['controller'], action: message['action'], **password_reset_query(token: @token, redirect_url: message['redirect-url'], resource_name: @resource.class.to_s)) %></p>

<p><%= t('.ignore_mail_msg') %></p>
<p><%= t('.no_changes_msg') %></p>
3 changes: 2 additions & 1 deletion lib/graphql_devise/mutations/resend_confirmation.rb
Expand Up @@ -19,7 +19,8 @@ def resolve(email:, redirect_url:)

resource.send_confirmation_instructions(
redirect_url: redirect_url,
template_path: ['graphql_devise/mailer']
template_path: ['graphql_devise/mailer'],
**controller.params.permit('controller', 'action').to_h.symbolize_keys
)

{ message: I18n.t('graphql_devise.confirmations.send_instructions', email: email) }
Expand Down
3 changes: 2 additions & 1 deletion lib/graphql_devise/mutations/send_password_reset.rb
Expand Up @@ -16,7 +16,8 @@ def resolve(email:, redirect_url:)
email: email,
provider: 'email',
redirect_url: redirect_url,
template_path: ['graphql_devise/mailer']
template_path: ['graphql_devise/mailer'],
**controller.params.permit('controller', 'action').to_h.symbolize_keys
)

if resource.errors.empty?
Expand Down
3 changes: 2 additions & 1 deletion lib/graphql_devise/mutations/sign_up.rb
Expand Up @@ -27,7 +27,8 @@ def resolve(confirm_success_url: nil, **attrs)
unless resource.confirmed?
resource.send_confirmation_instructions(
redirect_url: confirm_success_url,
template_path: ['graphql_devise/mailer']
template_path: ['graphql_devise/mailer'],
**controller.params.permit('controller', 'action').to_h.symbolize_keys
)
end

Expand Down
31 changes: 29 additions & 2 deletions spec/dummy/app/controllers/api/v1/graphql_controller.rb
Expand Up @@ -4,11 +4,13 @@ class GraphqlController < ApplicationController
include GraphqlDevise::Concerns::SetUserByToken

def graphql
render json: DummySchema.execute(params[:query], context: graphql_context(:user))
result = DummySchema.execute(params[:query], execute_params(params))

render json: result unless performed?
end

def interpreter
render json: InterpreterSchema.execute(params[:query], context: graphql_context(:user))
render json: InterpreterSchema.execute(params[:query], execute_params(params))
end

def failing_resource_name
Expand All @@ -17,6 +19,31 @@ def failing_resource_name

private

def execute_params(item)
{
operation_name: item[:operationName],
variables: ensure_hash(item[:variables]),
context: graphql_context(:user)
}
end

def ensure_hash(ambiguous_param)
case ambiguous_param
when String
if ambiguous_param.present?
ensure_hash(JSON.parse(ambiguous_param))
else
{}
end
when Hash, ActionController::Parameters
ambiguous_param
when nil
{}
else
raise ArgumentError, "Unexpected parameter: #{ambiguous_param}"
end
end

def verify_authenticity_token
end
end
Expand Down
11 changes: 10 additions & 1 deletion spec/dummy/app/graphql/dummy_schema.rb
Expand Up @@ -3,7 +3,16 @@ class DummySchema < GraphQL::Schema
query: Types::QueryType,
mutation: Types::MutationType,
resource_loaders: [
GraphqlDevise::ResourceLoader.new('User', only: [:login, :confirm_account]),
GraphqlDevise::ResourceLoader.new(
'User',
only: [
:login,
:confirm_account,
:send_password_reset,
:resend_confirmation,
:check_password_token
]
),
GraphqlDevise::ResourceLoader.new('Guest', only: [:logout])
]
)
Expand Down
1 change: 1 addition & 0 deletions spec/dummy/config/routes.rb
Expand Up @@ -27,6 +27,7 @@
at: '/api/v1/user_customer/graphql_auth'
)

get '/api/v1/graphql', to: 'api/v1/graphql#graphql'
post '/api/v1/graphql', to: 'api/v1/graphql#graphql'
post '/api/v1/interpreter', to: 'api/v1/graphql#interpreter'
post '/api/v1/failing', to: 'api/v1/graphql#failing_resource_name'
Expand Down
59 changes: 43 additions & 16 deletions spec/requests/mutations/resend_confirmation_spec.rb
Expand Up @@ -21,25 +21,52 @@
end

context 'when params are correct' do
it 'sends an email to the user with confirmation url and returns a success message' do
expect { post_request }.to change(ActionMailer::Base.deliveries, :count).by(1)
expect(json_response[:data][:userResendConfirmation]).to include(
message: 'You will receive an email with instructions for how to confirm your email address in a few minutes.'
)
context 'when using the gem schema' do
it 'sends an email to the user with confirmation url and returns a success message' do
expect { post_request }.to change(ActionMailer::Base.deliveries, :count).by(1)
expect(json_response[:data][:userResendConfirmation]).to include(
message: 'You will receive an email with instructions for how to confirm your email address in a few minutes.'
)

email = Nokogiri::HTML(ActionMailer::Base.deliveries.last.body.encoded)
link = email.css('a').first
confirm_link_msg_text = email.css('p')[1].inner_html
confirm_account_link_text = link.inner_html

expect(link['href']).to include('/api/v1/graphql_auth?')
expect(confirm_link_msg_text).to eq('You can confirm your account email through the link below:')
expect(confirm_account_link_text).to eq('Confirm my account')

expect do
get link['href']
user.reload
end.to change(user, :confirmed_at).from(NilClass).to(ActiveSupport::TimeWithZone)
end
end

context 'when using a custom schema' do
let(:custom_path) { '/api/v1/graphql' }

it 'sends an email to the user with confirmation url and returns a success message' do
expect { post_request(custom_path) }.to change(ActionMailer::Base.deliveries, :count).by(1)
expect(json_response[:data][:userResendConfirmation]).to include(
message: 'You will receive an email with instructions for how to confirm your email address in a few minutes.'
)

email = Nokogiri::HTML(ActionMailer::Base.deliveries.last.body.encoded)
link = email.css('a').first
confirm_link_msg_text = email.css('p')[1].inner_html
confirm_account_link_text = link.inner_html
email = Nokogiri::HTML(ActionMailer::Base.deliveries.last.body.encoded)
link = email.css('a').first
confirm_link_msg_text = email.css('p')[1].inner_html
confirm_account_link_text = link.inner_html

expect(confirm_link_msg_text).to eq('You can confirm your account email through the link below:')
expect(confirm_account_link_text).to eq('Confirm my account')
expect(link['href']).to include("#{custom_path}?")
expect(confirm_link_msg_text).to eq('You can confirm your account email through the link below:')
expect(confirm_account_link_text).to eq('Confirm my account')

# TODO: Move to feature spec
expect do
get link['href']
user.reload
end.to change(user, :confirmed_at).from(NilClass).to(ActiveSupport::TimeWithZone)
expect do
get link['href']
user.reload
end.to change(user, :confirmed_at).from(NilClass).to(ActiveSupport::TimeWithZone)
end
end

context 'when email address uses different casing' do
Expand Down
47 changes: 35 additions & 12 deletions spec/requests/mutations/send_password_reset_spec.rb
Expand Up @@ -20,21 +20,44 @@
end

context 'when params are correct' do
it 'sends password reset email' do
expect { post_request }.to change(ActionMailer::Base.deliveries, :count).by(1)
context 'when using the gem schema' do
it 'sends password reset email' do
expect { post_request }.to change(ActionMailer::Base.deliveries, :count).by(1)

expect(json_response[:data][:userSendPasswordReset]).to include(
message: 'You will receive an email with instructions on how to reset your password in a few minutes.'
)
expect(json_response[:data][:userSendPasswordReset]).to include(
message: 'You will receive an email with instructions on how to reset your password in a few minutes.'
)

email = Nokogiri::HTML(ActionMailer::Base.deliveries.last.body.encoded)
link = email.css('a').first
expect(link['href']).to include('/api/v1/graphql_auth?')

expect do
get link['href']
user.reload
end.to change(user, :allow_password_change).from(false).to(true)
end
end

context 'when using a custom schema' do
let(:custom_path) { '/api/v1/graphql' }

it 'sends password reset email' do
expect { post_request(custom_path) }.to change(ActionMailer::Base.deliveries, :count).by(1)

expect(json_response[:data][:userSendPasswordReset]).to include(
message: 'You will receive an email with instructions on how to reset your password in a few minutes.'
)

email = Nokogiri::HTML(ActionMailer::Base.deliveries.last.body.encoded)
link = email.css('a').first
email = Nokogiri::HTML(ActionMailer::Base.deliveries.last.body.encoded)
link = email.css('a').first
expect(link['href']).to include("#{custom_path}?")

# TODO: Move to feature spec
expect do
get link['href']
user.reload
end.to change(user, :allow_password_change).from(false).to(true)
expect do
get link['href']
user.reload
end.to change(user, :allow_password_change).from(false).to(true)
end
end
end

Expand Down
8 changes: 7 additions & 1 deletion spec/requests/queries/confirm_account_spec.rb
Expand Up @@ -22,7 +22,13 @@
context 'when confirmation token is correct' do
let(:token) { user.confirmation_token }

before { user.send_confirmation_instructions(template_path: ['graphql_devise/mailer']) }
before do
user.send_confirmation_instructions(
template_path: ['graphql_devise/mailer'],
controller: 'graphql_devise/graphql',
action: 'auth'
)
end

it 'confirms the resource and redirects to the sent url' do
expect do
Expand Down

0 comments on commit 8129eaf

Please sign in to comment.