lib/rspeckled/plugins/authentication.rb in rspeckled-0.0.51 vs lib/rspeckled/plugins/authentication.rb in rspeckled-0.0.52

- old
+ new

@@ -1,128 +1,100 @@ # frozen_string_literal: true +# rubocop:disable Layout/IndentHash RSpec.configure do |config| config.around(:each, :mock_auth => lambda { |v| !!v }) do |example| - options = example.metadata[:mock_auth] - - authentication_type = if options.is_a?(Hash) && options[:type] - options[:type] + options = if example.metadata[:mock_auth].is_a?(Hash) + example.metadata[:mock_auth] else - :json_web_token + { + :token => { + :roles => example.metadata[:mock_auth], + }, + } end - klass = case options - when TrueClass - User - when Hash - options[:class] || described_class - .name[/(.*?)::/, 1] - .concat('::User') - .constantize - else - options - end + options[:class] ||= described_class + .name[/\A([^:]+)::/, 1] + .concat('::User') + .constantize - underscored_class_name = klass + class_name_underscored = options[:class] .name[/(?:.*::)?(\w+)\z/, 1] .gsub(/([a-z])([A-Z])/, '\1_\2') .downcase - current_class_method = if options.is_a?(Hash) && options[:method] - options[:method] || :current_user - else - :"current_#{underscored_class_name}" - end + defaults = { + :type => :json_web_token, + :authentication_method => :"authenticate_#{class_name_underscored}!", + :class_instance_overrides => {}, + :class_instance_traits => {}, + :method => :"current_#{class_name_underscored}", + :strategy => :factory, + :successful? => true, + :token => { + :roles => 'standard', + }, + } - class_instance_overrides = if options.is_a?(Hash) && options[:class_instance_overrides] - options[:class_instance_overrides] - else - {} - end + options = defaults.deep_merge(options) - class_instance_traits = if options.is_a?(Hash) && options[:class_instance_traits] - options[:class_instance_traits] - else - {} + instance = case options[:strategy] + when :factory + FactoryBot.create(class_name_underscored.to_sym, *options[:class_instance_traits], options[:class_instance_overrides]) + when :instance + options[:class].new(options[:class_instance_overrides]) end - instance = if options.is_a?(Hash) && options[:strategy] == :instance - klass.new(class_instance_overrides) - else - FactoryBot.create(underscored_class_name.to_sym, *class_instance_traits, class_instance_overrides) - end + authentication_result = options[:successful?] ? instance : nil - inferred_auth_method = if options.is_a?(Hash) && options[:authentication_method] + case options[:type] + when :standard + authentication_controller_class = (example.metadata[:type] == :controller) ? described_class : ApplicationController + authentication_controller_instance = authentication_controller_class.new + authentication_method = if authentication_controller_instance.respond_to?(options[:authentication_method], true) options[:authentication_method] - else - :"authenticate_#{underscored_class_name}!" + elsif authentication_controller_instance.respond_to?(:authenticate, true) + :authenticate end - authentication_controller_class = if example.metadata[:type] == :controller - described_class - else - ApplicationController - end + authentication_controller_class.__send__(:define_method, authentication_method) { options[:successful?] } + authentication_controller_class.__send__(:define_method, options[:method]) { authentication_result } + authentication_controller_class.__send__(:helper_method, options[:method]) + example.example_group_instance.class.let(options[:method]) { authentication_result } - authentication_controller_instance = authentication_controller_class.new - - authentication_successful = if options.is_a?(Hash) && options.has_key?(:status) - options[:status] == :authorized - else - true - end - - authentication_result = authentication_successful ? instance : nil - - if authentication_type == :standard - authentication_method = if authentication_controller_instance.respond_to?(inferred_auth_method, true) - inferred_auth_method - elsif authentication_controller_instance.respond_to?(:authenticate, true) - :authenticate - end - - authentication_controller_class.__send__(:define_method, authentication_method) { authentication_successful } - authentication_controller_class.__send__(:define_method, current_class_method) { authentication_result } - authentication_controller_class.__send__(:helper_method, current_class_method) - example.example_group_instance.class.let(current_class_method) { authentication_result } - example.run - authentication_controller_class.__send__(:remove_method, current_class_method) - elsif authentication_type == :json_web_token - @token_data = if options.is_a?(Hash) && options[:data] - options[:data] - else - [ - { - 'aid' => options[:audience_id] || instance['account_id'] || instance['id'], - 'aud' => options[:audience] || instance.class.name, - 'exp' => options[:expired_at] || 1.day.from_now.utc.to_i, - 'iat' => Time.now.utc.to_i, - 'iss' => options[:issuer] || 'rspeckled', - 'jti' => SecureRandom.uuid, - 'nbf' => 1.day.ago.utc.to_i, - 'rol' => options[:roles] || 'standard', - 'sid' => options[:subject_id], - 'sub' => options[:subject], - }, - { - 'typ' => 'JWT', - 'cty' => 'application/json-web-token', - }, - ] - end + authentication_controller_class.__send__(:remove_method, options[:method]) + when :json_web_token + @token_data = [ + { + 'aid' => options[:token][:audience_id] || instance['account_id'] || instance['id'], + 'aud' => options[:token][:audience] || instance.class.name, + 'exp' => options[:token][:expired_at] || 1.day.from_now.utc.to_i, + 'iat' => options[:token][:issued_at] || Time.now.utc.to_i, + 'iss' => options[:token][:issuer] || 'rspeckled', + 'jti' => options[:token][:token_id] || SecureRandom.uuid, + 'nbf' => options[:token][:not_before] || 1.day.ago.utc.to_i, + 'rol' => options[:token][:roles] || 'standard', + 'sid' => options[:token][:subject_id], + 'sub' => options[:token][:subject], + }, + { + 'typ' => 'JWT', + 'cty' => 'application/json-web-token', + }, + ] - example.example_group_instance.class.let(current_class_method) { authentication_result } + example.example_group_instance.class.let(options[:method]) { authentication_result } example.run - else - fail ArgumentError, 'You must specify a valid type for the :mock_auth metadata' end - instance.delete unless options.is_a?(Hash) && options[:strategy] == :instance + instance.delete unless options[:strategy] == :instance end config.before(:each, :mock_auth => lambda { |v| !!v }) do |_example| request.env['X_JSON_WEB_TOKEN'] = @token_data end end +# rubocop:enable Layout/IndentHash