From 0b4eed72772eb721450e3d7d9708d6cfca14e6ef Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adri=C3=A1n?= Date: Mon, 21 Oct 2024 11:40:47 +0200 Subject: [PATCH] Initial commit --- .rubocop.yml | 19 ++ Gemfile | 19 ++ Gemfile.lock | 259 ++++++++++++++++++ Rakefile | 8 + config/application.rb | 3 + config/initializers/doorkeeper.rb | 14 + config/routes.rb | 6 + helios-auth.gemspec | 31 +++ lib/helios_auth.rb | 33 +++ lib/helios_auth/configuration.rb | 14 + lib/helios_auth/controller_additions.rb | 50 ++++ .../doorkeeper/authorizations_controller.rb | 35 +++ lib/helios_auth/doorkeeper_configuration.rb | 58 ++++ lib/helios_auth/model_additions.rb | 42 +++ .../oauth/authorizations_controller.rb | 25 ++ lib/helios_auth/railtie.rb | 17 ++ lib/helios_auth/version.rb | 5 + spec/examples.txt | 6 + spec/helios_auth/configuration_spec.rb | 26 ++ spec/helios_auth_spec.rb | 18 ++ spec/rails_helper.rb | 7 + spec/spec_helper.rb | 28 ++ 22 files changed, 723 insertions(+) create mode 100644 .rubocop.yml create mode 100644 Gemfile create mode 100644 Gemfile.lock create mode 100644 Rakefile create mode 100644 config/application.rb create mode 100644 config/initializers/doorkeeper.rb create mode 100644 config/routes.rb create mode 100644 helios-auth.gemspec create mode 100644 lib/helios_auth.rb create mode 100644 lib/helios_auth/configuration.rb create mode 100644 lib/helios_auth/controller_additions.rb create mode 100644 lib/helios_auth/doorkeeper/authorizations_controller.rb create mode 100644 lib/helios_auth/doorkeeper_configuration.rb create mode 100644 lib/helios_auth/model_additions.rb create mode 100644 lib/helios_auth/oauth/authorizations_controller.rb create mode 100644 lib/helios_auth/railtie.rb create mode 100644 lib/helios_auth/version.rb create mode 100644 spec/examples.txt create mode 100644 spec/helios_auth/configuration_spec.rb create mode 100644 spec/helios_auth_spec.rb create mode 100644 spec/rails_helper.rb create mode 100644 spec/spec_helper.rb diff --git a/.rubocop.yml b/.rubocop.yml new file mode 100644 index 0000000..3f6efcb --- /dev/null +++ b/.rubocop.yml @@ -0,0 +1,19 @@ +require: + - rubocop-performance + - rubocop-rspec + - rubocop-rake + +AllCops: + NewCops: enable + TargetRubyVersion: 3.0 + Exclude: + - 'spec/dummy/db/schema.rb' + - 'spec/dummy/config/**/*' + - 'bin/rails' + - 'Guardfile' + +Style/Documentation: + Enabled: false + +Metrics/MethodLength: + Max: 20 diff --git a/Gemfile b/Gemfile new file mode 100644 index 0000000..c235a45 --- /dev/null +++ b/Gemfile @@ -0,0 +1,19 @@ +# frozen_string_literal: true + +source 'https://rubygems.org' + +gemspec + +gem 'activesupport', '>= 5.0' +gem 'doorkeeper', '~> 5.6.9' +gem 'rails', '>= 6.0' +gem 'railties', '>= 5.0' +gem 'rake', '~> 13.0' + +group :development, :test do + gem 'rspec', '~> 3.10' + gem 'rubocop', '~> 1.67' + gem 'rubocop-performance', '~> 1.22.1' + gem 'rubocop-rake', '~> 0.6.0' + gem 'rubocop-rspec', '~> 3.1.0' +end diff --git a/Gemfile.lock b/Gemfile.lock new file mode 100644 index 0000000..115ce0a --- /dev/null +++ b/Gemfile.lock @@ -0,0 +1,259 @@ +PATH + remote: . + specs: + helios-auth (0.1.0) + activesupport (>= 5.0) + doorkeeper (~> 5.6.9) + rails (>= 6.0) + railties (>= 5.0) + +GEM + remote: https://rubygems.org/ + specs: + actioncable (7.1.4) + actionpack (= 7.1.4) + activesupport (= 7.1.4) + nio4r (~> 2.0) + websocket-driver (>= 0.6.1) + zeitwerk (~> 2.6) + actionmailbox (7.1.4) + actionpack (= 7.1.4) + activejob (= 7.1.4) + activerecord (= 7.1.4) + activestorage (= 7.1.4) + activesupport (= 7.1.4) + mail (>= 2.7.1) + net-imap + net-pop + net-smtp + actionmailer (7.1.4) + actionpack (= 7.1.4) + actionview (= 7.1.4) + activejob (= 7.1.4) + activesupport (= 7.1.4) + mail (~> 2.5, >= 2.5.4) + net-imap + net-pop + net-smtp + rails-dom-testing (~> 2.2) + actionpack (7.1.4) + actionview (= 7.1.4) + activesupport (= 7.1.4) + nokogiri (>= 1.8.5) + racc + rack (>= 2.2.4) + rack-session (>= 1.0.1) + rack-test (>= 0.6.3) + rails-dom-testing (~> 2.2) + rails-html-sanitizer (~> 1.6) + actiontext (7.1.4) + actionpack (= 7.1.4) + activerecord (= 7.1.4) + activestorage (= 7.1.4) + activesupport (= 7.1.4) + globalid (>= 0.6.0) + nokogiri (>= 1.8.5) + actionview (7.1.4) + activesupport (= 7.1.4) + builder (~> 3.1) + erubi (~> 1.11) + rails-dom-testing (~> 2.2) + rails-html-sanitizer (~> 1.6) + activejob (7.1.4) + activesupport (= 7.1.4) + globalid (>= 0.3.6) + activemodel (7.1.4) + activesupport (= 7.1.4) + activerecord (7.1.4) + activemodel (= 7.1.4) + activesupport (= 7.1.4) + timeout (>= 0.4.0) + activestorage (7.1.4) + actionpack (= 7.1.4) + activejob (= 7.1.4) + activerecord (= 7.1.4) + activesupport (= 7.1.4) + marcel (~> 1.0) + activesupport (7.1.4) + base64 + bigdecimal + concurrent-ruby (~> 1.0, >= 1.0.2) + connection_pool (>= 2.2.5) + drb + i18n (>= 1.6, < 2) + minitest (>= 5.1) + mutex_m + tzinfo (~> 2.0) + ast (2.4.2) + base64 (0.2.0) + bigdecimal (3.1.8) + builder (3.3.0) + concurrent-ruby (1.3.4) + connection_pool (2.4.1) + crass (1.0.6) + date (3.3.4) + diff-lcs (1.5.1) + doorkeeper (5.6.9) + railties (>= 5) + drb (2.2.1) + erubi (1.13.0) + globalid (1.2.1) + activesupport (>= 6.1) + i18n (1.14.6) + concurrent-ruby (~> 1.0) + io-console (0.7.2) + irb (1.14.1) + rdoc (>= 4.0.0) + reline (>= 0.4.2) + json (2.7.2) + language_server-protocol (3.17.0.3) + loofah (2.22.0) + crass (~> 1.0.2) + nokogiri (>= 1.12.0) + mail (2.8.1) + mini_mime (>= 0.1.1) + net-imap + net-pop + net-smtp + marcel (1.0.4) + mini_mime (1.1.5) + mini_portile2 (2.8.7) + minitest (5.25.1) + mutex_m (0.2.0) + net-imap (0.4.16) + date + net-protocol + net-pop (0.1.2) + net-protocol + net-protocol (0.2.2) + timeout + net-smtp (0.5.0) + net-protocol + nio4r (2.7.3) + nokogiri (1.16.7) + mini_portile2 (~> 2.8.2) + racc (~> 1.4) + parallel (1.26.3) + parser (3.3.5.0) + ast (~> 2.4.1) + racc + psych (5.1.2) + stringio + racc (1.8.1) + rack (3.1.7) + rack-session (2.0.0) + rack (>= 3.0.0) + rack-test (2.1.0) + rack (>= 1.3) + rackup (2.1.0) + rack (>= 3) + webrick (~> 1.8) + rails (7.1.4) + actioncable (= 7.1.4) + actionmailbox (= 7.1.4) + actionmailer (= 7.1.4) + actionpack (= 7.1.4) + actiontext (= 7.1.4) + actionview (= 7.1.4) + activejob (= 7.1.4) + activemodel (= 7.1.4) + activerecord (= 7.1.4) + activestorage (= 7.1.4) + activesupport (= 7.1.4) + bundler (>= 1.15.0) + railties (= 7.1.4) + rails-dom-testing (2.2.0) + activesupport (>= 5.0.0) + minitest + nokogiri (>= 1.6) + rails-html-sanitizer (1.6.0) + loofah (~> 2.21) + nokogiri (~> 1.14) + railties (7.1.4) + actionpack (= 7.1.4) + activesupport (= 7.1.4) + irb + rackup (>= 1.0.0) + rake (>= 12.2) + thor (~> 1.0, >= 1.2.2) + zeitwerk (~> 2.6) + rainbow (3.1.1) + rake (13.2.1) + rdoc (6.7.0) + psych (>= 4.0.0) + regexp_parser (2.9.2) + reline (0.5.10) + io-console (~> 0.5) + rspec (3.13.0) + rspec-core (~> 3.13.0) + rspec-expectations (~> 3.13.0) + rspec-mocks (~> 3.13.0) + rspec-core (3.13.0) + rspec-support (~> 3.13.0) + rspec-expectations (3.13.0) + diff-lcs (>= 1.2.0, < 2.0) + rspec-support (~> 3.13.0) + rspec-mocks (3.13.0) + diff-lcs (>= 1.2.0, < 2.0) + rspec-support (~> 3.13.0) + rspec-rails (7.0.1) + actionpack (>= 7.0) + activesupport (>= 7.0) + railties (>= 7.0) + rspec-core (~> 3.13) + rspec-expectations (~> 3.13) + rspec-mocks (~> 3.13) + rspec-support (~> 3.13) + rspec-support (3.13.1) + rubocop (1.67.0) + json (~> 2.3) + language_server-protocol (>= 3.17.0) + parallel (~> 1.10) + parser (>= 3.3.0.2) + rainbow (>= 2.2.2, < 4.0) + regexp_parser (>= 2.4, < 3.0) + rubocop-ast (>= 1.32.2, < 2.0) + ruby-progressbar (~> 1.7) + unicode-display_width (>= 2.4.0, < 3.0) + rubocop-ast (1.32.3) + parser (>= 3.3.1.0) + rubocop-performance (1.22.1) + rubocop (>= 1.48.1, < 2.0) + rubocop-ast (>= 1.31.1, < 2.0) + rubocop-rake (0.6.0) + rubocop (~> 1.0) + rubocop-rspec (3.1.0) + rubocop (~> 1.61) + ruby-progressbar (1.13.0) + stringio (3.1.1) + thor (1.3.2) + timeout (0.4.1) + tzinfo (2.0.6) + concurrent-ruby (~> 1.0) + unicode-display_width (2.6.0) + webrick (1.8.2) + websocket-driver (0.7.6) + websocket-extensions (>= 0.1.0) + websocket-extensions (0.1.5) + zeitwerk (2.6.18) + +PLATFORMS + ruby + +DEPENDENCIES + activesupport (>= 5.0) + bundler (~> 2.0) + doorkeeper (~> 5.6.9) + helios-auth! + rails (>= 6.0) + railties (>= 5.0) + rake (~> 13.0) + rspec (~> 3.10) + rspec-rails + rubocop (~> 1.67) + rubocop-performance (~> 1.22.1) + rubocop-rake (~> 0.6.0) + rubocop-rspec (~> 3.1.0) + +BUNDLED WITH + 2.5.17 diff --git a/Rakefile b/Rakefile new file mode 100644 index 0000000..82bb534 --- /dev/null +++ b/Rakefile @@ -0,0 +1,8 @@ +# frozen_string_literal: true + +require 'bundler/gem_tasks' +require 'rspec/core/rake_task' + +RSpec::Core::RakeTask.new(:spec) + +task default: :spec diff --git a/config/application.rb b/config/application.rb new file mode 100644 index 0000000..edae1b4 --- /dev/null +++ b/config/application.rb @@ -0,0 +1,3 @@ +# frozen_string_literal: true + +require 'doorkeeper' diff --git a/config/initializers/doorkeeper.rb b/config/initializers/doorkeeper.rb new file mode 100644 index 0000000..0d09298 --- /dev/null +++ b/config/initializers/doorkeeper.rb @@ -0,0 +1,14 @@ +# frozen_string_literal: true + +Doorkeeper.configure do + orm :active_record + + resource_owner_authenticator do + # Implementa aquí la lógica para autenticar al propietario del recurso + raise 'Por favor, implementa resource_owner_authenticator en config/initializers/doorkeeper.rb' + end + + base_controller 'ActionController::Base' + + # Agrega cualquier otra configuración necesaria para Doorkeeper aquí +end diff --git a/config/routes.rb b/config/routes.rb new file mode 100644 index 0000000..0cc3fd5 --- /dev/null +++ b/config/routes.rb @@ -0,0 +1,6 @@ +# frozen_string_literal: true + +Rails.application.routes.draw do + use_doorkeeper + # Resto de tus rutas... +end diff --git a/helios-auth.gemspec b/helios-auth.gemspec new file mode 100644 index 0000000..84a133d --- /dev/null +++ b/helios-auth.gemspec @@ -0,0 +1,31 @@ +# frozen_string_literal: true + +$LOAD_PATH.push File.expand_path('lib', __dir__) + +require 'helios_auth/version' + +Gem::Specification.new do |spec| + spec.name = 'helios-auth' + spec.version = HeliosAuth::VERSION + spec.authors = ['Your Name'] + spec.email = ['your.email@example.com'] + + spec.summary = 'Short summary of your gem' + spec.description = 'Longer description of your gem' + spec.homepage = 'https://github.com/bookingsync/helios-auth' + spec.license = 'MIT' + + spec.required_ruby_version = '>= 3.0' + + spec.files = Dir.chdir(File.expand_path(__dir__)) do + `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) } + end + spec.require_paths = ['lib'] + + spec.add_dependency 'activesupport', '>= 5.0' + spec.add_dependency 'doorkeeper', '~> 5.6.9' + spec.add_dependency 'rails', '>= 6.0' + spec.add_dependency 'railties', '>= 5.0' + + spec.metadata['rubygems_mfa_required'] = 'true' +end diff --git a/lib/helios_auth.rb b/lib/helios_auth.rb new file mode 100644 index 0000000..ad0640c --- /dev/null +++ b/lib/helios_auth.rb @@ -0,0 +1,33 @@ +# frozen_string_literal: true + +require 'rails' +require 'active_support/all' +require 'action_controller/railtie' +require 'doorkeeper' +require 'doorkeeper/rails/routes' +require 'helios_auth/doorkeeper_configuration' +require 'active_support' +require 'active_support/core_ext/module/attribute_accessors' +require 'helios_auth/version' +require 'helios_auth/configuration' +require 'helios_auth/controller_additions' +require 'helios_auth/model_additions' +require 'helios_auth/railtie' if defined?(Rails) +require 'helios_auth/oauth/authorizations_controller' +require 'helios_auth/doorkeeper/authorizations_controller' + +module HeliosAuth + class << self + def configuration + @configuration ||= Configuration.new + end + + def configure + yield(configuration) + end + + def reset_configuration! + @configuration = Configuration.new + end + end +end diff --git a/lib/helios_auth/configuration.rb b/lib/helios_auth/configuration.rb new file mode 100644 index 0000000..719ff99 --- /dev/null +++ b/lib/helios_auth/configuration.rb @@ -0,0 +1,14 @@ +# frozen_string_literal: true + +module HeliosAuth + class Configuration + attr_accessor :account_model, :legacy_authenticate_method, :doorkeeper_authorize_method + + def initialize(account_model: 'Account', legacy_authenticate_method: :authenticate_account!, + doorkeeper_authorize_method: :doorkeeper_authorize!) + @account_model = account_model + @legacy_authenticate_method = legacy_authenticate_method + @doorkeeper_authorize_method = doorkeeper_authorize_method + end + end +end diff --git a/lib/helios_auth/controller_additions.rb b/lib/helios_auth/controller_additions.rb new file mode 100644 index 0000000..cfab46f --- /dev/null +++ b/lib/helios_auth/controller_additions.rb @@ -0,0 +1,50 @@ +# frozen_string_literal: true + +module HeliosAuth + module ControllerAdditions + def self.included(base) + base.class_eval do + before_action :api_authenticate_account! + end + end + + # rubocop:disable Metrics/AbcSize + def api_authenticate_account! + session[:_bookingsync_account_id] = nil + token = request.headers['HTTP_AUTHORIZATION'].to_s.gsub('Bearer ', '') + + legacy_authenticate_method = HeliosAuth.configuration.legacy_authenticate_method + account_model = HeliosAuth.configuration.account_model.constantize + + return send(legacy_authenticate_method) if token.blank? + + account = account_model.from_token(token) + + if account.present? + session[:_bookingsync_account_id] = account.id + session[:current_account] = account + session[:account_id] = account.id + + true + else + session[:_bookingsync_account_id] = params[:_bookingsync_account_id] + session[:current_account] = params[:_bookingsync_account_id] + session[:account_id] = params[:_bookingsync_account_id] + + legacy_authenticate_method + end + end + # rubocop:enable Metrics/AbcSize + + def current_account + token = request.headers['HTTP_AUTHORIZATION'].to_s.gsub('Bearer ', '') + + return super if token.blank? + + public_send(HeliosAuth.configuration.doorkeeper_authorize_method) + + account_model = HeliosAuth.configuration.account_model.constantize + account_model.from_token(token) || super + end + end +end diff --git a/lib/helios_auth/doorkeeper/authorizations_controller.rb b/lib/helios_auth/doorkeeper/authorizations_controller.rb new file mode 100644 index 0000000..ca3783c --- /dev/null +++ b/lib/helios_auth/doorkeeper/authorizations_controller.rb @@ -0,0 +1,35 @@ +# frozen_string_literal: true + +require 'doorkeeper' + +module HeliosAuth + module Doorkeeper + class AuthorizationsController < ActionController::Base + before_action :allow_iframe + before_action :allow_bookingsync_iframe + + def allow_iframe + response.headers.except! 'X-Frame-Options' + response.headers['Content-Security-Policy'] = + "frame-ancestors 'self' * localhost *.local #{ENV.fetch('BOOKINGSYNC_URL', nil)} localhost:4000" + end + + def create + render json: auth.body, status: auth.status + end + + private + + def auth + @auth ||= ::Doorkeeper::OAuth::AuthorizationCodeRequest.new( + Doorkeeper.configuration, + grant_type: params[:grant_type], + client_id: params[:client_id], + client_secret: params[:client_secret], + redirect_uri: params[:redirect_uri], + code: params[:code] + ) + end + end + end +end diff --git a/lib/helios_auth/doorkeeper_configuration.rb b/lib/helios_auth/doorkeeper_configuration.rb new file mode 100644 index 0000000..8ef33c7 --- /dev/null +++ b/lib/helios_auth/doorkeeper_configuration.rb @@ -0,0 +1,58 @@ +# frozen_string_literal: true + +Doorkeeper.configure do + orm :active_record + + resource_owner_authenticator do + # if user is logged on the app use current_account, otherwise request auth from core + Account.first + end + + authorization_code_expires_in 10.minutes + access_token_expires_in 2.hours + + base_controller 'ApplicationController' + + use_refresh_token + reuse_access_token + + default_scopes :public + + enable_application_owner confirmation: false + grant_flows %w[authorization_code implicit password client_credentials] + + # bsa app requires user authorization. UI app doesn't require to be authorized again by user + skip_authorization do + true + end +end + +# Doorkeeper only uses resource_owner with password authentication +# This patch is to allow to use it with token authentication +module Doorkeeper + module Models + module ResourceOwnerable + extend ActiveSupport::Concern + + module ClassMethods + def by_resource_owner(resource_owner) + if Doorkeeper.configuration.polymorphic_resource_owner? + where(resource_owner: resource_owner) + else + where(resource_owner_id: resource_owner_id_for(resource_owner)) + end + end + + protected + + def resource_owner_id_for(resource_owner) + if resource_owner.respond_to?(:to_key) + resource_owner.uid + else + resource_owner + end + end + end + end + end +end diff --git a/lib/helios_auth/model_additions.rb b/lib/helios_auth/model_additions.rb new file mode 100644 index 0000000..31296d4 --- /dev/null +++ b/lib/helios_auth/model_additions.rb @@ -0,0 +1,42 @@ +# frozen_string_literal: true + +require 'doorkeeper' + +module HeliosAuth + module ModelAdditions + def self.included(base) + base.extend(ClassMethods) + end + + module ClassMethods + def acts_as_helios_auth_account + has_many :access_grants, + class_name: '::Doorkeeper::AccessGrant', + foreign_key: :resource_owner_id, + dependent: :delete_all + + has_many :access_tokens, + class_name: '::Doorkeeper::AccessToken', + foreign_key: :resource_owner_id, + dependent: :delete_all + + extend SingletonMethods + end + end + + module SingletonMethods + def from_token(token) + access_token = ::Doorkeeper::AccessToken + .where('revoked_at IS NULL AND (expires_in IS NULL) OR ' \ + '((extract(epoch FROM created_at) + expires_in) >= extract(epoch FROM NOW()))') + .where(token: token) + .last + + return unless access_token + + account_model = HeliosAuth.configuration.account_model.constantize + account_model.find_by(uid: access_token.resource_owner_id) + end + end + end +end diff --git a/lib/helios_auth/oauth/authorizations_controller.rb b/lib/helios_auth/oauth/authorizations_controller.rb new file mode 100644 index 0000000..88ef040 --- /dev/null +++ b/lib/helios_auth/oauth/authorizations_controller.rb @@ -0,0 +1,25 @@ +# frozen_string_literal: true + +require 'doorkeeper' + +module HeliosAuth + module Oauth + class AuthorizationsController < ::ActionController::Base + include Doorkeeper::Helpers::Controller + + before_action :allow_iframe + + def allow_iframe + response.headers.except! 'X-Frame-Options' + response.headers['Content-Security-Policy'] = + "frame-ancestors 'self' * localhost *.local #{ENV.fetch('BOOKINGSYNC_URL', nil)} localhost:4000" + + allow_bookingsync_iframe if defined?(:allow_bookingsync_iframe) + end + + def create + render json: auth.body, status: auth.status + end + end + end +end diff --git a/lib/helios_auth/railtie.rb b/lib/helios_auth/railtie.rb new file mode 100644 index 0000000..6fcefac --- /dev/null +++ b/lib/helios_auth/railtie.rb @@ -0,0 +1,17 @@ +# frozen_string_literal: true + +module HeliosAuth + class Railtie < Rails::Railtie + initializer 'helios_auth.controller_additions' do + ActiveSupport.on_load :action_controller do + include HeliosAuth::ControllerAdditions + end + end + + initializer 'helios_auth.model_additions' do + ActiveSupport.on_load :active_record do + include HeliosAuth::ModelAdditions + end + end + end +end diff --git a/lib/helios_auth/version.rb b/lib/helios_auth/version.rb new file mode 100644 index 0000000..92dfc22 --- /dev/null +++ b/lib/helios_auth/version.rb @@ -0,0 +1,5 @@ +# frozen_string_literal: true + +module HeliosAuth + VERSION = '0.1.0' +end diff --git a/spec/examples.txt b/spec/examples.txt new file mode 100644 index 0000000..ae79aaa --- /dev/null +++ b/spec/examples.txt @@ -0,0 +1,6 @@ +example_id | status | run_time | +--------------------------------- | ------ | --------------- | +./spec/aphrodite_spec.rb[1:1] | passed | 0.00036 seconds | +./spec/aphrodite_spec.rb[1:2:1] | passed | 0.0003 seconds | +./spec/configuration_spec.rb[1:1] | passed | 0.00003 seconds | +./spec/configuration_spec.rb[1:2] | passed | 0.00003 seconds | diff --git a/spec/helios_auth/configuration_spec.rb b/spec/helios_auth/configuration_spec.rb new file mode 100644 index 0000000..77a7e52 --- /dev/null +++ b/spec/helios_auth/configuration_spec.rb @@ -0,0 +1,26 @@ +# frozen_string_literal: true + +require 'rails_helper' + +RSpec.describe HeliosAuth::Configuration do + let(:config) { described_class.new } + + it 'has default value for account model' do + expect(config.account_model).to eq('Account') + end + + it 'has default value for legacy authenticate method' do + expect(config.legacy_authenticate_method).to eq(:authenticate_account!) + end + + it 'allows setting custom value for user' do + config.account_model = 'User' + expect(config.account_model).to eq('User') + end + + it 'allows setting custom value for legacy authenticate method' do + config.legacy_authenticate_method = :custom_auth + + expect(config.legacy_authenticate_method).to eq(:custom_auth) + end +end diff --git a/spec/helios_auth_spec.rb b/spec/helios_auth_spec.rb new file mode 100644 index 0000000..57a521b --- /dev/null +++ b/spec/helios_auth_spec.rb @@ -0,0 +1,18 @@ +# frozen_string_literal: true + +require 'rails_helper' + +RSpec.describe HeliosAuth do + it 'has a version number' do + expect(described_class::VERSION).not_to be_nil + end + + describe '.configure' do + it 'allows configuration of account_model' do + described_class.configure do |config| + config.account_model = 'User' + end + expect(described_class.configuration.account_model).to eq('User') + end + end +end diff --git a/spec/rails_helper.rb b/spec/rails_helper.rb new file mode 100644 index 0000000..600db4a --- /dev/null +++ b/spec/rails_helper.rb @@ -0,0 +1,7 @@ +# frozen_string_literal: true + +require 'spec_helper' + +ENV['RAILS_ENV'] ||= 'test' +require 'rspec/rails' +require 'doorkeeper/rake' diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb new file mode 100644 index 0000000..fec7c81 --- /dev/null +++ b/spec/spec_helper.rb @@ -0,0 +1,28 @@ +# frozen_string_literal: true + +require 'bundler/setup' +require 'active_support' +require 'active_support/core_ext/module/attribute_accessors' +require 'helios-auth' + +RSpec.configure do |config| + config.expect_with :rspec do |expectations| + expectations.include_chain_clauses_in_custom_matcher_descriptions = true + end + + config.mock_with :rspec do |mocks| + mocks.verify_partial_doubles = true + end + + config.shared_context_metadata_behavior = :apply_to_host_groups + config.filter_run_when_matching :focus + config.example_status_persistence_file_path = 'spec/examples.txt' + config.disable_monkey_patching! + config.warnings = true + + config.default_formatter = 'doc' if config.files_to_run.one? + + config.profile_examples = 10 + config.order = :random + Kernel.srand config.seed +end