lib/rspectacular/plugins/authentication.rb in rspectacular-0.44.0 vs lib/rspectacular/plugins/authentication.rb in rspectacular-0.45.0

- old
+ new

@@ -1,74 +1,70 @@ RSpec.configure do |config| config.around(:each, mock_auth: lambda { |v| !!v }) do |example| - options = example.metadata[:mock_auth] + options = example.metadata[:mock_auth] - klass = case options - when TrueClass - User - when Hash - options[:class] || User - else - options - end + klass = case options + when TrueClass + User + when Hash + options[:class] || User + else + options + end - underscored_class_name = klass. - name[/.*::(\w+)\z/, 1]. - gsub(/([a-z])([A-Z])/, '\1_\2'). - downcase + underscored_class_name = klass. + name[/(?:.*::)?(\w+)\z/, 1]. + gsub(/([a-z])([A-Z])/, '\1_\2'). + downcase - current_class_method = if options.is_a?(Hash) && options[:method] - options[:method] - else - :"current_#{underscored_class_name}" - end + current_class_method = if options.is_a?(Hash) && options[:method] + options[:method] + else + :"current_#{underscored_class_name}" + end - instance = if options.is_a?(Hash) && options[:strategy] == :instance - klass.new - else - FactoryGirl.create(underscored_class_name.to_sym) - end + instance = if options.is_a?(Hash) && options[:strategy] == :instance + klass.new + else + FactoryGirl.create(underscored_class_name.to_sym) + end - inferred_auth_method = if options.is_a?(Hash) && options[:authentication_method] - options[:authentication_method] - else - :"authenticate_#{underscored_class_name}!" - end + inferred_auth_method = if options.is_a?(Hash) && options[:authentication_method] + options[:authentication_method] + else + :"authenticate_#{underscored_class_name}!" + end - described_class_instance = described_class.new + authentication_controller_class = if example.metadata[:type] == :controller + described_class + else + ApplicationController + end - authentication_method = if described_class_instance.respond_to?(inferred_auth_method, true) - inferred_auth_method - elsif described_class_instance.respond_to?(:authenticate, true) - :authenticate - end + authentication_controller_instance = authentication_controller_class.new - if options[:status] == :unauthorized - described_class.send(:define_method, authentication_method) { false } - else - described_class.send(:define_method, authentication_method) { true } - end + 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 - example.example_group_instance.class.let(current_class_method) do - if options[:status] == :unauthorized - nil - else - instance - end - end + authentication_successful = if options.is_a?(Hash) && options.has_key?(:status) + options[:status] == :authorized + else + true + end - described_class.send(:define_method, current_class_method) do - if options[:status] == :unauthorized - nil - else - instance - end - end + authentication_result = authentication_successful ? instance : nil + 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 - described_class.send(:remove_method, current_class_method) - described_class.send(:remove_method, authentication_method) + authentication_controller_class.send(:remove_method, current_class_method) unless options.is_a?(Hash) && options[:strategy] == :instance instance.delete end end