-
Notifications
You must be signed in to change notification settings - Fork 36
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
David Revelo
committed
Feb 8, 2020
1 parent
08f6058
commit 215a94a
Showing
3 changed files
with
128 additions
and
0 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 |
---|---|---|
@@ -0,0 +1,37 @@ | ||
module GraphqlDevise | ||
class OperationSanitizer | ||
def self.call(resource:, default:, custom:, only:, skipped:) | ||
new( | ||
resource: resource, | ||
default: default, | ||
custom: custom, | ||
only: only, | ||
skipped: skipped | ||
).call | ||
end | ||
|
||
def initialize(resource:, default:, custom:, only:, skipped:) | ||
@resource = resource | ||
@mapping = resource.underscore.tr('/', '_').to_sym | ||
@default = default | ||
@custom = custom | ||
@only = only | ||
@skipped = skipped | ||
end | ||
|
||
def call | ||
result = @default | ||
result = result.merge(@custom.slice(*operations_whitelist)) | ||
result = result.slice(*@only) if @only.present? | ||
result = result.except(*@skipped) if @skipped.present? | ||
|
||
result.transform_keys { |k| "#{@mapping}_#{k}".to_sym } | ||
end | ||
|
||
private | ||
|
||
def operations_whitelist | ||
@default.keys | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
require 'rails_helper' | ||
|
||
RSpec.describe GraphqlDevise::OperationSanitizer do | ||
describe '.call' do | ||
subject(:result) do | ||
described_class.call( | ||
resource: resource, | ||
default: default, | ||
custom: custom, | ||
only: only, | ||
skipped: skipped | ||
) | ||
end | ||
|
||
context 'when the operations passed are mutations' do | ||
let(:resource) { 'User' } | ||
let(:custom) { {} } | ||
let(:skipped) { [] } | ||
let(:only) { [] } | ||
let(:default) do | ||
{ | ||
login: GraphqlDevise::Mutations::Login, | ||
logout: GraphqlDevise::Mutations::Logout, | ||
sign_up: GraphqlDevise::Mutations::SignUp, | ||
update_password: GraphqlDevise::Mutations::UpdatePassword, | ||
send_password_reset: GraphqlDevise::Mutations::SendPasswordReset, | ||
resend_confirmation: GraphqlDevise::Mutations::ResendConfirmation | ||
} | ||
end | ||
|
||
context 'when no other option besides default is passed' do | ||
it 'returns all the default operations appending mapping to the key' do | ||
expect(result).to eq( | ||
user_login: GraphqlDevise::Mutations::Login, | ||
user_logout: GraphqlDevise::Mutations::Logout, | ||
user_sign_up: GraphqlDevise::Mutations::SignUp, | ||
user_update_password: GraphqlDevise::Mutations::UpdatePassword, | ||
user_send_password_reset: GraphqlDevise::Mutations::SendPasswordReset, | ||
user_resend_confirmation: GraphqlDevise::Mutations::ResendConfirmation | ||
) | ||
end | ||
end | ||
|
||
context 'when there are custom operations' do | ||
let(:custom) do | ||
{ | ||
login: Mutations::Login, | ||
sign_up: Mutations::SignUp, | ||
query: GraphQL::Schema::Resolver | ||
} | ||
end | ||
|
||
it 'returns the default ops replacing the custom ones appending mapping to the key' do | ||
expect(result).to eq( | ||
user_login: Mutations::Login, | ||
user_logout: GraphqlDevise::Mutations::Logout, | ||
user_sign_up: Mutations::SignUp, | ||
user_update_password: GraphqlDevise::Mutations::UpdatePassword, | ||
user_send_password_reset: GraphqlDevise::Mutations::SendPasswordReset, | ||
user_resend_confirmation: GraphqlDevise::Mutations::ResendConfirmation | ||
) | ||
end | ||
end | ||
|
||
context 'when there are only operations' do | ||
let(:only) { [:sign_up, :update_password] } | ||
|
||
it 'returns only those default ops appending mapping to the key' do | ||
expect(result).to eq( | ||
user_sign_up: GraphqlDevise::Mutations::SignUp, | ||
user_update_password: GraphqlDevise::Mutations::UpdatePassword | ||
) | ||
end | ||
end | ||
|
||
context 'when there are skipped operations' do | ||
let(:skipped) { [:sign_up, :update_password] } | ||
|
||
it 'returns the default ops but the skipped appending mapping to the key' do | ||
expect(result).to eq( | ||
user_login: GraphqlDevise::Mutations::Login, | ||
user_logout: GraphqlDevise::Mutations::Logout, | ||
user_send_password_reset: GraphqlDevise::Mutations::SendPasswordReset, | ||
user_resend_confirmation: GraphqlDevise::Mutations::ResendConfirmation | ||
) | ||
end | ||
end | ||
end | ||
end | ||
end |