Retrospring/spec/controllers/anonymous_block_controller_...

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

104 lines
3.0 KiB
Ruby
Raw Normal View History

2022-06-14 15:08:58 -07:00
# frozen_string_literal: true
2022-06-14 15:17:02 -07:00
2022-06-14 15:08:58 -07:00
require "rails_helper"
2022-11-21 14:05:04 -08:00
describe AnonymousBlockController, type: :controller do
2022-06-14 15:08:58 -07:00
describe "#create" do
2023-02-11 17:23:43 -08:00
subject { post(:create, params:, format: :turbo_stream) }
2022-06-14 15:08:58 -07:00
context "user signed in" do
let(:user) { FactoryBot.create(:user) }
before do
sign_in(user)
end
context "when all required parameters are given" do
2022-06-14 15:08:58 -07:00
let(:question) { FactoryBot.create(:question, author_identifier: "someidentifier") }
2022-08-13 12:21:17 -07:00
let!(:inbox) { FactoryBot.create(:inbox, user:, question:) }
2022-06-14 15:08:58 -07:00
let(:params) do
{ question: question.id }
end
it "creates an anonymous block" do
expect { subject }.to(change { AnonymousBlock.count }.by(1))
end
2023-02-11 17:23:43 -08:00
it "contains the inbox entry removal turbo stream action" do
subject
expect(response.body).to include "turbo-stream action=\"remove\" target=\"inbox_#{inbox.id}"
end
end
context "when all required parameters are given, but no inbox entry exists" do
let(:question) { FactoryBot.create(:question, author_identifier: "someidentifier") }
let(:params) do
{ question: question.id }
end
it "creates an anonymous block" do
expect { subject }.to(change { AnonymousBlock.count }.by(1))
end
it "doesn't contain the inbox entry removal turbo stream action" do
subject
expect(response.body).not_to include "turbo-stream action=\"remove\" target=\"inbox_"
end
2022-06-14 15:08:58 -07:00
end
context "when blocking a user globally" do
let(:question) { FactoryBot.create(:question, author_identifier: "someidentifier") }
2022-08-13 12:21:17 -07:00
let!(:inbox) { FactoryBot.create(:inbox, user:, question:) }
let(:params) do
{ question: question.id, global: "true" }
end
context "as a moderator" do
before do
user.add_role(:moderator)
end
it "creates an site-wide anonymous block" do
expect { subject }.to(change { AnonymousBlock.count }.by(1))
expect(AnonymousBlock.last.user_id).to be_nil
end
end
context "as a regular user" do
it "does not create an anonymous block" do
2022-11-21 14:05:04 -08:00
expect { subject }.to raise_error(Pundit::NotAuthorizedError)
end
end
end
2022-06-14 15:08:58 -07:00
end
end
describe "#destroy" do
2022-11-21 14:07:18 -08:00
subject { delete(:destroy, params:) }
context "user signed in" do
let(:user) { FactoryBot.create(:user) }
before do
sign_in(user)
end
context "when all parameters are given" do
let(:question) { FactoryBot.create(:question, author_identifier: "someidentifier") }
2022-11-21 14:07:18 -08:00
let(:block) { AnonymousBlock.create(user:, identifier: "someidentifier", question:) }
let(:params) do
{ id: block.id }
end
2022-11-21 14:05:04 -08:00
it "destroys the anonymous block" do
subject
2022-11-21 14:05:04 -08:00
expect(AnonymousBlock.exists?(block.id)).to eq(false)
end
end
end
2022-06-14 15:08:58 -07:00
end
end