From 12e32af524bc177eae070c8de54c10c92c6a0704 Mon Sep 17 00:00:00 2001 From: Martin Lensment Date: Thu, 23 Apr 2015 10:59:45 +0300 Subject: [PATCH] Refactor email sending, tests --- .../registrar/invoices_controller.rb | 4 +--- app/models/invoice.rb | 6 +++--- spec/features/registrar/invoices_spec.rb | 21 ++++++++++++++++++- 3 files changed, 24 insertions(+), 7 deletions(-) diff --git a/app/controllers/registrar/invoices_controller.rb b/app/controllers/registrar/invoices_controller.rb index 452ca7a4b..f985e4670 100644 --- a/app/controllers/registrar/invoices_controller.rb +++ b/app/controllers/registrar/invoices_controller.rb @@ -20,9 +20,7 @@ class Registrar::InvoicesController < RegistrarController @invoice.billing_email = params[:invoice][:billing_email] - if @invoice.forward - pdf = @invoice.pdf(render_to_string('pdf', layout: false)) - InvoiceMailer.invoice_email(@invoice, pdf).deliver_now + if @invoice.forward(render_to_string('pdf', layout: false)) flash[:notice] = t('invoice_forwared') redirect_to([:registrar, @invoice]) else diff --git a/app/models/invoice.rb b/app/models/invoice.rb index 61a04e2a9..9c1f1484b 100644 --- a/app/models/invoice.rb +++ b/app/models/invoice.rb @@ -55,11 +55,11 @@ class Invoice < ActiveRecord::Base "invoice-#{number}.pdf" end - def forward + def forward(html) return false unless valid? - return false unless billing_email + return false unless billing_email.present? - # TODO: forward invoice + InvoiceMailer.invoice_email(self, pdf(html)).deliver_now true end diff --git a/spec/features/registrar/invoices_spec.rb b/spec/features/registrar/invoices_spec.rb index 60758ae5d..9fc049d6a 100644 --- a/spec/features/registrar/invoices_spec.rb +++ b/spec/features/registrar/invoices_spec.rb @@ -4,7 +4,7 @@ feature 'Invoices', type: :feature do before :all do create_settings @user = Fabricate(:api_user) - Fabricate(:invoice) + @invoice = Fabricate(:invoice, buyer: @user.registrar) end context 'as unknown user' do @@ -32,5 +32,24 @@ feature 'Invoices', type: :feature do visit '/registrar/invoices' page.should have_text('Invoices') end + + it 'should forward invoice' do + visit '/registrar/invoices' + click_link @invoice.to_s + click_link 'Forward invoice' + click_button 'Forward' + page.should have_text('Failed to forward invoice') + fill_in 'Billing email', with: 'test@test.ee' + click_button 'Forward' + page.should have_text('Invoice forwarded') + end + + it 'should download invoice' do + visit '/registrar/invoices' + click_link @invoice.to_s + click_link 'Download' + response_headers['Content-Type'].should == 'application/pdf' + response_headers['Content-Disposition'].should == "attachment; filename=\"#{@invoice.pdf_name}\"" + end end end