Commit 2fd360f8 authored by Bruno Freitas Tissei's avatar Bruno Freitas Tissei

Add cep validation when creating citizens

Signed-off-by: Bruno Freitas Tissei's avatarBruno Freitas Tissei <bft15@inf.ufpr.br>
parent a4ec6d6b
Pipeline #13305 passed with stage
in 4 minutes and 18 seconds
......@@ -86,8 +86,6 @@ module Api::V1
# Create new citizen associated with new dependant
citizen = Citizen.new(new_params)
citizen.active = true
citizen.city_id = Address.get_city_id(new_params[:cep])
# Add image to citizen if provided
if params[:dependant][:image]
......
......@@ -40,6 +40,8 @@ class Citizen < ApplicationRecord
validates_attachment_content_type :avatar,
:content_type => ["image/jpg", "image/jpeg", "image/png", "image/gif"]
before_create :set_address
before_save :set_address
scope :all_active, -> { where(active: true, responsible_id: nil) }
......@@ -156,4 +158,15 @@ class Citizen < ApplicationRecord
def cpf_required?
self.responsible_id.nil?
end
def set_address
address = Address.get_address(self.cep)
if not address.nil?
self.city_id = address.city_id
else
self.errors["cep"] << "#{self.cep} is invalid."
return false
end
end
end
......@@ -23,7 +23,7 @@ class Api::V1::Accounts::RegistrationsControllerTest < ActionDispatch::Integrati
@curitiba_city_hall = CityHall.new(
name: "Prefeitura de Curitiba",
cep: "1234567",
cep: "81530110",
neighborhood: "Test neighborhood",
address_street: "Test street",
address_number: "123",
......@@ -78,7 +78,7 @@ class Api::V1::Accounts::RegistrationsControllerTest < ActionDispatch::Integrati
post '/v1/auth', params: {
birth_date: "Apr 18 1997",
cep: "81530-110",
cep: "81530110",
cpf: "12345678910",
email: "test@example.com",
name: "Test Example",
......@@ -152,7 +152,7 @@ class Api::V1::Accounts::RegistrationsControllerTest < ActionDispatch::Integrati
post '/v1/auth', params: {
birth_date: "Jan 1 1980",
cep: "1122334",
cep: "81530110",
cpf: "52998224725",
email: "john@john.com",
name: "John",
......@@ -185,7 +185,7 @@ class Api::V1::Accounts::RegistrationsControllerTest < ActionDispatch::Integrati
@number_of_accounts = Account.count
post '/v1/auth', params: {
cep: "1122334",
cep: "81530110",
cpf: "10845922904",
email: "john@john.com",
name: "John",
......@@ -223,7 +223,7 @@ class Api::V1::Accounts::RegistrationsControllerTest < ActionDispatch::Integrati
post '/v1/auth', params: {
birth_date: "Jan 1 1980",
cep: "1122334",
cep: "81530110",
email: "john@john.com",
name: "John",
phone1: "12341234",
......
......@@ -64,7 +64,7 @@ class Api::V1::Accounts::SessionsControllerTest < ActionDispatch::IntegrationTes
end
it "should correspond to the current account" do
assert_equal @controller.current_account.citizen.id,
assert_equal @controller.current_user[0].id,
Account.where(uid: @body["data"]["uid"]).first.citizen.id
end
......
......@@ -20,7 +20,7 @@ class Api::V1::CitizensControllerTest < ActionDispatch::IntegrationTest
@citizen = Citizen.new(
cpf: "10845922904",
birth_date: "Apr 18 1997",
cep: "1234567",
cep: "81530110",
email: "test@example.com",
name: "Test Example",
phone1: "(12)1212-1212",
......@@ -165,7 +165,7 @@ class Api::V1::CitizensControllerTest < ActionDispatch::IntegrationTest
describe "Successful request to update citizen" do
before do
put '/v1/citizens/' + @citizen.id.to_s,
params: {citizen: {cep: "7654321"}, permission: "citizen"},
params: {citizen: {cep: "80530336"}, permission: "citizen"},
headers: @auth_headers
@resp_token = response.headers['access-token']
......@@ -180,13 +180,13 @@ class Api::V1::CitizensControllerTest < ActionDispatch::IntegrationTest
test "cep should have been changed" do
@citizen = Citizen.where(cpf: @citizen.cpf).first
assert_equal "7654321", @citizen.cep
assert_equal "80530336", @citizen.cep
end
end
describe "Unsuccessful resquest to update citizen that doesn't exists" do
before do
put '/v1/citizens/222', params: {citizen: {cep: "7654321"}, permission: "citizen"},
put '/v1/citizens/222', params: {citizen: {cep: "80530336"}, permission: "citizen"},
headers: @auth_headers
@body = JSON.parse(response.body)
......
......@@ -33,7 +33,7 @@ class CityHallsControllerTest < ActionDispatch::IntegrationTest
@joinville_city_hall = CityHall.new(
name: "Prefeitura de Joinville",
cep: "1234567",
cep: "89221005",
neighborhood: "Test neighborhood",
address_street: "Test street",
address_number: "123",
......@@ -48,7 +48,7 @@ class CityHallsControllerTest < ActionDispatch::IntegrationTest
cpf: "10845922904",
active: true,
birth_date: "Apr 18 1997",
cep: "1234567",
cep: "81530110",
email: "test@example.com",
name: "Test Example",
phone1: "(12)1212-1212",
......@@ -331,7 +331,7 @@ class CityHallsControllerTest < ActionDispatch::IntegrationTest
@city_hall = CityHall.where(city_id: @joinville.id).first
put '/v1/city_halls/' + @city_hall.id.to_s,
params: {city_hall: {cep: "7654321"}, permission: "citizen"},
params: {city_hall: {cep: "89218230"}, permission: "citizen"},
headers: @auth_headers
@resp_token = response.headers['access-token']
......@@ -346,13 +346,13 @@ class CityHallsControllerTest < ActionDispatch::IntegrationTest
test "cep should have been changed" do
@city_hall = CityHall.where(city_id: @joinville.id).first
assert_equal "7654321", @city_hall.cep
assert_equal "89218230", @city_hall.cep
end
end
describe "Unsuccessful resquest to update city hall that doesn't exists" do
before do
put '/v1/city_halls/222', params: {city_hall: {cep: "7654321"}, permission: "citizen"},
put '/v1/city_halls/222', params: {city_hall: {cep: "81530110"}, permission: "citizen"},
headers: @auth_headers
@body = JSON.parse(response.body)
......
......@@ -20,7 +20,7 @@ class OccupationsControllerTest < ActionDispatch::IntegrationTest
@citizen = Citizen.new(
cpf: "10845922904",
birth_date: "Apr 18 1997",
cep: "1234567",
cep: "80530336",
email: "test@example.com",
name: "Test Example",
phone1: "(12)1212-1212",
......@@ -40,7 +40,7 @@ class OccupationsControllerTest < ActionDispatch::IntegrationTest
@curitiba_city_hall = CityHall.new(
name: "Prefeitura de Curitiba",
cep: "1234567",
cep: "81530110",
neighborhood: "Test neighborhood",
address_street: "Test street",
address_number: "123",
......
......@@ -21,7 +21,7 @@ class Api::V1::ProfessionalsControllerTest < ActionDispatch::IntegrationTest
active: true,
cpf: "10845922904",
birth_date: "18/04/1997",
cep: "1234567",
cep: "81530110",
email: "test@example.com",
name: "Test Example",
phone1: "(12)1212-1212",
......@@ -40,7 +40,7 @@ class Api::V1::ProfessionalsControllerTest < ActionDispatch::IntegrationTest
@curitiba_city_hall = CityHall.new(
name: "Prefeitura de Curitiba",
cep: "1234567",
cep: "80530336",
neighborhood: "Test neighborhood",
address_street: "Test street",
address_number: "123",
......
......@@ -21,7 +21,7 @@ class Api::V1::SchedulesControllerTest < ActionDispatch::IntegrationTest
cpf: "10845922904",
active: true,
birth_date: "Apr 18 1997",
cep: "1234567",
cep: "89218230",
email: "test@example.com",
name: "Test Example",
phone1: "(12)1212-1212",
......@@ -40,7 +40,7 @@ class Api::V1::SchedulesControllerTest < ActionDispatch::IntegrationTest
@city_hall = CityHall.new(
name: "Prefeitura de Joinville",
cep: "81530110",
cep: "89218230",
neighborhood: "Aasdsd",
address_street: "asdasd",
address_number: "100",
......
......@@ -21,7 +21,7 @@ class SectorsControllerTest < ActionDispatch::IntegrationTest
cpf: "10845922904",
active: true,
birth_date: "Apr 18 1997",
cep: "1234567",
cep: "89218230",
email: "test@example.com",
name: "Test Example",
phone1: "(12)1212-1212",
......@@ -40,7 +40,7 @@ class SectorsControllerTest < ActionDispatch::IntegrationTest
@city_hall = CityHall.new(
name: "Prefeitura de Joinville",
cep: "81530110",
cep: "89218230",
neighborhood: "Aasdsd",
address_street: "asdasd",
address_number: "100",
......
......@@ -21,7 +21,7 @@ class Api::V1::ServicePlacesControllerTest < ActionDispatch::IntegrationTest
active: true,
cpf: "10845922904",
birth_date: "Apr 18 1997",
cep: "1234567",
cep: "81530110",
email: "test@example.com",
name: "Test Example",
phone1: "(12)1212-1212",
......
......@@ -21,7 +21,7 @@ class ServiceTypesControllerTest < ActionDispatch::IntegrationTest
cpf: "10845922904",
active: true,
birth_date: "Apr 18 1997",
cep: "1234567",
cep: "81530110",
email: "test@example.com",
name: "Test Example",
phone1: "(12)1212-1212",
......
......@@ -21,7 +21,7 @@ class Api::V1::ShiftsControllerTest < ActionDispatch::IntegrationTest
cpf: "10845922904",
active: true,
birth_date: "Apr 18 1997",
cep: "1234567",
cep: "89218230",
email: "test@example.com",
name: "Test Example",
phone1: "(12)1212-1212",
......@@ -40,7 +40,7 @@ class Api::V1::ShiftsControllerTest < ActionDispatch::IntegrationTest
@city_hall = CityHall.new(
name: "Prefeitura de Joinville",
cep: "81530110",
cep: "89218230",
neighborhood: "Aasdsd",
address_street: "asdasd",
address_number: "100",
......
......@@ -21,7 +21,7 @@ class SolicitationsControllerTest < ActionDispatch::IntegrationTest
active: true,
cpf: "10845922904",
birth_date: "Apr 18 1997",
cep: "1234567",
cep: "81530110",
email: "test@example.com",
name: "Test Example",
phone1: "(12)1212-1212",
......
......@@ -17,7 +17,7 @@ class CitizenTest < ActiveSupport::TestCase
@curitiba.save!
@citizen = Citizen.new(birth_date: "Apr 18 1997",
cep: "1234567",
cep: "81530110",
email: "test@example.com",
name: "Test Example",
phone1: "(12)1212-1212",
......@@ -46,7 +46,7 @@ class CitizenTest < ActiveSupport::TestCase
before do
@citizen = Citizen.new(birth_date: "Apr 18 1997",
cep: "1234567",
cep: "81530110",
email: "test@example.com",
name: "Test Example",
phone1: "(12)1212-1212",
......@@ -73,7 +73,7 @@ class CitizenTest < ActiveSupport::TestCase
describe "Missing city" do
before do
@citizen = Citizen.new(birth_date: "Apr 18 1997",
cep: "1234567",
cep: "81530110",
email: "test@example.com",
name: "Test Example",
phone1: "(12)1212-1212",
......@@ -99,7 +99,7 @@ class CitizenTest < ActiveSupport::TestCase
before do
@citizen = Citizen.new(cpf: "11111111111",
birth_date: "Apr 18 1997",
cep: "1234567",
cep: "81530110",
email: "test@example.com",
name: "Test Example",
phone1: "(12)1212-1212",
......@@ -124,7 +124,7 @@ class CitizenTest < ActiveSupport::TestCase
describe "Missing birth date" do
before do
@citizen = Citizen.new(cpf: "10845922904",
cep: "1234567",
cep: "81530110",
email: "test@example.com",
name: "Test Example",
phone1: "(12)1212-1212",
......@@ -175,7 +175,7 @@ class CitizenTest < ActiveSupport::TestCase
before do
@citizen = Citizen.new(cpf: "10845922904",
birth_date: "Apr 18 1997",
cep: "1234567",
cep: "81530110",
email: "test@example.com",
phone1: "(12)1212-1212",
rg: "1234567")
......@@ -200,7 +200,7 @@ class CitizenTest < ActiveSupport::TestCase
before do
@citizen = Citizen.new(cpf: "10845922904",
birth_date: "Apr 18 1997",
cep: "1234567",
cep: "81530110",
email: "test@example.com",
name: "Test Example",
rg: "1234567")
......@@ -225,7 +225,7 @@ class CitizenTest < ActiveSupport::TestCase
before do
@citizen = Citizen.new(cpf: "10845922904",
birth_date: "Apr 18 1997",
cep: "1234567",
cep: "81530110",
email: "test@example.com",
name: "Test Example",
phone1: "(12)1212-1212")
......
......@@ -13,7 +13,7 @@ class CityTest < ActiveSupport::TestCase
state_id: @parana.id)
@curitiba.save!
@city_hall = CityHall.new(cep: "1234567",
@city_hall = CityHall.new(cep: "81530110",
neighborhood: "Test neighborhood",
address_street: "Test street",
address_number: "123",
......@@ -76,7 +76,7 @@ class CityTest < ActiveSupport::TestCase
@curitiba.save!
@city_hall = CityHall.new(name: "Prefeitura de Curitiba",
cep: "1234567",
cep: "81530110",
address_street: "Test street",
address_number: "123",
city_id: @curitiba.id,
......@@ -107,7 +107,7 @@ class CityTest < ActiveSupport::TestCase
@curitiba.save!
@city_hall = CityHall.new(name: "Prefeitura de Curitiba",
cep: "1234567",
cep: "81530110",
neighborhood: "Test neighborhood",
address_number: "123",
city_id: @curitiba.id,
......@@ -138,7 +138,7 @@ class CityTest < ActiveSupport::TestCase
@curitiba.save!
@city_hall = CityHall.new(name: "Prefeitura de Curitiba",
cep: "1234567",
cep: "81530110",
neighborhood: "Test neighborhood",
address_street: "Test street",
city_id: @curitiba.id,
......@@ -169,7 +169,7 @@ class CityTest < ActiveSupport::TestCase
@curitiba.save!
@city_hall = CityHall.new(name: "Prefeitura de Curitiba",
cep: "1234567",
cep: "81530110",
neighborhood: "Test neighborhood",
address_street: "Test street",
address_number: "123",
......@@ -200,7 +200,7 @@ class CityTest < ActiveSupport::TestCase
@curitiba.save!
@city_hall = CityHall.new(name: "Prefeitura de Curitiba",
cep: "1234567",
cep: "81530110",
neighborhood: "Test neighborhood",
address_street: "Test street",
address_number: "123",
......@@ -231,7 +231,7 @@ class CityTest < ActiveSupport::TestCase
@curitiba.save!
@city_hall = CityHall.new(name: "Prefeitura de Curitiba",
cep: "1234567",
cep: "81530110",
neighborhood: "Test neighborhood",
address_street: "Test street",
address_number: "123",
......
......@@ -16,7 +16,7 @@ class DependantTest < ActiveSupport::TestCase
@citizen = Citizen.new(cpf: "10845922904",
active: true,
birth_date: "Apr 18 1997",
cep: "1234567",
cep: "89218230",
email: "test@example.com",
name: "Test Example",
phone1: "(12)1212-1212",
......
......@@ -12,7 +12,7 @@ class OccupationTest < ActiveSupport::TestCase
state_id: @parana.id)
@curitiba.save!
@curitiba_city_hall = CityHall.new(name: "Prefeitura de Curitiba",
cep: "1234567",
cep: "81530110",
neighborhood: "Test neighborhood",
address_street: "Test street",
address_number: "123",
......
......@@ -16,7 +16,7 @@ class ShiftTest < ActiveSupport::TestCase
@citizen = Citizen.new(cpf: "10845922904",
active: true,
birth_date: "Apr 18 1997",
cep: "1234567",
cep: "89218230",
email: "test@example.com",
name: "Test Example",
phone1: "(12)1212-1212",
......
......@@ -16,7 +16,7 @@ class ScheduleTest < ActiveSupport::TestCase
@citizen = Citizen.new(cpf: "10845922904",
active: true,
birth_date: "Apr 18 1997",
cep: "1234567",
cep: "89218230",
email: "test@example.com",
name: "Test Example",
phone1: "(12)1212-1212",
......
......@@ -16,7 +16,7 @@ class ShiftTest < ActiveSupport::TestCase
@citizen = Citizen.new(cpf: "10845922904",
active: true,
birth_date: "Apr 18 1997",
cep: "1234567",
cep: "89218230",
email: "test@example.com",
name: "Test Example",
phone1: "(12)1212-1212",
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment