Files
nairobi/spec/models/verification/residence_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

116 lines
3.5 KiB
Ruby

require "rails_helper"
describe Verification::Residence do
let!(:geozone) { create(:geozone, census_code: "01") }
let(:residence) { build(:verification_residence, document_number: "12345678Z") }
describe "validations" do
it "is valid" do
expect(residence).to be_valid
end
describe "dates" do
it "is valid with a valid date of birth" do
residence = Verification::Residence.new("date_of_birth(3i)" => "1", "date_of_birth(2i)" => "1", "date_of_birth(1i)" => "1980")
expect(residence.errors[:date_of_birth]).to be_empty
end
it "is not valid without a date of birth" do
residence = Verification::Residence.new("date_of_birth(3i)" => "", "date_of_birth(2i)" => "", "date_of_birth(1i)" => "")
expect(residence).not_to be_valid
expect(residence.errors[:date_of_birth]).to include("can't be blank")
end
end
it "validates user has allowed age" do
residence = Verification::Residence.new("date_of_birth(3i)" => "1",
"date_of_birth(2i)" => "1",
"date_of_birth(1i)" => 5.years.ago.year.to_s)
expect(residence).not_to be_valid
expect(residence.errors[:date_of_birth]).to include("You don't have the required age to participate")
end
it "validates uniquness of document_number" do
user = create(:user)
residence.user = user
residence.save!
build(:verification_residence)
residence.valid?
expect(residence.errors[:document_number]).to include("has already been taken")
end
it "validates census terms" do
residence.terms_of_service = nil
expect(residence).not_to be_valid
end
end
describe "new" do
it "upcases document number" do
residence = Verification::Residence.new(document_number: "x1234567z")
expect(residence.document_number).to eq("X1234567Z")
end
it "removes all characters except numbers and letters" do
residence = Verification::Residence.new(document_number: " 12.345.678 - B")
expect(residence.document_number).to eq("12345678B")
end
end
describe "save" do
it "stores document number, document type, geozone, date of birth and gender" do
user = create(:user)
residence.user = user
residence.save!
user.reload
expect(user.document_number).to eq("12345678Z")
expect(user.document_type).to eq("1")
expect(user.date_of_birth.year).to eq(1980)
expect(user.date_of_birth.month).to eq(12)
expect(user.date_of_birth.day).to eq(31)
expect(user.gender).to eq("male")
expect(user.geozone).to eq(geozone)
end
end
describe "tries" do
it "increases tries after a call to the Census" do
residence.postal_code = "28011"
residence.valid?
expect(residence.user.lock.tries).to eq(1)
end
it "does not increase tries after a validation error" do
residence.postal_code = ""
residence.valid?
expect(residence.user.lock).to be nil
end
end
describe "Failed census call" do
it "stores failed census API calls" do
residence = build(:verification_residence, :invalid, document_number: "12345678Z")
residence.save
expect(FailedCensusCall.count).to eq(1)
expect(FailedCensusCall.first).to have_attributes(
user_id: residence.user.id,
document_number: "12345678Z",
document_type: "1",
date_of_birth: Date.new(1980, 12, 31),
postal_code: "28001"
)
end
end
end