Nest API users under registrar in admin area

This commit is contained in:
Artur Beljajev 2019-12-09 21:34:36 +02:00
parent c680b688e4
commit de6934625c
10 changed files with 65 additions and 62 deletions

View file

@ -2,7 +2,6 @@ module Admin
class ApiUsersController < BaseController class ApiUsersController < BaseController
load_and_authorize_resource load_and_authorize_resource
before_action :set_api_user, only: [:show, :edit, :update, :destroy] before_action :set_api_user, only: [:show, :edit, :update, :destroy]
before_action :find_registrar, only: %i[new create]
def index def index
@q = ApiUser.includes(:registrar).search(params[:q]) @q = ApiUser.includes(:registrar).search(params[:q])
@ -10,15 +9,15 @@ module Admin
end end
def new def new
@api_user = ApiUser.new @api_user = registrar.api_users.build
end end
def create def create
@api_user = @registrar.api_users.build(api_user_params) @api_user = registrar.api_users.build(api_user_params)
if @api_user.save if @api_user.save
flash[:notice] = I18n.t('record_created') flash[:notice] = I18n.t('record_created')
redirect_to [:admin, @api_user] redirect_to admin_registrar_api_user_path(@api_user.registrar, @api_user)
else else
flash.now[:alert] = I18n.t('failed_to_create_record') flash.now[:alert] = I18n.t('failed_to_create_record')
render 'new' render 'new'
@ -38,7 +37,7 @@ module Admin
if @api_user.update(api_user_params) if @api_user.update(api_user_params)
flash[:notice] = I18n.t('record_updated') flash[:notice] = I18n.t('record_updated')
redirect_to [:admin, @api_user] redirect_to admin_registrar_api_user_path(@api_user.registrar, @api_user)
else else
flash.now[:alert] = I18n.t('failed_to_update_record') flash.now[:alert] = I18n.t('failed_to_update_record')
render 'edit' render 'edit'
@ -48,7 +47,7 @@ module Admin
def destroy def destroy
if @api_user.destroy if @api_user.destroy
flash[:notice] = I18n.t('record_deleted') flash[:notice] = I18n.t('record_deleted')
redirect_to admin_api_users_path redirect_to admin_registrar_path(@api_user.registrar)
else else
flash.now[:alert] = I18n.t('failed_to_delete_record') flash.now[:alert] = I18n.t('failed_to_delete_record')
render 'show' render 'show'
@ -66,8 +65,8 @@ module Admin
:identity_code, { roles: [] }) :identity_code, { roles: [] })
end end
def find_registrar def registrar
@registrar = Registrar.find(params[:registrar_id]) Registrar.find(params[:registrar_id])
end end
end end
end end

View file

@ -34,7 +34,7 @@ module Admin
if @certificate.destroy if @certificate.destroy
flash[:notice] = I18n.t('record_deleted') flash[:notice] = I18n.t('record_deleted')
redirect_to admin_api_user_path(@api_user) redirect_to admin_registrar_api_user_path(@api_user.registrar, @api_user)
else else
flash.now[:alert] = I18n.t('failed_to_delete_record') flash.now[:alert] = I18n.t('failed_to_delete_record')
render 'show' render 'show'

View file

@ -1,4 +1,4 @@
= form_for([:admin, @registrar, @api_user], html: {class: 'form-horizontal', autocomplete: 'off'}) do |f| = form_for([:admin, @api_user.registrar, @api_user], html: {class: 'form-horizontal', autocomplete: 'off'}) do |f|
= render 'shared/full_errors', object: @api_user = render 'shared/full_errors', object: @api_user
.row .row

View file

@ -1,5 +1,5 @@
- content_for :actions do - content_for :actions do
= link_to(t(:back_to_api_user), [:admin, @api_user], class: 'btn btn-default') = link_to(t(:back_to_api_user), admin_registrar_api_user_path(@api_user.registrar, @api_user), class: 'btn btn-default')
= render 'shared/title', name: "#{t(:edit)}: #{@api_user.username}" = render 'shared/title', name: "#{t(:edit)}: #{@api_user.username}"
= render 'form' = render 'form'

View file

@ -15,7 +15,7 @@
%tbody %tbody
- @api_users.each do |api_user| - @api_users.each do |api_user|
%tr %tr
%td= link_to api_user, [:admin, api_user] %td= link_to api_user, admin_registrar_api_user_path(api_user.registrar, api_user)
%td= link_to api_user.registrar, [:admin, api_user.registrar] %td= link_to api_user.registrar, [:admin, api_user.registrar]
%td= api_user.active %td= api_user.active
.row .row

View file

@ -1,6 +1,6 @@
- content_for :actions do - content_for :actions do
= link_to(t(:edit), edit_admin_api_user_path(@api_user), class: 'btn btn-default') = link_to(t(:edit), edit_admin_registrar_api_user_path(@api_user.registrar, @api_user), class: 'btn btn-default')
= link_to(t(:delete), admin_api_user_path(@api_user), = link_to(t(:delete), admin_registrar_api_user_path(@api_user.registrar, @api_user),
method: :delete, data: { confirm: t(:are_you_sure) }, class: 'btn btn-danger') method: :delete, data: { confirm: t(:are_you_sure) }, class: 'btn btn-danger')
= render 'shared/title', name: @api_user.username = render 'shared/title', name: @api_user.username

View file

@ -20,7 +20,7 @@
.panel-body .panel-body
%dl.dl-horizontal %dl.dl-horizontal
%dt= t(:api_user) %dt= t(:api_user)
%dd= link_to(@certificate.api_user, [:admin, @api_user]) %dd= link_to(@certificate.api_user, [:admin, @api_user.registrar, @api_user])
%dt= t(:common_name) %dt= t(:common_name)
%dd= @certificate.common_name %dd= @certificate.common_name

View file

@ -259,7 +259,7 @@ Rails.application.routes.draw do
end end
resources :registrars do resources :registrars do
resources :api_users, except: %i[show edit update destroy] resources :api_users, except: %i[index]
resources :white_ips resources :white_ips
end end
@ -270,7 +270,8 @@ Rails.application.routes.draw do
end end
resources :admin_users resources :admin_users
resources :api_users, except: %i[new create] do # /admin/api_users is mainly for manual testing
resources :api_users, only: :index do
resources :certificates do resources :certificates do
member do member do
post 'sign' post 'sign'

View file

@ -9,50 +9,6 @@ class AdminApiUsersSystemTest < ApplicationSystemTestCase
visit admin_api_users_path visit admin_api_users_path
api_user = users(:api_bestnames) api_user = users(:api_bestnames)
assert_link api_user.username, href: admin_api_user_path(api_user) assert_link api_user.username, href: admin_registrar_api_user_path(api_user.registrar, api_user)
end
def test_shows_api_user_details
api_user = users(:api_bestnames)
visit admin_api_user_path(api_user)
assert_text "Username #{api_user.username}"
assert_text "Password #{api_user.plain_text_password}"
assert_link api_user.registrar.name, href: admin_registrar_path(api_user.registrar)
assert_text "Role #{api_user.roles.first}"
assert_text "Active #{api_user.active}"
end
def test_updates_api_user
api_user = users(:api_bestnames)
new_username = 'new username'
assert_not_equal new_username, api_user.name
visit admin_api_user_path(api_user)
click_link_or_button 'Edit'
fill_in 'Username', with: new_username
click_link_or_button 'Save'
assert_text 'Record updated'
assert_text "Username #{new_username}"
end
def test_deletes_api_user
api_user = unassociated_api_user
visit admin_api_user_path(api_user)
click_on 'Delete'
assert_text 'Record deleted'
end
private
def unassociated_api_user
new_api_user = users(:api_bestnames).dup
new_api_user.username = "unique-#{rand(100)}"
new_api_user.save!
new_api_user
end end
end end

View file

@ -18,10 +18,57 @@ class AdminRegistrarsApiUsersSystemTest < ApplicationSystemTestCase
assert_text 'Record created' assert_text 'Record created'
assert_text "Username #{username}" assert_text "Username #{username}"
new_api_user = ApiUser.last
assert_current_path admin_registrar_api_user_path(registrar, new_api_user)
end
def test_shows_api_user_details
api_user = users(:api_bestnames)
visit admin_registrar_path(api_user.registrar)
click_on api_user.username
assert_text "Username #{api_user.username}"
assert_text "Password #{api_user.plain_text_password}"
assert_link api_user.registrar.name, href: admin_registrar_path(api_user.registrar)
assert_text "Role #{api_user.roles.first}"
assert_text "Active #{api_user.active}"
end
def test_updates_api_user
api_user = users(:api_bestnames)
new_username = 'new username'
assert_not_equal new_username, api_user.username
visit admin_registrar_api_user_path(api_user.registrar, api_user)
click_link_or_button 'Edit'
fill_in 'Username', with: new_username
click_link_or_button 'Save'
assert_text 'Record updated'
assert_text "Username #{new_username}"
assert_current_path admin_registrar_api_user_path(api_user.registrar, api_user)
end
def test_deletes_api_user
api_user = unassociated_api_user
visit admin_registrar_api_user_path(api_user.registrar, api_user)
click_on 'Delete'
assert_text 'Record deleted'
assert_current_path admin_registrar_path(api_user.registrar)
end end
private private
def unassociated_api_user
new_api_user = users(:api_bestnames).dup
new_api_user.username = "unique-#{rand(100)}"
new_api_user.save!
new_api_user
end
def valid_password def valid_password
'testtest' 'testtest'
end end