Skip to content

Commit

Permalink
Add operation sanitizer class
Browse files Browse the repository at this point in the history
  • Loading branch information
David Revelo committed Feb 8, 2020
1 parent 08f6058 commit 215a94a
Show file tree
Hide file tree
Showing 3 changed files with 128 additions and 0 deletions.
1 change: 1 addition & 0 deletions lib/graphql_devise.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
require 'graphql_devise/error_codes'
require 'graphql_devise/user_error'
require 'graphql_devise/detailed_user_error'
require 'graphql_devise/rails/operation_sanitizer'
require 'graphql_devise/concerns/controller_methods'

module GraphqlDevise
Expand Down
37 changes: 37 additions & 0 deletions lib/graphql_devise/rails/operation_sanitizer.rb
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
90 changes: 90 additions & 0 deletions spec/operation_sanitizer_spec.rb
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

0 comments on commit 215a94a

Please sign in to comment.