Fix erroring data update script (#12628)
* Fix erroring data update script * Add spec to ensure that script doesn't impact any other user if tech_admin users don't exist * Comment out erroring script, delete its associated spec * Add replacement AddSingleResourceRoleToTechAdmins data update script
This commit is contained in:
parent
20f4890051
commit
d42ae2bc9a
4 changed files with 60 additions and 32 deletions
|
|
@ -1,10 +1,16 @@
|
|||
module DataUpdateScripts
|
||||
class AddSingleResourceAdminRoleToUsersWithTechAdmin
|
||||
def run
|
||||
users_with_tech_admin_role = Role.find_by(name: "tech_admin").users
|
||||
users_with_tech_admin_role.find_each do |user|
|
||||
user.add_role(:single_resource_admin, DataUpdateScript)
|
||||
end
|
||||
# This script causes errors when it runs on Forems that do not have any
|
||||
# users with a tech_admin role.
|
||||
# 20210209185037_add_single_resource_role_to_tech_admins is the script
|
||||
# that should override this one as a replacement.
|
||||
|
||||
# users_with_tech_admin_role = Role.find_by(name: "tech_admin").users
|
||||
|
||||
# users_with_tech_admin_role.find_each do |user|
|
||||
# user.add_role(:single_resource_admin, DataUpdateScript)
|
||||
# end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
|||
|
|
@ -0,0 +1,12 @@
|
|||
module DataUpdateScripts
|
||||
class AddSingleResourceRoleToTechAdmins
|
||||
def run
|
||||
users_with_tech_admin_role = Role.find_by(name: "tech_admin")&.users
|
||||
return unless users_with_tech_admin_role
|
||||
|
||||
users_with_tech_admin_role.find_each do |user|
|
||||
user.add_role(:single_resource_admin, DataUpdateScript)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
@ -1,28 +0,0 @@
|
|||
require "rails_helper"
|
||||
require Rails.root.join(
|
||||
"lib/data_update_scripts/20210203104631_add_single_resource_admin_role_to_users_with_tech_admin.rb",
|
||||
)
|
||||
|
||||
describe DataUpdateScripts::AddSingleResourceAdminRoleToUsersWithTechAdmin do
|
||||
let!(:tech_admin1) { create(:user, :tech_admin) }
|
||||
let!(:tech_admin2) { create(:user, :tech_admin) }
|
||||
let!(:admin) { create(:user, :admin) }
|
||||
|
||||
it "adds single_resource_admin roles to users with tech_admin roles" do
|
||||
described_class.new.run
|
||||
|
||||
expect(tech_admin1.reload.roles.pluck(:name)).to include("single_resource_admin")
|
||||
expect(tech_admin2.reload.roles.pluck(:name)).to include("single_resource_admin")
|
||||
end
|
||||
|
||||
it "sets the correct resource type for the single_resource_admin role" do
|
||||
described_class.new.run
|
||||
expect(tech_admin2.reload.roles.pluck(:resource_type)).to include("DataUpdateScript")
|
||||
end
|
||||
|
||||
it "does not add single_resource_admin roles alongside other roles" do
|
||||
described_class.new.run
|
||||
expect(admin.reload.roles.pluck(:name)).not_to include("single_resource_admin")
|
||||
end
|
||||
|
||||
end
|
||||
|
|
@ -0,0 +1,38 @@
|
|||
require "rails_helper"
|
||||
require Rails.root.join(
|
||||
"lib/data_update_scripts/20210209185037_add_single_resource_role_to_tech_admins.rb",
|
||||
)
|
||||
|
||||
describe DataUpdateScripts::AddSingleResourceRoleToTechAdmins do
|
||||
let!(:admin) { create(:user, :admin) }
|
||||
|
||||
context "without any tech_admin users" do
|
||||
it "does not add any roles to any other users with roles" do
|
||||
expect do
|
||||
described_class.new.run
|
||||
end.not_to change(admin.reload.roles, :count)
|
||||
end
|
||||
end
|
||||
|
||||
context "with tech_admin users" do
|
||||
let!(:tech_admin1) { create(:user, :tech_admin) }
|
||||
let!(:tech_admin2) { create(:user, :tech_admin) }
|
||||
|
||||
it "adds single_resource_admin roles to users with tech_admin roles" do
|
||||
described_class.new.run
|
||||
|
||||
expect(tech_admin1.reload.roles.pluck(:name)).to include("single_resource_admin")
|
||||
expect(tech_admin2.reload.roles.pluck(:name)).to include("single_resource_admin")
|
||||
end
|
||||
|
||||
it "sets the correct resource type for the single_resource_admin role" do
|
||||
described_class.new.run
|
||||
expect(tech_admin2.reload.roles.pluck(:resource_type)).to include("DataUpdateScript")
|
||||
end
|
||||
|
||||
it "does not add single_resource_admin roles alongside other roles" do
|
||||
described_class.new.run
|
||||
expect(admin.reload.roles.pluck(:name)).not_to include("single_resource_admin")
|
||||
end
|
||||
end
|
||||
end
|
||||
Loading…
Add table
Reference in a new issue