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
6 changes: 6 additions & 0 deletions app/controllers/works_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,12 @@ def new
@hide_dashboard = true
@unposted = current_user.unposted_work

# Check if collection is closed and user doesn't have permission to post
if @collection&.closed? && !@collection&.user_is_maintainer?(current_user)
flash[:error] = t(".closed_collection", collection_title: @collection.title)
redirect_to collection_path(@collection) and return
end

if params[:load_unposted] && @unposted
@work = @unposted
@chapter = @work.first_chapter
Expand Down
2 changes: 2 additions & 0 deletions config/locales/controllers/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -304,6 +304,8 @@ en:
page_title: "%{username} - Drafts"
edit_tags:
page_title: Edit Work Tags
new:
closed_collection: Sorry, the collection %{collection_title} is closed. New works cannot be added to it.
show:
page_title:
unrevealed: Mystery Work
Expand Down
54 changes: 54 additions & 0 deletions spec/controllers/works/default_rails_actions_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,60 @@ def call_with_params(params)
it_redirects_to_simple(user_path(banned_user))
expect(flash[:error]).to include("Your account has been banned.")
end

context "when collection is closed" do
let(:collection) { create(:collection, title: "Excalibur", collection_preference: create(:collection_preference, closed: true)) }
let(:user) { create(:user) }

before { fake_login_known_user(user) }

it "redirects to collection page with error for non-maintainers" do
get :new, params: { collection_id: collection.name }
it_redirects_to_with_error(collection_path(collection), "Sorry, the collection Excalibur is closed. New works cannot be added to it.")
end
end

context "when collection is closed but user is owner" do
let(:user) { create(:user) }
let(:collection) { create(:collection, title: "Excalibur", collection_preference: create(:collection_preference, closed: true)) }
let!(:participant) { collection.collection_participants.create(pseud: user.default_pseud, participant_role: CollectionParticipant::OWNER) }

before do
fake_login_known_user(user)
end

it "allows access to new work form" do
get :new, params: { collection_id: collection.name }
expect(response).to render_template("new")
end
end

context "when collection is closed but user is maintainer" do
let(:user) { create(:user) }
let(:collection) { create(:collection, title: "Excalibur", collection_preference: create(:collection_preference, closed: true)) }
let!(:participant) { collection.collection_participants.create(pseud: user.default_pseud, participant_role: CollectionParticipant::MODERATOR) }
Copy link
Contributor

Choose a reason for hiding this comment

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

If you want to make this a bit more concise (and drop the eager let!) you could create the participant with factorybot and just use participant.user (superseding the user declaration).


before do
fake_login_known_user(user)
end

it "allows access to new work form" do
get :new, params: { collection_id: collection.name }
expect(response).to render_template("new")
end
end

context "when collection is open" do
let(:collection) { create(:collection, title: "Excalibur", collection_preference: create(:collection_preference, closed: false)) }
let(:user) { create(:user) }

before { fake_login_known_user(user) }

it "allows access to new work form" do
get :new, params: { collection_id: collection.name }
expect(response).to render_template("new")
end
end
end

describe "create" do
Expand Down
Loading