From ef648a53bab7620021c2cc371caf105a1a6a6bcc Mon Sep 17 00:00:00 2001 From: Anton Khorev Date: Thu, 23 Jan 2025 17:43:30 +0300 Subject: [PATCH] Add user transient attribute to oauth_access_token factory --- test/abilities/api_abilities_test.rb | 2 +- test/abilities/api_capability_test.rb | 8 ++++---- ...oauth2_authorized_applications_controller_test.rb | 12 ++++++------ test/factories/oauth_access_token.rb | 6 +++++- test/models/user_test.rb | 2 +- test/test_helper.rb | 2 +- 6 files changed, 18 insertions(+), 14 deletions(-) diff --git a/test/abilities/api_abilities_test.rb b/test/abilities/api_abilities_test.rb index a68704f1a..0c97dc6a0 100644 --- a/test/abilities/api_abilities_test.rb +++ b/test/abilities/api_abilities_test.rb @@ -36,7 +36,7 @@ end class ModeratorApiAbilityTest < ApiAbilityTest test "Note permissions" do - token = create(:oauth_access_token, :scopes => %w[write_notes], :resource_owner_id => create(:moderator_user).id) + token = create(:oauth_access_token, :scopes => %w[write_notes], :user => create(:moderator_user)) ability = ApiAbility.new token [:index, :create, :comment, :feed, :show, :search, :close, :reopen, :destroy].each do |action| diff --git a/test/abilities/api_capability_test.rb b/test/abilities/api_capability_test.rb index 8f5272c50..ca679dd71 100644 --- a/test/abilities/api_capability_test.rb +++ b/test/abilities/api_capability_test.rb @@ -26,7 +26,7 @@ class ChangesetCommentApiCapabilityTest < ActiveSupport::TestCase end test "as a moderator with permissionless token" do - token = create(:oauth_access_token, :resource_owner_id => create(:moderator_user).id) + token = create(:oauth_access_token, :user => create(:moderator_user)) ability = ApiAbility.new token [:create, :destroy, :restore].each do |action| @@ -35,7 +35,7 @@ class ChangesetCommentApiCapabilityTest < ActiveSupport::TestCase end test "as a moderator with write_api token" do - token = create(:oauth_access_token, :resource_owner_id => create(:moderator_user).id, :scopes => %w[write_api]) + token = create(:oauth_access_token, :user => create(:moderator_user), :scopes => %w[write_api]) ability = ApiAbility.new token [:create, :destroy, :restore].each do |action| @@ -68,7 +68,7 @@ class NoteApiCapabilityTest < ActiveSupport::TestCase end test "as a moderator with permissionless token" do - token = create(:oauth_access_token, :resource_owner_id => create(:moderator_user).id) + token = create(:oauth_access_token, :user => create(:moderator_user)) ability = ApiAbility.new token [:destroy].each do |action| @@ -77,7 +77,7 @@ class NoteApiCapabilityTest < ActiveSupport::TestCase end test "as a moderator with write_notes token" do - token = create(:oauth_access_token, :resource_owner_id => create(:moderator_user).id, :scopes => %w[write_notes]) + token = create(:oauth_access_token, :user => create(:moderator_user), :scopes => %w[write_notes]) ability = ApiAbility.new token [:destroy].each do |action| diff --git a/test/controllers/oauth2_authorized_applications_controller_test.rb b/test/controllers/oauth2_authorized_applications_controller_test.rb index 584d00ce8..a2bce266d 100644 --- a/test/controllers/oauth2_authorized_applications_controller_test.rb +++ b/test/controllers/oauth2_authorized_applications_controller_test.rb @@ -18,10 +18,10 @@ class Oauth2AuthorizedApplicationsControllerTest < ActionDispatch::IntegrationTe user = create(:user) application1 = create(:oauth_application) create(:oauth_access_grant, :resource_owner_id => user.id, :application => application1) - create(:oauth_access_token, :resource_owner_id => user.id, :application => application1) + create(:oauth_access_token, :user => user, :application => application1) application2 = create(:oauth_application) create(:oauth_access_grant, :resource_owner_id => user.id, :application => application2) - create(:oauth_access_token, :resource_owner_id => user.id, :application => application2) + create(:oauth_access_token, :user => user, :application => application2) create(:oauth_application) get oauth_authorized_applications_path @@ -39,9 +39,9 @@ class Oauth2AuthorizedApplicationsControllerTest < ActionDispatch::IntegrationTe user = create(:user) application1 = create(:oauth_application, :scopes => %w[read_prefs write_prefs write_diary read_gpx write_gpx]) create(:oauth_access_grant, :resource_owner_id => user.id, :application => application1, :scopes => %w[read_prefs write_prefs]) - create(:oauth_access_token, :resource_owner_id => user.id, :application => application1, :scopes => %w[read_prefs write_prefs]) + create(:oauth_access_token, :user => user, :application => application1, :scopes => %w[read_prefs write_prefs]) create(:oauth_access_grant, :resource_owner_id => user.id, :application => application1, :scopes => %w[read_prefs write_diary]) - create(:oauth_access_token, :resource_owner_id => user.id, :application => application1, :scopes => %w[read_prefs write_diary]) + create(:oauth_access_token, :user => user, :application => application1, :scopes => %w[read_prefs write_diary]) get oauth_authorized_applications_path assert_redirected_to login_path(:referer => oauth_authorized_applications_path) @@ -64,10 +64,10 @@ class Oauth2AuthorizedApplicationsControllerTest < ActionDispatch::IntegrationTe user = create(:user) application1 = create(:oauth_application) create(:oauth_access_grant, :resource_owner_id => user.id, :application => application1) - create(:oauth_access_token, :resource_owner_id => user.id, :application => application1) + create(:oauth_access_token, :user => user, :application => application1) application2 = create(:oauth_application) create(:oauth_access_grant, :resource_owner_id => user.id, :application => application2) - create(:oauth_access_token, :resource_owner_id => user.id, :application => application2) + create(:oauth_access_token, :user => user, :application => application2) create(:oauth_application) delete oauth_authorized_application_path(:id => application1.id) diff --git a/test/factories/oauth_access_token.rb b/test/factories/oauth_access_token.rb index 6a8b62f6c..dce8c6520 100644 --- a/test/factories/oauth_access_token.rb +++ b/test/factories/oauth_access_token.rb @@ -2,6 +2,10 @@ FactoryBot.define do factory :oauth_access_token, :class => "Doorkeeper::AccessToken" do application :factory => :oauth_application - resource_owner_id { create(:user).id } + resource_owner_id { user.id } + + transient do + user { create(:user) } # rubocop:disable FactoryBot/FactoryAssociationWithStrategy + end end end diff --git a/test/models/user_test.rb b/test/models/user_test.rb index 5c1c5a26f..1bcdec44d 100644 --- a/test/models/user_test.rb +++ b/test/models/user_test.rb @@ -306,7 +306,7 @@ class UserTest < ActiveSupport::TestCase def test_soft_destroy_revokes_oauth2_tokens user = create(:user) - oauth_access_token = create(:oauth_access_token, :resource_owner_id => user.id) + oauth_access_token = create(:oauth_access_token, :user => user) assert_equal 1, user.access_tokens.not_expired.count user.soft_destroy diff --git a/test/test_helper.rb b/test/test_helper.rb index 79d5d0d33..90a33dbf6 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -138,7 +138,7 @@ module ActiveSupport def bearer_authorization_header(token_or_user = nil, scopes: Oauth::SCOPES) token = case token_or_user when nil then create(:oauth_access_token, :scopes => scopes).token - when User then create(:oauth_access_token, :resource_owner_id => token_or_user.id, :scopes => scopes).token + when User then create(:oauth_access_token, :user => token_or_user, :scopes => scopes).token when Doorkeeper::AccessToken then token_or_user.token when String then token_or_user end -- 2.39.5