Skip to content

Commit

Permalink
Upgrade Doorkeeper version
Browse files Browse the repository at this point in the history
  • Loading branch information
nbulaj committed Mar 26, 2024
1 parent ae87116 commit f86e68d
Show file tree
Hide file tree
Showing 13 changed files with 122 additions and 48 deletions.
8 changes: 0 additions & 8 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand All @@ -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'
Expand Down
2 changes: 1 addition & 1 deletion .rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -77,4 +77,4 @@ Layout/DotPosition:
Layout/LineLength:
Exclude:
- spec/**/*
Max: 100
Max: 120
3 changes: 3 additions & 0 deletions .rubocop_todo.yml
Original file line number Diff line number Diff line change
Expand Up @@ -68,3 +68,6 @@ Style/Documentation:
Style/OptionalBooleanParameter:
Exclude:
- 'lib/doorkeeper-mongodb/compatible.rb'

Style/IfUnlessModifier:
Enabled: false
6 changes: 5 additions & 1 deletion Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -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"
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
```

---
Expand Down
22 changes: 18 additions & 4 deletions Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,38 @@
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

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."
Expand Down
2 changes: 1 addition & 1 deletion doorkeeper
Submodule doorkeeper updated 160 files
11 changes: 7 additions & 4 deletions doorkeeper-mongodb.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -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
5 changes: 4 additions & 1 deletion gemfiles/Gemfile.common.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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"
62 changes: 38 additions & 24 deletions lib/doorkeeper-mongodb/mixins/mongoid/access_token_mixin.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -404,15 +424,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
Expand All @@ -426,26 +460,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
Expand Down
19 changes: 19 additions & 0 deletions spec/dummy/config/mongoid8.yml
Original file line number Diff line number Diff line change
@@ -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
20 changes: 18 additions & 2 deletions spec/support/orm/mongoid.rb
Original file line number Diff line number Diff line change
@@ -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` :(
Expand All @@ -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
6 changes: 6 additions & 0 deletions spec/support/orm/mongoid8.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# frozen_string_literal: true

require_relative "mongoid"

Mongoid.logger.level = Logger::ERROR
Mongo::Logger.logger.level = Logger::ERROR

0 comments on commit f86e68d

Please sign in to comment.