Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
17 changed files
with
226 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,15 @@ | ||
require 'graphql_devise/model/with_email_updater' | ||
|
||
module GraphqlDevise | ||
module Concerns | ||
Model = DeviseTokenAuth::Concerns::User | ||
|
||
Model.module_eval do | ||
attr_accessor :confirmation_url, :confirmation_success_url | ||
|
||
def update_with_email(attributes = {}) | ||
GraphqlDevise::Model::WithEmailUpdater.new(self, attributes).call | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
module GraphqlDevise | ||
module Model | ||
class WithEmailUpdater | ||
def initialize(resource, attributes) | ||
@attributes = attributes | ||
@resource = resource | ||
end | ||
|
||
def call | ||
resource_attributes = @attributes.except(:schema_url, :confirmation_success_url) | ||
|
||
if resource_attributes.key?(:email) && @resource.respond_to?(:unconfirmed_email=) | ||
unless @attributes[:schema_url].present? && (@attributes[:confirmation_success_url].present? || DeviseTokenAuth.default_confirm_success_url.present?) | ||
raise( | ||
GraphqlDevise::Error, | ||
'Method `update_with_email` requires attributes `confirmation_success_url` and `schema_url` for email reconfirmation to work' | ||
) | ||
end | ||
|
||
@resource.assign_attributes(resource_attributes) | ||
return false unless @resource.valid? | ||
|
||
@resource.unconfirmed_email = @resource.email | ||
@resource.confirmation_token = nil | ||
@resource.email = if Devise.activerecord51? | ||
@resource.email_in_database | ||
else | ||
@resource.email_was | ||
end | ||
@resource.send(:generate_confirmation_token) | ||
|
||
saved = @resource.save | ||
|
||
if saved | ||
@resource.send_confirmation_instructions( | ||
redirect_url: @attributes[:confirmation_success_url] || DeviseTokenAuth.default_confirm_success_url, | ||
template_path: ['graphql_devise/mailer'], | ||
schema_url: @attributes[:schema_url] | ||
) | ||
end | ||
|
||
saved | ||
else | ||
@resource.update(resource_attributes) | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
module Mutations | ||
class UpdateUser < GraphQL::Schema::Mutation | ||
field :user, Types::UserType, null: false | ||
|
||
argument :email, String, required: false | ||
argument :name, String, required: false | ||
|
||
def resolve(**attrs) | ||
user = context[:current_resource] | ||
|
||
schema_url = context[:controller].full_url_without_params | ||
|
||
user.update_with_email( | ||
attrs.merge(schema_url: schema_url, confirmation_success_url: 'https://google.com') | ||
) | ||
|
||
{ user: user } | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
5 changes: 5 additions & 0 deletions
5
spec/dummy/db/migrate/20200621182414_remove_uncofirmed_email_from_admins.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
class RemoveUncofirmedEmailFromAdmins < ActiveRecord::Migration[6.0] | ||
def change | ||
remove_column :admins, :unconfirmed_email, :string | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
require 'rails_helper' | ||
|
||
RSpec.describe GraphqlDevise::Model::WithEmailUpdater do | ||
describe '#call' do | ||
subject(:updater) { described_class.new(resource, attributes).call } | ||
|
||
context 'when the model does not have an unconfirmed_email column' do | ||
let(:resource) { create(:admin, :confirmed) } | ||
|
||
context 'when attributes contain email' do | ||
let(:attributes) { { email: 'new@gmail.com', schema_url: 'http://localhost/test', confirmation_success_url: 'https://google.com' } } | ||
|
||
it 'does not postpone email update' do | ||
expect do | ||
updater | ||
resource.reload | ||
end.to change(resource, :email).from(resource.email).to('new@gmail.com').and( | ||
change(resource, :uid).from(resource.uid).to('new@gmail.com') | ||
) | ||
end | ||
end | ||
end | ||
|
||
context 'when the model has an unconfirmed_email column' do | ||
let(:resource) { create(:user, :confirmed) } | ||
|
||
context 'when attributes do not contain email' do | ||
let(:attributes) { { name: 'Updated Name', schema_url: 'http://localhost/test', confirmation_success_url: 'https://google.com' } } | ||
|
||
it 'updates resource, ignores url params' do | ||
expect do | ||
updater | ||
resource.reload | ||
end.to change(resource, :name).from(resource.name).to('Updated Name') | ||
end | ||
end | ||
|
||
context 'when attributes contain email' do | ||
context 'when schema_url is missing' do | ||
let(:attributes) { { email: 'new@gmail.com', name: 'Updated Name' } } | ||
|
||
it 'raises an error' do | ||
expect { updater }.to raise_error( | ||
GraphqlDevise::Error, | ||
'Method `update_with_email` requires attributes `confirmation_success_url` and `schema_url` for email reconfirmation to work' | ||
) | ||
end | ||
end | ||
|
||
context 'when only confirmation_success_url is missing' do | ||
let(:attributes) { { email: 'new@gmail.com', name: 'Updated Name', schema_url: 'http://localhost/test' } } | ||
|
||
it 'uses DTA default_confirm_success_url on the email' do | ||
expect { updater }.to change(ActionMailer::Base.deliveries, :count).by(1) | ||
|
||
email = ActionMailer::Base.deliveries.first | ||
expect(email.body.decoded).to include(CGI.escape('https://google.com')) | ||
end | ||
end | ||
|
||
context 'when both required urls are provided' do | ||
let(:attributes) { { email: 'new@gmail.com', name: 'Updated Name', schema_url: 'http://localhost/test', confirmation_success_url: 'https://google.com' } } | ||
|
||
it 'postpones email update' do | ||
expect do | ||
updater | ||
resource.reload | ||
end.to not_change(resource, :email).from(resource.email).and( | ||
not_change(resource, :uid).from(resource.uid) | ||
).and( | ||
change(resource, :unconfirmed_email).from(nil).to('new@gmail.com') | ||
).and( | ||
change(resource, :name).from(resource.name).to('Updated Name') | ||
) | ||
end | ||
|
||
it 'sends out a confirmation email to the unconfirmed_email' do | ||
expect { updater }.to change(ActionMailer::Base.deliveries, :count).by(1) | ||
|
||
email = ActionMailer::Base.deliveries.first | ||
expect(email.to).to contain_exactly('new@gmail.com') | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters