-
Notifications
You must be signed in to change notification settings - Fork 45
Added ResendConfirmation GraphQL method. #35
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 14 commits
d6324c4
898a652
7e27c65
ac9c1ab
ee76d39
1e09349
083570f
9ee3fbc
537e3f2
2236216
d1539bf
53e7bd4
0ced80c
4e83a8b
58014a4
247819e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,32 @@ | ||
| module GraphqlDevise | ||
| module Mutations | ||
| class ResendConfirmation < Base | ||
| argument :email, String, required: true, prepare: ->(email, _) { email.downcase } | ||
| argument :redirect_url, String, required: true | ||
|
|
||
| field :message, String, null: false | ||
|
aarona marked this conversation as resolved.
|
||
|
|
||
| def resolve(email:, redirect_url:) | ||
| resource = controller.find_resource(:uid, email) | ||
|
|
||
| if resource | ||
| yield resource if block_given? | ||
|
|
||
| raise_user_error("Email #{I18n.t('errors.messages.already_confirmed')}") if resource.confirmed? | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why interpolate a localized string here? Just put the entire string in the locales file please
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The reason why I did this is because it seemed you wanted to fall back on Devise locales. The errors.message.already_confirmed value is:
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. yes please, move the entire message to our own locales file. I think the rule for localized messages should be that ANY message we use directly in OUR code, should go in our locales file. Otherwise we depend on the other gems to keep those keys on their locales files, if for any reason a version of those gems changes the messages, it could affect our gem. I'll have to check the rest of the code for the same thing, but for this PR, please make sure any localized message YOU use in the code has an entry in our locales file (the entire message always as some languages might have a completely different way to say something)
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes check the I'll fix mine and update this PR.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Actually it looks like both mailers do this. I'll create a PR for both later. |
||
|
|
||
| resource.send_confirmation_instructions({ | ||
| redirect_url: redirect_url, | ||
| template_path: ['graphql_devise/mailer'] | ||
| }) | ||
|
|
||
| { | ||
| authenticable: resource, | ||
| message: I18n.t('devise.confirmations.send_instructions', email: email) | ||
| } | ||
|
aarona marked this conversation as resolved.
|
||
| else | ||
| raise_user_error(I18n.t('graphql_devise.confirmations.user_not_found', email: email)) | ||
| end | ||
| end | ||
| end | ||
| end | ||
| end | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,5 @@ | ||
| <p><%= t(:welcome).capitalize + ' ' + @email %>!</p> | ||
|
|
||
| <p><%= t '.confirm_link_msg' %> </p> | ||
| <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> |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,83 @@ | ||
| require 'rails_helper' | ||
|
|
||
| RSpec.describe 'Resend confirmation' do | ||
| include_context 'with graphql query request' | ||
|
|
||
| let(:user) { create(:user, confirmed_at: nil) } | ||
| let(:email) { user.email } | ||
| let(:id) { user.id } | ||
| let(:redirect) { Faker::Internet.url } | ||
| let(:query) do | ||
| <<-GRAPHQL | ||
| mutation { | ||
| userResendConfirmation( | ||
| email:"#{email}", | ||
| redirectUrl:"#{redirect}" | ||
| ) { | ||
| message | ||
| authenticable { | ||
| id | ||
| } | ||
| } | ||
| } | ||
| GRAPHQL | ||
| 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 | ||
|
aarona marked this conversation as resolved.
Outdated
|
||
| expect(json_response[:data][:userResendConfirmation]).to include( | ||
| authenticable: { | ||
| id: id, | ||
| email: email | ||
| }, | ||
| 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(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) | ||
| end | ||
|
|
||
| context 'when the user has already been confirmed' do | ||
| before { user.confirm } | ||
|
|
||
| it 'does *NOT* send an email and raises an error' do | ||
| expect { post_request }.to change(ActionMailer::Base.deliveries, :count).by 0 | ||
|
aarona marked this conversation as resolved.
Outdated
|
||
| expect(json_response[:data][:userResendConfirmation]).to be_nil | ||
| expect(json_response[:errors]).to contain_exactly( | ||
| hash_including( | ||
| message: "Email was already confirmed, please try signing in", | ||
| extensions: { code: 'USER_ERROR' } | ||
| ) | ||
| ) | ||
| end | ||
| end | ||
| end | ||
|
|
||
| context "when the email isn't in the system" do | ||
| let(:email) { 'nothere@gmail.com' } | ||
|
|
||
| it 'does *NOT* send an email and raises an error' do | ||
| expect { post_request }.to not_change(ActionMailer::Base.deliveries, :count) | ||
| expect(json_response[:data][:userResendConfirmation]).to be_nil | ||
| expect(json_response[:errors]).to contain_exactly( | ||
| hash_including( | ||
| message: "Unable to find user with email '#{email}'.", | ||
| extensions: { code: 'USER_ERROR' } | ||
| ) | ||
| ) | ||
| end | ||
| end | ||
| end | ||
Uh oh!
There was an error while loading. Please reload this page.