Skip to content

Commit

Permalink
Ajusta testes para melhorar o coverage (98.67%)
Browse files Browse the repository at this point in the history
  • Loading branch information
RyanOxon committed Jul 22, 2024
1 parent 37711bd commit 4cceaa2
Show file tree
Hide file tree
Showing 9 changed files with 87 additions and 14 deletions.
4 changes: 0 additions & 4 deletions app/controllers/application_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,6 @@ def authorize_condo_manager(condo)
not_authorized_redirect unless authorize_manager(condo)
end

def authorize_condo_resident(condo)
not_authorized_redirect if authorize_resident(condo)
end

def authorize_user(condo)
not_authorized_redirect unless authorize_manager(condo) || authorize_resident(condo)
end
Expand Down
7 changes: 5 additions & 2 deletions app/controllers/condos_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,12 @@ def add_manager

def associate_manager
@manager = Manager.find(params[:manager_id])
return redirect_to @condo, notice: I18n.t('notices.condo.manager_associated') if @condo.managers << @manager
if @condo.managers.exclude?(@manager) && (@condo.managers << @manager)
return redirect_to @condo,
notice: I18n.t('notices.condo.manager_associated')
end

redirect_to @condo, alert: I18n.t('notices.condo.manager_not_associated')
redirect_to @condo, alert: I18n.t('alerts.condo.manager_not_associated')
end

def residents
Expand Down
4 changes: 0 additions & 4 deletions app/controllers/towers_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -78,10 +78,6 @@ def tower_params
params.require(:tower).permit :name, :floor_quantity, :units_per_floor
end

def condo_id_param
params.require :condo_id
end

def authenticate_manager!
return redirect_to root_path, notice: I18n.t('alerts.tower.access_denied') if resident_signed_in?

Expand Down
4 changes: 1 addition & 3 deletions app/models/bill.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,7 @@ def total_value_formatted
end

def rent_fee_value_formatted
return "R$ #{format('%.2f', values['rent_fee_cents'] / 100.0).gsub('.', ',')}" if values['rent_fee_cents'].positive?

false
"R$ #{format('%.2f', values['rent_fee_cents'] / 100.0).gsub('.', ',')}"
end

def self.format_value(value)
Expand Down
10 changes: 10 additions & 0 deletions spec/models/resident_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,5 +33,15 @@
expect(resident.errors).to include :registration_number
expect(resident.errors.full_messages).to include 'CPF deve estar no seguinte formato: XXX.XXX.XXX-XX'
end

it 'receipt must be valid' do
resident = build :resident, :with_residence
resident.receipt.attach(io: File.open('spec/support/images/test_image.txt'),
filename: 'test_image.txt')

expect(resident).not_to be_valid
expect(resident.errors).to include :receipt
expect(resident.errors.full_messages).to include 'Comprovante deve ser um PDF, JPEG, JPG, ou PNG'
end
end
end
28 changes: 28 additions & 0 deletions spec/requests/condo_requests_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
require 'rails_helper'

describe 'Condo requests' do
context 'POST /condos/:id/associate_manager' do
it 'and condo manager is already associated' do
manager = create :manager
condo_manager = create :manager, is_super: false
condo = create :condo
condo.managers << condo_manager

login_as manager, scope: :manager
post associate_manager_condo_path(condo), params: { manager_id: condo_manager.id }

expect(flash[:alert]).to eq 'Não foi possível adicionar o administrador'
end
end

context 'GET /condos/:id/residents' do
it 'and condo not found' do
manager = create :manager

login_as manager, scope: :manager
get residents_condo_path(1)

expect(response).to have_http_status :not_found
end
end
end
1 change: 1 addition & 0 deletions spec/support/images/test_image.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Para testar o tipo de imagem
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,22 @@
expect(page).not_to have_content 'ver mais'
end
end

it 'with missing params' do
manager = create :manager, full_name: 'Rodrigo Silva'
condo = create :condo
condo.managers << manager

login_as manager, scope: :manager
visit condo_path condo
within '#announcement_board' do
click_on 'Novo'
end
fill_in 'Título', with: ''
click_on 'Salvar'

expect(page).to have_current_path new_condo_announcement_path condo
expect(page).to have_content 'Mensagem não pode ficar em branco'
expect(page).to have_content 'Título não pode ficar em branco'
end
end
25 changes: 24 additions & 1 deletion spec/system/fees/user_send_receipt_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
expect(page).to have_content 'Para continuar, faça login ou registre-se.'
end

it 'and succesfully send the image to the external api' do
it 'and successfully send the image to the external api' do
condo = create :condo
resident = create(:resident, :with_residence, condo:)
json_data_details = Rails.root.join('spec/support/json/bill_1_details.json').read
Expand Down Expand Up @@ -89,4 +89,27 @@
expect(page).to have_content 'Impossível enviar o comprovante ao servidor do PagueAluguel'
expect(current_path).to eq bills_path
end

it 'and fail to sends the image to the external api because of missing image' do
condo = create :condo
resident = create(:resident, :with_residence, condo:)
json_data_details = Rails.root.join('spec/support/json/bill_1_details.json').read
fake_response_details = double('faraday_response', body: json_data_details, success?: true)
fake_response_open_bills = double('faraday_response', body: '{"bills":[]}', success?: true)
fake_response_receipt = double('faraday_response', body: '{"message":"Comprovante recebido com sucesso."}',
success?: true)

allow(Faraday).to receive(:get).with("http://localhost:4000/api/v1/units/#{resident.residence.id}/bills").and_return(fake_response_open_bills)
allow(Faraday).to receive(:get).with("http://localhost:4000/api/v1/bills/#{@first_bill_id_from_five_json}").and_return(fake_response_details)
allow(Faraday).to receive(:post).and_return(fake_response_receipt)

login_as resident, scope: :resident
visit bill_path @first_bill_id_from_five_json
click_on 'Enviar Comprovante'

click_on 'Enviar'

expect(page).to have_content 'Comprovante não pode ficar vazio'
expect(current_path).to eq new_bill_receipt_path @first_bill_id_from_five_json
end
end

0 comments on commit 4cceaa2

Please sign in to comment.