Skip to content

Commit

Permalink
2401: Use different syntax (#437)
Browse files Browse the repository at this point in the history
* Use different syntax

* Remove

* Use execute instead of invoke
  • Loading branch information
allthesignals authored Feb 7, 2025
1 parent 44f2dc8 commit bfd308c
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 22 deletions.
22 changes: 8 additions & 14 deletions app/lib/tasks/users.rake
Original file line number Diff line number Diff line change
@@ -1,29 +1,23 @@
namespace :users do
desc "Promote an existing account to service account status, returning an access token"
task promote_to_service_account: :environment do
ActiveRecord::Base.transaction do
user = User.find(ENV["id"])
puts "Found user #{user.id} with #{user.api_access_tokens.count} existing tokens"
task :promote_to_service_account, [ :user_id ] => [ :environment ] do |_, user_id|
user = User.find(user_id)

if user && user.update(is_service_account: true)
puts "Updated user to service account"
user.api_access_tokens.create!
puts "Created new token"
if user && user.update(is_service_account: true)
token = user.api_access_tokens.create

puts "User #{ENV["id"]} (#{user.email}) is now a service account"
puts "User now has #{user.api_access_tokens.count} tokens"
end
puts "User #{user_id} (#{user.email}) is now a service account: #{token.access_token}"
end
end

desc "Demote an existing account, deleting all associated access tokens"
task demote_service_account: :environment do
user = User.find(ENV["id"])
task :demote_service_account, [ :user_id ] => [ :environment ] do |_, user_id|
user = User.find(user_id)

if user && user.update(is_service_account: false)
user.api_access_tokens.update_all(deleted_at: Time.now)

puts "User #{ENV["id"]} (#{user.email}) is no longer a service account."
puts "User #{user_id} (#{user.email}) is no longer a service account."
end
end
end
11 changes: 3 additions & 8 deletions app/spec/lib/users_rake_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,15 @@
describe "promotes and demotes user service accounts" do
it "promotes a user to a service account" do
user = create(:user)
ENV["id"] = user.id.to_s

expect {
Rake::Task['users:promote_to_service_account'].execute
}.to change { user.reload.api_access_tokens.count }.by(1)

Rake::Task['users:promote_to_service_account'].execute(user.id.to_s)
user.reload
expect(user.is_service_account).to eq(true)
expect(user.api_access_tokens.count).to eq(1)
end

it "demotes a service account user and removes all access tokens" do
user = create(:user, :with_access_token, is_service_account: true)
ENV["id"] = user.id.to_s
Rake::Task['users:demote_service_account'].execute
Rake::Task['users:demote_service_account'].execute(user.id.to_s)
user.reload
expect(user.is_service_account).to eq(false)
expect(user.api_access_tokens.where(deleted_at: nil).count).to eq(0)
Expand Down

0 comments on commit bfd308c

Please sign in to comment.