From c119c9a628b18ecaf8c64306071cc53edd56dae8 Mon Sep 17 00:00:00 2001 From: "Chris Baudouin, Jr" Date: Sun, 24 May 2020 01:38:45 -0400 Subject: [PATCH] feat(users): Adds ability to view all users and admins (#216) * feat(users): Adds ability to view all users and admins refactor(admins): Moves hard-coded strings to en.yml * style(users): Removes whitespace for style Co-authored-by: Peter Kos --- .../javascripts/manage/lib/setupDataTables.js | 19 +++- .../manage/questionnaires_controller.rb | 2 +- ...mins_controller.rb => users_controller.rb} | 32 ++---- app/datatables/admin_datatable.rb | 4 +- app/datatables/user_datatable.rb | 42 +++++++ .../layouts/manage/application.html.haml | 9 +- app/views/manage/admins/_form.html.haml | 19 ---- app/views/manage/admins/edit.html.haml | 7 -- app/views/manage/admins/index.html.haml | 19 ---- app/views/manage/admins/new.html.haml | 7 -- app/views/manage/admins/show.html.haml | 61 ---------- .../manage/trackable_events/show.html.haml | 2 +- app/views/manage/users/_form.html.haml | 15 +++ app/views/manage/users/edit.html.haml | 6 + app/views/manage/users/index.html.haml | 41 +++++++ app/views/manage/users/show.html.haml | 59 ++++++++++ config/locales/en.yml | 52 +++++++++ config/routes.rb | 6 +- .../manage/questionnaires_controller_test.rb | 2 +- ...oller_test.rb => users_controller_test.rb} | 106 +++++++----------- 20 files changed, 302 insertions(+), 208 deletions(-) rename app/controllers/manage/{admins_controller.rb => users_controller.rb} (53%) create mode 100644 app/datatables/user_datatable.rb delete mode 100644 app/views/manage/admins/_form.html.haml delete mode 100644 app/views/manage/admins/edit.html.haml delete mode 100644 app/views/manage/admins/index.html.haml delete mode 100644 app/views/manage/admins/new.html.haml delete mode 100644 app/views/manage/admins/show.html.haml create mode 100644 app/views/manage/users/_form.html.haml create mode 100644 app/views/manage/users/edit.html.haml create mode 100644 app/views/manage/users/index.html.haml create mode 100644 app/views/manage/users/show.html.haml rename test/controllers/manage/{admins_controller_test.rb => users_controller_test.rb} (60%) diff --git a/app/assets/javascripts/manage/lib/setupDataTables.js b/app/assets/javascripts/manage/lib/setupDataTables.js index 586c63577..2b04d7975 100644 --- a/app/assets/javascripts/manage/lib/setupDataTables.js +++ b/app/assets/javascripts/manage/lib/setupDataTables.js @@ -17,6 +17,23 @@ var setupDataTables = function () { }); $('.datatable.users').DataTable({ + order: [5, 'desc'], + columns: [ + { orderable: true, data: 'id', visible: false }, + { orderable: true, data: 'email' }, + { orderable: true, data: 'role' }, + { orderable: false, data: 'questionnaire'}, + { orderable: true, data: 'active' }, + { orderable: true, data: 'created_at' }, + { orderable: true, data: 'current_sign_in_at', visible: false }, + { orderable: true, data: 'last_sign_in_at', visible: false }, + { orderable: true, data: 'current_sign_in_ip', visible: false }, + { orderable: true, data: 'last_sign_in_ip', visible: false }, + { orderable: true, data: 'sign_in_count', visible: false }, + ], + }); + + $('.datatable.admins').DataTable({ order: [1, 'asc'], columns: [ { orderable: true, data: 'id', visible: false }, @@ -29,7 +46,7 @@ var setupDataTables = function () { { orderable: true, data: 'last_sign_in_at', visible: false }, { orderable: true, data: 'current_sign_in_ip', visible: false }, { orderable: true, data: 'last_sign_in_ip', visible: false }, - { orderable: true, data: 'sign_in_count', visible: false }, + { orderable: true, data: 'sign_in_count', visible: true }, ], }); diff --git a/app/controllers/manage/questionnaires_controller.rb b/app/controllers/manage/questionnaires_controller.rb index 70873a9b8..3e31de6a7 100644 --- a/app/controllers/manage/questionnaires_controller.rb +++ b/app/controllers/manage/questionnaires_controller.rb @@ -93,7 +93,7 @@ def convert_to_admin user = @questionnaire.user @questionnaire.destroy user.update_attributes(role: :admin) - redirect_to edit_manage_admin_path(user) + redirect_to edit_manage_user_path(user) end def destroy diff --git a/app/controllers/manage/admins_controller.rb b/app/controllers/manage/users_controller.rb similarity index 53% rename from app/controllers/manage/admins_controller.rb rename to app/controllers/manage/users_controller.rb index f22d6d794..c00222e0e 100644 --- a/app/controllers/manage/admins_controller.rb +++ b/app/controllers/manage/users_controller.rb @@ -1,4 +1,5 @@ -class Manage::AdminsController < Manage::ApplicationController +class Manage::UsersController < Manage::ApplicationController + before_action :require_full_admin before_action :find_user, only: [:show, :edit, :update, :destroy] respond_to :html, :json @@ -7,43 +8,34 @@ def index respond_with(:manage, User.where(role: [:admin, :admin_limited_access, :event_tracking])) end - def datatable - render json: AdminDatatable.new(params, view_context: view_context) + def user_datatable + render json: UserDatatable.new(params, view_context: view_context) end - def show - respond_with(:manage, @user) + def admin_datatable + render json: AdminDatatable.new(params, view_context: view_context) end - def new - @user = ::User.new + def show respond_with(:manage, @user) end def edit end - def create - @user = ::User.new(user_params.merge(password: Devise.friendly_token.first(10))) - if @user.save - @user.send_reset_password_instructions - flash[:notice] = "Created account for #{@user.email} and sent email with link to set a password" - end - respond_with(:manage, @user, location: manage_admins_path) - end - def update @user.update_attributes(user_params) - respond_with(:manage, @user, location: manage_admins_path) + respond_with(:manage, @user, location: manage_users_path) end def destroy + if @user.questionnaire.present? + @user.questionnaire.destroy + end @user.destroy - respond_with(:manage, @user, location: manage_admins_path) + respond_with(:manage, @user, location: manage_users_path) end - private - def user_params params.require(:user).permit( :email, :password, :password_confirmation, :remember_me, :role, :is_active, :receive_weekly_report diff --git a/app/datatables/admin_datatable.rb b/app/datatables/admin_datatable.rb index 62bdea5f5..16612e8be 100644 --- a/app/datatables/admin_datatable.rb +++ b/app/datatables/admin_datatable.rb @@ -1,5 +1,5 @@ class AdminDatatable < ApplicationDatatable - def_delegators :@view, :link_to, :manage_admin_path, :bold, :display_datetime + def_delegators :@view, :link_to, :manage_user_path, :bold, :display_datetime def view_columns @view_columns ||= { @@ -23,7 +23,7 @@ def data records.map do |record| { id: record.id, - email: link_to(bold(record.email), manage_admin_path(record)), + email: link_to(bold(record.email), manage_user_path(record)), role: record.role.titleize, active: record.is_active ? 'Active'.html_safe : 'Inactive'.html_safe, receive_weekly_report: yes_no_display(record.receive_weekly_report), diff --git a/app/datatables/user_datatable.rb b/app/datatables/user_datatable.rb new file mode 100644 index 000000000..e287155d7 --- /dev/null +++ b/app/datatables/user_datatable.rb @@ -0,0 +1,42 @@ +class UserDatatable < ApplicationDatatable + def_delegators :@view, :link_to, :manage_user_path, :manage_questionnaire_path, :bold, :display_datetime + + def view_columns + @view_columns ||= { + id: { source: "User.id" }, + email: { source: "User.email" }, + role: { source: "User.role", searchable: false }, + active: { source: "User.is_active", searchable: false }, + created_at: { source: "User.created_at", searchable: false }, + current_sign_in_at: { source: "User.current_sign_in_at", searchable: false }, + last_sign_in_at: { source: "User.last_sign_in_at", searchable: false }, + current_sign_in_ip: { source: "User.current_sign_in_ip" }, + last_sign_in_ip: { source: "User.last_sign_in_ip" }, + sign_in_count: { source: "User.sign_in_count", searchable: false }, + } + end + + private + + def data + records.map do |record| + { + id: record.id, + email: link_to(bold(record.email), manage_user_path(record)), + role: record.role.titleize, + questionnaire: record.questionnaire.present? ? link_to(bold("View »".html_safe), manage_questionnaire_path(record.questionnaire.id)) : 'None', + active: record.is_active ? 'Active'.html_safe : 'Inactive'.html_safe, + created_at: display_datetime(record.created_at), + current_sign_in_at: display_datetime(record.current_sign_in_at), + last_sign_in_at: display_datetime(record.last_sign_in_at), + current_sign_in_ip: record.current_sign_in_ip == "::1" ? "127.0.0.1" : record.current_sign_in_ip, + last_sign_in_ip: record.last_sign_in_ip == "::1" ? "127.0.0.1" : record.last_sign_in_ip, + sign_in_count: record.sign_in_count, + } + end + end + + def get_raw_records + User.all + end +end diff --git a/app/views/layouts/manage/application.html.haml b/app/views/layouts/manage/application.html.haml index 7339462f9..ca57834d6 100644 --- a/app/views/layouts/manage/application.html.haml +++ b/app/views/layouts/manage/application.html.haml @@ -38,10 +38,11 @@ = active_link_to manage_checkins_path, class: "nav-link" do .fa.fa-drivers-license-o.fa-fw.icon-space-r-half Check-in - %li.nav-item - = active_link_to manage_admins_path, class: "nav-link" do - .fa.fa-users.fa-fw.icon-space-r-half - Admins + - if current_user.admin? + %li.nav-item + = active_link_to manage_users_path, class: "nav-link" do + .fa.fa-users.fa-fw.icon-space-r-half + = t(:title, scope: 'pages.manage.users') %li.nav-item = active_link_to manage_messages_path, class: "nav-link" do .fa.fa-bullhorn.fa-fw.icon-space-r-half diff --git a/app/views/manage/admins/_form.html.haml b/app/views/manage/admins/_form.html.haml deleted file mode 100644 index 6baa82497..000000000 --- a/app/views/manage/admins/_form.html.haml +++ /dev/null @@ -1,19 +0,0 @@ -.form-container - = bs_horizontal_simple_form_for @user, url: url_for(action: @user.new_record? ? "create" : "update", controller: "admins"), html: { "data-validate" => "form" } do |f| - - - if @user.new_record? - %p - If a user already exists, first delete the questionnaire in the - = link_to "questionnaires manager.", manage_questionnaires_path - - if f.error_notification.present? - #disclaimer - = f.error_notification - - .form-inputs - = f.input :email, input_html: { "data-validate" => "presence" }, required: true - = f.input :role, collection: User.roles.to_a.collect{|c| [c[0].titleize, c[0]]}, include_blank: false - = f.input :is_active, collection: [['Active', true], ['Inactive', false]], as: :radio_buttons - = f.input :receive_weekly_report, collection: [['Yes', true], ['No', false]], as: :radio_buttons - - .center - = f.button :submit, value: ( @user.new_record? ? 'Create' : 'Save' ), class: 'btn-primary' diff --git a/app/views/manage/admins/edit.html.haml b/app/views/manage/admins/edit.html.haml deleted file mode 100644 index fd0b9af57..000000000 --- a/app/views/manage/admins/edit.html.haml +++ /dev/null @@ -1,7 +0,0 @@ -= render "layouts/manage/page_title", title: "Edit Admin", subtitle: @user.email do - .btn-group - = link_to 'Cancel', manage_admin_path(@user), class: 'btn btn-sm btn-outline-secondary' - = link_to 'Delete', manage_admin_path(@user), method: :delete, data: { confirm: "Are you sure? #{@user.email} will be permanently deleted. This action is irreversible." }, class: 'btn btn-sm btn-outline-secondary' - - -= render 'form' diff --git a/app/views/manage/admins/index.html.haml b/app/views/manage/admins/index.html.haml deleted file mode 100644 index ed72eef9e..000000000 --- a/app/views/manage/admins/index.html.haml +++ /dev/null @@ -1,19 +0,0 @@ -= render "layouts/manage/page_title", title: "Admins" do - = link_to "New Admin", new_manage_admin_path, class: "btn btn-sm btn-outline-secondary" - -%div - %table.users.datatable.table.table-striped.table-hover{ "data-source" => datatable_manage_admins_path(format: :json) } - %thead - %tr - %th ID - %th Email - %th Role - %th Login access - %th Weekly report - %th Registered on - %th Signed-in on - %th Previous signed-in on - %th Signed-in IP - %th Previous signed-in IP - %th Sign-in count - %tbody diff --git a/app/views/manage/admins/new.html.haml b/app/views/manage/admins/new.html.haml deleted file mode 100644 index 1a6257ff4..000000000 --- a/app/views/manage/admins/new.html.haml +++ /dev/null @@ -1,7 +0,0 @@ -= render "layouts/manage/page_title", title: "New Admin" - -= render 'form' - -.form-container - .center - = link_to 'Back', manage_admins_path diff --git a/app/views/manage/admins/show.html.haml b/app/views/manage/admins/show.html.haml deleted file mode 100644 index 73f498e0f..000000000 --- a/app/views/manage/admins/show.html.haml +++ /dev/null @@ -1,61 +0,0 @@ -= render "layouts/manage/page_title", title: @user.email do - .btn-group - = link_to 'Edit', edit_manage_admin_path(@user), class: 'btn btn-sm btn-outline-secondary' - = link_to 'Delete', manage_admin_path(@user), method: :delete, data: { confirm: "Are you sure? #{@user.email} will be permanently deleted. This action is irreversible." }, class: 'btn btn-sm btn-outline-secondary' - -.row.mt-2.mb-4 - .col-lg-6 - %p - %b Email address: - = @user.email - - %p - %b Role: - = @user.role.titleize - - %p - %b Login access: - - if @user.is_active - %span.badge.badge-success Active - - else - %span.badge.badge-danger Inactive - - %p - %b Receive weekly report: - - if @user.receive_weekly_report - %span.badge.badge-success Yes - - if !@user.is_active - %br - %small - %span.fa.fa-info-circle.icon-space-r-half - Will not receive while user is inactive - - else - %span.badge.badge-secondary No - - %p - %b Registered: - = display_datetime(@user.created_at) - - %p - %b Signed-in on: - = display_datetime(@user.current_sign_in_at) - - %p - %b Previous signed-in on: - = display_datetime(@user.last_sign_in_at) - - %p - %b Signed-in IP: - = @user.current_sign_in_ip == "::1" ? "127.0.0.1" : @user.current_sign_in_ip - - %p - %b Previous signed-in IP: - = @user.last_sign_in_ip == "::1" ? "127.0.0.1" : @user.last_sign_in_ip - - %p - %b Sign-in count: - = @user.sign_in_count - - .col-lg-6 - %h4.border-bottom.pb-2.mb-3 Change History - = render "model_history", model: @user diff --git a/app/views/manage/trackable_events/show.html.haml b/app/views/manage/trackable_events/show.html.haml index fca74599b..3933ba017 100644 --- a/app/views/manage/trackable_events/show.html.haml +++ b/app/views/manage/trackable_events/show.html.haml @@ -11,7 +11,7 @@ = link_to @trackable_event.trackable_tag.name, manage_trackable_tag_path(@trackable_event.trackable_tag) %p %b User: - = link_to @trackable_event.user.email, manage_admin_path(@trackable_event.user) + = link_to @trackable_event.user.email, manage_user_path(@trackable_event.user) = link_to 'Edit', edit_manage_trackable_event_path(@trackable_event) \| diff --git a/app/views/manage/users/_form.html.haml b/app/views/manage/users/_form.html.haml new file mode 100644 index 000000000..ca1af575d --- /dev/null +++ b/app/views/manage/users/_form.html.haml @@ -0,0 +1,15 @@ +.form-container + = bs_horizontal_simple_form_for @user, url: url_for(action: "update", controller: "users"), html: { "data-validate" => "form" } do |f| + + - if f.error_notification.present? + #disclaimer + = f.error_notification + + .form-inputs + = f.input :email, input_html: { "data-validate" => "presence" }, required: true + = f.input :role, collection: User.roles.to_a.collect{|c| [c[0].titleize, c[0]]}, include_blank: false + = f.input :is_active, collection: [[t(:active, scope: "pages.manage.users.edit.form"), true], [t(:inactive, scope: "pages.manage.users.edit.form"), false]], as: :radio_buttons + = f.input :receive_weekly_report, collection: [[t(:yes, scope: "pages.manage.users.edit.form"), true], [t(:no, scope: "pages.manage.users.edit.form"), false]], as: :radio_buttons + + .center + = f.button :submit, value: ( t(:save, scope: "pages.manage.users.edit.form") ), class: 'btn-primary' diff --git a/app/views/manage/users/edit.html.haml b/app/views/manage/users/edit.html.haml new file mode 100644 index 000000000..f2f911fa0 --- /dev/null +++ b/app/views/manage/users/edit.html.haml @@ -0,0 +1,6 @@ += render "layouts/manage/page_title", title: t(:title, scope: "pages.manage.users.edit", user_email: @user.email), subtitle: @user.email do + .btn-group + = link_to t(:cancel, scope: "pages.manage.users.edit"), manage_user_path(@user), class: 'btn btn-sm btn-outline-secondary' + = link_to t(:delete, scope: "pages.manage.users.edit"), manage_user_path(@user), method: :delete, data: { confirm: "Are you sure? #{@user.email} will be permanently deleted. This action is irreversible." }, class: 'btn btn-sm btn-outline-secondary' + += render 'form' diff --git a/app/views/manage/users/index.html.haml b/app/views/manage/users/index.html.haml new file mode 100644 index 000000000..f9b63be42 --- /dev/null +++ b/app/views/manage/users/index.html.haml @@ -0,0 +1,41 @@ += render "layouts/manage/page_title", title: t(:title, scope: 'pages.manage.users') + +.row + .col + %h5.dashboard-container-title + = t(:users, scope: 'pages.manage.users') + %table.users.datatable.table.table-striped.table-hover{ "data-source" => user_datatable_manage_users_path(format: :json) } + %thead + %tr + %th= t(:id, scope: 'pages.manage.users.table') + %th= t(:email, scope: 'pages.manage.users.table') + %th= t(:role, scope: 'pages.manage.users.table') + %th= t(:questionnaire, scope: 'pages.manage.users.table') + %th= t(:login_access, scope: 'pages.manage.users.table') + %th= t(:registered_on, scope: 'pages.manage.users.table') + %th= t(:signed_in_on, scope: 'pages.manage.users.table') + %th= t(:previous_signed_in_on, scope: 'pages.manage.users.table') + %th= t(:signed_in_ip, scope: 'pages.manage.users.table') + %th= t(:previous_signed_in_ip, scope: 'pages.manage.users.table') + %th= t(:sign_in_count, scope: 'pages.manage.users.table') + %tbody + +.row + .col + %h5.dashboard-container-title + = t(:staff, scope: 'pages.manage.users', hackathon_name: HackathonConfig['name']) + %table.admins.datatable.table.table-striped.table-hover{ "data-source" => admin_datatable_manage_users_path(format: :json) } + %thead + %tr + %th= t(:id, scope: 'pages.manage.users.table') + %th= t(:email, scope: 'pages.manage.users.table') + %th= t(:role, scope: 'pages.manage.users.table') + %th= t(:login_access, scope: 'pages.manage.users.table') + %th= t(:weekly_report, scope: 'pages.manage.users.table') + %th= t(:registered_on, scope: 'pages.manage.users.table') + %th= t(:signed_in_on, scope: 'pages.manage.users.table') + %th= t(:previous_signed_in_on, scope: 'pages.manage.users.table') + %th= t(:signed_in_ip, scope: 'pages.manage.users.table') + %th= t(:previous_signed_in_ip, scope: 'pages.manage.users.table') + %th= t(:sign_in_count, scope: 'pages.manage.users.table') + %tbody diff --git a/app/views/manage/users/show.html.haml b/app/views/manage/users/show.html.haml new file mode 100644 index 000000000..7b3387e32 --- /dev/null +++ b/app/views/manage/users/show.html.haml @@ -0,0 +1,59 @@ += render "layouts/manage/page_title", title: t(:title, scope: "pages.manage.users.show", user_email: @user.email) do + .btn-group + = link_to t(:edit, scope: "pages.manage.users.show"), edit_manage_user_path(@user), class: 'btn btn-sm btn-outline-secondary' + = link_to t(:delete, scope: "pages.manage.users.show"), manage_user_path(@user), method: :delete, data: { confirm: "Are you sure? #{@user.email} along with their questionnaire will be permanently deleted. This action is irreversible." }, class: 'btn btn-sm btn-outline-secondary' + +.row.mt-2.mb-4 + .col-lg-6 + .card.mb-3 + .card-header= t(:user_information, scope: "pages.manage.users.show") + .card-body + .row + %dt.col-md-5= t(:email_address, scope: "pages.manage.users.show") + %dd.col-md-7= @user.email + .row + %dt.col-md-5= t(:role, scope: "pages.manage.users.show") + %dd.col-md-7= @user.role.titleize + .row + %dt.col-md-5= t(:login_access, scope: "pages.manage.users.show") + %dd.col-md-7 + - if @user.is_active + %span.badge.badge-success= t(:active, scope: "pages.manage.users.show") + - else + %span.badge.badge-danger= t(:inactive, scope: "pages.manage.users.show") + .row + %dt.col-md-5= t(:receive_weekly_report, scope: "pages.manage.users.show") + %dd.col-md-7 + - if @user.receive_weekly_report + %span.badge.badge-success= t(:yes, scope: "pages.manage.users.show") + - if !@user.is_active + %br + %small + %span.fa.fa-info-circle.icon-space-r-half + = t(:inactive_warning, scope: "pages.manage.users.show") + - else + %span.badge.badge-secondary= t(:no, scope: "pages.manage.users.show") + .row + %dt.col-md-5= t(:registered, scope: "pages.manage.users.show") + %dd.col-md-7= display_datetime(@user.created_at) + .row + %dt.col-md-5= t(:signed_in_on, scope: "pages.manage.users.show") + %dd.col-md-7= display_datetime(@user.current_sign_in_at) + .row + %dt.col-md-5= t(:previous_signed_in_on, scope: "pages.manage.users.show") + %dd.col-md-7= display_datetime(@user.last_sign_in_at) + .row + %dt.col-md-5= t(:signed_in_ip, scope: "pages.manage.users.show") + %dd.col-md-7= @user.current_sign_in_ip == "::1" ? "127.0.0.1" : @user.current_sign_in_ip + .row + %dt.col-md-5= t(:previous_signed_in_ip, scope: "pages.manage.users.show") + %dd.col-md-7= @user.last_sign_in_ip == "::1" ? "127.0.0.1" : @user.last_sign_in_ip + .row + %dt.col-md-5= t(:sign_in_count, scope: "pages.manage.users.show") + %dd.col-md-7= @user.sign_in_count + + .col-lg-6 + .card.mb-3 + .card-header= t(:change_history, scope: "pages.manage.users.show") + .card-body.pb-2 + = render "model_history", model: @user diff --git a/config/locales/en.yml b/config/locales/en.yml index b883e9c5e..060806f62 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -117,3 +117,55 @@ en: custom_css: Custom CSS homepage_url: Homepage URL thanks_for_rsvp_message: Thanks For RSVP Message + pages: + manage: + users: + title: Users & Staff + users: All Users + staff: "%{hackathon_name} Staff" + table: + id: ID + email: Email + role: Role + questionnaire: Questionnaire + login_access: Login access + weekly_report: Weekly report + registered_on: Registered on + signed_in_on: Signed-in on + previous_signed_in_on: Previous signed-in on + signed_in_ip: Signed-in ip + previous_signed_in_ip: Previous signed-in IP + sign_in_count: Sign-in count + show: + title: "%{user_email}" + edit: Edit + reset_password: Reset Password + delete: Delete + user_information: User Information + email_address: Email address + role: Role + login_access: Login access + active: Active + inactive: Inactive + receive_weekly_report: Receive weekly report + yes: Yes + no: No + inactive_warning: Will not receive while user is inactive + registered: Registered + signed_in_on: Signed-in on + previous_signed_in_on: Previous signed-in on + signed_in_ip: Signed-in ip + previous_signed_in_ip: Previous signed-in IP + sign_in_count: Sign-in count + change_history: Change History + edit: + title: Edit User + subtitle: "%{user_email}" + cancel: Cancel + delete: Delete + form: + active: Active + inactive: Inactive + yes: Yes + no: No + save: Save diff --git a/config/routes.rb b/config/routes.rb index bc8661abe..0cc0da386 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -55,8 +55,10 @@ resources :checkins do post :datatable, on: :collection end - resources :admins do - post :datatable, on: :collection + resources :users do + post :user_datatable, on: :collection + post :admin_datatable, on: :collection + patch :reset_password, on: :member end resources :messages do get :preview, on: :member diff --git a/test/controllers/manage/questionnaires_controller_test.rb b/test/controllers/manage/questionnaires_controller_test.rb index 6e814b071..8499dc1f0 100644 --- a/test/controllers/manage/questionnaires_controller_test.rb +++ b/test/controllers/manage/questionnaires_controller_test.rb @@ -324,7 +324,7 @@ class Manage::QuestionnairesControllerTest < ActionController::TestCase patch :convert_to_admin, params: { id: @questionnaire } assert assigns(:questionnaire).user.admin? assert_nil assigns(:questionnaire).user.reload.questionnaire - assert_redirected_to edit_manage_admin_path(assigns(:questionnaire).user) + assert_redirected_to edit_manage_user_path(assigns(:questionnaire).user) end should "destroy questionnaire" do diff --git a/test/controllers/manage/admins_controller_test.rb b/test/controllers/manage/users_controller_test.rb similarity index 60% rename from test/controllers/manage/admins_controller_test.rb rename to test/controllers/manage/users_controller_test.rb index 2900a3dd1..1a2b5dc42 100644 --- a/test/controllers/manage/admins_controller_test.rb +++ b/test/controllers/manage/users_controller_test.rb @@ -1,53 +1,46 @@ require 'test_helper' -class Manage::AdminsControllerTest < ActionController::TestCase +class Manage::UsersControllerTest < ActionController::TestCase setup do @user = create(:user) end context "while not authenticated" do - should "redirect to sign in page on manage_admins#index" do + should "redirect to sign in page on manage_users#index" do get :index assert_response :redirect assert_redirected_to new_user_session_path end - should "not allow access to manage_admins datatables api" do - post :datatable, format: :json, params: { "columns[0][data]" => "" } + should "not allow access to manage_users user datatables api" do + post :user_datatable, format: :json, params: { "columns[0][data]" => "" } assert_response 401 end - should "not allow access to manage_admins#show" do - get :show, params: { id: @user } - assert_response :redirect - assert_redirected_to new_user_session_path + should "not allow access to manage_users admin datatables api" do + post :admin_datatable, format: :json, params: { "columns[0][data]" => "" } + assert_response 401 end - should "not allow access to manage_admins#new" do - get :new, params: { id: @user } + should "not allow access to manage_users#show" do + get :show, params: { id: @user } assert_response :redirect assert_redirected_to new_user_session_path end - should "not allow access to manage_admins#edit" do + should "not allow access to manage_users#edit" do get :edit, params: { id: @user } assert_response :redirect assert_redirected_to new_user_session_path end - should "not allow access to manage_admins#create" do - post :create, params: { user: { email: "test@example.com" } } - assert_response :redirect - assert_redirected_to new_user_session_path - end - - should "not allow access to manage_admins#update" do + should "not allow access to manage_users#update" do patch :update, params: { id: @user, user: { email: "test@example.com" } } assert_response :redirect assert_redirected_to new_user_session_path end - should "not allow access to manage_admins#destroy" do + should "not allow access to manage_users#destroy" do patch :destroy, params: { id: @user } assert_response :redirect assert_redirected_to new_user_session_path @@ -60,49 +53,43 @@ class Manage::AdminsControllerTest < ActionController::TestCase sign_in @user end - should "not allow access to manage_admins#index" do + should "not allow access to manage_users#index" do get :index assert_response :redirect assert_redirected_to root_path end - should "not allow access to manage_admins datatables api" do - post :datatable, format: :json, params: { "columns[0][data]" => "" } + should "not allow access to manage_users users datatables api" do + post :user_datatable, format: :json, params: { "columns[0][data]" => "" } assert_response :redirect assert_redirected_to root_path end - should "not allow access to manage_admins#new" do - get :new, params: { id: @user } + should "not allow access to manage_users admin datatables api" do + post :admin_datatable, format: :json, params: { "columns[0][data]" => "" } assert_response :redirect assert_redirected_to root_path end - should "not allow access to manage_admins#show" do + should "not allow access to manage_users#show" do get :show, params: { id: @user } assert_response :redirect assert_redirected_to root_path end - should "not allow access to manage_admins#edit" do + should "not allow access to manage_users#edit" do get :edit, params: { id: @user } assert_response :redirect assert_redirected_to root_path end - should "not allow access to manage_admins#create" do - post :create, params: { user: { email: "test@example.com" } } - assert_response :redirect - assert_redirected_to root_path - end - - should "not allow access to manage_admins#update" do + should "not allow access to manage_users#update" do patch :update, params: { id: @user, user: { email: "test@example.com" } } assert_response :redirect assert_redirected_to root_path end - should "not allow access to manage_admins#destroy" do + should "not allow access to manage_users#destroy" do patch :destroy, params: { id: @user } assert_response :redirect assert_redirected_to root_path @@ -116,49 +103,42 @@ class Manage::AdminsControllerTest < ActionController::TestCase sign_in @user end - should "allow access to manage_admins#index" do + should "not allow access to manage_users#index" do get :index - assert_response :success + assert_redirected_to root_path end - should "allow access to manage_admins datatables api" do - post :datatable, format: :json, params: { "columns[0][data]" => "" } - assert_response :success + should "not allow access to manage_users users datatables api" do + post :user_datatable, format: :json, params: { "columns[0][data]" => "" } + assert_redirected_to root_path end - should "allow access to manage_admins#show" do - get :show, params: { id: @user } - assert_response :success + should "not allow access to manage_users admins datatables api" do + post :admin_datatable, format: :json, params: { "columns[0][data]" => "" } + assert_redirected_to root_path end - should "not allow access to manage_admins#new" do - get :new - assert_response :redirect - assert_redirected_to manage_admins_path + should "allow access to manage_users#show" do + get :show, params: { id: @user } + assert_redirected_to root_path end - should "not allow access to manage_admins#edit" do + should "not allow access to manage_users#edit" do get :edit, params: { id: @user } assert_response :redirect - assert_redirected_to manage_admins_path - end - - should "not allow access to manage_admins#create" do - post :create, params: { user: { email: "test@example.com" } } - assert_response :redirect - assert_redirected_to manage_admins_path + assert_redirected_to manage_users_path end - should "not allow access to manage_admins#update" do + should "not allow access to manage_users#update" do patch :update, params: { id: @user, user: { email: "test@example.com" } } assert_response :redirect - assert_redirected_to manage_admins_path + assert_redirected_to manage_users_path end - should "not allow access to manage_admins#destroy" do + should "not allow access to manage_users#destroy" do patch :destroy, params: { id: @user } assert_response :redirect - assert_redirected_to manage_admins_path + assert_redirected_to manage_users_path end end @@ -169,7 +149,7 @@ class Manage::AdminsControllerTest < ActionController::TestCase sign_in @user end - should "allow access to manage_admins#index" do + should "allow access to manage_users#index" do get :index assert_response :success end @@ -179,14 +159,14 @@ class Manage::AdminsControllerTest < ActionController::TestCase # should "create a new admin" do # post :create, params: { user: { email: "test@example.com", role: 'admin' } } # assert_response :redirect - # assert_redirected_to manage_admins_path + # assert_redirected_to manage_users_path # assert assigns(:user).admin?, "new user should be an admin" # end # should "create a new limited access admin" do # post :create, params: { user: { email: "test@example.com", role: 'admin_limited_access' } } # assert_response :redirect - # assert_redirected_to manage_admins_path + # assert_redirected_to manage_users_path # assert !assigns(:user).admin?, "new user should not be an admin" # assert assigns(:user).admin_limited_access?, "new user should be a limited access admin" # end @@ -215,14 +195,14 @@ class Manage::AdminsControllerTest < ActionController::TestCase # should "update user" do # patch :update, params: { id: @user, user: { email: "test@example.coma" } } - # assert_redirected_to manage_admins_path + # assert_redirected_to manage_users_path # end # should "destroy user" do # assert_difference('User.count', -1) do # patch :destroy, params: { id: @user } # end - # assert_redirected_to manage_admins_path + # assert_redirected_to manage_users_path # end end end