docbrown/spec/services/articles/builder_spec.rb
Jeremy Friesen cf53758476
Removing Articles::Builder making policy decision (#16536)
* Removing Articles::Builder making policy decision

This change is a refactoring through triangulation.  Given that
`ArticlePolicy#new?` returned true, I'm prepared to assume that calling
`authorize(Article)` in all cases is acceptable.

So to narrow the Builder making a policy decision I renamed the returned
value to reflect what it was actually doing in the logic.  And in
renaming, flipped the polarity of the boolean.  Why the flip?  Because
`needs_authorization == !store_location`.

In consultation with Allison and Jennie, I'm proceeding with a short-cut
to get me unstuck.  That unstuck is namely "I need to ensure that the
articles#new action can go through authorization."

Given that I'll be spending time in the authorization layer, I hope
these noted short-cuts and comments will be useful in future spelunking
efforts regarding authorization.

Closes #16529

* Update app/policies/article_policy.rb

Co-authored-by: Michael Kohl <me@citizen428.net>

* Disabling spec

* Update app/policies/article_policy.rb

Co-authored-by: Dwight Scott <dwight@forem.com>

* Update app/policies/article_policy.rb

Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>

Co-authored-by: Michael Kohl <me@citizen428.net>
Co-authored-by: Dwight Scott <dwight@forem.com>
Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
2022-02-14 11:39:22 -05:00

151 lines
4.7 KiB
Ruby

require "rails_helper"
RSpec.describe Articles::Builder, type: :service do
let(:user) { create(:user) }
let(:tag) { nil }
let(:prefill) { nil }
context "when tag_user_editor_v2" do
let(:user) { create(:user) }
let(:tag) { create(:tag) }
let(:submission_template) { tag.submission_template_customized(user.name).to_s }
let(:correct_attributes) do
{
body_markdown: submission_template.split("---").last.to_s.strip,
cached_tag_list: tag.name,
processed_html: "",
user_id: user.id,
title: submission_template.split("title:")[1].to_s.split("\n")[0].to_s.strip
}
end
it "initializes an article with the correct attributes and does not store location" do
subject, store_location = described_class.call(user, tag, prefill)
expect(subject).to be_an_instance_of(Article)
expect(subject).to have_attributes(correct_attributes)
expect(store_location).to be false
end
end
context "when tag_user" do
let(:tag) { create(:tag, submission_template: "submission_template") }
let(:correct_attributes) do
{
body_markdown: tag.submission_template_customized(user.name),
processed_html: "",
user_id: user.id
}
end
it "initializes an article with the correct attributes and does not store location" do
subject, store_location = described_class.call(user, tag, prefill)
expect(subject).to be_an_instance_of(Article)
expect(subject).to have_attributes(correct_attributes)
expect(store_location).to be false
end
end
context "when prefill_user_editor_v2" do
let(:user) { create(:user) }
let(:prefill) { "dsdweewewew" }
let(:correct_attributes) do
{
body_markdown: prefill.split("---").last.to_s.strip,
cached_tag_list: prefill.split("tags:")[1].to_s.split("\n")[0].to_s.strip,
processed_html: "",
user_id: user.id,
title: prefill.split("title:")[1].to_s.split("\n")[0].to_s.strip
}
end
it "initializes an article with the correct attributes and does not store location" do
subject, store_location = described_class.call(user, tag, prefill)
expect(subject).to be_an_instance_of(Article)
expect(subject).to have_attributes(correct_attributes)
expect(store_location).to be false
end
end
context "when prefill_user" do
let(:prefill) { "dsdweewewew" }
let(:correct_attributes) do
{
body_markdown: prefill,
processed_html: "",
user_id: user.id
}
end
it "initializes an article with the correct attributes and does not store location" do
subject, store_location = described_class.call(user, tag, prefill)
expect(subject).to be_an_instance_of(Article)
expect(subject).to have_attributes(correct_attributes)
expect(store_location).to be false
end
end
context "when tag" do
let(:user) { create(:user) }
let(:tag) { create(:tag) }
let(:correct_attributes) do
{
body_markdown: "---\ntitle: \npublished: false\ndescription: \ntags: #{tag.name}\n---\n\n",
processed_html: "",
user_id: user.id
}
end
it "initializes an article with the correct attributes and stores location" do
user.setting.update(editor_version: "v1")
subject, store_location = described_class.call(user, tag, prefill)
expect(subject).to be_an_instance_of(Article)
expect(subject).to have_attributes(correct_attributes)
expect(store_location).to be true
end
end
context "when user_editor_v2" do
let(:user) { create(:user) }
let(:correct_attributes) do
{
user_id: user.id
}
end
it "initializes an article with the correct attributes and does stores location" do
subject, store_location = described_class.call(user, tag, prefill)
expect(subject).to be_an_instance_of(Article)
expect(subject).to have_attributes(correct_attributes)
expect(store_location).to be true
end
end
context "when user_editor_v1" do
let(:user) { create(:user) }
let(:correct_attributes) do
body = "---\ntitle: \npublished: false\ndescription: \ntags: " \
"\n//cover_image: https://direct_url_to_image.jpg\n---\n\n"
{
body_markdown: body,
processed_html: "",
user_id: user.id
}
end
it "initializes an article with the correct attributes and does stores location" do
user.setting.update(editor_version: "v1")
subject, store_location = described_class.call(user, tag, prefill)
expect(subject).to be_an_instance_of(Article)
expect(subject).to have_attributes(correct_attributes)
expect(store_location).to be true
end
end
end