-
Notifications
You must be signed in to change notification settings - Fork 45
Create sign up mutation #7
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
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 |
|---|---|---|
|
|
@@ -16,3 +16,4 @@ | |
| /*.sqlite3 | ||
| /spec/dummy/db/development.sqlite3 | ||
| /spec/dummy/db/test.sqlite3 | ||
| /*.gem | ||
This file contains hidden or 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 hidden or 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 hidden or 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,61 @@ | ||
| module GraphqlDevise | ||
| module Mutations | ||
| class SignUp < Base | ||
| argument :email, String, required: true | ||
| argument :password, String, required: true | ||
| argument :password_confirmation, String, required: true | ||
| argument :confirm_success_url, String, required: false | ||
| argument :config_name, String, required: false | ||
|
|
||
| def resolve(confirm_success_url: nil, config_name: nil, **attrs) | ||
| resource = resource_class.new(provider: provider, **attrs) | ||
|
|
||
| if resource.present? | ||
| resource.skip_confirmation_notification! if resource.respond_to?(:skip_confirmation_notification!) | ||
|
|
||
| if resource.save | ||
| yield resource if block_given? | ||
|
|
||
| if confirmable_enabled?(resource) && !resource.confirmed? | ||
| # user will require email authentication | ||
| resource.send_confirmation_instructions( | ||
| client_config: config_name, | ||
| redirect_url: confirm_success_url | ||
| ) | ||
| end | ||
|
|
||
| set_auth_headers(resource) if active_for_authentication?(resource) | ||
|
|
||
| { authenticable: resource } | ||
| else | ||
| clean_up_passwords(resource) | ||
| raise_user_error_list( | ||
| I18n.t('graphql_devise.registration_failed'), | ||
| errors: resource.errors.full_messages | ||
| ) | ||
| end | ||
| else | ||
| raise_user_error(I18n.t('graphql_devise.resource_build_failed')) | ||
| end | ||
| end | ||
|
|
||
| protected | ||
|
|
||
| def confirmable_enabled?(resource) | ||
| resource.respond_to?(:confirmed_at) | ||
| end | ||
|
|
||
| def active_for_authentication?(resource) | ||
| resource.active_for_authentication? | ||
| end | ||
|
|
||
| def provider | ||
| end | ||
|
|
||
| def clean_up_passwords(resource) | ||
| controller.send(:clean_up_passwords, resource) | ||
| end | ||
| end | ||
| end | ||
| end | ||
This file contains hidden or 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,15 @@ | ||
| module GraphqlDevise | ||
| module MailerHelper | ||
| def confirmation_query(token:, config:, redirect_url:) | ||
| raw = <<-GRAPHQL | ||
| confirmAccount($token:ID!,$clientConfig:String,redirect:String!){ | ||
| userConfirmAccount(token:$token,clientConfig:$clientConfig,redirect:$redirect | ||
| ){ | ||
| success,errors | ||
| } | ||
| }&variables={token:"#{token}",clientConfig:"#{config}",redirect:"#{redirect_url}"} | ||
| GRAPHQL | ||
| ERB::Util.url_encode(raw.gsub("\n", '').gsub(' ', '')) | ||
| end | ||
| end | ||
| end |
This file contains hidden or 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 @@ | ||
| <p><%= t(:welcome).capitalize + ' ' + @email %>!</p> | ||
|
|
||
| <p><%= t '.confirm_link_msg' %> </p> | ||
|
|
||
| <p><%= link_to t('.confirm_account_link'), api_v1_graphql_auth_url, query: confirmation_query(token: @token, config: message['client-config'].to_s, redirect_url: message['redirect-url']).html_safe %></p> |
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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,3 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| module ActionDispatch::Routing | ||
| class Mapper | ||
| def mount_graphql_devise_for(resource, opts = {}) | ||
|
|
@@ -21,6 +19,7 @@ def mount_graphql_devise_for(resource, opts = {}) | |
| default_mutations = { | ||
| login: GraphqlDevise::Mutations::Login, | ||
| logout: GraphqlDevise::Mutations::Logout, | ||
| sign_up: GraphqlDevise::Mutations::SignUp, | ||
| update_password: GraphqlDevise::Mutations::UpdatePassword | ||
| }.freeze | ||
|
|
||
|
|
@@ -38,6 +37,8 @@ def mount_graphql_devise_for(resource, opts = {}) | |
| GraphqlDevise::Types::MutationType.field("#{mapping_name}_#{action}", mutation: used_mutation) | ||
| end | ||
|
|
||
| Devise.mailer.send(:add_template_helper, GraphqlDevise::MailerHelper) | ||
|
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. There might be an engine way not to need this, but is the method not public?
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. Let me take a look, but it is private doc |
||
|
|
||
| devise_scope mapping_name.to_sym do | ||
| post "#{path}/graphql_auth", to: 'graphql_devise/graphql#auth' | ||
| end | ||
|
|
||
This file contains hidden or 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,12 @@ | ||
| module Mutations | ||
| class SignUp < GraphqlDevise::Mutations::SignUp | ||
| argument :name, String, required: false | ||
|
|
||
| field :user, Types::UserType, null: true | ||
|
|
||
| def resolve(email:, **attrs) | ||
| original_payload = super | ||
| original_payload.merge(user: original_payload[:authenticable]) | ||
| end | ||
| end | ||
| end |
This file contains hidden or 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 hidden or 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,6 @@ | ||
| Rails.application.routes.draw do | ||
| mount_graphql_devise_for 'User', at: 'api/v1', mutations: { | ||
| login: Mutations::Login | ||
| login: Mutations::Login, | ||
| sign_up: Mutations::SignUp | ||
| } | ||
| end |
This file contains hidden or 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,71 @@ | ||
| require 'rails_helper' | ||
|
|
||
| RSpec.describe '' do | ||
| include_context 'with graphql query request' | ||
|
|
||
| let(:name) { Faker::Name.name } | ||
| let(:password) { Faker::Internet.password } | ||
| let(:email) { Faker::Internet.email } | ||
| let(:redirect) { Faker::Internet.url } | ||
| let(:query) do | ||
| <<-GRAPHQL | ||
| mutation { | ||
| userSignUp( | ||
| email: "#{email}" | ||
| name: "#{name}" | ||
| password: "#{password}" | ||
| passwordConfirmation: "#{password}" | ||
| confirmSuccessUrl: "#{redirect}" | ||
| ) { | ||
| user { | ||
| name | ||
| } | ||
| } | ||
| } | ||
| GRAPHQL | ||
| end | ||
|
|
||
| context 'when params are correct' do | ||
| it 'creates a new resource that requires confirmation' do | ||
| expect { post_request }.to( | ||
| change(User, :count).by(1) | ||
| .and(change(ActionMailer::Base.deliveries, :count).by(1)) | ||
| ) | ||
|
|
||
| user = User.last | ||
| expect(user).not_to be_active_for_authentication | ||
| expect(user.confirmed_at).to be_nil | ||
| expect(user.valid_password?(password)).to be_truthy | ||
| expect(json_response[:data][:userSignUp]).to include( | ||
| user: { | ||
| email: email, | ||
| name: name | ||
| } | ||
| ) | ||
|
|
||
| email = ActionMailer::Base.deliveries.last | ||
| query = ERB::Util.url_encode("confirmAccount($token:ID!,$clientConfig:String,redirect:String!){userConfirmAccount(token:$token,clientConfig:$clientConfig,redirect:$redirect){success,errors}}&variables={token:\"#{user.confirmation_token}\",clientConfig:\"default\",redirect:\"#{redirect}\"}").html_safe | ||
| expect(email.body.encoded).to match(/query="#{query}"/) | ||
| end | ||
| end | ||
|
|
||
| context 'when required params are missing' do | ||
| let(:email) { '' } | ||
|
|
||
| it 'does *NOT* create resource a resource nor send an email' do | ||
| expect { post_request }.to( | ||
| not_change(User, :count) | ||
| .and(not_change(ActionMailer::Base.deliveries, :count)) | ||
| ) | ||
|
|
||
| expect(json_response[:data][:userSignUp]).to be_nil | ||
| expect(json_response[:errors]).to containing_exactly( | ||
| hash_including( | ||
| message: "User couldn't be registered", | ||
| extensions: { code: 'USER_ERROR', detailed_errors: ["Email can't be blank"] } | ||
| ) | ||
| ) | ||
| end | ||
| end | ||
| end |
This file contains hidden or 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 @@ | ||
| RSpec::Matchers.define_negated_matcher :not_change, :change |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This one seems redundant when you can call that directly on resource