diff --git a/app/models/user.rb b/app/models/user.rb index f2db67c69..2fc6b8353 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -104,6 +104,10 @@ class User < Principal has_many :email_addresses, :dependent => :delete_all has_many :reactions, dependent: :delete_all has_many :webhooks, dependent: :destroy + has_many :oauth_access_grants, :class_name => 'Doorkeeper::AccessGrant', + :foreign_key => :resource_owner_id, :dependent => :delete_all + has_many :oauth_access_tokens, :class_name => 'Doorkeeper::AccessToken', + :foreign_key => :resource_owner_id, :dependent => :delete_all belongs_to :auth_source diff --git a/test/unit/user_test.rb b/test/unit/user_test.rb index 8a63017c9..82d859518 100644 --- a/test/unit/user_test.rb +++ b/test/unit/user_test.rb @@ -400,6 +400,43 @@ class UserTest < ActiveSupport::TestCase assert_nil Token.find_by_id(token.id) end + def test_destroy_should_delete_oauth_access_grants + application = Doorkeeper::Application.create!( + :name => 'Test App', + :redirect_uri => 'http://localhost/callback', + :scopes => 'view_issues' + ) + grant = Doorkeeper::AccessGrant.create!( + :application_id => application.id, + :resource_owner_id => 2, + :redirect_uri => application.redirect_uri, + :scopes => 'view_issues', + :expires_in => 600 + ) + + User.find(2).destroy + assert_nil User.find_by_id(2) + assert_nil Doorkeeper::AccessGrant.find_by_id(grant.id) + end + + def test_destroy_should_delete_oauth_access_tokens + application = Doorkeeper::Application.create!( + :name => 'Test App', + :redirect_uri => 'http://localhost/callback', + :scopes => 'view_issues' + ) + token = Doorkeeper::AccessToken.create!( + :application_id => application.id, + :resource_owner_id => 2, + :scopes => 'view_issues', + :expires_in => 7200 + ) + + User.find(2).destroy + assert_nil User.find_by_id(2) + assert_nil Doorkeeper::AccessToken.find_by_id(token.id) + end + def test_destroy_should_delete_watchers issue = Issue.create!(:project_id => 1, :author_id => 1, :tracker_id => 1, :subject => 'foo')