From b91d752eb8d33166b55ba51c0c2e9361a76f870a Mon Sep 17 00:00:00 2001 From: Nikita Bulai Date: Mon, 25 Mar 2024 14:06:12 +0300 Subject: [PATCH] Upgrade Doorkeeper version --- .github/workflows/test.yml | 31 ------- .rubocop.yml | 2 +- .rubocop_todo.yml | 81 +++++++++++++++++-- Gemfile | 6 +- README.md | 4 +- Rakefile | 22 ++++- doorkeeper | 2 +- doorkeeper-mongodb.gemspec | 11 ++- gemfiles/Gemfile.common.rb | 5 +- .../mixins/mongoid/access_token_mixin.rb | 67 +++++++++------ spec/dummy/config/mongoid8.yml | 19 +++++ spec/support/orm/mongoid.rb | 20 ++++- spec/support/orm/mongoid8.rb | 6 ++ 13 files changed, 198 insertions(+), 78 deletions(-) create mode 100644 spec/dummy/config/mongoid8.yml create mode 100644 spec/support/orm/mongoid8.rb diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index f68a921..8852858 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -14,10 +14,6 @@ jobs: matrix: include: # mongoid7 requires activemodel >= 5.1, < 8.0 - - orm: 'mongoid7' - ruby: '2.6' - rails: '6.0' - experimental: false - orm: 'mongoid7' ruby: '2.7' rails: '6.0' @@ -36,10 +32,6 @@ jobs: experimental: false # mongoid8 requires activemodel >= 5.1, < 8.0 - - orm: 'mongoid8' - ruby: '2.6' - rails: '6.0' - experimental: false - orm: 'mongoid8' ruby: '2.7' rails: '6.0' @@ -57,29 +49,6 @@ jobs: rails: '7.0' experimental: false - # jruby - - orm: 'mongoid7' - ruby: 'jruby' - rails: '6.1' - experimental: false - - - orm: 'mongoid8' - ruby: 'jruby' - rails: '6.1' - experimental: false - - # truffleruby - - orm: 'mongoid7' - ruby: 'truffleruby' - rails: '6.1' - experimental: false - - # truffleruby - - orm: 'mongoid8' - ruby: 'truffleruby' - rails: '6.1' - experimental: false - # experimental - orm: 'mongoid7' ruby: 'head' diff --git a/.rubocop.yml b/.rubocop.yml index ee9c9a2..2db9de7 100644 --- a/.rubocop.yml +++ b/.rubocop.yml @@ -77,4 +77,4 @@ Layout/DotPosition: Layout/LineLength: Exclude: - spec/**/* - Max: 100 + Max: 120 diff --git a/.rubocop_todo.yml b/.rubocop_todo.yml index a48709e..95670bc 100644 --- a/.rubocop_todo.yml +++ b/.rubocop_todo.yml @@ -1,6 +1,6 @@ # This configuration was generated by # `rubocop --auto-gen-config` -# on 2022-06-25 04:37:21 UTC using RuboCop version 1.29.1. +# on 2024-03-26 14:42:18 UTC using RuboCop version 1.29.1. # The point is for the user to remove these configuration records # one by one as the offenses are removed from the code base. # Note that changes in the inspected code, or installation of new @@ -22,25 +22,43 @@ Gemspec/RequiredRubyVersion: - 'doorkeeper-mongodb.gemspec' # Offense count: 1 +# This cop supports safe auto-correction (--auto-correct). +# Configuration parameters: AllowHeredoc, AllowURI, URISchemes, IgnoreCopDirectives, AllowedPatterns, IgnoredPatterns. +# URISchemes: http, https +Layout/LineLength: + Max: 121 + +# Offense count: 1 +# This cop supports safe auto-correction (--auto-correct). +Lint/AmbiguousOperatorPrecedence: + Exclude: + - 'spec/support/doorkeeper_rspec.rb' + +# Offense count: 3 # Configuration parameters: IgnoredMethods, CountRepeatedAttributes. Metrics/AbcSize: - Max: 20 + Max: 34 -# Offense count: 1 +# Offense count: 2 # Configuration parameters: CountComments, CountAsOne, ExcludedMethods, IgnoredMethods. # IgnoredMethods: refine Metrics/BlockLength: Max: 64 -# Offense count: 1 +# Offense count: 2 # Configuration parameters: CountComments, CountAsOne, ExcludedMethods, IgnoredMethods. Metrics/MethodLength: - Max: 27 + Max: 29 # Offense count: 2 # Configuration parameters: CountComments, CountAsOne. Metrics/ModuleLength: - Max: 140 + Max: 143 + +# Offense count: 1 +Rails/I18nLocaleTexts: + Exclude: + - 'spec/dummy/config/initializers/doorkeeper.rb' # Offense count: 4 # Configuration parameters: ForbiddenMethods, AllowedMethods. @@ -51,20 +69,69 @@ Rails/SkipsModelValidations: - 'lib/doorkeeper-mongodb/mixins/mongoid/access_grant_mixin.rb' - 'lib/doorkeeper-mongodb/mixins/mongoid/access_token_mixin.rb' +# Offense count: 1 +# This cop supports safe auto-correction (--auto-correct). +Rails/StripHeredoc: + Exclude: + - 'spec/support/doorkeeper_rspec.rb' + # Offense count: 1 # This cop supports safe auto-correction (--auto-correct). Rake/Desc: Exclude: - 'Rakefile' -# Offense count: 30 +# Offense count: 34 # Configuration parameters: AllowedConstants. Style/Documentation: Enabled: false +# Offense count: 2 +# This cop supports safe auto-correction (--auto-correct). +# Configuration parameters: AllowedVars. +Style/FetchEnvVar: + Exclude: + - 'spec/support/doorkeeper_rspec.rb' + +# Offense count: 8 +# This cop supports safe auto-correction (--auto-correct). +Style/IfUnlessModifier: + Exclude: + - 'lib/doorkeeper-mongodb/mixins/mongoid/access_grant_mixin.rb' + - 'lib/doorkeeper-mongodb/mixins/mongoid/access_token_mixin.rb' + - 'lib/doorkeeper-mongodb/mixins/mongoid/application_mixin.rb' + - 'lib/doorkeeper/orm/mongoid4/application.rb' + - 'lib/doorkeeper/orm/mongoid5/application.rb' + - 'lib/doorkeeper/orm/mongoid6/application.rb' + - 'lib/doorkeeper/orm/mongoid7/application.rb' + - 'lib/doorkeeper/orm/mongoid8/application.rb' + +# Offense count: 1 +# This cop supports safe auto-correction (--auto-correct). +# Configuration parameters: EnforcedStyle. +# SupportedStyles: line_count_dependent, lambda, literal +Style/Lambda: + Exclude: + - 'lib/doorkeeper-mongodb/mixins/mongoid/access_token_mixin.rb' + # Offense count: 1 # Configuration parameters: AllowedMethods. # AllowedMethods: respond_to_missing? Style/OptionalBooleanParameter: Exclude: - 'lib/doorkeeper-mongodb/compatible.rb' + +# Offense count: 1 +# This cop supports safe auto-correction (--auto-correct). +# Configuration parameters: EnforcedStyle, ConsistentQuotesInMultiline. +# SupportedStyles: single_quotes, double_quotes +Style/StringLiterals: + Exclude: + - 'spec/dummy/config.ru' + +# Offense count: 1 +# This cop supports safe auto-correction (--auto-correct). +# Configuration parameters: AllowHeredoc, AllowURI, URISchemes, IgnoreCopDirectives, AllowedPatterns, IgnoredPatterns. +# URISchemes: http, https +Layout/LineLength: + Max: 121 diff --git a/Gemfile b/Gemfile index f3fd554..eca9f78 100755 --- a/Gemfile +++ b/Gemfile @@ -7,15 +7,19 @@ source "https://rubygems.org" gemspec +gem "bcrypt", "~> 3.1", require: false +gem "database_cleaner-mongoid" gem "mongoid" gem "rake" gem "rspec-core" gem "rspec-expectations" gem "rspec-mocks" -gem "rspec-rails", "~> 4.0.0" +gem "rspec-rails", "~> 6.0.0" gem "rspec-support" gem "rubocop", "~> 1.29.1" gem "rubocop-performance", require: false gem "rubocop-rails", require: false gem "rubocop-rake", require: false gem "rubocop-rspec", require: false +gem "sprockets-rails" +gem "timecop" diff --git a/README.md b/README.md index a920386..d0f28f2 100644 --- a/README.md +++ b/README.md @@ -57,8 +57,8 @@ variables defined in `.travis.yml` file. To run locally, you need to choose a gemfile, with a command similar to: ```bash -$ export RAILS=5.1 -$ export BUNDLE_GEMFILE=$PWD/gemfiles/Gemfile.mongoid6.rb +$ export RAILS=6.0 +$ export BUNDLE_GEMFILE=$PWD/gemfiles/Gemfile.mongoid7.rb ``` --- diff --git a/Rakefile b/Rakefile index eeaa52b..17d7c0a 100644 --- a/Rakefile +++ b/Rakefile @@ -3,16 +3,30 @@ require "bundler/setup" require "rspec/core/rake_task" +class ExtensionIntegrator + def self.gsub(filepath, pattern, value) + file = File.read(filepath) + updated_file = file.gsub(pattern, value) + File.open(filepath, "w") { |line| line.puts(updated_file) } + end +end + task :load_doorkeeper do `rm -rf spec/` `git checkout spec` - unless Dir.exist?("doorkeeper") - `git submodule init` - `git submodule update` + if Dir["doorkeeper/*"].empty? + puts `git submodule init` + puts `git submodule update` end `cp -r -n doorkeeper/spec .` `rm -rf spec/generators/` # we are not ActiveRecord `rm -rf spec/validators/` + ExtensionIntegrator.gsub( + "spec/spec_helper.rb", + 'require "database_cleaner"', + "", + ) + `rm ./spec/models/doorkeeper/application_spec.rb` `bundle exec rspec` end @@ -20,7 +34,7 @@ desc "Update Git submodules." task :update_submodules do Rake::Task["load_doorkeeper"].invoke if Dir["doorkeeper/*"].empty? - `git submodule foreach git pull origin master` + `git submodule foreach git pull origin main` end desc "Default: run specs." diff --git a/doorkeeper b/doorkeeper index 88aea0f..9c2b1d3 160000 --- a/doorkeeper +++ b/doorkeeper @@ -1 +1 @@ -Subproject commit 88aea0f7080eb6699cf80f9de57938b44eb28235 +Subproject commit 9c2b1d35e94cf905dcd3e4f742cf27c83978c368 diff --git a/doorkeeper-mongodb.gemspec b/doorkeeper-mongodb.gemspec index 63c7cd3..4d5374e 100644 --- a/doorkeeper-mongodb.gemspec +++ b/doorkeeper-mongodb.gemspec @@ -21,10 +21,13 @@ Gem::Specification.new do |gem| gem.add_dependency "doorkeeper", ">= 5.2", "< 6.0" - gem.add_development_dependency "coveralls" - gem.add_development_dependency "database_cleaner", "~> 1.6.0" - gem.add_development_dependency "factory_bot", "~> 4.8" - gem.add_development_dependency "generator_spec", "~> 0.9.4" + gem.add_development_dependency "capybara" + gem.add_development_dependency "coveralls_reborn" + gem.add_development_dependency "database_cleaner-mongoid" + gem.add_development_dependency "factory_bot", "~> 6.0" + gem.add_development_dependency "generator_spec", "~> 0.10.0" gem.add_development_dependency "grape" + gem.add_development_dependency "rake", ">= 11.3.0" gem.add_development_dependency "rspec-rails" + gem.add_development_dependency "timecop" end diff --git a/gemfiles/Gemfile.common.rb b/gemfiles/Gemfile.common.rb index 1e4b1fe..4d53585 100644 --- a/gemfiles/Gemfile.common.rb +++ b/gemfiles/Gemfile.common.rb @@ -8,8 +8,11 @@ gem "doorkeeper", "~> #{ENV.fetch("DOORKEEPER", "5.0")}" gem "bcrypt" +gem "database_cleaner-mongoid" gem "rspec-core" gem "rspec-expectations" gem "rspec-mocks" -gem "rspec-rails", "~> 4.0.0" +gem "rspec-rails", "~> 6.0.0" gem "rspec-support" +gem "sprockets-rails" +gem "timecop" diff --git a/lib/doorkeeper-mongodb/mixins/mongoid/access_token_mixin.rb b/lib/doorkeeper-mongodb/mixins/mongoid/access_token_mixin.rb index fe71f57..e6dca17 100644 --- a/lib/doorkeeper-mongodb/mixins/mongoid/access_token_mixin.rb +++ b/lib/doorkeeper-mongodb/mixins/mongoid/access_token_mixin.rb @@ -53,6 +53,26 @@ module AccessTokenMixin before_validation :generate_refresh_token, on: :create, if: :use_refresh_token? + + # Returns non-expired and non-revoked access tokens + scope :not_expired, -> { + relation = where(revoked_at: nil) + + relation.where( + { + "$expr": { + "$gt": [ + { + "$add": ["$created_at", { "$multiply": ["$expires_in", 1000] }], + }, + Time.now.utc, + ], + }, + }, + ).or( + relation.where(expires_in: nil), + ) + } end module ClassMethods @@ -192,6 +212,10 @@ def find_or_create_for(*args) expires_in = attributes[:expires_in] use_refresh_token = attributes[:use_refresh_token] + token_attributes = attributes.except( + :application, :resource_owner, :scopes, :expires_in, :use_refresh_token + ) + if Doorkeeper.configuration.reuse_access_token access_token = matching_token_for(application, resource_owner, scopes) @@ -204,6 +228,7 @@ def find_or_create_for(*args) scopes: scopes, expires_in: expires_in, use_refresh_token: use_refresh_token, + **token_attributes, ) end @@ -404,15 +429,29 @@ def generate_refresh_token def generate_token self.created_at ||= Time.now.utc - @raw_token = token_generator.generate( + @raw_token = token_generator.generate(attributes_for_token_generator) + secret_strategy.store_secret(self, :token, @raw_token) + @raw_token + end + + def attributes_for_token_generator + { resource_owner_id: resource_owner_id, scopes: scopes, application: application, expires_in: expires_in, created_at: created_at, - ) - secret_strategy.store_secret(self, :token, @raw_token) - @raw_token + }.tap do |attributes| + if Doorkeeper.config.try(:polymorphic_resource_owner?) + attributes[:resource_owner] = resource_owner + end + + if Doorkeeper.config.respond_to?(:custom_access_token_attributes) + Doorkeeper.config.custom_access_token_attributes.each do |attribute_name| + attributes[attribute_name] = public_send(attribute_name) + end + end + end end def token_generator @@ -426,26 +465,6 @@ def token_generator rescue NameError raise Doorkeeper::Errors::TokenGeneratorNotFound, "#{generator_name} not found" end - - # Returns non-expired and non-revoked access tokens - def not_expired - relation = where(revoked_at: nil) - - relation.where( - { - "$expr": { - "$gt": [ - { - "$add": ["$created_at", { "$multiply": ["$expires_in", 1000] }], - }, - Time.now.utc, - ], - }, - }, - ).or( - relation.where(expires_in: nil), - ) - end end end end diff --git a/spec/dummy/config/mongoid8.yml b/spec/dummy/config/mongoid8.yml new file mode 100644 index 0000000..e5bc584 --- /dev/null +++ b/spec/dummy/config/mongoid8.yml @@ -0,0 +1,19 @@ +development: + clients: + default: + database: doorkeeper-mongoid8-development + hosts: + - localhost:27017 + options: + write: + w: 1 + +test: + clients: + default: + database: doorkeeper-mongoid7-test + hosts: + - localhost:27017 + options: + write: + w: 1 diff --git a/spec/support/orm/mongoid.rb b/spec/support/orm/mongoid.rb index cdd543b..7d198a2 100644 --- a/spec/support/orm/mongoid.rb +++ b/spec/support/orm/mongoid.rb @@ -1,7 +1,9 @@ # frozen_string_literal: true -DatabaseCleaner[:mongoid].strategy = :truncation -DatabaseCleaner[:mongoid].clean_with :truncation +require "database_cleaner/mongoid" + +DatabaseCleaner[:mongoid].strategy = :deletion +DatabaseCleaner[:mongoid].clean_with :deletion # Monkey-patch for origin Doorkeeper specs that # has `resource_owner.id + 1` :( @@ -11,10 +13,24 @@ def +(_other) end end +module WithCustomAttributes + extend ActiveSupport::Concern + + included do + field :tenant_name, type: String + end +end + RSpec.configure do |config| + config.filter_run_excluding active_record: true + config.before do Doorkeeper::Application.create_indexes Doorkeeper::AccessGrant.create_indexes Doorkeeper::AccessToken.create_indexes + + # To support custom attributes + Doorkeeper::AccessToken.include WithCustomAttributes + Doorkeeper::AccessGrant.include WithCustomAttributes end end diff --git a/spec/support/orm/mongoid8.rb b/spec/support/orm/mongoid8.rb new file mode 100644 index 0000000..9779410 --- /dev/null +++ b/spec/support/orm/mongoid8.rb @@ -0,0 +1,6 @@ +# frozen_string_literal: true + +require_relative "mongoid" + +Mongoid.logger.level = Logger::ERROR +Mongo::Logger.logger.level = Logger::ERROR