mirror of
https://github.com/internetee/registry.git
synced 2025-07-29 22:16:19 +02:00
Merge pull request #2531 from internetee/68-creating-syncronization-of-invoice-changes
creating sync with billing
This commit is contained in:
commit
da514f9a50
11 changed files with 715 additions and 60 deletions
105
test/models/invoice_state_machinte_test.rb
Normal file
105
test/models/invoice_state_machinte_test.rb
Normal file
|
@ -0,0 +1,105 @@
|
|||
require 'test_helper'
|
||||
|
||||
class InvoiceTest < ActiveSupport::TestCase
|
||||
include ActionMailer::TestHelper
|
||||
|
||||
setup do
|
||||
@invoice = invoices(:one)
|
||||
@unpaid = invoices(:unpaid)
|
||||
|
||||
stub_request(:post, 'https://eis_billing_system:3000/api/v1/invoice_generator/invoice_status')
|
||||
.with(
|
||||
body: '{"invoice_number":2,"status":"paid"}'
|
||||
)
|
||||
.to_return(status: 200, body: '', headers: {})
|
||||
end
|
||||
|
||||
def test_unpaid_invoice_can_be_change_status_to_paid
|
||||
assert !@unpaid.paid?
|
||||
|
||||
InvoiceStateMachine.new(invoice: @unpaid, status: 'paid').call
|
||||
@unpaid.reload
|
||||
|
||||
assert @unpaid.paid?
|
||||
end
|
||||
|
||||
def test_no_any_errors_if_invoice_with_unpaid_status_set_again_unpaid
|
||||
assert !@unpaid.paid?
|
||||
|
||||
InvoiceStateMachine.new(invoice: @unpaid, status: 'unpaid').call
|
||||
@unpaid.reload
|
||||
|
||||
assert !@unpaid.paid?
|
||||
assert @unpaid.errors.empty?
|
||||
end
|
||||
|
||||
def test_only_unpaid_invoice_can_be_cancelled
|
||||
assert !@unpaid.paid?
|
||||
|
||||
InvoiceStateMachine.new(invoice: @unpaid, status: 'cancelled').call
|
||||
@unpaid.reload
|
||||
|
||||
assert @unpaid.cancelled?
|
||||
|
||||
assert @invoice.paid?
|
||||
InvoiceStateMachine.new(invoice: @invoice, status: 'cancelled').call
|
||||
@invoice.reload
|
||||
|
||||
assert_equal @invoice.errors.full_messages.join, 'Inavalid state cancelled'
|
||||
assert @invoice.errors.present?
|
||||
end
|
||||
|
||||
def test_cancelled_invoiced_cannot_be_unpaid
|
||||
assert !@unpaid.paid?
|
||||
|
||||
InvoiceStateMachine.new(invoice: @unpaid, status: 'cancelled').call
|
||||
@unpaid.reload
|
||||
|
||||
assert @unpaid.cancelled?
|
||||
|
||||
InvoiceStateMachine.new(invoice: @unpaid, status: 'unpaid').call
|
||||
@unpaid.reload
|
||||
|
||||
assert @unpaid.cancelled?
|
||||
|
||||
assert @unpaid.errors.present?
|
||||
assert_equal @unpaid.errors.full_messages.join, 'Inavalid state unpaid'
|
||||
end
|
||||
|
||||
def test_if_paid_invoice_not_have_response_from_everypay_it_can_be_unpaid_back
|
||||
assert !@unpaid.paid?
|
||||
|
||||
InvoiceStateMachine.new(invoice: @unpaid, status: 'paid').call
|
||||
@unpaid.reload
|
||||
|
||||
assert @unpaid.paid?
|
||||
assert_nil @unpaid.payment_orders.last.payment_reference?
|
||||
|
||||
InvoiceStateMachine.new(invoice: @unpaid, status: 'unpaid').call
|
||||
@unpaid.reload
|
||||
|
||||
assert !@unpaid.paid?
|
||||
end
|
||||
|
||||
def test_if_paid_invoice_has_response_from_everypay_it_cannot_be_rollback
|
||||
assert !@unpaid.paid?
|
||||
|
||||
InvoiceStateMachine.new(invoice: @unpaid, status: 'paid').call
|
||||
@unpaid.reload
|
||||
|
||||
assert @unpaid.paid?
|
||||
payment_order = @unpaid.payment_orders.last
|
||||
payment_order.response = {}
|
||||
payment_order.response[:payment_reference] = 'responsefromeveryapy'
|
||||
payment_order.save && payment_order.reload
|
||||
|
||||
assert @unpaid.payment_orders.last.payment_reference?
|
||||
|
||||
InvoiceStateMachine.new(invoice: @unpaid, status: 'unpaid').call
|
||||
@unpaid.reload
|
||||
|
||||
assert @unpaid.paid?
|
||||
assert @unpaid.errors.present?
|
||||
assert_equal @unpaid.errors.full_messages.join, 'Inavalid state unpaid'
|
||||
end
|
||||
end
|
|
@ -6,6 +6,8 @@ class InvoiceTest < ActiveSupport::TestCase
|
|||
setup do
|
||||
@invoice = invoices(:one)
|
||||
Spy.on_instance_method(EisBilling::BaseController, :authorized).and_return(true)
|
||||
stub_request(:post, 'https://eis_billing_system:3000/api/v1/invoice_generator/invoice_status')
|
||||
.to_return(status: :ok, headers: {})
|
||||
end
|
||||
|
||||
def test_fixture_is_valid
|
||||
|
@ -117,32 +119,32 @@ class InvoiceTest < ActiveSupport::TestCase
|
|||
transaction.sum = 250
|
||||
|
||||
invoice_n = Invoice.order(number: :desc).last.number
|
||||
stub_request(:post, "https://eis_billing_system:3000/api/v1/invoice_generator/invoice_number_generator")
|
||||
stub_request(:post, 'https://eis_billing_system:3000/api/v1/invoice_generator/invoice_number_generator')
|
||||
.to_return(status: 200, body: "{\"invoice_number\":\"#{invoice_n + 3}\"}", headers: {})
|
||||
|
||||
stub_request(:post, "https://eis_billing_system:3000/api/v1/invoice_generator/invoice_generator")
|
||||
.to_return(status: 200, body: "{\"everypay_link\":\"http://link.test\"}", headers: {})
|
||||
stub_request(:post, 'https://eis_billing_system:3000/api/v1/invoice_generator/invoice_generator')
|
||||
.to_return(status: 200, body: '{"everypay_link":"http://link.test"}', headers: {})
|
||||
|
||||
stub_request(:put, "https://registry:3000/eis_billing/e_invoice_response").
|
||||
to_return(status: 200, body: "{\"invoice_number\":\"#{invoice_n + 3}\"}, {\"date\":\"#{Time.zone.now-10.minutes}\"}", headers: {})
|
||||
stub_request(:put, 'https://registry:3000/eis_billing/e_invoice_response')
|
||||
.to_return(status: 200, body: "{\"invoice_number\":\"#{invoice_n + 3}\"}, {\"date\":\"#{Time.zone.now - 10.minutes}\"}", headers: {})
|
||||
|
||||
stub_request(:post, "https://eis_billing_system:3000/api/v1/e_invoice/e_invoice").
|
||||
to_return(status: 200, body: "", headers: {})
|
||||
stub_request(:post, 'https://eis_billing_system:3000/api/v1/e_invoice/e_invoice')
|
||||
.to_return(status: 200, body: '', headers: {})
|
||||
|
||||
invoice = Invoice.create_from_transaction!(transaction)
|
||||
assert_equal 250, invoice.total
|
||||
|
||||
invoice_n = Invoice.order(number: :desc).last.number
|
||||
stub_request(:post, "https://eis_billing_system:3000/api/v1/invoice_generator/invoice_number_generator").
|
||||
to_return(status: 200, body: "{\"invoice_number\":\"#{invoice_n + 4}\"}", headers: {})
|
||||
stub_request(:post, 'https://eis_billing_system:3000/api/v1/invoice_generator/invoice_number_generator')
|
||||
.to_return(status: 200, body: "{\"invoice_number\":\"#{invoice_n + 4}\"}", headers: {})
|
||||
|
||||
transaction.sum = 146.88
|
||||
invoice = Invoice.create_from_transaction!(transaction)
|
||||
assert_equal 146.88, invoice.total
|
||||
|
||||
invoice_n = Invoice.order(number: :desc).last.number
|
||||
stub_request(:post, "https://eis_billing_system:3000/api/v1/invoice_generator/invoice_number_generator").
|
||||
to_return(status: 200, body: "{\"invoice_number\":\"#{invoice_n + 5}\"}", headers: {})
|
||||
stub_request(:post, 'https://eis_billing_system:3000/api/v1/invoice_generator/invoice_number_generator')
|
||||
.to_return(status: 200, body: "{\"invoice_number\":\"#{invoice_n + 5}\"}", headers: {})
|
||||
|
||||
transaction.sum = 0.99
|
||||
invoice = Invoice.create_from_transaction!(transaction)
|
||||
|
@ -151,14 +153,14 @@ class InvoiceTest < ActiveSupport::TestCase
|
|||
|
||||
def test_emails_invoice_after_creating_topup_invoice
|
||||
invoice_n = Invoice.order(number: :desc).last.number
|
||||
stub_request(:post, "https://eis_billing_system:3000/api/v1/invoice_generator/invoice_generator").
|
||||
to_return(status: 200, body: "{\"everypay_link\":\"http://link.test\"}", headers: {})
|
||||
stub_request(:post, 'https://eis_billing_system:3000/api/v1/invoice_generator/invoice_generator')
|
||||
.to_return(status: 200, body: '{"everypay_link":"http://link.test"}', headers: {})
|
||||
|
||||
stub_request(:put, "https://registry:3000/eis_billing/e_invoice_response").
|
||||
to_return(status: 200, body: "{\"invoice_number\":\"#{invoice_n + 3}\"}, {\"date\":\"#{Time.zone.now-10.minutes}\"}", headers: {})
|
||||
stub_request(:put, 'https://registry:3000/eis_billing/e_invoice_response')
|
||||
.to_return(status: 200, body: "{\"invoice_number\":\"#{invoice_n + 3}\"}, {\"date\":\"#{Time.zone.now - 10.minutes}\"}", headers: {})
|
||||
|
||||
stub_request(:post, "https://eis_billing_system:3000/api/v1/e_invoice/e_invoice").
|
||||
to_return(status: 200, body: "", headers: {})
|
||||
stub_request(:post, 'https://eis_billing_system:3000/api/v1/e_invoice/e_invoice')
|
||||
.to_return(status: 200, body: '', headers: {})
|
||||
|
||||
registrar = registrars(:bestnames)
|
||||
transaction = bank_transactions(:one).dup
|
||||
|
@ -172,4 +174,73 @@ class InvoiceTest < ActiveSupport::TestCase
|
|||
Invoice.create_from_transaction!(transaction)
|
||||
end
|
||||
end
|
||||
|
||||
def test_create_payment_order
|
||||
everypay_response = {
|
||||
'some' => 'some'
|
||||
}
|
||||
@invoice.payment_orders.delete_all
|
||||
@invoice.account_activity.delete and @invoice.reload
|
||||
assert @invoice.payment_orders.empty?
|
||||
assert_nil @invoice.account_activity
|
||||
|
||||
@invoice.process_payment(payment_type: 'PaymentOrders::EveryPay',
|
||||
everypay_response: everypay_response,
|
||||
payment_status: 'paid',
|
||||
sum: @invoice.total,
|
||||
transaction_time: Time.zone.now - 10.minutes - 23.seconds)
|
||||
|
||||
@invoice.reload
|
||||
|
||||
assert_equal @invoice.payment_orders.count, 1
|
||||
assert @invoice.account_activity.present?
|
||||
end
|
||||
|
||||
def test_should_raise_error_if_bill_already_paid
|
||||
everypay_response = {
|
||||
'some' => 'some'
|
||||
}
|
||||
assert @invoice.payment_orders.present?
|
||||
|
||||
assert_raises(ActiveRecord::RecordInvalid) do
|
||||
@invoice.process_payment(payment_type: 'PaymentOrders::EveryPay',
|
||||
everypay_response: everypay_response,
|
||||
payment_status: 'paid',
|
||||
sum: @invoice.total,
|
||||
transaction_time: Time.zone.now - 10.minutes - 23.seconds)
|
||||
end
|
||||
end
|
||||
|
||||
def test_should_manually_autobin_invoice
|
||||
@invoice.payment_orders.destroy_all && @invoice.account_activity.destroy
|
||||
@invoice.reload
|
||||
|
||||
account = @invoice.buyer.accounts.first
|
||||
item = @invoice.items.first
|
||||
|
||||
assert @invoice.payment_orders.empty?
|
||||
assert @invoice.account_activity.nil?
|
||||
assert_equal account.balance.to_f, 100.0
|
||||
assert_equal item.price, 5.0
|
||||
|
||||
@invoice.autobind_manually
|
||||
@invoice.reload
|
||||
account.reload
|
||||
|
||||
assert_equal account.balance.to_f, 105.0
|
||||
assert @invoice.payment_orders.present?
|
||||
assert @invoice.account_activity.present?
|
||||
end
|
||||
|
||||
def test_cannot_to_increase_balance_already_paid_invoice_by_manually_autobind
|
||||
assert @invoice.paid?
|
||||
|
||||
account = @invoice.buyer.accounts.first
|
||||
assert_equal account.balance.to_f, 100.0
|
||||
|
||||
@invoice.autobind_manually
|
||||
@invoice.reload
|
||||
|
||||
assert_equal account.balance.to_f, 100.0
|
||||
end
|
||||
end
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue