docbrown/spec/policies/video_policy_spec.rb
Jeremy Friesen 61554af5e6
Ensuring the VideoPolicy adhears to article creation (#16762)
Videos are a conceptual subset of Articles (see
[VideosController#create][1] for supporting "evidence").  As such, we
want to ensure they conform to the expectations of the ArticlePolicy (as
described in forem/forem#16483).

This feature enhancement does a few things:

1. Renames swaps the `VideoPolicy#new?` and `VideoPolicy#create?`
   declarations (reversing the alias direction)
2. Moves methods out of the concrete ArticlesPolicy into the
   ApplicationPolicy.
3. Exposes these user oriented methods for external
   consumers (e.g. making future refactoring work easier).  The happy
   benefit is that this already removes a bit of duplicated logic.
4. Highlights that while yes the Policy is correct, we also may be
   flirting with the concept of some sort of User "properties as it relates
   to authorization" logic (good standing users, established users,
   etc.)
5. Reworked the timing of a guard clause and setting of instance variables.

[1]:
33e9bac0f7/app/controllers/videos_controller.rb (L18-L22))

Relates to #16483
Closes forem/forem#16728

To test this, I'm relying on the existing test suite.  I envision that
shifting from returning false for suspended users to raising an
exception might cause some false positive errors.  Note, in the
[AppliciationController][2] we handle the responses.  Paired with
[config/appliciation.rb][3], we handle the policy exceptions.

[2]:33e9bac0f7/app/controllers/application_controller.rb (L32)
[3]:33e9bac0f7/config/application.rb (L71-L81)
2022-03-07 15:27:18 -05:00

75 lines
2 KiB
Ruby

require "rails_helper"
RSpec.describe VideoPolicy do
let(:user) { User.new }
let(:policy) { described_class.new(user, nil) }
let(:enabled) { true }
let(:limit_post_creation_to_admins) { false }
before do
allow(Settings::General).to receive(:enable_video_upload).and_return(enabled)
allow(ArticlePolicy).to receive(:limit_post_creation_to_admins?).and_return(limit_post_creation_to_admins)
end
describe "#create?" do
subject(:predicate) { policy.create? }
context "when user is suspended" do
let(:enabled) { true }
let(:user) { create(:user, :suspended) }
it { within_block_is_expected.to raise_error(ApplicationPolicy::UserSuspendedError) }
end
context "when user is not signed-in" do
let(:enabled) { true }
let(:user) { nil }
it { within_block_is_expected.to raise_error(ApplicationPolicy::UserRequiredError) }
end
context "when user has been registered for awhile" do
let(:user) { build(:user) }
before { user.created_at = 3.weeks.ago }
it { is_expected.to be_truthy }
end
context "when user has just registered" do
let(:enabled) { true }
let(:user) { build(:user) }
before { user.created_at = 1.hour.ago }
it { is_expected.to be_falsey }
end
context "when video upload is not enabled" do
let(:enabled) { false }
let(:user) { build(:user) }
before { user.created_at = 1.hour.ago }
it { is_expected.to be_falsey }
end
context "when limit_post_creation_to_admins and user not admin" do
let(:limit_post_creation_to_admins) { true }
let(:user) { build(:user) }
before { user.created_at = 3.weeks.ago }
it { is_expected.to be_falsey }
end
context "when limit_post_creation_to_admins and user is a new admin" do
let(:limit_post_creation_to_admins) { true }
let(:user) { build(:user, :admin) }
before { user.created_at = 1.week.ago }
it { is_expected.to be_truthy }
end
end
end