From ab831aad6ba542a08df53fdb5e0195dd89911be3 Mon Sep 17 00:00:00 2001 From: Matijs van Zuijlen Date: Fri, 23 Oct 2026 12:47:04 +0200 Subject: [PATCH 1/2] Re-organise specs for Admin::ArticlesController - Inline the share example that was only used once - Clarify some descriptions --- .../admin/articles_controller_spec.rb | 169 +++++++++--------- 1 file changed, 88 insertions(+), 81 deletions(-) diff --git a/spec/controllers/admin/articles_controller_spec.rb b/spec/controllers/admin/articles_controller_spec.rb index 3dafa153..8b20a753 100644 --- a/spec/controllers/admin/articles_controller_spec.rb +++ b/spec/controllers/admin/articles_controller_spec.rb @@ -9,7 +9,7 @@ create(:blog) end - describe "index" do + describe "#index" do let(:publisher) { create(:user, :as_publisher) } let!(:article) { create(:article) } @@ -148,7 +148,88 @@ end describe "#create" do - shared_examples_for "create action" do + context "as a publisher" do + let(:publisher) { create(:user, :as_publisher) } + let(:article_params) { { title: "posted via tests!", body: "a good boy" } } + + before do + sign_in publisher + @user = publisher + end + + it "creates an article" do + expect do + post :create, params: { article: article_params } + end.to change(Article, :count).by(1) + end + + context "when publishing a new article now" do + before do + post :create, params: { article: article_params } + end + + it { expect(response).to redirect_to(action: :index) } + it { expect(flash[:success]).to eq(I18n.t("admin.articles.create.success")) } + + it "publishes the article" do + expect(assigns(:article)).to be_published + end + + it "links the article to the signed-in user" do + expect(assigns(:article).user).to eq(publisher) + end + end + + context "when creating a draft" do + let(:article_params) do + { title: "posted via tests!", body: "a good boy", draft: "true" } + end + + before do + post :create, params: { article: article_params } + end + + it "does not publish the article" do + expect(assigns(:article)).not_to be_published + end + end + + context "writing for the future" do + let(:article_params) do + { title: "posted via tests!", body: "a good boy", + published_at: 1.hour.from_now.to_s } + end + + before do + post :create, params: { article: article_params } + end + + it "does not create a short url" do + expect(Redirect.count).to eq 0 + end + + it "creates a trigger to publish the article" do + expect(Trigger.count).to eq 1 + end + + it "does not publish the article" do + expect(assigns(:article)).to be_publication_pending + end + + it "sets the publication time in the future" do + expect(assigns(:article).published_at).to be > 10.minutes.from_now + end + end + end + + context "as an admin" do + let(:admin) { create(:user, :as_admin) } + + before do + sign_in admin + @user = admin + end + def base_article(options = {}) { title: "posted via tests!", body: "A good body", @@ -234,92 +315,18 @@ def base_article(options = {}) end end - describe "with an unrelated draft in the database" do + context "with an unrelated draft in the database" do before do @draft = create(:article, state: "draft") end - describe "saving new article as draft" do - it "leaves the original draft in existence" do - post :create, params: { article: base_article(draft: "save as draft") } - expect(assigns(:article).id).not_to eq(@draft.id) - expect(Article.find(@draft.id)).not_to be_nil - end + it "saves the article as a fresh draft" do + post :create, params: { article: base_article(draft: "save as draft") } + expect(assigns(:article).id).not_to eq(@draft.id) + expect(Article.find(@draft.id)).not_to be_nil end end end - - context "as a publisher" do - let(:publisher) { create(:user, :as_publisher) } - let(:article_params) { { title: "posted via tests!", body: "a good boy" } } - - before do - sign_in publisher - @user = publisher - end - - it "creates an article" do - expect do - post :create, params: { article: article_params } - end.to change(Article, :count).by(1) - end - - context "classic" do - before { post :create, params: { article: article_params } } - - it { expect(response).to redirect_to(action: :index) } - it { expect(flash[:success]).to eq(I18n.t("admin.articles.create.success")) } - - it { expect(assigns(:article)).to be_published } - it { expect(assigns(:article).user).to eq(publisher) } - - context "when doing a draft" do - let(:article_params) do - { title: "posted via tests!", body: "a good boy", draft: "true" } - end - - it { expect(assigns(:article)).not_to be_published } - end - end - - context "writing for the future" do - let(:article_params) do - { title: "posted via tests!", body: "a good boy", - published_at: 1.hour.from_now.to_s } - end - - before do - post :create, params: { article: article_params } - end - - it "does not create a short url" do - expect(Redirect.count).to eq 0 - end - - it "creates a trigger to publish the article" do - expect(Trigger.count).to eq 1 - end - - it "does not publish the article" do - expect(assigns(:article)).to be_publication_pending - end - - it "sets the publication time in the future" do - expect(assigns(:article).published_at).to be > 10.minutes.from_now - end - end - end - - context "as an admin" do - let(:admin) { create(:user, :as_admin) } - - before do - sign_in admin - @user = admin - end - - it_behaves_like "create action" - end end describe "#edit" do From 8b0d90a97de1b1ef3a6b0eac6660b71d6c9a39a0 Mon Sep 17 00:00:00 2001 From: Matijs van Zuijlen Date: Fri, 23 Oct 2026 12:48:09 +0200 Subject: [PATCH 2/2] Block publisher overwriting other user's articles in create action --- app/controllers/admin/articles_controller.rb | 5 +- .../admin/articles_controller_spec.rb | 52 +++++++++++++++++++ 2 files changed, 56 insertions(+), 1 deletion(-) diff --git a/app/controllers/admin/articles_controller.rb b/app/controllers/admin/articles_controller.rb index f2368632..29c3e33d 100644 --- a/app/controllers/admin/articles_controller.rb +++ b/app/controllers/admin/articles_controller.rb @@ -37,7 +37,10 @@ def edit def create article_factory = Article::Factory.new(this_blog, current_user) - @article = article_factory.get_or_build_from(params[:article][:id]) + id = params[:article][:id].presence + return if id && !access_granted?(id) + + @article = article_factory.get_or_build_from(id) update_article_attributes diff --git a/spec/controllers/admin/articles_controller_spec.rb b/spec/controllers/admin/articles_controller_spec.rb index 8b20a753..a70d075b 100644 --- a/spec/controllers/admin/articles_controller_spec.rb +++ b/spec/controllers/admin/articles_controller_spec.rb @@ -220,6 +220,40 @@ expect(assigns(:article).published_at).to be > 10.minutes.from_now end end + + context "with an existing draft" do + before do + @draft = create(:article, body: "draft", state: "draft", user: publisher) + post :create, + params: { article: { id: @draft.id, body: "update", published: true } } + end + + it "updates the draft" do + expect(Article.find(@draft.id).body).to eq "update" + end + + it "makes the draft published" do + expect(Article.find(@draft.id)).to be_published + end + end + + context "with an existing draft belonging to another user" do + let(:other_user) { create(:user, :as_publisher) } + + before do + @draft = create(:article, body: "draft", state: "draft", user: other_user) + post :create, + params: { article: { id: @draft.id, body: "update", published: true } } + end + + it "does not update the draft" do + @draft.reload + aggregate_failures do + expect(@draft.body).to eq "draft" + expect(@draft).not_to be_published + end + end + end end context "as an admin" do @@ -566,6 +600,24 @@ def base_article(options = {}) end end end + + context "with another user's article" do + let(:article) { create(:article, body: "another *textile* test") } + let(:body) { "not the *same* text" } + + before do + put :update, + params: { id: article.id, + article: { body: body } } + end + + it "does not update the other user's article" do + aggregate_failures do + expect(response).to redirect_to(action: "index") + expect(article.reload.body).not_to eq(body) + end + end + end end end