diff --git a/app/controllers/repp/v1/accounts_controller.rb b/app/controllers/repp/v1/accounts_controller.rb index 0204e7943..301bed95f 100644 --- a/app/controllers/repp/v1/accounts_controller.rb +++ b/app/controllers/repp/v1/accounts_controller.rb @@ -73,6 +73,22 @@ module Repp message: I18n.t('registrar.settings.balance_auto_reload.destroy.disabled')) end + api :put, '/repp/v1/accounts/switch_user' + desc 'Switch user to another api user' + def switch_user + new_user = ApiUser.find(account_params[:new_user_id]) + unless current_user.linked_with?(new_user) + handle_non_epp_errors(new_user, 'Cannot switch to unlinked user') + return + end + + @current_user = new_user + data = auth_values_to_data(registrar: current_user.registrar) + message = I18n.t('registrar.current_user.switch.switched', new_user: new_user) + token = Base64.urlsafe_encode64("#{new_user.username}:#{new_user.plain_text_password}") + render_success(data: { token: token, registrar: data }, message: message) + end + api :get, '/repp/v1/accounts/balance' desc "Get account's balance" def balance @@ -90,7 +106,7 @@ module Repp private def account_params - params.require(:account).permit(:billing_email, :iban) + params.require(:account).permit(:billing_email, :iban, :new_user_id) end def index_params diff --git a/app/controllers/repp/v1/invoices_controller.rb b/app/controllers/repp/v1/invoices_controller.rb index 8add1920b..bd807e7b8 100644 --- a/app/controllers/repp/v1/invoices_controller.rb +++ b/app/controllers/repp/v1/invoices_controller.rb @@ -11,6 +11,7 @@ module Repp q = records.ransack(search_params) q.sorts = 'created_at desc' if q.sorts.empty? invoices = q.result(distinct: true) + limited_invoices = invoices.limit(limit).offset(offset) .includes(:items, :account_activity, :buyer) diff --git a/app/controllers/repp/v1/registrar/auth_controller.rb b/app/controllers/repp/v1/registrar/auth_controller.rb index a93d75c36..a15230c78 100644 --- a/app/controllers/repp/v1/registrar/auth_controller.rb +++ b/app/controllers/repp/v1/registrar/auth_controller.rb @@ -23,22 +23,6 @@ module Repp render_success(data: { token: token, username: user.username }) end - 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]) - unless current_user.linked_with?(new_user) - handle_non_epp_errors(new_user, 'Cannot switch to unlinked user') - return - end - - @current_user = new_user - data = auth_values_to_data(registrar: current_user.registrar) - message = I18n.t('registrar.current_user.switch.switched', new_user: new_user) - token = Base64.urlsafe_encode64("#{new_user.username}:#{new_user.plain_text_password}") - render_success(data: { token: token, registrar: data }, message: message) - end - private def auth_params diff --git a/config/routes.rb b/config/routes.rb index 81b6dc95c..180a4687d 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -83,6 +83,9 @@ Rails.application.routes.draw do post 'update_auto_reload_balance' get 'disable_auto_reload_balance' end + member do + put 'switch_user' + end end resources :invoices, only: %i[index show] do collection do @@ -117,7 +120,6 @@ Rails.application.routes.draw do resources :auth, only: %i[index] do collection do post '/tara_callback', to: 'auth#tara_callback' - put '/switch_user', to: 'auth#switch_user' end end end diff --git a/test/integration/repp/v1/registrar/auth/switch_user_test.rb b/test/integration/repp/v1/accounts/switch_user_test.rb similarity index 84% rename from test/integration/repp/v1/registrar/auth/switch_user_test.rb rename to test/integration/repp/v1/accounts/switch_user_test.rb index beb5c0727..c8935d038 100644 --- a/test/integration/repp/v1/registrar/auth/switch_user_test.rb +++ b/test/integration/repp/v1/accounts/switch_user_test.rb @@ -1,6 +1,6 @@ require 'test_helper' -class ReppV1RegistrarSwitchUserTest < ActionDispatch::IntegrationTest +class ReppV1AccountsSwitchUserTest < ActionDispatch::IntegrationTest def setup @user = users(:api_bestnames) token = Base64.encode64("#{@user.username}:#{@user.plain_text_password}") @@ -13,12 +13,12 @@ class ReppV1RegistrarSwitchUserTest < ActionDispatch::IntegrationTest new_user = users(:api_goodnames) new_user.update(identity_code: '1234') request_body = { - auth: { + account: { new_user_id: new_user.id, }, } - put '/repp/v1/registrar/auth/switch_user', headers: @auth_headers, params: request_body + put '/repp/v1/accounts/switch_user', headers: @auth_headers, params: request_body json = JSON.parse(response.body, symbolize_names: true) assert_response :ok @@ -38,12 +38,12 @@ class ReppV1RegistrarSwitchUserTest < ActionDispatch::IntegrationTest new_user = users(:api_goodnames) new_user.update(identity_code: '4444') request_body = { - auth: { + account: { new_user_id: new_user.id, }, } - put '/repp/v1/registrar/auth/switch_user', headers: @auth_headers, params: request_body + put '/repp/v1/accounts/switch_user', headers: @auth_headers, params: request_body json = JSON.parse(response.body, symbolize_names: true) assert_response :bad_request