mirror of
https://github.com/openfoodfoundation/openfoodnetwork
synced 2026-01-30 21:27:17 +00:00
Add tests around invoice printing in PDF
- use PDF Reader to read pdf and get its content - check with we print the payment description information section with the right value
This commit is contained in:
1
Gemfile
1
Gemfile
@@ -153,6 +153,7 @@ group :test do
|
||||
gem 'test-prof'
|
||||
gem 'webmock'
|
||||
gem 'rails-controller-testing'
|
||||
gem 'pdf-reader'
|
||||
# See spec/spec_helper.rb for instructions
|
||||
# gem 'perftools.rb'
|
||||
end
|
||||
|
||||
11
Gemfile.lock
11
Gemfile.lock
@@ -47,6 +47,7 @@ PATH
|
||||
GEM
|
||||
remote: https://rubygems.org/
|
||||
specs:
|
||||
Ascii85 (1.1.0)
|
||||
actioncable (5.2.6)
|
||||
actionpack (= 5.2.6)
|
||||
nio4r (~> 2.0)
|
||||
@@ -113,6 +114,7 @@ GEM
|
||||
activerecord (>= 4.2)
|
||||
addressable (2.7.0)
|
||||
public_suffix (>= 2.0.2, < 5.0)
|
||||
afm (0.2.2)
|
||||
andand (1.3.3)
|
||||
angular-rails-templates (1.1.0)
|
||||
railties (>= 4.2, < 7)
|
||||
@@ -284,6 +286,7 @@ GEM
|
||||
temple (>= 0.8.0)
|
||||
tilt
|
||||
hashdiff (1.0.1)
|
||||
hashery (2.1.2)
|
||||
highline (2.0.3)
|
||||
i18n (1.8.10)
|
||||
concurrent-ruby (~> 1.0)
|
||||
@@ -384,6 +387,12 @@ GEM
|
||||
xml-simple
|
||||
paypal-sdk-merchant (1.117.2)
|
||||
paypal-sdk-core (~> 0.3.0)
|
||||
pdf-reader (2.4.2)
|
||||
Ascii85 (~> 1.0)
|
||||
afm (~> 0.2.1)
|
||||
hashery (~> 2.0)
|
||||
ruby-rc4
|
||||
ttfunk
|
||||
pg (1.2.3)
|
||||
power_assert (2.0.0)
|
||||
pry (0.13.1)
|
||||
@@ -571,6 +580,7 @@ GEM
|
||||
thread_safe (0.3.6)
|
||||
tilt (2.0.10)
|
||||
timecop (0.9.4)
|
||||
ttfunk (1.7.0)
|
||||
tzinfo (1.2.9)
|
||||
thread_safe (~> 0.1)
|
||||
uglifier (4.2.0)
|
||||
@@ -694,6 +704,7 @@ DEPENDENCIES
|
||||
paperclip (~> 3.4.1)
|
||||
paranoia (~> 2.4)
|
||||
paypal-sdk-merchant (= 1.117.2)
|
||||
pdf-reader
|
||||
pg (~> 1.2.3)
|
||||
pry
|
||||
pry-byebug
|
||||
|
||||
103
spec/features/admin/invoice_print_spec.rb
Normal file
103
spec/features/admin/invoice_print_spec.rb
Normal file
@@ -0,0 +1,103 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
require "spec_helper"
|
||||
|
||||
feature '
|
||||
As an administrator
|
||||
I want to print a invoice as PDF
|
||||
', js: false do
|
||||
include WebHelper
|
||||
include AuthenticationHelper
|
||||
|
||||
let(:user) { create(:user) }
|
||||
let(:product) { create(:simple_product) }
|
||||
let(:distributor) { create(:distributor_enterprise, owner: user, charges_sales_tax: true) }
|
||||
let(:order_cycle) do
|
||||
create(:simple_order_cycle, name: 'One', distributors: [distributor],
|
||||
variants: [product.variants.first])
|
||||
end
|
||||
|
||||
let(:order) do
|
||||
create(:order_with_totals_and_distribution, user: user, distributor: distributor,
|
||||
order_cycle: order_cycle, state: 'complete',
|
||||
payment_state: 'balance_due')
|
||||
end
|
||||
|
||||
describe "that contains right Payment Description at Checkout information" do
|
||||
let!(:payment_method1) do
|
||||
create(:stripe_sca_payment_method, distributors: [distributor], description: "description1")
|
||||
end
|
||||
let!(:payment_method2) do
|
||||
create(:stripe_sca_payment_method, distributors: [distributor], description: "description2")
|
||||
end
|
||||
|
||||
context "with no payment" do
|
||||
it "do not display the payment description information" do
|
||||
login_as_admin_and_visit spree.print_admin_order_path(order)
|
||||
convert_pdf_to_page
|
||||
expect(page).to have_no_content 'Payment Description at Checkout'
|
||||
end
|
||||
end
|
||||
|
||||
context "with one payment" do
|
||||
let!(:payment1) do
|
||||
create(:payment, order: order, state: 'completed', payment_method: payment_method1)
|
||||
end
|
||||
before do
|
||||
order.save!
|
||||
end
|
||||
|
||||
it "display the payment description section" do
|
||||
login_as_admin_and_visit spree.print_admin_order_path(order)
|
||||
convert_pdf_to_page
|
||||
expect(page).to have_content 'Payment Description at Checkout'
|
||||
expect(page).to have_content 'description1'
|
||||
end
|
||||
end
|
||||
|
||||
context "with two payments, and one that failed" do
|
||||
before do
|
||||
order.update payments: []
|
||||
order.payments << create(:payment, order: order, state: 'completed',
|
||||
payment_method: payment_method1, created_at: 1.day.ago)
|
||||
order.payments << create(:payment, order: order, state: 'failed',
|
||||
payment_method: payment_method2, created_at: 2.days.ago)
|
||||
order.save!
|
||||
end
|
||||
|
||||
it "display the payment description section and use the one from the completed payment" do
|
||||
login_as_admin_and_visit spree.print_admin_order_path(order)
|
||||
convert_pdf_to_page
|
||||
expect(page).to have_content 'Payment Description at Checkout'
|
||||
expect(page).to have_content 'description1'
|
||||
end
|
||||
end
|
||||
|
||||
context "with two completed payments" do
|
||||
before do
|
||||
order.update payments: []
|
||||
order.payments << create(:payment, order: order, state: 'completed',
|
||||
payment_method: payment_method1, created_at: 2.days.ago)
|
||||
order.payments << create(:payment, order: order, state: 'completed',
|
||||
payment_method: payment_method2, created_at: 1.day.ago)
|
||||
order.save!
|
||||
end
|
||||
|
||||
it "display the payment description section and use the one from the last payment" do
|
||||
login_as_admin_and_visit spree.print_admin_order_path(order)
|
||||
convert_pdf_to_page
|
||||
expect(page).to have_content 'Payment Description at Checkout'
|
||||
expect(page).to have_content 'description2'
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
def convert_pdf_to_page
|
||||
temp_pdf = Tempfile.new('pdf')
|
||||
temp_pdf << page.source.force_encoding('UTF-8')
|
||||
reader = PDF::Reader.new(temp_pdf)
|
||||
pdf_text = reader.pages.map(&:text)
|
||||
temp_pdf.close
|
||||
page.driver.response.instance_variable_set('@body', pdf_text)
|
||||
end
|
||||
Reference in New Issue
Block a user