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

View file

@ -34,7 +34,7 @@ module Admin
if @certificate.destroy
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
flash.now[:alert] = I18n.t('failed_to_delete_record')
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
.row

View file

@ -1,5 +1,5 @@
- 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 'form'

View file

@ -15,7 +15,7 @@
%tbody
- @api_users.each do |api_user|
%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= api_user.active
.row

View file

@ -1,6 +1,6 @@
- content_for :actions do
= link_to(t(:edit), edit_admin_api_user_path(@api_user), class: 'btn btn-default')
= link_to(t(:delete), admin_api_user_path(@api_user),
= link_to(t(:edit), edit_admin_registrar_api_user_path(@api_user.registrar, @api_user), class: 'btn btn-default')
= 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')
= render 'shared/title', name: @api_user.username

View file

@ -20,7 +20,7 @@
.panel-body
%dl.dl-horizontal
%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)
%dd= @certificate.common_name

View file

@ -259,7 +259,7 @@ Rails.application.routes.draw do
end
resources :registrars do
resources :api_users, except: %i[show edit update destroy]
resources :api_users, except: %i[index]
resources :white_ips
end
@ -270,7 +270,8 @@ Rails.application.routes.draw do
end
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
member do
post 'sign'

View file

@ -9,50 +9,6 @@ class AdminApiUsersSystemTest < ApplicationSystemTestCase
visit admin_api_users_path
api_user = users(:api_bestnames)
assert_link api_user.username, href: admin_api_user_path(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
assert_link api_user.username, href: admin_registrar_api_user_path(api_user.registrar, api_user)
end
end

View file

@ -18,10 +18,57 @@ class AdminRegistrarsApiUsersSystemTest < ApplicationSystemTestCase
assert_text 'Record created'
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
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
'testtest'
end