From cf1c8685464ea8c8c56c5cb82733be592bbf46c6 Mon Sep 17 00:00:00 2001 From: Sylvain Date: Thu, 1 Dec 2016 11:37:09 +0100 Subject: [PATCH] test suite is now testing pdf files content --- Gemfile | 1 + Gemfile.lock | 13 ++++++++++++- test/test_helper.rb | 34 ++++++++++++++++++++++++++++++++++ 3 files changed, 47 insertions(+), 1 deletion(-) diff --git a/Gemfile b/Gemfile index b0250b9a7..f0f5a271f 100644 --- a/Gemfile +++ b/Gemfile @@ -62,6 +62,7 @@ group :test do gem 'webmock' gem 'vcr' gem 'byebug' + gem 'pdf-reader' end group :production do diff --git a/Gemfile.lock b/Gemfile.lock index 6e602afad..694325a16 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -1,6 +1,7 @@ GEM remote: https://rubygems.org/ specs: + Ascii85 (1.0.2) aasm (4.1.0) actionmailer (4.2.5) actionpack (= 4.2.5) @@ -41,6 +42,7 @@ GEM thread_safe (~> 0.3, >= 0.3.4) tzinfo (~> 1.1) addressable (2.3.8) + afm (0.2.2) ansi (1.5.0) api-pagination (4.3.0) apipie-rails (0.3.6) @@ -177,6 +179,7 @@ GEM has_secure_token (1.0.0) activerecord (>= 3.0) hashdiff (0.3.0) + hashery (2.1.2) hashie (3.4.2) highline (1.7.1) hike (1.2.3) @@ -265,6 +268,12 @@ GEM httparty (~> 0.13) orm_adapter (0.5.0) pdf-core (0.5.1) + pdf-reader (1.4.0) + Ascii85 (~> 1.0.0) + afm (~> 0.2.1) + hashery (~> 2.0) + ruby-rc4 + ttfunk pg (0.18.1) pkg-config (1.1.7) prawn (2.0.1) @@ -335,6 +344,7 @@ GEM netrc (~> 0.7) rolify (4.0.0) ruby-progressbar (1.7.5) + ruby-rc4 (0.1.5) rubyzip (1.1.7) rufus-scheduler (3.0.9) tzinfo @@ -488,6 +498,7 @@ DEPENDENCIES omniauth omniauth-oauth2 openlab_ruby + pdf-reader pg prawn prawn-table @@ -522,4 +533,4 @@ DEPENDENCIES webmock BUNDLED WITH - 1.12.5 + 1.13.1 diff --git a/test/test_helper.rb b/test/test_helper.rb index 792006eac..484055c31 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -68,6 +68,31 @@ class ActiveSupport::TestCase assert File.exist?(invoice.file), 'Invoice PDF was not generated' + # now we check the file content + reader = PDF::Reader.new(invoice.file) + assert_equal 1, reader.page_count # single page invoice + + ht_amount = invoice.total + page = reader.pages.first + lines = page.text.scan(/^.+/) + lines.each do |line| + # check that the numbers printed into the PDF file match the total stored in DB + if line.include? I18n.t('invoices.total_amount') + assert_equal invoice.total / 100.0, parse_amount_from_invoice_line(line), 'Invoice total rendered in the PDF file does not match' + end + + # check that the VAT was correctly applied if it was configured + if line.include? I18n.t('invoices.including_total_excluding_taxes') + ht_amount = parse_amount_from_invoice_line(line) + end + end + + if Setting.find_by(name: 'invoice_VAT-active').value == 'true' + vat_rate = Setting.find_by({name: 'invoice_VAT-rate'}).value.to_f + assert_equal (invoice.total / (vat_rate / 100 + 1)), ht_amount, 'Total excluding taxes rendered in the PDF file is not computed correct' + else + assert_equal invoice.total, ht_amount, 'VAT information was rendered in the PDF file despite that VAT was disabled' + end File.delete(invoice.file) end @@ -89,6 +114,15 @@ class ActiveSupport::TestCase skip('Unable to test export which is not of the category "statistics"') end end + + private + + # Parse a line of text read from a PDF file and return the price included inside + # Line of text should be of form 'Label $10.00' + # @returns {float} + def parse_amount_from_invoice_line line + line[line.rindex(' ')+1..-1].tr(I18n.t('number.currency.format.unit'), '').to_f + end end class ActionDispatch::IntegrationTest