2019-01-09 12:07:31 +01:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2021-01-04 16:26:44 +01:00
|
|
|
require 'test_helper'
|
|
|
|
|
2019-01-09 12:07:31 +01:00
|
|
|
class InvoicesTest < ActionDispatch::IntegrationTest
|
|
|
|
# Called before every test method runs. Can be used
|
|
|
|
# to set up fixture information.
|
|
|
|
def setup
|
|
|
|
@admin = User.find_by(username: 'admin')
|
|
|
|
login_as(@admin, scope: :user)
|
|
|
|
end
|
|
|
|
|
|
|
|
test 'admin list invoices' do
|
2020-03-13 17:10:38 +01:00
|
|
|
post '/api/invoices/list', params: { query: {
|
2019-01-09 12:07:31 +01:00
|
|
|
number: '',
|
|
|
|
customer: '',
|
|
|
|
date: nil,
|
|
|
|
order_by: '-reference',
|
|
|
|
page: 1,
|
|
|
|
size: 20 # test db may have < 20 invoices
|
2020-03-13 17:10:38 +01:00
|
|
|
} }.to_json, headers: default_headers
|
2019-01-09 12:07:31 +01:00
|
|
|
|
|
|
|
# Check response format & status
|
|
|
|
assert_equal 200, response.status, response.body
|
2020-03-13 17:10:38 +01:00
|
|
|
assert_equal Mime[:json], response.content_type
|
2019-01-09 12:07:31 +01:00
|
|
|
|
|
|
|
# Check that we have all invoices
|
|
|
|
invoices = json_response(response.body)
|
|
|
|
assert_equal Invoice.count, invoices.size, 'some invoices are missing'
|
|
|
|
|
|
|
|
# Check that invoices are ordered by reference
|
2021-01-04 16:26:44 +01:00
|
|
|
first_invoice = Invoice.order(:reference).limit(1).first
|
|
|
|
last_invoice = Invoice.order(reference: :desc).limit(1).first
|
|
|
|
assert_equal last_invoice.reference, invoices.first[:reference]
|
|
|
|
assert_equal first_invoice.reference, invoices.last[:reference]
|
2019-01-09 12:07:31 +01:00
|
|
|
end
|
|
|
|
|
2019-01-10 12:40:42 +01:00
|
|
|
test 'admin generates a refund' do
|
2019-12-02 15:29:05 +01:00
|
|
|
date = DateTime.current.iso8601
|
2019-01-10 12:40:42 +01:00
|
|
|
|
2020-03-13 17:10:38 +01:00
|
|
|
post '/api/invoices', params: { avoir: {
|
2019-01-10 12:40:42 +01:00
|
|
|
avoir_date: date,
|
2019-09-17 14:48:06 +02:00
|
|
|
payment_method: 'cash',
|
2019-01-10 12:40:42 +01:00
|
|
|
description: 'Lorem ipsum',
|
|
|
|
invoice_id: 4,
|
|
|
|
invoice_items_ids: [4],
|
|
|
|
subscription_to_expire: false
|
2020-03-13 17:10:38 +01:00
|
|
|
} }.to_json, headers: default_headers
|
2019-01-10 12:40:42 +01:00
|
|
|
|
|
|
|
# Check response format & status
|
|
|
|
assert_equal 201, response.status, response.body
|
2020-03-13 17:10:38 +01:00
|
|
|
assert_equal Mime[:json], response.content_type
|
2019-01-10 12:40:42 +01:00
|
|
|
|
|
|
|
# Check that the refund match
|
|
|
|
refund = json_response(response.body)
|
|
|
|
avoir = Avoir.find(refund[:id])
|
|
|
|
|
|
|
|
assert_dates_equal date, refund[:avoir_date]
|
|
|
|
assert_dates_equal date, refund[:date]
|
2019-09-17 14:48:06 +02:00
|
|
|
assert_equal 'cash', refund[:payment_method]
|
2019-01-10 12:40:42 +01:00
|
|
|
assert_equal false, refund[:has_avoir]
|
|
|
|
assert_equal 4, refund[:invoice_id]
|
|
|
|
assert_equal 4, refund[:items][0][:invoice_item_id]
|
|
|
|
assert_match %r{^[0-9]+/A$}, refund[:reference]
|
|
|
|
assert_equal 'Lorem ipsum', avoir.description
|
2019-06-03 16:51:43 +02:00
|
|
|
|
|
|
|
# Check footprint
|
|
|
|
assert avoir.check_footprint
|
2019-01-10 12:40:42 +01:00
|
|
|
end
|
|
|
|
|
2019-01-10 14:16:59 +01:00
|
|
|
test 'admin fails generates a refund in closed period' do
|
|
|
|
date = '2015-10-01T13:09:55+01:00'.to_datetime
|
|
|
|
|
2020-03-13 17:10:38 +01:00
|
|
|
post '/api/invoices', params: { avoir: {
|
2019-01-10 14:16:59 +01:00
|
|
|
avoir_date: date,
|
2019-09-17 14:48:06 +02:00
|
|
|
payment_method: 'cash',
|
2019-01-10 14:16:59 +01:00
|
|
|
description: 'Unable to refund',
|
|
|
|
invoice_id: 5,
|
|
|
|
invoice_items_ids: [5],
|
|
|
|
subscription_to_expire: false
|
2020-03-13 17:10:38 +01:00
|
|
|
} }.to_json, headers: default_headers
|
2019-01-10 14:16:59 +01:00
|
|
|
|
|
|
|
# Check response format & status
|
|
|
|
assert_equal 422, response.status, response.body
|
2020-03-13 17:10:38 +01:00
|
|
|
assert_equal Mime[:json], response.content_type
|
2019-01-10 14:16:59 +01:00
|
|
|
|
|
|
|
# Check the error was handled
|
2019-01-10 15:12:22 +01:00
|
|
|
assert_match(/#{I18n.t('errors.messages.in_closed_period')}/, response.body)
|
2019-01-10 14:16:59 +01:00
|
|
|
end
|
2019-01-09 12:07:31 +01:00
|
|
|
end
|