diff --git a/app/controllers/repp/v1/invoices_controller.rb b/app/controllers/repp/v1/invoices_controller.rb index 34c04ff72..5d37d6881 100644 --- a/app/controllers/repp/v1/invoices_controller.rb +++ b/app/controllers/repp/v1/invoices_controller.rb @@ -47,7 +47,7 @@ module Repp .merge!(recipient: recipient) }) end - api :post, '/repp/v1/invoices/:id/cancel' + api :put, '/repp/v1/invoices/:id/cancel' desc 'Cancel a specific invoice' def cancel action = Actions::InvoiceCancel.new(@invoice) @@ -72,7 +72,7 @@ module Repp serializer = Serializers::Repp::Invoice.new(invoice, simplify: true) render_success(data: { invoice: serializer.to_json }) else - handle_errors(deposit) + handle_non_epp_errors(deposit) end end @@ -108,7 +108,7 @@ module Repp end def serialized_invoices(invoices) - return invoices.pluck(:number) unless index_params[:details] == 'true' + return invoices.map(&:number) unless index_params[:details] == 'true' simple = index_params[:simple] == 'true' || false invoices.map { |i| Serializers::Repp::Invoice.new(i, simplify: simple).to_json } diff --git a/app/controllers/repp/v1/registrar/auth_controller.rb b/app/controllers/repp/v1/registrar/auth_controller.rb index f91cc8637..ccc7d2e66 100644 --- a/app/controllers/repp/v1/registrar/auth_controller.rb +++ b/app/controllers/repp/v1/registrar/auth_controller.rb @@ -22,7 +22,7 @@ module Repp render_success(data: { token: token, username: user.username }) end - api :put, '/repp/v1/registrar/auth/switch_user/:new_user_id' + api :put, '/repp/v1/registrar/auth/switch_user' desc 'Switch session to another api user' def switch_user new_user = ApiUser.find(auth_params[:new_user_id]) diff --git a/config/routes.rb b/config/routes.rb index 6f336f4d4..2ea79e1c0 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -87,11 +87,11 @@ Rails.application.routes.draw do resources :invoices, only: %i[index show] do collection do get ':id/download', to: 'invoices#download' - get ':id/cancel', to: 'invoices#cancel' post 'add_credit' end member do post 'send_to_recipient', to: 'invoices#send_to_recipient' + put 'cancel', to: 'invoices#cancel' end end resources :auctions, only: %i[index] diff --git a/test/integration/repp/v1/account/activities_list_test.rb b/test/integration/repp/v1/account/activities_list_test.rb new file mode 100644 index 000000000..b492cc098 --- /dev/null +++ b/test/integration/repp/v1/account/activities_list_test.rb @@ -0,0 +1,70 @@ +require 'test_helper' + +class ReppV1AccountActivitiesListTest < 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_account_activities + get repp_v1_account_path, headers: @auth_headers + json = JSON.parse(response.body, symbolize_names: true) + + assert_response :ok + + assert_equal @user.registrar.cash_account.activities.count, json[:data][:count] + assert_equal @user.registrar.cash_account.activities.count, json[:data][:activities].length + + assert json[:data][:activities][0].is_a? Hash + end + + def test_respects_limit + get repp_v1_account_path(limit: 1), headers: @auth_headers + json = JSON.parse(response.body, symbolize_names: true) + + assert_response :ok + + assert_equal 1, json[:data][:activities].length + end + + def test_respects_offset + offset = 1 + get repp_v1_account_path(offset: offset), headers: @auth_headers + json = JSON.parse(response.body, symbolize_names: true) + + assert_response :ok + + assert_equal (@user.registrar.cash_account.activities.count - offset), json[:data][:activities].length + end + + def test_returns_account_activities_by_search_query + search_params = { + description_matches: '%renew%', + } + get repp_v1_account_path(q: search_params), headers: @auth_headers + json = JSON.parse(response.body, symbolize_names: true) + + assert_response :ok + + assert_equal json[:data][:activities].length, 1 + assert json[:data][:activities][0].is_a? Hash + end + + def test_returns_account_activities_by_sort_query + activity = account_activities(:renew_cash) + sort_params = { + s: 'activity_type asc', + } + get repp_v1_account_path(q: sort_params), headers: @auth_headers + json = JSON.parse(response.body, symbolize_names: true) + + assert_response :ok + + assert_equal @user.registrar.cash_account.activities.count, json[:data][:count] + assert_equal @user.registrar.cash_account.activities.count, json[:data][:activities].length + assert_equal json[:data][:activities][0][:description], activity.description + end +end diff --git a/test/integration/repp/v1/account/balance_test.rb b/test/integration/repp/v1/account/balance_test.rb index a8416be0a..a4d41ed53 100644 --- a/test/integration/repp/v1/account/balance_test.rb +++ b/test/integration/repp/v1/account/balance_test.rb @@ -10,6 +10,8 @@ class ReppV1BalanceTest < ActionDispatch::IntegrationTest @auth_headers = { 'Authorization' => token } end + + def test_can_query_balance get '/repp/v1/account/balance', headers: @auth_headers json = JSON.parse(response.body, symbolize_names: true) diff --git a/test/integration/repp/v1/account/details_test.rb b/test/integration/repp/v1/account/details_test.rb new file mode 100644 index 000000000..b0d359d74 --- /dev/null +++ b/test/integration/repp/v1/account/details_test.rb @@ -0,0 +1,22 @@ +require 'test_helper' + +class ReppV1AccountDetailsTest < 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_account_details + get '/repp/v1/account/details', 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 @user.registrar.billing_email, json[:data][:account][:billing_email] + end +end \ No newline at end of file diff --git a/test/integration/repp/v1/account/update_auto_reload_balance_test.rb b/test/integration/repp/v1/account/update_auto_reload_balance_test.rb new file mode 100644 index 000000000..6201750c3 --- /dev/null +++ b/test/integration/repp/v1/account/update_auto_reload_balance_test.rb @@ -0,0 +1,69 @@ +require 'test_helper' + +class ReppV1AccountUpdateAutoReloadBalanceTest < 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_updates_auto_reload_balance + amount = 100 + threshold = 10 + request_body = { + type: { + amount: amount, + threshold: threshold, + }, + } + + assert_nil @user.registrar.settings['balance_auto_reload'] + + post '/repp/v1/account/update_auto_reload_balance', headers: @auth_headers, + params: request_body + json = JSON.parse(response.body, symbolize_names: true) + assert_response :ok + assert_equal 1000, json[:code] + assert_equal 'Balance Auto-Reload setting has been updated', json[:message] + + @user.registrar.reload + + assert_equal amount, @user.registrar.settings['balance_auto_reload']['type']['amount'] + assert_equal threshold, @user.registrar.settings['balance_auto_reload']['type']['threshold'] + end + + def test_returns_error_if_type_has_wrong_attributes + min_deposit = 10 + request_body = { + type: { + amount: 5, + threshold: -1, + }, + } + Setting.minimum_deposit = min_deposit + + post '/repp/v1/account/update_auto_reload_balance', headers: @auth_headers, + params: request_body + json = JSON.parse(response.body, symbolize_names: true) + + assert_response :bad_request + amount_error = "Amount must be greater than or equal to #{min_deposit.to_f}" + threshold = 'Threshold must be greater than or equal to 0' + assert_equal "#{amount_error}, #{threshold}", json[:message] + end + + def test_disables_auto_reload_balance + get '/repp/v1/account/disable_auto_reload_balance', headers: @auth_headers + json = JSON.parse(response.body, symbolize_names: true) + + assert_response :ok + assert_equal 1000, json[:code] + assert_equal 'Balance Auto-Reload setting has been disabled', json[:message] + + @user.registrar.reload + + assert_nil @user.registrar.settings['balance_auto_reload'] + end +end \ No newline at end of file diff --git a/test/integration/repp/v1/account/update_details_test.rb b/test/integration/repp/v1/account/update_details_test.rb new file mode 100644 index 000000000..3ab415cd8 --- /dev/null +++ b/test/integration/repp/v1/account/update_details_test.rb @@ -0,0 +1,30 @@ +require 'test_helper' + +class ReppV1AccountUpdateDetailsTest < 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_updates_details + request_body = { + account: { + billing_email: 'donaldtrump@yandex.ru', + iban: 'GB331111111111111111', + }, + } + + put '/repp/v1/account', headers: @auth_headers, params: request_body + json = JSON.parse(response.body, symbolize_names: true) + + assert_response :ok + assert_equal 1000, json[:code] + assert_equal 'Your account has been updated', json[:message] + + assert_equal(request_body[:account][:billing_email], @user.registrar.billing_email) + assert_equal(request_body[:account][:iban], @user.registrar.iban) + end +end \ No newline at end of file diff --git a/test/integration/repp/v1/contacts/list_test.rb b/test/integration/repp/v1/contacts/list_test.rb index 6cc3e4669..979d3ea40 100644 --- a/test/integration/repp/v1/contacts/list_test.rb +++ b/test/integration/repp/v1/contacts/list_test.rb @@ -12,7 +12,7 @@ class ReppV1ContactsListTest < ActionDispatch::IntegrationTest def test_returns_registrar_contacts get repp_v1_contacts_path, headers: @auth_headers json = JSON.parse(response.body, symbolize_names: true) - + assert_response :ok assert_equal @user.registrar.contacts.count, json[:data][:count] @@ -21,7 +21,6 @@ class ReppV1ContactsListTest < ActionDispatch::IntegrationTest assert json[:data][:contacts][0].is_a? String end - def test_returns_detailed_registrar_contacts get repp_v1_contacts_path(details: true), headers: @auth_headers json = JSON.parse(response.body, symbolize_names: true) @@ -52,4 +51,32 @@ class ReppV1ContactsListTest < ActionDispatch::IntegrationTest assert_equal (@user.registrar.contacts.count - offset), json[:data][:contacts].length end + + def test_returns_detailed_registrar_contacts_by_search_query + search_params = { + ident_type_eq: 'priv', + } + get repp_v1_contacts_path(details: true, q: search_params), headers: @auth_headers + json = JSON.parse(response.body, symbolize_names: true) + + assert_response :ok + + assert_equal json[:data][:contacts].length, 3 + assert json[:data][:contacts][0].is_a? Hash + end + + def test_returns_detailed_registrar_contacts_by_sort_query + contact = contacts(:william) + sort_params = { + s: 'name desc', + } + get repp_v1_contacts_path(details: true, q: sort_params), headers: @auth_headers + json = JSON.parse(response.body, symbolize_names: true) + + assert_response :ok + + assert_equal @user.registrar.contacts.count, json[:data][:count] + assert_equal @user.registrar.contacts.count, json[:data][:contacts].length + assert_equal json[:data][:contacts][0][:code], contact.code + end end diff --git a/test/integration/repp/v1/contacts/search_test.rb b/test/integration/repp/v1/contacts/search_test.rb new file mode 100644 index 000000000..ceeefc7be --- /dev/null +++ b/test/integration/repp/v1/contacts/search_test.rb @@ -0,0 +1,43 @@ +require 'test_helper' + +class ReppV1ContactsSearchTest < 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_searches_all_contacts_by_id + contact = contacts(:john) + get "/repp/v1/contacts/search/#{contact.code}", headers: @auth_headers + json = JSON.parse(response.body, symbolize_names: true) + + assert_response :ok + assert json[:data].is_a? Array + assert_equal json[:data][0][:value], contact.code + assert_equal json[:data][0][:label], "#{contact.code} #{contact.name}" + assert_equal json[:data][0][:selected], true + end + + def test_searches_all_contacts_by_query + get '/repp/v1/contacts/search', headers: @auth_headers, params: { query: 'j' } + json = JSON.parse(response.body, symbolize_names: true) + + assert_response :ok + assert json[:data].is_a? Array + assert_equal json[:data].length, 2 + assert_equal json[:data][0][:selected], false + assert_equal json[:data][1][:selected], false + end + + def test_searches_all_contacts_by_wrong_query + get '/repp/v1/contacts/search', headers: @auth_headers, params: { query: '000' } + json = JSON.parse(response.body, symbolize_names: true) + + assert_response :ok + assert json[:data].is_a? Array + assert_equal json[:data].length, 0 + end +end \ No newline at end of file diff --git a/test/integration/repp/v1/domains/list_test.rb b/test/integration/repp/v1/domains/list_test.rb index 645947b57..205017a6c 100644 --- a/test/integration/repp/v1/domains/list_test.rb +++ b/test/integration/repp/v1/domains/list_test.rb @@ -64,4 +64,32 @@ class ReppV1DomainsListTest < ActionDispatch::IntegrationTest serialized_domain = Serializers::Repp::Domain.new(domain).to_json assert_equal serialized_domain.as_json, json[:data][:domain].as_json end + + def test_returns_detailed_registrar_domains_by_search_query + search_params = { + name_matches: '%library%', + } + get repp_v1_domains_path(details: true, q: search_params), headers: @auth_headers + json = JSON.parse(response.body, symbolize_names: true) + + assert_response :ok + + assert_equal json[:data][:domains].length, 1 + assert json[:data][:domains][0].is_a? Hash + end + + def test_returns_detailed_registrar_domains_by_sort_query + domain = domains(:shop) + sort_params = { + s: 'name desc', + } + get repp_v1_domains_path(details: true, q: sort_params), headers: @auth_headers + json = JSON.parse(response.body, symbolize_names: true) + + assert_response :ok + + assert_equal @user.registrar.domains.count, json[:data][:count] + assert_equal @user.registrar.domains.count, json[:data][:domains].length + assert_equal json[:data][:domains][0][:name], domain.name + end end diff --git a/test/integration/repp/v1/invoices/add_credit_test.rb b/test/integration/repp/v1/invoices/add_credit_test.rb new file mode 100644 index 000000000..71f2c77d3 --- /dev/null +++ b/test/integration/repp/v1/invoices/add_credit_test.rb @@ -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 \ No newline at end of file diff --git a/test/integration/repp/v1/invoices/cancel_test.rb b/test/integration/repp/v1/invoices/cancel_test.rb new file mode 100644 index 000000000..1041d1d1f --- /dev/null +++ b/test/integration/repp/v1/invoices/cancel_test.rb @@ -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 \ No newline at end of file diff --git a/test/integration/repp/v1/invoices/download_test.rb b/test/integration/repp/v1/invoices/download_test.rb new file mode 100644 index 000000000..cbb9de585 --- /dev/null +++ b/test/integration/repp/v1/invoices/download_test.rb @@ -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 \ No newline at end of file diff --git a/test/integration/repp/v1/invoices/list_test.rb b/test/integration/repp/v1/invoices/list_test.rb new file mode 100644 index 000000000..5dfe2d53f --- /dev/null +++ b/test/integration/repp/v1/invoices/list_test.rb @@ -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 \ No newline at end of file diff --git a/test/integration/repp/v1/invoices/send_test.rb b/test/integration/repp/v1/invoices/send_test.rb new file mode 100644 index 000000000..77fe9997f --- /dev/null +++ b/test/integration/repp/v1/invoices/send_test.rb @@ -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 \ No newline at end of file diff --git a/test/integration/repp/v1/invoices/show_test.rb b/test/integration/repp/v1/invoices/show_test.rb new file mode 100644 index 000000000..74feb42ac --- /dev/null +++ b/test/integration/repp/v1/invoices/show_test.rb @@ -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 \ No newline at end of file diff --git a/test/integration/repp/v1/registrar/auth/check_info_test.rb b/test/integration/repp/v1/registrar/auth/check_info_test.rb new file mode 100644 index 000000000..65b2cf5f4 --- /dev/null +++ b/test/integration/repp/v1/registrar/auth/check_info_test.rb @@ -0,0 +1,39 @@ +require 'test_helper' + +class ReppV1RegistrarAuthCheckInfoTest < 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_valid_user_auth_values + get '/repp/v1/registrar/auth', 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 json[:data][:username], @user.username + assert json[:data][:roles].include? 'super' + assert_equal json[:data][:registrar_name], 'Best Names' + assert_equal json[:data][:balance][:amount].to_f, @user.registrar.cash_account.balance + assert json[:data][:abilities].is_a? Hash + end + + def test_invalid_user_login + token = Base64.encode64("#{@user.username}:0066600") + token = "Basic #{token}" + + auth_headers = { 'Authorization' => token } + + get '/repp/v1/registrar/auth', headers: auth_headers + json = JSON.parse(response.body, symbolize_names: true) + + assert_response :unauthorized + assert_equal json[:message], 'Invalid authorization information' + end +end \ No newline at end of file diff --git a/test/integration/repp/v1/registrar/auth/switch_user_test.rb b/test/integration/repp/v1/registrar/auth/switch_user_test.rb new file mode 100644 index 000000000..beb5c0727 --- /dev/null +++ b/test/integration/repp/v1/registrar/auth/switch_user_test.rb @@ -0,0 +1,52 @@ +require 'test_helper' + +class ReppV1RegistrarSwitchUserTest < 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_switches_to_linked_api_user + new_user = users(:api_goodnames) + new_user.update(identity_code: '1234') + request_body = { + auth: { + new_user_id: new_user.id, + }, + } + + put '/repp/v1/registrar/auth/switch_user', headers: @auth_headers, params: request_body + json = JSON.parse(response.body, symbolize_names: true) + + assert_response :ok + assert_equal 1000, json[:code] + assert_equal "You are now signed in as a user \"#{new_user.username}\"", json[:message] + + user_token = Base64.urlsafe_encode64("#{new_user.username}:#{new_user.plain_text_password}") + assert_equal json[:data][:token], user_token + assert_equal json[:data][:registrar][:username], new_user.username + assert json[:data][:registrar][:roles].include? 'super' + assert_equal json[:data][:registrar][:registrar_name], 'Good Names' + assert_equal json[:data][:registrar][:balance][:amount].to_f, new_user.registrar.cash_account.balance + assert json[:data][:registrar][:abilities].is_a? Hash + end + + def test_switches_to_unlinked_api_user + new_user = users(:api_goodnames) + new_user.update(identity_code: '4444') + request_body = { + auth: { + new_user_id: new_user.id, + }, + } + + put '/repp/v1/registrar/auth/switch_user', headers: @auth_headers, params: request_body + json = JSON.parse(response.body, symbolize_names: true) + + assert_response :bad_request + assert_equal 'Cannot switch to unlinked user', json[:message] + end +end \ No newline at end of file diff --git a/test/integration/repp/v1/registrar/auth/tara_callback_test.rb b/test/integration/repp/v1/registrar/auth/tara_callback_test.rb new file mode 100644 index 000000000..56d881c87 --- /dev/null +++ b/test/integration/repp/v1/registrar/auth/tara_callback_test.rb @@ -0,0 +1,46 @@ +require 'test_helper' + +class ReppV1RegistrarAuthTaraCallbackTest < ActionDispatch::IntegrationTest + def setup + @user = users(:api_bestnames) + username = nil + password = nil + token = Base64.encode64("#{username}:#{password}") + token = "Basic #{token}" + + @auth_headers = { 'Authorization' => token } + end + + def test_validates_user_from_omniauth_params + request_body = { + auth: { + uid: 'EE1234', + }, + } + + post '/repp/v1/registrar/auth/tara_callback', 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] + + user_token = Base64.urlsafe_encode64("#{@user.username}:#{@user.plain_text_password}") + assert_equal json[:data][:username], @user.username + assert_equal json[:data][:token], user_token + end + + def test_invalidates_user_with_wrong_omniauth_params + request_body = { + auth: { + uid: '33333', + }, + } + + post '/repp/v1/registrar/auth/tara_callback', headers: @auth_headers, params: request_body + json = JSON.parse(response.body, symbolize_names: true) + + assert_response :bad_request + assert_equal 'No such user', json[:message] + end +end \ No newline at end of file diff --git a/test/integration/repp/v1/registrar/summary_test.rb b/test/integration/repp/v1/registrar/summary_test.rb new file mode 100644 index 000000000..257eb067f --- /dev/null +++ b/test/integration/repp/v1/registrar/summary_test.rb @@ -0,0 +1,44 @@ +require 'test_helper' + +class ReppV1RegistrarSummaryTest < 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_checks_user_summary_info + get '/repp/v1/registrar/summary', 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 json[:data][:username], @user.username + assert_equal json[:data][:registrar_name], 'Best Names' + assert_equal json[:data][:domains], @user.registrar.domains.count + assert_equal json[:data][:contacts], @user.registrar.contacts.count + assert json[:data][:notification].is_a? Hash + assert_equal json[:data][:notifications_count], @user.unread_notifications.count + end + + def test_checks_limited_user_summary_info + @user.update(roles: ['billing']) + get '/repp/v1/registrar/summary', 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 json[:data][:username], @user.username + assert_equal json[:data][:registrar_name], 'Best Names' + assert_nil json[:data][:domains] + assert_nil json[:data][:contacts] + assert_nil json[:data][:notification] + assert_nil json[:data][:notifications_count] + end +end \ No newline at end of file