Skip to content

Commit

Permalink
AO3-6634 Prefer update! for roles
Browse files Browse the repository at this point in the history
  • Loading branch information
sarken committed Feb 24, 2024
1 parent fbf8443 commit c9ce111
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 23 deletions.
80 changes: 60 additions & 20 deletions spec/controllers/admin_posts_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,13 @@
content: "AdminPost content long enough to pass validation" } }

authorized_roles = %w[superadmin board board_assistants_team communications support translation]
unauthorized_roles = Admin::VALID_ROLES - authorized_roles + [nil]
unauthorized_roles = Admin::VALID_ROLES - authorized_roles

context "when admin does not have correct authorization" do
let(:admin) { create(:admin) }

unauthorized_roles.each do |admin_role|
role_description = admin_role.nil? ? "no role" : "the role #{admin_role}"

context "with #{role_description}" do
context "with #{admin_role}" do
it "redirects with error" do
admin.update!(roles: [admin_role])
fake_login_admin(admin)
Expand All @@ -43,6 +41,16 @@
end
end
end

context "with no role" do
it "redirects with error" do
admin.update!(roles: [])
fake_login_admin(admin)
post :create, params: { admin_post: base_params }

it_redirects_to_with_error(root_url, "Sorry, only an authorized admin can access the page you were trying to reach.")
end
end
end

context "when admin has correct authorization" do
Expand Down Expand Up @@ -93,13 +101,11 @@
let(:post) { create(:admin_post) }

authorized_roles = %w[superadmin board board_assistants_team communications support translation]
unauthorized_roles = Admin::VALID_ROLES - authorized_roles + [nil]
unauthorized_roles = Admin::VALID_ROLES - authorized_roles

context "when admin does not have correct authorization" do
unauthorized_roles.each do |admin_role|
role_description = admin_role.nil? ? "no role" : "the role #{admin_role}"

context "with #{role_description}" do
context "with the role #{admin_role}" do
it "redirects with error" do
admin.update!(roles: [admin_role])
fake_login_admin(admin)
Expand All @@ -109,6 +115,16 @@
end
end
end

context "with no role" do
it "redirects with error" do
admin.update!(roles: [])
fake_login_admin(admin)
put :update, params: { id: post.id, admin_post: { admin_id: admin.id } }

it_redirects_to_with_error(root_url, "Sorry, only an authorized admin can access the page you were trying to reach.")
end
end
end

context "when admin has correct authorization" do
Expand Down Expand Up @@ -177,13 +193,11 @@
let(:admin) { create(:admin) }
let(:post) { create(:admin_post) }
authorized_roles = %w[superadmin board board_assistants_team communications support translation]
unauthorized_roles = Admin::VALID_ROLES - authorized_roles + [nil]
unauthorized_roles = Admin::VALID_ROLES - authorized_roles

context "when admin does not have correct authorization" do
unauthorized_roles.each do |admin_role|
role_description = admin_role.nil? ? "no role" : "the role #{admin_role}"

context "with #{role_description}" do
context "with the role #{admin_role}" do
it "redirects with error" do
admin.update!(roles: [admin_role])
fake_login_admin(admin)
Expand All @@ -193,6 +207,16 @@
end
end
end

context "with no role" do
it "redirects with error" do
admin.update!(roles: [])
fake_login_admin(admin)
get :edit, params: { id: post.id }

it_redirects_to_with_error(root_url, "Sorry, only an authorized admin can access the page you were trying to reach.")
end
end
end

context "when admin has correct authorization" do
Expand All @@ -214,13 +238,11 @@
let(:admin) { create(:admin) }
let(:post) { create(:admin_post) }
authorized_roles = %w[superadmin board board_assistants_team communications support translation]
unauthorized_roles = Admin::VALID_ROLES - authorized_roles + [nil]
unauthorized_roles = Admin::VALID_ROLES - authorized_roles

context "when admin does not have correct authorization" do
unauthorized_roles.each do |admin_role|
role_description = admin_role.nil? ? "no role" : "the role #{admin_role}"

context "with #{role_description}" do
context "with the role #{admin_role}" do
it "redirects with error" do
admin.update!(roles: [admin_role])
fake_login_admin(admin)
Expand All @@ -230,6 +252,16 @@
end
end
end

context "with no role" do
it "redirects with error" do
admin.update!(roles: [])
fake_login_admin(admin)
get :edit, params: { id: post.id }

it_redirects_to_with_error(root_url, "Sorry, only an authorized admin can access the page you were trying to reach.")
end
end
end

context "when admin has correct authorization" do
Expand All @@ -251,13 +283,11 @@
let(:admin) { create(:admin) }
let(:post) { create(:admin_post) }
authorized_roles = %w[superadmin board board_assistants_team communications support translation]
unauthorized_roles = Admin::VALID_ROLES - authorized_roles + [nil]
unauthorized_roles = Admin::VALID_ROLES - authorized_roles

context "when admin does not have correct authorization" do
unauthorized_roles.each do |admin_role|
role_description = admin_role.nil? ? "no role" : "the role #{admin_role}"

context "with #{role_description}" do
context "with the role #{admin_role}" do
it "redirects with error" do
admin.update!(roles: [admin_role])
fake_login_admin(admin)
Expand All @@ -267,6 +297,16 @@
end
end
end

context "with no role" do
it "redirects with error" do
admin.update!(roles: [])
fake_login_admin(admin)
delete :destroy, params: { id: post.id }

it_redirects_to_with_error(root_url, "Sorry, only an authorized admin can access the page you were trying to reach.")
end
end
end

context "when admin has correct authorization" do
Expand Down
6 changes: 3 additions & 3 deletions spec/controllers/comments_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3641,7 +3641,7 @@
end

it "PUT #unfreeze successfully unfreezes the comment" do
comment.update(iced: true)
comment.update!(iced: true)
put :unfreeze, params: { id: comment.id }
it_redirects_to_with_comment_notice(
work_path(comment.ultimate_parent, show_comments: true, anchor: :comments),
Expand Down Expand Up @@ -3741,7 +3741,7 @@

context "PUT #unfreeze" do
it "does not permit unfreezing of the comment when admin has no role" do
comment.update(iced: true)
comment.update!(iced: true)
admin.update!(roles: [])
fake_login_admin(admin)
put :unfreeze, params: { id: comment.id }
Expand All @@ -3750,7 +3750,7 @@

%w[superadmin policy_and_abuse].each do |admin_role|
it "successfully unfreezes the comment when admin has #{admin_role} role" do
comment.update(iced: true)
comment.update!(iced: true)
admin.update!(roles: [admin_role])
fake_login_admin(admin)
put :unfreeze, params: { id: comment.id }
Expand Down

0 comments on commit c9ce111

Please sign in to comment.