Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rafatoração de chamadas da API para proprietário #130

Merged
merged 1 commit into from
Jul 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions app/models/property_owner.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,16 @@ class PropertyOwner < ApplicationRecord
private

def document_number_must_be_valid
return if cpf_valid?(document_number)
return if document_number_valid?(document_number)

errors.add(:document_number, :invalid, message: I18n.t('devise.failure.invalid_document_number'))
end

def cpf_valid?(cpf)
return false if cpf.blank?
def document_number_valid?(document_number)
return false if document_number.blank?

url = "#{Rails.configuration.api['base_url']}/property?cpf=#{cpf}"
registration_number = CPF.new(document_number).formatted
url = "#{Rails.configuration.api['base_url']}/check_owner?registration_number=#{registration_number}"
response = Faraday.get(url)
response.success?
end
Expand Down
7 changes: 5 additions & 2 deletions app/models/unit.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,11 @@ def self.find(unit_id)
build_new_unit(data)
end

def self.find_all_by_owner(cpf)
response = Faraday.get("#{Rails.configuration.api['base_url']}/get_owner_properties?registration_number=#{cpf}")
def self.find_all_by_owner(document_number)
registration_number = CPF.new(document_number).formatted
response = Faraday.get(
"#{Rails.configuration.api['base_url']}/get_owner_properties?registration_number=#{registration_number}"
)
return [] unless response.success?

data = JSON.parse(response.body)
Expand Down
14 changes: 9 additions & 5 deletions spec/models/property_owner_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
RSpec.describe PropertyOwner, type: :model do
context 'shoulda_matchers' do
before do
allow_any_instance_of(PropertyOwner).to receive(:cpf_valid?).and_return(true)
allow_any_instance_of(PropertyOwner).to receive(:document_number_valid?).and_return(true)
end
it { should validate_presence_of(:email) }
it { should validate_presence_of(:password) }
Expand All @@ -12,11 +12,13 @@
end

it 'com CPF válido' do
cpf = CPF.generate
cpf = CPF.new(CPF.generate).formatted
fake_response = double('faraday_response', success?: true, body: 'Proprietário')

allow(Faraday).to(
receive(:get).with("#{Rails.configuration.api['base_url']}/property?cpf=#{cpf}").and_return(fake_response)
receive(:get).with(
"#{Rails.configuration.api['base_url']}/check_owner?registration_number=#{cpf}"
).and_return(fake_response)
)

owner = PropertyOwner.new(email: 'solidsnake@mgs.com', password: 'password', document_number: cpf)
Expand All @@ -25,11 +27,13 @@
end

it 'com CPF inválido' do
cpf = CPF.generate
cpf = CPF.new(CPF.generate).formatted
fake_response = double('faraday_response', success?: false, body: 'Proprietário')

allow(Faraday).to(
receive(:get).with("#{Rails.configuration.api['base_url']}/property?cpf=#{cpf}").and_return(fake_response)
receive(:get).with(
"#{Rails.configuration.api['base_url']}/check_owner?registration_number=#{cpf}"
).and_return(fake_response)
)

owner = PropertyOwner.new(email: 'solidsnake@mgs.com', password: 'password', document_number: cpf)
Expand Down
2 changes: 1 addition & 1 deletion spec/models/unit_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@

context '.find_all_by_owner' do
it 'retorna todas as unidades de um proprietario' do
cpf = CPF.generate
cpf = CPF.new(CPF.generate).formatted
data = Rails.root.join('spec/support/json/owner_units.json').read
response = double('response', success?: true, body: data)
allow(Faraday).to(
Expand Down
Loading