From de6934625c17d1581ffe5f185ff9a42f76de962c Mon Sep 17 00:00:00 2001 From: Artur Beljajev Date: Mon, 9 Dec 2019 21:34:36 +0200 Subject: [PATCH] Nest API users under registrar in admin area --- app/controllers/admin/api_users_controller.rb | 15 +++--- .../admin/certificates_controller.rb | 2 +- app/views/admin/api_users/_form.haml | 2 +- app/views/admin/api_users/edit.haml | 2 +- app/views/admin/api_users/index.haml | 2 +- app/views/admin/api_users/show.haml | 4 +- app/views/admin/certificates/show.haml | 2 +- config/routes.rb | 5 +- test/system/admin_area/api_users_test.rb | 46 +----------------- .../admin_area/registrars/api_users_test.rb | 47 +++++++++++++++++++ 10 files changed, 65 insertions(+), 62 deletions(-) diff --git a/app/controllers/admin/api_users_controller.rb b/app/controllers/admin/api_users_controller.rb index 0f2b9e914..3afc9ddf8 100644 --- a/app/controllers/admin/api_users_controller.rb +++ b/app/controllers/admin/api_users_controller.rb @@ -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 diff --git a/app/controllers/admin/certificates_controller.rb b/app/controllers/admin/certificates_controller.rb index 636a69367..d338b2e9f 100644 --- a/app/controllers/admin/certificates_controller.rb +++ b/app/controllers/admin/certificates_controller.rb @@ -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' diff --git a/app/views/admin/api_users/_form.haml b/app/views/admin/api_users/_form.haml index 851956e54..23b245aea 100644 --- a/app/views/admin/api_users/_form.haml +++ b/app/views/admin/api_users/_form.haml @@ -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 diff --git a/app/views/admin/api_users/edit.haml b/app/views/admin/api_users/edit.haml index 867d084d5..cd32618fa 100644 --- a/app/views/admin/api_users/edit.haml +++ b/app/views/admin/api_users/edit.haml @@ -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' diff --git a/app/views/admin/api_users/index.haml b/app/views/admin/api_users/index.haml index 22e1fee79..3d5e6a320 100644 --- a/app/views/admin/api_users/index.haml +++ b/app/views/admin/api_users/index.haml @@ -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 diff --git a/app/views/admin/api_users/show.haml b/app/views/admin/api_users/show.haml index 2e13445d1..998a6c078 100644 --- a/app/views/admin/api_users/show.haml +++ b/app/views/admin/api_users/show.haml @@ -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 diff --git a/app/views/admin/certificates/show.haml b/app/views/admin/certificates/show.haml index 821d7ec9a..30d095f65 100644 --- a/app/views/admin/certificates/show.haml +++ b/app/views/admin/certificates/show.haml @@ -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 diff --git a/config/routes.rb b/config/routes.rb index fb39f4307..8cc5a5721 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -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' diff --git a/test/system/admin_area/api_users_test.rb b/test/system/admin_area/api_users_test.rb index c05975e4f..d79434ef6 100644 --- a/test/system/admin_area/api_users_test.rb +++ b/test/system/admin_area/api_users_test.rb @@ -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 diff --git a/test/system/admin_area/registrars/api_users_test.rb b/test/system/admin_area/registrars/api_users_test.rb index 3be5586b8..69288265f 100644 --- a/test/system/admin_area/registrars/api_users_test.rb +++ b/test/system/admin_area/registrars/api_users_test.rb @@ -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