Files
nairobi/spec/models/proposal_notification_spec.rb
Javi Martín 7ca55c44e0 Apply Rails/SaveBang rubocop rule
Having exceptions is better than having silent bugs.

There are a few methods I've kept the same way they were.

The `RelatedContentScore#score_with_opposite` method is a bit peculiar:
it creates scores for both itself and the opposite related content,
which means the opposite related content will try to create the same
scores as well.

We've already got a test to check `Budget::Ballot#add_investment` when
creating a line fails ("Edge case voting a non-elegible investment").

Finally, the method `User#send_oauth_confirmation_instructions` doesn't
update the record when the email address isn't already present, leading
to the test "Try to register with the email of an already existing user,
when an unconfirmed email was provided by oauth" fo fail if we raise an
exception for an invalid user. That's because updating a user's email
doesn't update the database automatically, but instead a confirmation
email is sent.

There are also a few false positives for classes which don't have bang
methods (like the GraphQL classes) or destroying attachments.

For these reasons, I'm adding the rule with a "Refactor" severity,
meaning it's a rule we can break if necessary.
2019-10-23 14:39:31 +02:00

171 lines
5.0 KiB
Ruby

require "rails_helper"
describe ProposalNotification do
let(:notification) { build(:proposal_notification) }
it "is valid" do
expect(notification).to be_valid
end
it "is not valid without a title" do
notification.title = nil
expect(notification).not_to be_valid
end
it "is not valid without a body" do
notification.body = nil
expect(notification).not_to be_valid
end
it "is not valid without an associated proposal" do
notification.proposal = nil
expect(notification).not_to be_valid
end
describe "public_for_api scope" do
it "returns proposal notifications" do
notification = create(:proposal_notification, proposal: create(:proposal))
expect(ProposalNotification.public_for_api).to eq [notification]
end
it "blocks proposal notifications whose proposal is hidden" do
create(:proposal_notification, proposal: create(:proposal, :hidden))
expect(ProposalNotification.public_for_api).to be_empty
end
it "blocks proposal notifications without proposal" do
build(:proposal_notification, proposal: nil).save!(validate: false)
expect(ProposalNotification.public_for_api).to be_empty
end
end
describe "minimum interval between notifications" do
before do
Setting[:proposal_notification_minimum_interval_in_days] = 3
end
it "is not valid if below minimum interval" do
proposal = create(:proposal)
create(:proposal_notification, proposal: proposal)
notification2 = build(:proposal_notification, proposal: proposal.reload)
expect(notification2).not_to be_valid
end
it "is valid if notifications above minimum interval" do
proposal = create(:proposal)
create(:proposal_notification, proposal: proposal, created_at: 4.days.ago)
notification2 = build(:proposal_notification, proposal: proposal.reload)
expect(notification2).to be_valid
end
it "is valid if no notifications sent" do
notification1 = build(:proposal_notification)
expect(notification1).to be_valid
end
end
describe "notifications in-app" do
let(:notifiable) { create(model_name(ProposalNotification)) }
let(:proposal) { notifiable.proposal }
describe "#notification_title" do
it "returns the proposal title" do
notification = create(:notification, notifiable: notifiable)
expect(notification.notifiable_title).to eq notifiable.proposal.title
end
end
describe "#notification_action" do
it "returns the correct action" do
notification = create(:notification, notifiable: notifiable)
expect(notification.notifiable_action).to eq "proposal_notification"
end
end
describe "notifiable_available?" do
it "returns true when the proposal is available" do
notification = create(:notification, notifiable: notifiable)
expect(notification.notifiable_available?).to be(true)
end
it "returns false when the proposal is not available" do
notification = create(:notification, notifiable: notifiable)
notifiable.proposal.destroy!
expect(notification.notifiable_available?).to be(false)
end
end
describe "check_availability" do
it "returns true if the resource is present, not hidden, nor retired" do
notification = create(:notification, notifiable: notifiable)
expect(notification.check_availability(proposal)).to be(true)
end
it "returns false if the resource is not present" do
notification = create(:notification, notifiable: notifiable)
notifiable.proposal.really_destroy!
expect(notification.check_availability(proposal)).to be(false)
end
it "returns false if the resource is hidden" do
notification = create(:notification, notifiable: notifiable)
notifiable.proposal.hide
expect(notification.check_availability(proposal)).to be(false)
end
it "returns false if the resource is retired" do
notification = create(:notification, notifiable: notifiable)
notifiable.proposal.update!(retired_at: Time.current,
retired_explanation: "Unfeasible reason explanation",
retired_reason: "unfeasible")
expect(notification.check_availability(proposal)).to be(false)
end
end
describe "#moderate_system_email" do
let(:admin) { create(:administrator) }
let(:proposal) { create(:proposal) }
let(:proposal_notification) { build(:proposal_notification, proposal: proposal) }
let(:notification) { create(:notification, notifiable: proposal_notification) }
it "removes all notifications related to the proposal notification" do
proposal_notification.moderate_system_email(admin.user)
expect(Notification.all.count).to be(0)
end
it "records the moderation action in the Activity table" do
proposal_notification.moderate_system_email(admin.user)
expect(Activity.last.actionable_type).to eq("ProposalNotification")
end
end
end
end