Refactor invoice PDF generation, download and delivery

- Remove `Que::Mailer` (#895)
- Extract controllers
- Extract translations
- Convert HAML to ERB
- Add mailer preview
- Improve UI
- Remove unused routes
- Add tests
This commit is contained in:
Artur Beljajev 2019-04-07 19:10:11 +03:00
parent 7e0fd30125
commit 27ea790b28
30 changed files with 288 additions and 138 deletions

View file

@ -9,6 +9,7 @@ one:
vat_rate: 0.1
total: 16.50
reference_no: 13
number: 1
for_payments_test:
number: 1

View file

@ -10,7 +10,7 @@ bestnames:
country_code: US
accounting_customer_code: bestnames
language: en
billing_email: billing@example.com
billing_email: billing@bestnames.test
website: https://bestnames.test
reference_no: 13

View file

@ -6,8 +6,14 @@ class AdminAreaInvoicesIntegrationTest < ApplicationIntegrationTest
sign_in users(:admin)
end
def test_download_invoice_pdf
get admin_invoice_download_pdf_path(@invoice)
def test_downloads_invoice
assert_equal 1, @invoice.number
get download_admin_invoice_path(@invoice)
assert_response :ok
assert_equal 'application/pdf', response.headers['Content-Type']
assert_equal 'attachment; filename="invoice-1.pdf"', response.headers['Content-Disposition']
assert_not_empty response.body
end
end

View file

@ -6,8 +6,14 @@ class RegistrarAreaInvoicesIntegrationTest < ApplicationIntegrationTest
sign_in users(:api_bestnames)
end
def test_download_invoice_pdf
get download_pdf_registrar_invoice_path(@invoice)
def test_downloads_invoice
assert_equal 1, @invoice.number
get download_registrar_invoice_path(@invoice)
assert_response :ok
assert_equal 'application/pdf', response.headers['Content-Type']
assert_equal 'attachment; filename="invoice-1.pdf"', response.headers['Content-Disposition']
assert_not_empty response.body
end
end

View file

@ -0,0 +1,22 @@
require 'test_helper'
class InvoiceMailerTest < ActiveSupport::TestCase
include ActionMailer::TestHelper
setup do
@invoice = invoices(:one)
ActionMailer::Base.deliveries.clear
end
def test_delivers_invoice_email
assert_equal 1, @invoice.number
email = InvoiceMailer.invoice_email(invoice: @invoice, recipient: 'billing@bestnames.test')
.deliver_now
assert_emails 1
assert_equal ['billing@bestnames.test'], email.to
assert_equal 'Invoice no. 1', email.subject
assert email.attachments['invoice-1.pdf']
end
end

View file

@ -0,0 +1,6 @@
class InvoiceMailerPreview < ActionMailer::Preview
def invoice_email
invoice = Invoice.first
InvoiceMailer.invoice_email(invoice: invoice, recipient: 'billing@registrar.test')
end
end

View file

@ -1,9 +1,13 @@
require 'test_helper'
class AdminAreaInvoicesTest < ApplicationSystemTestCase
include ActionMailer::TestHelper
setup do
sign_in users(:admin)
@invoice = invoices(:one)
ActionMailer::Base.deliveries.clear
end
def test_cancels_an_invoice
@ -17,4 +21,23 @@ class AdminAreaInvoicesTest < ApplicationSystemTestCase
assert @invoice.cancelled?
assert_text 'Invoice has been cancelled'
end
def test_invoice_delivery_form_is_pre_populated_with_billing_email_of_a_registrar
assert_equal 'billing@bestnames.test', @invoice.buyer.billing_email
visit new_admin_invoice_delivery_url(@invoice)
assert_field 'Recipient', with: 'billing@bestnames.test'
end
def test_delivers_an_invoice
visit admin_invoice_url(@invoice)
click_on 'Send'
fill_in 'Recipient', with: 'billing@registrar.test'
click_on 'Send'
assert_emails 1
email = ActionMailer::Base.deliveries.first
assert_equal ['billing@registrar.test'], email.to
assert_current_path admin_invoice_path(@invoice)
assert_text 'Invoice has been sent'
end
end

View file

@ -1,9 +1,13 @@
require 'test_helper'
class RegistrarAreaInvoicesTest < ApplicationSystemTestCase
include ActionMailer::TestHelper
setup do
sign_in users(:api_bestnames)
@invoice = invoices(:one)
ActionMailer::Base.deliveries.clear
end
def test_cancels_an_invoice
@ -17,4 +21,23 @@ class RegistrarAreaInvoicesTest < ApplicationSystemTestCase
assert @invoice.cancelled?
assert_text 'Invoice has been cancelled'
end
def test_invoice_delivery_form_is_pre_populated_with_billing_email_of_a_registrar
assert_equal 'billing@bestnames.test', @invoice.buyer.billing_email
visit new_registrar_invoice_delivery_url(@invoice)
assert_field 'Recipient', with: 'billing@bestnames.test'
end
def test_delivers_an_invoice
visit registrar_invoice_url(@invoice)
click_on 'Send'
fill_in 'Recipient', with: 'billing@registrar.test'
click_on 'Send'
assert_emails 1
email = ActionMailer::Base.deliveries.first
assert_equal ['billing@registrar.test'], email.to
assert_current_path registrar_invoice_path(@invoice)
assert_text 'Invoice has been sent'
end
end