Added additional tests to REPP API

This commit is contained in:
Sergei Tsõganov 2022-06-08 12:07:21 +03:00
parent b505de2f0f
commit b502c2779e
21 changed files with 796 additions and 7 deletions

View file

@ -0,0 +1,94 @@
require 'test_helper'
class ReppV1InvoicesAddCreditTest < ActionDispatch::IntegrationTest
def setup
@user = users(:api_bestnames)
token = Base64.encode64("#{@user.username}:#{@user.plain_text_password}")
token = "Basic #{token}"
@auth_headers = { 'Authorization' => token }
@original_registry_vat_rate = Setting.registry_vat_prc
eis_response = OpenStruct.new(body: '{"everypay_link":"https://link.test"}')
Spy.on_instance_method(EisBilling::AddDeposits, :send_invoice).and_return(eis_response)
Spy.on_instance_method(EisBilling::BaseController, :authorized).and_return(true)
end
teardown do
Setting.registry_vat_prc = @original_registry_vat_rate
end
def test_generates_add_credit_invoice_with_billing_system
request_body = {
invoice: {
amount: 100,
description: 'Add credit',
},
}
Setting.registry_vat_prc = 0.1
ENV['billing_system_integrated'] = 'true'
if Feature.billing_system_integrated?
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 + 3}\"}", headers: {})
stub_request(:post, 'https://eis_billing_system:3000/api/v1/e_invoice/e_invoice')
.to_return(status: 200, body: '', headers: {})
end
post '/repp/v1/invoices/add_credit', headers: @auth_headers,
params: request_body
json = JSON.parse(response.body, symbolize_names: true)
assert_response :ok
assert_equal 1000, json[:code]
assert_equal 'Command completed successfully', json[:message]
assert_not json[:data][:invoice][:paid]
assert json[:data][:invoice][:payable]
assert json[:data][:invoice][:cancellable]
assert_equal json[:data][:invoice][:payment_link], 'https://link.test'
assert_equal json[:data][:invoice][:total], 110.to_f.to_s
end
def test_generates_add_credit_invoice_without_billing_system
request_body = {
invoice: {
amount: 100,
description: 'Add credit',
},
}
Setting.registry_vat_prc = 0.1
ENV['billing_system_integrated'] = 'false'
post '/repp/v1/invoices/add_credit', headers: @auth_headers,
params: request_body
json = JSON.parse(response.body, symbolize_names: true)
assert_response :ok
assert_equal 1000, json[:code]
assert_equal 'Command completed successfully', json[:message]
assert_not json[:data][:invoice][:paid]
assert json[:data][:invoice][:payable]
assert json[:data][:invoice][:cancellable]
assert_equal json[:data][:invoice][:total], 110.to_f.to_s
end
def test_generates_add_credit_invoice_with_invalid_amount
request_body = {
invoice: {
amount: 0.4,
description: 'Add credit',
},
}
Setting.minimum_deposit = 0.5
post '/repp/v1/invoices/add_credit', headers: @auth_headers,
params: request_body
json = JSON.parse(response.body, symbolize_names: true)
assert_response :bad_request
assert_equal "Amount is too small. Minimum deposit is #{Setting.minimum_deposit} EUR", json[:message]
end
end

View file

@ -0,0 +1,44 @@
require 'test_helper'
class ReppV1InvoicesCancelTest < ActionDispatch::IntegrationTest
def setup
@user = users(:api_bestnames)
token = Base64.encode64("#{@user.username}:#{@user.plain_text_password}")
token = "Basic #{token}"
@auth_headers = { 'Authorization' => token }
end
def test_cancels_invoice
invoice = invoices(:one)
invoice.account_activity = nil
assert invoice.cancellable?
stub_request(:post, 'https://eis_billing_system:3000/api/v1/invoice_generator/invoice_status')
.to_return(status: :ok, headers: {})
put "/repp/v1/invoices/#{invoice.id}/cancel", headers: @auth_headers
json = JSON.parse(response.body, symbolize_names: true)
assert_response :ok
assert_equal 1000, json[:code]
assert_equal 'Command completed successfully', json[:message]
invoice.reload
assert invoice.cancelled?
assert json[:data][:invoice].is_a? Hash
end
def test_cancels_uncancellable_invoice
invoice = invoices(:one)
assert_not invoice.cancellable?
put "/repp/v1/invoices/#{invoice.id}/cancel", headers: @auth_headers
json = JSON.parse(response.body, symbolize_names: true)
assert_response :bad_request
assert_equal 'Invoice status prohibits operation', json[:message]
invoice.reload
assert_not invoice.cancelled?
end
end

View file

@ -0,0 +1,22 @@
require 'test_helper'
class ReppV1InvoicesDownloadTest < ActionDispatch::IntegrationTest
def setup
@user = users(:api_bestnames)
token = Base64.encode64("#{@user.username}:#{@user.plain_text_password}")
token = "Basic #{token}"
@auth_headers = { 'Authorization' => token }
end
def test_returns_invoice_as_pdf
invoice = @user.registrar.invoices.first
get "/repp/v1/invoices/#{invoice.id}/download", headers: @auth_headers
assert_response :ok
assert_equal 'application/pdf', response.headers['Content-Type']
assert_equal "attachment; filename=\"Invoice-2.pdf\"; filename*=UTF-8''Invoice-2.pdf", response.headers['Content-Disposition']
assert_not_empty response.body
end
end

View file

@ -0,0 +1,85 @@
require 'test_helper'
class ReppV1InvoicesListTest < ActionDispatch::IntegrationTest
def setup
@user = users(:api_bestnames)
token = Base64.encode64("#{@user.username}:#{@user.plain_text_password}")
token = "Basic #{token}"
@auth_headers = { 'Authorization' => token }
end
def test_returns_registrar_invoices
get repp_v1_invoices_path, headers: @auth_headers
json = JSON.parse(response.body, symbolize_names: true)
assert_response :ok
assert_equal @user.registrar.invoices.count, json[:data][:count]
assert_equal @user.registrar.invoices.count, json[:data][:invoices].length
assert json[:data][:invoices][0].is_a? Integer
end
def test_returns_detailed_registrar_invoices
get repp_v1_invoices_path(details: true), headers: @auth_headers
json = JSON.parse(response.body, symbolize_names: true)
assert_response :ok
assert_equal @user.registrar.invoices.count, json[:data][:count]
assert_equal @user.registrar.invoices.count, json[:data][:invoices].length
assert json[:data][:invoices][0].is_a? Hash
end
def test_returns_detailed_registrar_invoices_by_search_query
invoice = @user.registrar.invoices.last
invoice.update(number: 15_008)
search_params = {
number_gteq: 15_000,
}
get repp_v1_invoices_path(details: true, q: search_params), headers: @auth_headers
json = JSON.parse(response.body, symbolize_names: true)
assert_response :ok
assert_equal json[:data][:invoices].length, 1
assert json[:data][:invoices][0].is_a? Hash
assert_equal json[:data][:invoices][0][:id], invoice.id
end
def test_returns_detailed_registrar_invoices_by_sort_query
invoice = invoices(:unpaid)
sort_params = {
s: 'number desc',
}
get repp_v1_invoices_path(details: true, q: sort_params), headers: @auth_headers
json = JSON.parse(response.body, symbolize_names: true)
assert_response :ok
assert_equal @user.registrar.invoices.count, json[:data][:count]
assert_equal @user.registrar.invoices.count, json[:data][:invoices].length
assert_equal json[:data][:invoices][0][:id], invoice.id
end
def test_respects_limit
get repp_v1_invoices_path(details: true, limit: 1), headers: @auth_headers
json = JSON.parse(response.body, symbolize_names: true)
assert_response :ok
assert_equal 1, json[:data][:invoices].length
end
def test_respects_offset
offset = 1
get repp_v1_invoices_path(details: true, offset: offset), headers: @auth_headers
json = JSON.parse(response.body, symbolize_names: true)
assert_response :ok
assert_equal (@user.registrar.invoices.count - offset), json[:data][:invoices].length
end
end

View file

@ -0,0 +1,39 @@
require 'test_helper'
class ReppV1InvoicesSendTest < ActionDispatch::IntegrationTest
def setup
@user = users(:api_bestnames)
token = Base64.encode64("#{@user.username}:#{@user.plain_text_password}")
token = "Basic #{token}"
@auth_headers = { 'Authorization' => token }
end
def test_sends_invoice_to_recipient
invoice = invoices(:one)
recipient = 'donaldtrump@yandex.ru'
request_body = {
invoice: {
id: invoice.id,
recipient: recipient,
},
}
post "/repp/v1/invoices/#{invoice.id}/send_to_recipient", headers: @auth_headers,
params: request_body
json = JSON.parse(response.body, symbolize_names: true)
assert_equal 1, invoice.number
assert_response :ok
assert_equal 1000, json[:code]
assert_equal 'Command completed successfully', json[:message]
assert_equal json[:data][:invoice][:id], invoice.id
assert_equal json[:data][:invoice][:recipient], recipient
email = ActionMailer::Base.deliveries.last
assert_emails 1
assert_equal [recipient], email.to
assert_equal 'Invoice no. 1', email.subject
assert email.attachments['invoice-1.pdf']
end
end

View file

@ -0,0 +1,33 @@
require 'test_helper'
class ReppV1InvoicesShowTest < ActionDispatch::IntegrationTest
def setup
@user = users(:api_bestnames)
token = Base64.encode64("#{@user.username}:#{@user.plain_text_password}")
token = "Basic #{token}"
@auth_headers = { 'Authorization' => token }
end
def test_returns_error_when_not_found
get repp_v1_invoice_path(id: 'definitelynotexistant'), headers: @auth_headers
json = JSON.parse(response.body, symbolize_names: true)
assert_response :not_found
assert_equal 2303, json[:code]
assert_equal 'Object does not exist', json[:message]
end
def test_shows_existing_invoice
invoice = @user.registrar.invoices.first
get repp_v1_invoice_path(id: invoice.id), headers: @auth_headers
json = JSON.parse(response.body, symbolize_names: true)
assert_response :ok
assert_equal 1000, json[:code]
assert_equal 'Command completed successfully', json[:message]
assert_equal invoice.id, json[:data][:invoice][:id]
end
end