Moved switch user method to accounts

This commit is contained in:
Sergei Tsõganov 2022-06-15 12:56:54 +03:00
parent 6c9fb2b025
commit de5872fb40
5 changed files with 26 additions and 23 deletions

View file

@ -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

View file

@ -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)

View file

@ -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

View file

@ -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

View file

@ -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