diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index f7a0e0b..b8a23c2 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -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 diff --git a/app/controllers/condos_controller.rb b/app/controllers/condos_controller.rb index b0b152b..5b21f6d 100644 --- a/app/controllers/condos_controller.rb +++ b/app/controllers/condos_controller.rb @@ -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 diff --git a/app/controllers/towers_controller.rb b/app/controllers/towers_controller.rb index b369de0..892b843 100644 --- a/app/controllers/towers_controller.rb +++ b/app/controllers/towers_controller.rb @@ -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? diff --git a/app/models/bill.rb b/app/models/bill.rb index 4ff6e4d..cb539b1 100644 --- a/app/models/bill.rb +++ b/app/models/bill.rb @@ -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) diff --git a/spec/models/resident_spec.rb b/spec/models/resident_spec.rb index 583d48d..db8fb97 100644 --- a/spec/models/resident_spec.rb +++ b/spec/models/resident_spec.rb @@ -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 diff --git a/spec/requests/condo_requests_spec.rb b/spec/requests/condo_requests_spec.rb new file mode 100644 index 0000000..5b19efa --- /dev/null +++ b/spec/requests/condo_requests_spec.rb @@ -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 diff --git a/spec/support/images/test_image.txt b/spec/support/images/test_image.txt new file mode 100644 index 0000000..90b16f0 --- /dev/null +++ b/spec/support/images/test_image.txt @@ -0,0 +1 @@ +Para testar o tipo de imagem \ No newline at end of file diff --git a/spec/system/announcements/manager_registers_new_announcement_spec.rb b/spec/system/announcements/manager_registers_new_announcement_spec.rb index 67ce5a8..a2cd970 100644 --- a/spec/system/announcements/manager_registers_new_announcement_spec.rb +++ b/spec/system/announcements/manager_registers_new_announcement_spec.rb @@ -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 diff --git a/spec/system/fees/user_send_receipt_spec.rb b/spec/system/fees/user_send_receipt_spec.rb index b6e0ae8..8fbcd20 100644 --- a/spec/system/fees/user_send_receipt_spec.rb +++ b/spec/system/fees/user_send_receipt_spec.rb @@ -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 @@ -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