Skip to content

Commit ff6108a

Browse files
brianjaustinzrei
andauthored
AO3-7388 Redirect to user skin page with error if trying to preview a skin you can't use (#5813)
This reverts commit 8c294d1. Co-authored-by: Lim Zhe Rui <88964793+zrei@users.noreply.github.com>
1 parent f5f6f67 commit ff6108a

3 files changed

Lines changed: 206 additions & 2 deletions

File tree

app/controllers/skins_controller.rb

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
class SkinsController < ApplicationController
2-
before_action :users_only, only: [:new, :create, :destroy]
2+
before_action :users_only, only: [:new, :create, :destroy, :preview]
33
before_action :load_skin, except: [:index, :new, :create, :unset]
44
before_action :check_ownership_or_admin, only: [:edit, :update]
55
before_action :check_ownership, only: [:confirm_delete, :destroy]
6-
before_action :check_visibility, only: [:show]
6+
before_action :check_visibility, only: [:show, :preview]
77
before_action :check_editability, only: [:edit, :update, :confirm_delete, :destroy]
88

99
#### ACTIONS
@@ -129,6 +129,11 @@ def update
129129

130130
# Get /skins/1/preview
131131
def preview
132+
if @skin.is_a?(WorkSkin) || @skin.unusable?
133+
flash[:error] = t(".cannot_preview")
134+
redirect_to user_skins_path(current_user) and return
135+
end
136+
132137
flash[:notice] = []
133138
flash[:notice] << ts("You are previewing the skin %{title}. This is a randomly chosen page.", title: @skin.title)
134139
flash[:notice] << ts("Go back or click any link to remove the skin.")

config/locales/controllers/en.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -314,6 +314,8 @@ en:
314314
index:
315315
public_site_page_title: Public Site Skins
316316
public_work_page_title: Public Work Skins
317+
preview:
318+
cannot_preview: Sorry, you can't preview that skin.
317319
set:
318320
failure: Sorry, but only certain skins can be used this way (for performance reasons). Please drop a support request if you'd like %{skin_title} to be added!
319321
skin_page: "%{skin_title} skin page"

spec/controllers/skins_controller_spec.rb

Lines changed: 197 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -622,4 +622,201 @@
622622
end
623623
end
624624
end
625+
626+
describe "GET #preview" do
627+
let(:skin_creator) { create(:user) }
628+
let(:other_user) { create(:user) }
629+
subject { get :preview, params: { id: skin.id } }
630+
631+
shared_examples "a skin admins cannot preview" do
632+
before do
633+
fake_login_admin(admin)
634+
end
635+
636+
context "when logged in as an admin with no role" do
637+
let(:admin) { create(:admin, roles: []) }
638+
639+
it "redirects with an error" do
640+
subject
641+
# This actually redirects to the root path
642+
it_redirects_to_user_login_with_error
643+
end
644+
end
645+
646+
Admin::VALID_ROLES.each do |role|
647+
context "when logged in as an admin with role #{role}" do
648+
let(:admin) { create(:admin, roles: [role]) }
649+
650+
it "redirects with an error" do
651+
subject
652+
# This actually redirects to the root path
653+
it_redirects_to_user_login_with_error
654+
end
655+
end
656+
end
657+
end
658+
659+
shared_examples "a skin guests cannot preview" do
660+
context "when not logged in" do
661+
it "errors and redirects to user_login" do
662+
subject
663+
it_redirects_to_user_login_with_error
664+
end
665+
end
666+
end
667+
668+
shared_examples "a public skin that cannot be previewed" do
669+
context "when logged in as the skin creator" do
670+
it "errors and redirects to user_skins_path" do
671+
fake_login_known_user(skin.author)
672+
subject
673+
674+
it_redirects_to_with_error(user_skins_path(skin.author), "Sorry, you can't preview that skin.")
675+
end
676+
end
677+
678+
context "when logged in as a user who isn't the skin author" do
679+
it "errors and redirects to user_skins_path" do
680+
fake_login_known_user(other_user)
681+
subject
682+
683+
it_redirects_to_with_error(user_skins_path(other_user), "Sorry, you can't preview that skin.")
684+
end
685+
end
686+
687+
context "when logged in as an admin" do
688+
it_behaves_like "a skin admins cannot preview"
689+
end
690+
691+
context "when not logged in" do
692+
it_behaves_like "a skin guests cannot preview"
693+
end
694+
end
695+
696+
shared_examples "a non-public skin that cannot be previewed" do
697+
context "when logged in as the skin creator" do
698+
it "errors and redirects to user_skins_path" do
699+
fake_login_known_user(skin.author)
700+
subject
701+
702+
it_redirects_to_with_error(user_skins_path(skin.author), "Sorry, you can't preview that skin.")
703+
end
704+
end
705+
706+
context "when logged in as a user who isn't the skin author" do
707+
it "errors and redirects to user_path" do
708+
fake_login_known_user(other_user)
709+
subject
710+
711+
it_redirects_to_with_error(user_path(other_user), "Sorry, you don't have permission to access the page you were trying to reach.")
712+
end
713+
end
714+
715+
context "when logged in as an admin" do
716+
it_behaves_like "a skin admins cannot preview"
717+
end
718+
719+
context "when not logged in" do
720+
it_behaves_like "a skin guests cannot preview"
721+
end
722+
end
723+
724+
context "with workskin" do
725+
context "when workskin is public" do
726+
let(:skin) { create(:work_skin, :public, title: "Work Skin", author: skin_creator) }
727+
728+
it_behaves_like "a public skin that cannot be previewed"
729+
end
730+
731+
context "when workskin is not public" do
732+
let(:skin) { create(:work_skin, title: "Work Skin", author: skin_creator) }
733+
734+
it_behaves_like "a non-public skin that cannot be previewed"
735+
end
736+
end
737+
738+
context "with parent only site skin" do
739+
context "when site skin is public" do
740+
let(:skin) { create(:skin, :public, title: "Parent Only Site Skin", unusable: true, author: skin_creator) }
741+
742+
it_behaves_like "a public skin that cannot be previewed"
743+
end
744+
745+
context "when site skin is not public" do
746+
let(:skin) { create(:skin, title: "Parent Only Site Skin", unusable: true, author: skin_creator) }
747+
748+
it_behaves_like "a non-public skin that cannot be previewed"
749+
end
750+
end
751+
752+
context "with accessible site skin" do
753+
let(:success) { it_redirects_to_simple(tag_works_path(tag, site_skin: skin.id)) }
754+
let(:tag) { create(:canonical_fandom) }
755+
756+
before do
757+
FilterCount.create!(
758+
filter: tag,
759+
public_works_count: 10,
760+
unhidden_works_count: 10
761+
)
762+
end
763+
764+
context "when site skin is public" do
765+
let(:skin) { create(:skin, :public, title: "Accessible Site Skin", author: skin_creator) }
766+
767+
context "when logged in as the skin creator" do
768+
it "succeeds" do
769+
fake_login_known_user(skin.author)
770+
subject
771+
success
772+
end
773+
end
774+
775+
context "when logged in as a user who isn't the skin author" do
776+
it "succeeds" do
777+
fake_login
778+
subject
779+
success
780+
end
781+
end
782+
783+
context "when logged in as an admin" do
784+
it_behaves_like "a skin admins cannot preview"
785+
end
786+
787+
context "when not logged in" do
788+
it_behaves_like "a skin guests cannot preview"
789+
end
790+
end
791+
792+
context "when site skin is not public" do
793+
let(:skin) { create(:skin, title: "Accessible Site Skin", author: skin_creator) }
794+
795+
context "when logged in as the skin author" do
796+
it "succeeds" do
797+
fake_login_known_user(skin.author)
798+
subject
799+
success
800+
end
801+
end
802+
803+
context "when logged in as a user who isn't the skin author" do
804+
it "redirects with an error" do
805+
fake_login_known_user(other_user)
806+
subject
807+
808+
it_redirects_to_with_error(user_path(other_user), "Sorry, you don't have permission to access the page you were trying to reach.")
809+
end
810+
end
811+
812+
context "when logged in as an admin" do
813+
it_behaves_like "a skin admins cannot preview"
814+
end
815+
816+
context "when not logged in" do
817+
it_behaves_like "a skin guests cannot preview"
818+
end
819+
end
820+
end
821+
end
625822
end

0 commit comments

Comments
 (0)