Files
nairobi/spec/models/legislation/process_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

230 lines
8.4 KiB
Ruby

require "rails_helper"
describe Legislation::Process do
let(:process) { create(:legislation_process) }
it_behaves_like "acts as paranoid", :legislation_process
it_behaves_like "globalizable", :legislation_process
it "is valid" do
expect(process).to be_valid
end
describe "dates validations" do
it "is invalid if debate_start_date is present but debate_end_date is not" do
process = build(:legislation_process, debate_start_date: Date.current, debate_end_date: "")
expect(process).to be_invalid
expect(process.errors.messages[:debate_end_date]).to include("can't be blank")
end
it "is invalid if debate_end_date is present but debate_start_date is not" do
process = build(:legislation_process, debate_start_date: nil, debate_end_date: Date.current)
expect(process).to be_invalid
expect(process.errors.messages[:debate_start_date]).to include("can't be blank")
end
it "is invalid if allegations_start_date is present but debate_end_date is not" do
process = build(:legislation_process, allegations_start_date: Date.current,
allegations_end_date: "")
expect(process).to be_invalid
expect(process.errors.messages[:allegations_end_date]).to include("can't be blank")
end
it "is invalid if debate_end_date is present but allegations_start_date is not" do
process = build(:legislation_process, allegations_start_date: nil,
allegations_end_date: Date.current)
expect(process).to be_invalid
expect(process.errors.messages[:allegations_start_date]).to include("can't be blank")
end
end
describe "date ranges validations" do
it "is invalid if end_date is before start_date" do
process = build(:legislation_process, start_date: Date.current,
end_date: Date.current - 1.day)
expect(process).to be_invalid
expect(process.errors.messages[:end_date]).to include("must be on or after the start date")
end
it "is valid if end_date is the same as start_date" do
process = build(:legislation_process, start_date: Date.current - 1.day,
end_date: Date.current - 1.day)
expect(process).to be_valid
end
it "is valid if debate_end_date is the same as debate_start_date" do
process = build(:legislation_process, debate_start_date: Date.current - 1.day,
debate_end_date: Date.current - 1.day)
expect(process).to be_valid
end
it "is invalid if debate_end_date is before debate_start_date" do
process = build(:legislation_process, debate_start_date: Date.current,
debate_end_date: Date.current - 1.day)
expect(process).to be_invalid
expect(process.errors.messages[:debate_end_date])
.to include("must be on or after the debate start date")
end
it "is valid if draft_end_date is the same as draft_start_date" do
process = build(:legislation_process, draft_start_date: Date.current - 1.day,
draft_end_date: Date.current - 1.day)
expect(process).to be_valid
end
it "is invalid if draft_end_date is before draft_start_date" do
process = build(:legislation_process, draft_start_date: Date.current,
draft_end_date: Date.current - 1.day)
expect(process).to be_invalid
expect(process.errors.messages[:draft_end_date])
.to include("must be on or after the draft start date")
end
it "is invalid if allegations_end_date is before allegations_start_date" do
process = build(:legislation_process, allegations_start_date: Date.current,
allegations_end_date: Date.current - 1.day)
expect(process).to be_invalid
expect(process.errors.messages[:allegations_end_date])
.to include("must be on or after the comments start date")
end
it "is valid if allegations_end_date is the same as allegations_start_date" do
process = build(:legislation_process, allegations_start_date: Date.current - 1.day,
allegations_end_date: Date.current - 1.day)
expect(process).to be_valid
end
end
describe "filter scopes" do
describe "open and past filters" do
let!(:process_1) do
create(:legislation_process, start_date: Date.current - 2.days, end_date: Date.current + 1.day)
end
let!(:process_2) do
create(:legislation_process, start_date: Date.current + 1.day, end_date: Date.current + 3.days)
end
let!(:process_3) do
create(:legislation_process, start_date: Date.current - 4.days, end_date: Date.current - 3.days)
end
it "filters open" do
open_processes = ::Legislation::Process.open
expect(open_processes).to eq [process_1]
expect(open_processes).not_to include [process_2]
end
it "filters past" do
past_processes = ::Legislation::Process.past
expect(past_processes).to eq [process_3]
end
end
it "filters draft phase" do
process_before_draft = create(
:legislation_process,
draft_start_date: Date.current - 3.days,
draft_end_date: Date.current - 2.days
)
process_with_draft_disabled = create(
:legislation_process,
draft_start_date: Date.current - 2.days,
draft_end_date: Date.current + 2.days,
draft_phase_enabled: false
)
process_with_draft_enabled = create(
:legislation_process,
draft_start_date: Date.current - 2.days,
draft_end_date: Date.current + 2.days,
draft_phase_enabled: true
)
process_with_draft_only_today = create(
:legislation_process,
draft_start_date: Date.current,
draft_end_date: Date.current,
draft_phase_enabled: true
)
processes_not_in_draft = ::Legislation::Process.not_in_draft
expect(processes_not_in_draft).to match_array [process_before_draft, process_with_draft_disabled]
expect(processes_not_in_draft).not_to include(process_with_draft_enabled)
expect(processes_not_in_draft).not_to include(process_with_draft_only_today)
end
end
describe "#status" do
it "detects planned phase" do
process.update_attributes!(start_date: Date.current + 2.days)
expect(process.status).to eq(:planned)
end
it "detects closed phase" do
process.update_attributes!(end_date: Date.current - 2.days)
expect(process.status).to eq(:closed)
end
it "detects open phase" do
expect(process.status).to eq(:open)
end
end
describe "Header colors" do
it "valid format colors" do
process1 = create(:legislation_process, background_color: "123", font_color: "#fff")
process2 = create(:legislation_process, background_color: "#fff", font_color: "123")
process3 = create(:legislation_process, background_color: "", font_color: "")
process4 = create(:legislation_process, background_color: "#abf123", font_color: "fff123")
expect(process1).to be_valid
expect(process2).to be_valid
expect(process3).to be_valid
expect(process4).to be_valid
end
it "invalid format colors" do
expect do
create(:legislation_process, background_color: "#123ghi", font_color: "#fff")
end.to raise_error(ActiveRecord::RecordInvalid, "Validation failed: Background color is invalid")
expect do
create(:legislation_process, background_color: "#fff", font_color: "ggg")
end.to raise_error(ActiveRecord::RecordInvalid, "Validation failed: Font color is invalid")
end
end
describe "milestone_tags" do
context "without milestone_tags" do
let(:process) { create(:legislation_process) }
it "do not have milestone_tags" do
expect(process.milestone_tag_list).to eq([])
expect(process.milestone_tags).to eq([])
end
it "add a new milestone_tag" do
process.milestone_tag_list = "tag1,tag2"
expect(process.milestone_tag_list).to eq(["tag1", "tag2"])
end
end
context "with milestone_tags" do
let(:process) { create(:legislation_process, :with_milestone_tags) }
it "has milestone_tags" do
expect(process.milestone_tag_list.count).to eq(1)
end
end
end
end