diff --git a/features/works/work_edit_multiple.feature b/features/works/work_edit_multiple.feature index 70901adc566..32b14af41de 100644 --- a/features/works/work_edit_multiple.feature +++ b/features/works/work_edit_multiple.feature @@ -43,7 +43,7 @@ Feature: Edit Multiple Works And I should see "Glorious" And I should see "Excellent" When I set the fandom to "Random" - And I press "Update All Works" + And I press "Update All Works" Then I should see "Your edits were put through" And I should see "Random" And I should not see "SGA" diff --git a/features/works/work_edit_tags.feature b/features/works/work_edit_tags.feature index f0bea61729e..9e6e4d5c669 100644 --- a/features/works/work_edit_tags.feature +++ b/features/works/work_edit_tags.feature @@ -83,3 +83,19 @@ Feature: Edit tags on a work And I press "Cancel" When I view the work "Work 1" Then I should see "Fandom: testing" + + Scenario: User can preview a work's tags and return to edit them + Given I have loaded the "tags" fixture + And I am logged in as a random user + And I post the work "Work 1" + And I view the work "Work 1" + And I follow "Edit Tags" + And I fill in "Additional Tags" with "classic" + When I press "Preview" + Then I should see "Preview Tags" + And I should not see the default work content + When "AO3-3455" is fixed + And I should see "classic" within "Additional Tags" + When I press "Edit" + Then I should see "Edit Work Tags" + And I should see "classic" within "Additional Tags" diff --git a/spec/controllers/works/default_rails_actions_spec.rb b/spec/controllers/works/default_rails_actions_spec.rb index 8b853c18528..1a705a6d3e4 100644 --- a/spec/controllers/works/default_rails_actions_spec.rb +++ b/spec/controllers/works/default_rails_actions_spec.rb @@ -176,10 +176,41 @@ def call_with_params(params) end end + describe "edit" do + let(:user) { create(:user) } + let!(:work) { create(:work, authors: [user.default_pseud]) } + + before do + fake_login_known_user(user) + end + + it "redirects to orphan work page if only author is being removed" do + get :edit, params: { id: work.id, remove: "me" } + expect(response).to redirect_to controller: "orphans", action: "new", work_id: work.id + end + end + + describe "destroy" do + let(:user) { create(:user) } + let!(:work) { create(:work, authors: [user.default_pseud]) } + + before do + fake_login_known_user(user) + end + + it "sets flash message in case of error" do + allow_any_instance_of(Work).to receive(:destroy).and_raise("Cannot save") + + delete :destroy, params: { id: work } + expect(flash[:error]).to eq("We couldn't delete that right now, sorry! Please try again later.") + end + end + describe "create" do + let(:user) { create(:user) } + before do - @user = create(:user) - fake_login_known_user(@user) + fake_login_known_user(user) end it "doesn't allow a user to create a work in a series that they don't own" do @@ -208,7 +239,7 @@ def call_with_params(params) it "renders new if the work has invalid pseuds" do work_attributes = attributes_for(:work).except(:posted) - work_attributes[:author_attributes] = { ids: @user.pseud_ids, + work_attributes[:author_attributes] = { ids: user.pseud_ids, byline: "*impossible*" } post :create, params: { work: work_attributes } expect(response).to render_template("new") @@ -216,11 +247,28 @@ def call_with_params(params) include "Invalid creator: Could not find a pseud *impossible*." end + it "renders new if edit_button params set" do + work_attributes = attributes_for(:work).except(:posted) + post :create, params: { work: work_attributes, edit_button: true } + expect(response).to render_template("new") + end + + context "with cancel_button params" do + before do + work_attributes = attributes_for(:work) + post :create, params: { work: work_attributes, cancel_button: true } + end + + it "redirects to user page with notice" do + it_redirects_to_with_notice(user, "New work posting canceled.") + end + end + it "renders new if the work has ambiguous pseuds" do create(:pseud, name: "ambiguous") create(:pseud, name: "ambiguous") work_attributes = attributes_for(:work).except(:posted) - work_attributes[:author_attributes] = { ids: @user.pseud_ids, + work_attributes[:author_attributes] = { ids: user.pseud_ids, byline: "ambiguous" } post :create, params: { work: work_attributes } expect(response).to render_template("new") @@ -277,45 +325,70 @@ def call_with_params(params) end describe "index" do - before do - @fandom = create(:canonical_fandom) - @work = create(:work, fandom_string: @fandom.name) - end + let(:fandom) { create(:canonical_fandom) } + let!(:work) { create(:work, fandom_string: fandom.name) } it "returns the work" do get :index - expect(assigns(:works)).to include(@work) + expect(assigns(:works)).to include(work) end it "sets the fandom when given a fandom id" do - params = { fandom_id: @fandom.id } + params = { fandom_id: fandom.id } get :index, params: params - expect(assigns(:fandom)).to eq(@fandom) + expect(assigns(:fandom)).to eq(fandom) end - describe "without caching" do + it "redirects to tag page for noncanonical tags" do + noncanonical_tag = create(:character) + get :index, params: { id: work, tag_id: noncanonical_tag.name } + expect(response).to redirect_to(tag_path(noncanonical_tag)) + end + + context "without caching" do before do AdminSetting.first.update_attribute(:enable_test_caching, false) end + after do + allow(controller).to receive(:use_caching?).and_call_original + end + it "returns the result with different works the second time" do get :index - expect(assigns(:works)).to include(@work) + expect(assigns(:works)).to include(work) work2 = create(:work) get :index expect(assigns(:works)).to include(work2) end + + it "when tag is a synonym redirects to the merger's work index" do + noncanonical_fandom = create(:fandom, merger: fandom) + get :index, params: { id: work, tag_id: noncanonical_fandom.name } + expect(response).to redirect_to(tag_works_path(fandom)) + end + + it "when tag is a synonym when collection is specified redirects to the merger's collection works index" do + noncanonical_fandom = create(:fandom, canonical: false, merger: fandom) + collection = create(:collection) + get :index, params: { id: work, tag_id: noncanonical_fandom.name, collection_id: collection } + expect(response).to redirect_to(collection_tag_works_path(collection, fandom)) + end end - describe "with caching" do + context "with caching" do before do AdminSetting.first.update_attribute(:enable_test_caching, true) end + after do + allow(controller).to receive(:use_caching?).and_call_original + end + context "with NO owner tag" do it "returns the same result the second time when a new work is created within the expiration time" do get :index - expect(assigns(:works)).to include(@work) + expect(assigns(:works)).to include(work) work2 = create(:work) run_all_indexing_jobs get :index @@ -324,44 +397,64 @@ def call_with_params(params) end context "with a valid owner tag" do + let!(:fandom2) { create(:canonical_fandom) } + let!(:work2) { create(:work, fandom_string: fandom2.name) } + before do - @fandom2 = create(:canonical_fandom) - @work2 = create(:work, fandom_string: @fandom2.name) run_all_indexing_jobs end it "only gets works under that tag" do - get :index, params: { tag_id: @fandom.name } - expect(assigns(:works).items).to include(@work) - expect(assigns(:works).items).not_to include(@work2) + get :index, params: { tag_id: fandom.name } + expect(assigns(:works).items).to include(work) + expect(assigns(:works).items).not_to include(work2) end it "shows different results on second page" do - get :index, params: { tag_id: @fandom.name, page: 2 } - expect(assigns(:works).items).not_to include(@work) + get :index, params: { tag_id: fandom.name, page: 2 } + expect(assigns(:works).items).not_to include(work) + end + + context "when suspend_filter_counts is on" do + before do + allow(controller).to receive(:fetch_admin_settings).and_return(true) + AdminSetting.first.update_attribute(:suspend_filter_counts, true) + admin_settings = AdminSetting.first + controller.instance_variable_set("@admin_settings", admin_settings) + end + + after do + allow(controller).to receive(:fetch_admin_settings).and_call_original + end + + it "shows the work in the index" do + get :index, params: { tag_id: fandom.name } + expect(assigns(:works)).to include(work) + end end context "with restricted works" do + let!(:work2) { create(:work, fandom_string: fandom.name, restricted: true) } + before do - @work2 = create(:work, fandom_string: @fandom.name, restricted: true) run_all_indexing_jobs end - it "shows restricted works to guests" do - get :index, params: { tag_id: @fandom.name } - expect(assigns(:works).items).to include(@work) - expect(assigns(:works).items).not_to include(@work2) + it "hides them from guests, showing only unrestricted works" do + get :index, params: { tag_id: fandom.name } + expect(assigns(:works).items).to include(work) + expect(assigns(:works).items).not_to include(work2) end end context "when tag is a synonym" do - let(:fandom_synonym) { create(:fandom, merger: @fandom) } + let(:fandom_synonym) { create(:fandom, merger: fandom) } it "redirects to the merger's work index" do params = { tag_id: fandom_synonym.name } get :index, params: params - it_redirects_to tag_works_path(@fandom) + it_redirects_to tag_works_path(fandom) end context "when collection is specified" do @@ -370,7 +463,7 @@ def call_with_params(params) it "redirects to the merger's collection works index" do params = { tag_id: fandom_synonym.name, collection_id: collection.name } get :index, params: params - it_redirects_to collection_tag_works_path(collection, @fandom) + it_redirects_to collection_tag_works_path(collection, fandom) end end end @@ -417,7 +510,7 @@ def call_with_params(params) params = { user_id: user.login } get :index, params: params expect(assigns(:works).items).to include(user_work, pseud_work) - expect(assigns(:works).items).not_to include(@work) + expect(assigns(:works).items).not_to include(work) end context "with a valid pseud" do @@ -425,7 +518,7 @@ def call_with_params(params) params = { user_id: user.login, pseud_id: pseud.name } get :index, params: params expect(assigns(:works).items).to include(pseud_work) - expect(assigns(:works).items).not_to include(user_work, @work) + expect(assigns(:works).items).not_to include(user_work, work) end end @@ -434,7 +527,7 @@ def call_with_params(params) params = { user_id: user.login, pseud_id: "nonexistent_pseud" } get :index, params: params expect(assigns(:works).items).to include(user_work, pseud_work) - expect(assigns(:works).items).not_to include(@work) + expect(assigns(:works).items).not_to include(work) end end end @@ -442,22 +535,22 @@ def call_with_params(params) describe "update" do let(:update_user) { create(:user) } - let(:update_work) { + let!(:update_work) do work = create(:work, authors: [update_user.default_pseud]) create(:chapter, work: work) work - } + end before do fake_login_known_user(update_user) end it "doesn't allow the user to add a series that they don't own" do - @series = create(:series) - attrs = { series_attributes: { id: @series.id } } + series = create(:series) + attrs = { series_attributes: { id: series.id } } expect { put :update, params: { id: update_work.id, work: attrs } - }.not_to change { @series.works.all.count } + }.not_to change { series.works.all.count } expect(response).to render_template :edit expect(assigns[:work].errors.full_messages).to \ include("You can't add a work to that series.") diff --git a/spec/controllers/works/multiple_actions_spec.rb b/spec/controllers/works/multiple_actions_spec.rb index 9f633a216f2..504594be3c8 100644 --- a/spec/controllers/works/multiple_actions_spec.rb +++ b/spec/controllers/works/multiple_actions_spec.rb @@ -132,7 +132,6 @@ end let(:other_editor_pseud) { create(:pseud, user: multiple_works_user) } - let(:work_params) { { work: { diff --git a/spec/controllers/works/tags_spec.rb b/spec/controllers/works/tags_spec.rb new file mode 100644 index 00000000000..2c11d26c5fe --- /dev/null +++ b/spec/controllers/works/tags_spec.rb @@ -0,0 +1,20 @@ +# frozen_string_literal: true + +require "spec_helper" + +describe WorksController do + include LoginMacros + include RedirectExpectationHelper + + let(:user) { create(:user) } + let(:work) { create(:work, authors: [user.default_pseud]) } + + describe "preview_tags" do + it "renders preview tags" do + fake_login_known_user(user) + + get :preview_tags, params: { id: work } + expect(response).to render_template "preview_tags" + end + end +end