Skip to content
Merged
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: 4 additions & 1 deletion app/controllers/admin/articles_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
221 changes: 140 additions & 81 deletions spec/controllers/admin/articles_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
create(:blog)
end

describe "index" do
describe "#index" do
let(:publisher) { create(:user, :as_publisher) }
let!(:article) { create(:article) }

Expand Down Expand Up @@ -148,7 +148,122 @@
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

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
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",
Expand Down Expand Up @@ -234,91 +349,17 @@ 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
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
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

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

Expand Down Expand Up @@ -559,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

Expand Down
Loading