Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow deletion of api keys with media #2044

Merged
merged 1 commit into from
Sep 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion app/models/api_key.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@ class ApiKey < ApplicationRecord

validate :validate_team_api_keys_limit, on: :create

has_one :bot_user, dependent: :destroy
has_one :bot_user

after_destroy :delete_bot_user

# Reimplement this method in your application
def self.applications
Expand Down Expand Up @@ -52,6 +54,14 @@ def create_bot_user
end
end

def delete_bot_user
if bot_user.present? && bot_user.owns_media?
bot_user.update(api_key_id: nil)
else
bot_user.destroy
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just a note that I would suggest a test for this case but I see that the coverage is still 100% so it's already covered by some other test.

end
end

def set_user_and_team
self.user = User.current unless User.current.nil?
self.team = Team.current unless Team.current.nil?
Expand Down
3 changes: 1 addition & 2 deletions app/models/concerns/user_private.rb
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,7 @@ def set_blank_email_for_unconfirmed_user
end

def can_destroy_user
count = ProjectMedia.where(user_id: self.id).count
throw :abort if count > 0
throw :abort if owns_media?
end

def set_user_notification_settings(type, enabled)
Expand Down
5 changes: 5 additions & 0 deletions app/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -435,6 +435,11 @@ def self.reset_change_password(inputs)
user
end

def owns_media?
ProjectMedia.where(user_id: self.id).count > 0
end


# private
#
# Please add private methods to app/models/concerns/user_private.rb
Expand Down
18 changes: 18 additions & 0 deletions test/models/api_key_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -64,4 +64,22 @@ class ApiKeyTest < ActiveSupport::TestCase
end
end
end

test "should delete API key even if key's user has been used to create media" do
a = create_api_key
b = create_bot_user
b.api_key = a
b.save!
pm = create_project_media user: b

assert_equal b, pm.user

assert_difference 'ApiKey.count', -1 do
a.destroy
end

assert_raises ActiveRecord::RecordNotFound do
ApiKey.find(a.id)
end
end
end
Loading