From f6b1db2ccee9ddc1d49d6e5b23252c23d41fa7a1 Mon Sep 17 00:00:00 2001 From: Maciej Szlosarczyk Date: Thu, 19 Apr 2018 10:44:40 +0300 Subject: [PATCH] Fix leaking test --- .../registrar/payments_controller.rb | 19 ++-- app/models/payments/bank_link.rb | 4 +- spec/factories/registrar.rb | 1 - test/fixtures/bank_transactions.yml | 5 + test/fixtures/invoices.yml | 4 +- .../registrar/billing/balance_top_up_test.rb | 3 + .../invoices/new_invoice_payment_test.rb | 16 +-- .../registrar/invoices/new_test.rb | 8 -- .../invoices/payment_callback_test.rb | 0 .../registrar/invoices/payment_return_test.rb | 99 +++++++++++++++++++ test/models/payments/bank_link_test.rb | 2 +- test/models/registry_test.rb | 4 + 12 files changed, 134 insertions(+), 31 deletions(-) create mode 100644 test/integration/registrar/invoices/payment_callback_test.rb create mode 100644 test/integration/registrar/invoices/payment_return_test.rb diff --git a/app/controllers/registrar/payments_controller.rb b/app/controllers/registrar/payments_controller.rb index d70dcce7e..2998ecbc7 100644 --- a/app/controllers/registrar/payments_controller.rb +++ b/app/controllers/registrar/payments_controller.rb @@ -6,25 +6,19 @@ class Registrar skip_before_action :authenticate_user!, :check_ip_restriction, only: [:back, :callback] # before_action :check_bank - # to handle existing model we should - # get invoice_id and then get number - # build BankTransaction without connection with right reference number - # do not connect transaction and invoice # TODO: Refactor to :new def pay invoice = Invoice.find(params[:invoice_id]) opts = { return_url: self.registrar_return_payment_with_url(params[:bank], invoice_id: invoice.id), # TODO: Add required URL - response_url: "https://5fd921b0.ngrok.io/registrar/pay/callback/every_pay" + response_url: "https://53e21cc8.ngrok.io/registrar/pay/callback/every_pay" } @payment = ::Payments.create_with_type(params[:bank], invoice, opts) @payment.create_transaction end - # connect invoice and transaction - # both back and IPN # TODO: Refactor to be restful def back invoice = Invoice.find(params[:invoice_id]) @@ -60,11 +54,14 @@ class Registrar private - # def banks - # ENV['payments_banks'].split(",").map(&:strip) - # end + def check_supported_payment_method + unless supported_payment_method? + raise StandardError.new("Not supported payment method") + end + end - def check_bank + + def supported_payment_method? raise StandardError.new("Not Implemented bank") unless banks.include?(params[:bank]) end end diff --git a/app/models/payments/bank_link.rb b/app/models/payments/bank_link.rb index 1ae0436cd..8451f676f 100644 --- a/app/models/payments/bank_link.rb +++ b/app/models/payments/bank_link.rb @@ -1,7 +1,7 @@ module Payments class BankLink < Base # TODO: Remove magic numbers, convert certain fields to proper constants - # TODO: Remove hashrockets + # DONE: Remove hashrockets def form_fields @fields ||= hash = {} hash["VK_SERVICE"] = "1012" @@ -36,6 +36,8 @@ module Payments private + SUCCESS_FIELDS = + def validate_success pars = %w(VK_SERVICE VK_VERSION VK_SND_ID VK_REC_ID VK_STAMP VK_T_NO VK_AMOUNT VK_CURR VK_REC_ACC VK_REC_NAME VK_SND_ACC VK_SND_NAME VK_REF VK_MSG VK_T_DATETIME).freeze diff --git a/spec/factories/registrar.rb b/spec/factories/registrar.rb index 90ea38a45..ab46553a0 100644 --- a/spec/factories/registrar.rb +++ b/spec/factories/registrar.rb @@ -7,7 +7,6 @@ FactoryBot.define do city 'test' state 'test' zip 'test' - vat_rate 0.1 email 'test@test.com' country_code 'EE' accounting_customer_code 'test' diff --git a/test/fixtures/bank_transactions.yml b/test/fixtures/bank_transactions.yml index b0b4d661c..290be1597 100644 --- a/test/fixtures/bank_transactions.yml +++ b/test/fixtures/bank_transactions.yml @@ -1,3 +1,8 @@ one: sum: 1 currency: EUR + +for_payments_test: + description: "Order nr. 1" + currency: "EUR" + iban: "1234" diff --git a/test/fixtures/invoices.yml b/test/fixtures/invoices.yml index c4a8037a3..71189f6b6 100644 --- a/test/fixtures/invoices.yml +++ b/test/fixtures/invoices.yml @@ -34,4 +34,6 @@ overdue: for_payments_test: <<: *DEFAULTS - total: 100.00 + total: 12.00 + id: 1 + number: 1 diff --git a/test/integration/registrar/billing/balance_top_up_test.rb b/test/integration/registrar/billing/balance_top_up_test.rb index c77c9ff37..15face70c 100644 --- a/test/integration/registrar/billing/balance_top_up_test.rb +++ b/test/integration/registrar/billing/balance_top_up_test.rb @@ -6,6 +6,7 @@ class BalanceTopUpTest < ActionDispatch::IntegrationTest end def test_creates_new_invoice + original_vat_prc = Setting.registry_vat_prc Setting.registry_vat_prc = 0.1 visit registrar_invoices_url @@ -21,5 +22,7 @@ class BalanceTopUpTest < ActionDispatch::IntegrationTest assert_equal BigDecimal(10), invoice.vat_rate assert_equal BigDecimal('28.05'), invoice.total assert_text 'Please pay the following invoice' + + Setting.registry_vat_prc = original_vat_prc end end diff --git a/test/integration/registrar/invoices/new_invoice_payment_test.rb b/test/integration/registrar/invoices/new_invoice_payment_test.rb index ae830107a..c3f48e3d0 100644 --- a/test/integration/registrar/invoices/new_invoice_payment_test.rb +++ b/test/integration/registrar/invoices/new_invoice_payment_test.rb @@ -18,21 +18,21 @@ class NewInvoicePaymentTest < ActionDispatch::IntegrationTest create_invoice_and_visit_its_page click_link_or_button 'Seb' form = page.find('form') - assert_equal 'https://www.seb.ee/cgi-bin/dv.sh/ipank.r', form['action'] - assert_equal 'post', form['method'] - assert_equal '220.00', form.find_by_id('VK_AMOUNT', visible: false).value + assert_equal('https://www.seb.ee/cgi-bin/dv.sh/ipank.r', form['action']) + assert_equal('post', form['method']) + assert_equal('240.00', form.find_by_id('VK_AMOUNT', visible: false).value) end def test_create_new_Every_Pay_payment create_invoice_and_visit_its_page click_link_or_button 'Every pay' expected_hmac_fields = 'account_id,amount,api_username,callback_url,' + - 'customer_url,hmac_fields,nonce,order_reference,timestamp,transaction_type' + 'customer_url,hmac_fields,nonce,order_reference,timestamp,transaction_type' form = page.find('form') - assert_equal 'https://igw-demo.every-pay.com/transactions/', form['action'] - assert_equal 'post', form['method'] - assert_equal expected_hmac_fields, form.find_by_id('hmac_fields', visible: false).value - assert_equal '220.0', form.find_by_id('amount', visible: false).value + assert_equal('https://igw-demo.every-pay.com/transactions/', form['action']) + assert_equal('post', form['method']) + assert_equal(expected_hmac_fields, form.find_by_id('hmac_fields', visible: false).value) + assert_equal('240.0', form.find_by_id('amount', visible: false).value) end end diff --git a/test/integration/registrar/invoices/new_test.rb b/test/integration/registrar/invoices/new_test.rb index 89a3dd8a3..35957dddc 100644 --- a/test/integration/registrar/invoices/new_test.rb +++ b/test/integration/registrar/invoices/new_test.rb @@ -4,14 +4,6 @@ class NewInvoiceTest < ActionDispatch::IntegrationTest setup do @user = users(:api_bestnames) login_as @user - @original_vat_rate = @user.registrar.vat_rate - @user.registrar.vat_rate = 0.2 - end - - teardown do - @user.registrar.vat_rate = @original_vat_rate - AccountActivity.destroy_all - Invoice.destroy_all end def test_show_balance diff --git a/test/integration/registrar/invoices/payment_callback_test.rb b/test/integration/registrar/invoices/payment_callback_test.rb new file mode 100644 index 000000000..e69de29bb diff --git a/test/integration/registrar/invoices/payment_return_test.rb b/test/integration/registrar/invoices/payment_return_test.rb new file mode 100644 index 000000000..9b4aef46d --- /dev/null +++ b/test/integration/registrar/invoices/payment_return_test.rb @@ -0,0 +1,99 @@ +require 'test_helper' + +class PaymentReturnTest < ActionDispatch::IntegrationTest + setup do + @user = users(:api_bestnames) + login_as @user + end + + def create_invoice_with_items + @invoice = invoices(:for_payments_test) + invoice_item = invoice_items(:one) + + @invoice.invoice_items << invoice_item + @invoice.invoice_items << invoice_item + @user.registrar.invoices << @invoice + end + + def every_pay_request_params + { + nonce: "392f2d7748bc8cb0d14f263ebb7b8932", + timestamp: "1524136727", + api_username: "ca8d6336dd750ddb", + transaction_result: "completed", + payment_reference: "fd5d27b59a1eb597393cd5ff77386d6cab81ae05067e18d530b10f3802e30b56", + payment_state: "settled", + amount: "12.0", + order_reference: "e468a2d59a731ccc546f2165c3b1a6", + account_id: "EUR3D1", + cc_type: "master_card", + cc_last_four_digits: "0487", + cc_month: "10", + cc_year: "2018", + cc_holder_name: "John Doe", + hmac_fields: "account_id,amount,api_username,cc_holder_name,cc_last_four_digits,cc_month,cc_type,cc_year,hmac_fields,nonce,order_reference,payment_reference,payment_state,timestamp,transaction_result", + hmac: "72fc94f117389cf5d34dba18a18d20886edb2bbb", + invoice_id: "12900000", + } + end + + def bank_link_request_params + { + "VK_SERVICE": "1111", + "VK_VERSION": "008", + "VK_SND_ID": "KIAupMEE's", + "VK_AMOUNT": "12.00", + "VK_REC_ID": "1235", + "VK_CURR": "EUR", + "VK_T_NO": "1234", + "VK_STAMP": "ahdfjkadsfhjk", + "VK_REC_ACC": "1234", + "VK_REC_NAME": "John Doe", + "VK_SND_ACC": "1234", + "VK_SND_NAME": "Doe John", + "VK_REF": "1234", + "VK_MSG": "Foo", + "VK_T_DATETIME": "2018-04-19T15:52:59+0300", + invoice_id: "12900000", + } + end + + def test_every_pay_return_creates_activity_redirects_to_invoice_path + create_invoice_with_items + request_params = every_pay_request_params.merge(invoice_id: @invoice.id) + + account_activity_count = AccountActivity.count + post "/registrar/pay/return/every_pay", request_params + assert_equal(302, response.status) + assert_redirected_to(registrar_invoice_path(@invoice)) + assert_equal(account_activity_count + 1, AccountActivity.count) + end + + def test_Every_Pay_return_raises_RecordNotFound + create_invoice_with_items + request_params = every_pay_request_params.merge(invoice_id: "178907") + assert_raises(ActiveRecord::RecordNotFound) do + post "/registrar/pay/return/every_pay", request_params + end + end + + def test_bank_link_return_redirects_to_invoice_paths + skip("Need credentials to model the expected request") + create_invoice_with_items + request_params = every_pay_request_params.merge(invoice_id: @invoice.id) + account_activity_count = AccountActivity.count + + post "/registrar/pay/return/seb", request_params + assert_equal(302, response.status) + assert_redirected_to(registrar_invoice_path(@invoice)) + assert_equal(account_activity_count + 1, AccountActivity.count) + end + + def test_bank_link_return + create_invoice_with_items + request_params = bank_link_request_params.merge(invoice_id: "178907") + assert_raises(ActiveRecord::RecordNotFound) do + post "/registrar/pay/return/seb", request_params + end + end +end diff --git a/test/models/payments/bank_link_test.rb b/test/models/payments/bank_link_test.rb index 5297a530d..ef5b26d02 100644 --- a/test/models/payments/bank_link_test.rb +++ b/test/models/payments/bank_link_test.rb @@ -4,7 +4,7 @@ class BankLinkTest < ActiveSupport::TestCase def setup super @invoice = invoices(:valid) - params = {return_url: 'return.url', response_url: 'response_url'} + params = {return_url: 'return.url', response_url: 'response.url'} @bank_link = Payments::BankLink.new('seb', @invoice, params) travel_to '2018-04-01 00:30' diff --git a/test/models/registry_test.rb b/test/models/registry_test.rb index 90a603e15..80c88c8bf 100644 --- a/test/models/registry_test.rb +++ b/test/models/registry_test.rb @@ -10,7 +10,11 @@ class RegistryTest < ActiveSupport::TestCase end def test_vat_rate + original_vat_prc = Setting.registry_vat_prc Setting.registry_vat_prc = 0.25 + assert_equal BigDecimal(25), @registry.vat_rate + + Setting.registry_vat_prc = original_vat_prc end end