Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 0 additions & 5 deletions app/controllers/works_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -601,11 +601,6 @@ def post_draft
@user = current_user
@work = Work.find(params[:id])

unless @user.is_author_of?(@work)
flash[:error] = ts('You can only post your own works.')
redirect_to(current_user) && return
end

if @work.posted
flash[:error] = ts('That work is already posted. Do you want to edit it instead?')
redirect_to(edit_user_work_path(@user, @work)) && return
Expand Down
14 changes: 14 additions & 0 deletions features/admins/admin_works.feature
Original file line number Diff line number Diff line change
Expand Up @@ -400,6 +400,20 @@ Feature: Admin Actions for Works, Comments, Series, Bookmarks
Then I should see "Deutsch"
And I should not see "English"

Scenario: Admin can edit language on drafts
Given basic languages
And I am logged in
And the draft "Wrong Language"
When I am logged in as a "policy_and_abuse" admin
And I view the work "Wrong Language"
And I follow "Edit Work"
When I select "Deutsch" from "Choose a language"
And I press "Save Draft"
Then I should see "Tags were successfully updated."
And I should see "This work is a draft"
And I should see "Deutsch"
But I should not see "English"

Scenario: When admin edits tags and language on works at the same time, both Activities entries are added
Given basic languages
And the work "Wrong Tags and Language"
Expand Down
8 changes: 0 additions & 8 deletions features/step_definitions/work_steps.rb
Original file line number Diff line number Diff line change
Expand Up @@ -392,14 +392,6 @@
step "the periodic tag count task is run"
end

Then /^I should see the default work content$/ do
page.should have_content(DEFAULT_CONTENT)
end

Then /^I should not see the default work content$/ do
page.should_not have_content(DEFAULT_CONTENT)
end

When /^I fill in basic work tags$/ do
select(DEFAULT_RATING, from: "Rating")
fill_in("Fandoms", with: DEFAULT_FANDOM)
Expand Down
2 changes: 1 addition & 1 deletion features/works/work_edit_multiple.feature
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,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"
Expand Down
6 changes: 5 additions & 1 deletion features/works/work_edit_tags.feature
Original file line number Diff line number Diff line change
Expand Up @@ -95,11 +95,15 @@ Feature: Edit tags on a work
Then I should see the page title "Work 1 -"
And I should see "Fandom: testing"

Scenario: Preview edit tags
Scenario: Preview edit tags and return to edit them
Given I am logged in as "regularuser"
And I post the work "Some Work"
And I follow "Edit Tags"
And I fill in "Fandoms" with "exclusive fandom"
And I press "Preview"
Then I should see the page title "Preview Work Tags"
And I should see "Fandom: exclusive fandom"
And I should see "Preview Tags"
When I press "Edit"
Then I should see "Edit Work Tags"
And the "Fandoms" field should contain "exclusive fandom"
114 changes: 69 additions & 45 deletions spec/controllers/works/default_rails_actions_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -257,15 +257,14 @@ def call_with_params(params)
before { fake_login_known_user(user) }

it "doesn't allow a user to create a work in a series that they don't own" do
@series = create(:series)
series = create(:series)
work_attributes = attributes_for(:work).except(:posted)
work_attributes[:series_attributes] = { id: @series.id }
expect {
work_attributes[:series_attributes] = { id: series.id }
expect do
post :create, params: { work: work_attributes }
}.not_to change { @series.works.all.count }
end.not_to change { series.works.all.count }
expect(response).to render_template :new
expect(assigns[:work].errors.full_messages).to \
include("You can't add a work to that series.")
expect(assigns[:work].errors.full_messages).to include("You can't add a work to that series.")
end

it "doesn't allow a user to submit only a pseud that is not theirs" do
Expand Down Expand Up @@ -388,6 +387,20 @@ def call_with_params(params)
end.to raise_error ActiveRecord::RecordNotFound
end
end

context "when collection is given but the work is not in the collection" do
let(:unrelated_collection) { create(:collection) }

before do
work.update!(collections: [create(:collection)])
end

it "redirects to the work" do
get :show, params: { id: work.id, collection_id: unrelated_collection.name }

it_redirects_to work_path(work)
end
end
end

describe "share" do
Expand All @@ -409,20 +422,18 @@ 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 "when the fandom id is invalid" do
Expand All @@ -437,33 +448,33 @@ def call_with_params(params)
it "returns the work" do
params = { fandom_id: nil }
get :index, params: params
expect(assigns(:works)).to include(@work)
expect(assigns(:works)).to include(work)
end
end

describe "without caching" do
context "without caching" do
before do
AdminSetting.first.update_attribute(:enable_test_caching, false)
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
end

describe "with caching" do
context "with caching" do
before do
AdminSetting.first.update_attribute(:enable_test_caching, true)
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
Expand All @@ -472,44 +483,45 @@ 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 "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
Expand All @@ -518,7 +530,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
Expand Down Expand Up @@ -565,15 +577,15 @@ 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
it "includes only works for that pseud" do
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

Expand All @@ -582,7 +594,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
Expand Down Expand Up @@ -620,11 +632,11 @@ 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

context "when logged in as admin", work_search: false do
let(:work) { create(:work) }
Expand All @@ -650,14 +662,13 @@ def call_with_params(params)
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 } }
expect {
series = create(:series)
attrs = { series_attributes: { id: series.id } }
expect do
put :update, params: { id: update_work.id, work: attrs }
}.not_to change { @series.works.all.count }
end.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.")
expect(assigns(:work).errors.full_messages).to include("You can't add a work to that series.")
end

it "redirects to the edit page if the work could not be saved" do
Expand Down Expand Up @@ -1057,5 +1068,18 @@ def call_with_params(params)
.to raise_exception(ActiveRecord::RecordNotFound)
end
end

context "when the work deletion errors out" do
before do
allow_any_instance_of(Work).to receive(:destroy) { raise ActiveRecord::RecordNotDestroyed }

fake_login_known_user(work.users.first)
end

it "sets flash message" do
delete :destroy, params: { id: work }
expect(flash[:error]).to eq("We couldn't delete that right now, sorry! Please try again later.")
end
end
end
end
2 changes: 0 additions & 2 deletions spec/controllers/works/drafts_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,6 @@
it "should display an error if the current user is not the owner of the specified work" do
random_work = create(:draft)
put :post_draft, params: { id: random_work.id }
# There is code to return a different message in the action, but it is unreachable using a web request
# as the application_controller redirects the user first
it_redirects_to_with_error(work_path(random_work),
"Sorry, you don't have permission to access the page you were trying to reach.")
end
Expand Down
14 changes: 14 additions & 0 deletions spec/controllers/works/importing_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,20 @@
expect(flash[:error]).to eq "Did you want to enter a URL?"
end

context "when the work doesn't save" do
before do
WebMock.stub_request(:get, /import-site-without-tags/)
.to_return(status: 200,
body: "stubbed response",
headers: {})
end

it "shows an error message" do
get :import, params: { urls: "http://import-site-without-tags.net", language_id: "en", work: { fandom_string: "Testing" } }
expect(flash[:error]).to eq "We were only partially able to import this work and couldn't save it. Please review below!"
end
end

it "there is an external author name but importing_for_others is NOT turned on" do
params = {
urls: "url1, url2",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,15 @@
it_behaves_like "can update work tags and language"
end
end

describe "GET #preview_tags" do

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wasn't sure about adding this - this is never linked anywhere on the site, the action is only used for render inside the controller. But it does indeed exist and is indeed accessible to users, so I guess we should at least make sure it doesn't error.

let(:work) { create(:work) }

it "renders preview tags" do
fake_login_known_user(work.users.first)

get :preview_tags, params: { id: work }
expect(response).to render_template "preview_tags"
end
end
end
Loading