From 22819efc644743cfc5164c006453bf3aff11bbc8 Mon Sep 17 00:00:00 2001 From: "Leo Hsu (Hsu, Chih-Chuan)" <75620039+LeoHsuProgrammingLab@users.noreply.github.com> Date: Thu, 31 Oct 2024 03:11:43 -0500 Subject: [PATCH] Revert "Revert "Admin management display page"" --- README.md | 2 ++ .../app/controllers/admins_controller.rb | 4 ++- rails_root/app/helpers/admins_helper.rb | 3 ++ rails_root/app/models/survey_response.rb | 8 ++--- rails_root/app/views/admins/index.html.erb | 28 +++++++++------- rails_root/app/views/layouts/_header.html.erb | 4 +++ rails_root/config/routes.rb | 4 ++- rails_root/features/admin_management.feature | 12 +++++++ .../admin_management_steps.rb | 29 +++++++++++++++++ rails_root/spec/helpers/admins_helper_spec.rb | 2 ++ .../spec/requests/admin_dashboard_spec.rb | 32 +++++++++++++++++++ rails_root/spec/requests/admins_spec.rb | 11 ++++--- .../spec/views/admins/index.html.erb_spec.rb | 4 ++- 13 files changed, 119 insertions(+), 24 deletions(-) create mode 100644 rails_root/features/admin_management.feature create mode 100644 rails_root/features/step_definitions/admin_management_steps.rb create mode 100644 rails_root/spec/requests/admin_dashboard_spec.rb diff --git a/README.md b/README.md index 11b60bb..35dc92e 100644 --- a/README.md +++ b/README.md @@ -152,3 +152,5 @@ heroku run rails db:seed * Sai Nithin * Vinayaka Hegde * [Legacy Code](https://github.com/tamu-edu-students/csce606-ELRC-Synergistic-Leadership-Theory) + + diff --git a/rails_root/app/controllers/admins_controller.rb b/rails_root/app/controllers/admins_controller.rb index 83e0bb5..1551c9c 100644 --- a/rails_root/app/controllers/admins_controller.rb +++ b/rails_root/app/controllers/admins_controller.rb @@ -1,4 +1,6 @@ -#admins_controller.rb +# frozen_string_literal: true + +# admins_controller.rb class AdminsController < ApplicationController def index @survey_responses = SurveyResponse.get_all_responses(page: params[:page], per_page: 20) diff --git a/rails_root/app/helpers/admins_helper.rb b/rails_root/app/helpers/admins_helper.rb index d4f7b34..8955410 100644 --- a/rails_root/app/helpers/admins_helper.rb +++ b/rails_root/app/helpers/admins_helper.rb @@ -1,2 +1,5 @@ +# frozen_string_literal: true + +# helper method for the admin controller module AdminsHelper end diff --git a/rails_root/app/models/survey_response.rb b/rails_root/app/models/survey_response.rb index f1e61ef..9415290 100644 --- a/rails_root/app/models/survey_response.rb +++ b/rails_root/app/models/survey_response.rb @@ -16,10 +16,10 @@ class SurveyResponse < ApplicationRecord belongs_to :profile, class_name: 'SurveyProfile' - has_many :invitations, - foreign_key: :parent_response_id, - class_name: 'Invitation', - dependent: :destroy + has_many :invitations, + foreign_key: :parent_response_id, + class_name: 'Invitation', + dependent: :destroy def self.create_from_params(user_id, params) # FIXME: When we look up things and fail, we should use more descriptive exceptions instead of ActiveRecord::RecordNotFound diff --git a/rails_root/app/views/admins/index.html.erb b/rails_root/app/views/admins/index.html.erb index d2db529..a332500 100644 --- a/rails_root/app/views/admins/index.html.erb +++ b/rails_root/app/views/admins/index.html.erb @@ -8,22 +8,26 @@
- - - - - - + + + + + + + <% if @survey_responses.present? %> <% @survey_responses.each do |response| %> - - - - - - + + + + + + + <% end %> <% end %> diff --git a/rails_root/app/views/layouts/_header.html.erb b/rails_root/app/views/layouts/_header.html.erb index c4cbf95..83c614a 100644 --- a/rails_root/app/views/layouts/_header.html.erb +++ b/rails_root/app/views/layouts/_header.html.erb @@ -23,6 +23,10 @@ + + diff --git a/rails_root/config/routes.rb b/rails_root/config/routes.rb index 32d6ccb..bac42dc 100644 --- a/rails_root/config/routes.rb +++ b/rails_root/config/routes.rb @@ -8,7 +8,9 @@ get 'home/index' get 'about', to: 'about#index' - get '/admin', to: 'admins#index' + get '/admin', to: 'admins#index', as: 'admin_dashboard' + #get 'survey_responses/:id', to: 'survey_responses#show', as: 'survey_response' + # get 'survey', to: 'survey_responses#new', as: 'survey' # get 'survey/page/:page', to: 'survey_responses#survey', as: 'survey_page' diff --git a/rails_root/features/admin_management.feature b/rails_root/features/admin_management.feature new file mode 100644 index 0000000..f1e6a72 --- /dev/null +++ b/rails_root/features/admin_management.feature @@ -0,0 +1,12 @@ +Feature: Admin Dashboard Access + I want to see the Admin Dashboard link + So that I can access administrative functions + + Background: + Given I am on the homepage + And I try to login + + Scenario: Principal sees "Admin Dashboard" link and clicks it + Then I should see "Admin Dashboard" in the header + When I click on "Admin Dashboard" + Then I should be on the Admin Dashboard page diff --git a/rails_root/features/step_definitions/admin_management_steps.rb b/rails_root/features/step_definitions/admin_management_steps.rb new file mode 100644 index 0000000..b187b95 --- /dev/null +++ b/rails_root/features/step_definitions/admin_management_steps.rb @@ -0,0 +1,29 @@ +# frozen_string_literal: true + +# Step definition for navigating to the homepage +Given('I am on the homepage') do + visit root_path + expect(page).to have_current_path(root_path) # Ensure the user is on the homepage +end + +Given('I have logged in as a {string}') do |role| + # Create a user with the specified role (principal, teacher, superintendent) + @user = FactoryBot.create(:survey_profile, role:) + allow_any_instance_of(ApplicationController).to receive(:current_user_id).and_return(@user.user_id) +end + +Then('I should see {string} in the header') do |link_text| + # Check that the Admin Dashboard link is present + expect(page).to have_link(link_text) +end + +When('I click on {string}') do |link_text| + # Click the Admin Dashboard link + click_link(link_text) +end + +Then('I should be on the Admin Dashboard page') do + # Verify that the user is redirected to the Admin Dashboard page + expect(current_path).to eq(admin_dashboard_path) + expect(page).to have_content('Admin Dashboard') +end diff --git a/rails_root/spec/helpers/admins_helper_spec.rb b/rails_root/spec/helpers/admins_helper_spec.rb index 67e4a8d..e99f449 100644 --- a/rails_root/spec/helpers/admins_helper_spec.rb +++ b/rails_root/spec/helpers/admins_helper_spec.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + require 'rails_helper' # Specs in this file have access to a helper object that includes diff --git a/rails_root/spec/requests/admin_dashboard_spec.rb b/rails_root/spec/requests/admin_dashboard_spec.rb new file mode 100644 index 0000000..d4d8124 --- /dev/null +++ b/rails_root/spec/requests/admin_dashboard_spec.rb @@ -0,0 +1,32 @@ +# frozen_string_literal: true + +require 'rails_helper' + +RSpec.describe 'Admin Dashboard Access', type: :request do + # Create a user with the principal role (you can change this to other roles if needed) + let(:user) { FactoryBot.create(:survey_profile, role: 'Teacher') } + + before do + allow_any_instance_of(SurveyResponsesController).to receive(:current_user_id).and_return(user.user_id) + end + + describe 'GET / (Homepage)' do + it 'displays the Admin Dashboard link in the header' do + # Visit the homepage + get root_path + + expect(response).to have_http_status(:ok) + expect(response.body).to include('Admin Dashboard') + end + end + + describe 'GET /admin_dashboard' do + it 'redirects to the Admin Dashboard page when clicked' do + # Visit the Admin Dashboard page + get admin_dashboard_path + + expect(response).to have_http_status(:ok) + expect(response.body).to include('Admin Dashboard') + end + end +end diff --git a/rails_root/spec/requests/admins_spec.rb b/rails_root/spec/requests/admins_spec.rb index 355fdd0..a7c195d 100644 --- a/rails_root/spec/requests/admins_spec.rb +++ b/rails_root/spec/requests/admins_spec.rb @@ -1,11 +1,12 @@ +# frozen_string_literal: true + require 'rails_helper' -RSpec.describe "Admins", type: :request do - describe "GET /index" do - it "returns http success" do - get "/admins/index" +RSpec.describe 'Admins', type: :request do + describe 'GET /index' do + it 'returns http success' do + get '/admins/index' expect(response).to have_http_status(:success) end end - end diff --git a/rails_root/spec/views/admins/index.html.erb_spec.rb b/rails_root/spec/views/admins/index.html.erb_spec.rb index 094b2f0..5aa5e8f 100644 --- a/rails_root/spec/views/admins/index.html.erb_spec.rb +++ b/rails_root/spec/views/admins/index.html.erb_spec.rb @@ -1,5 +1,7 @@ +# frozen_string_literal: true + require 'rails_helper' -RSpec.describe "admins/index.html.erb", type: :view do +RSpec.describe 'admins/index.html.erb', type: :view do pending "add some examples to (or delete) #{__FILE__}" end
IDProfileShare CodeCreated At
IDProfileShare CodeCreated AtView Survey
<%= response.id %><%= response.profile.user_id %><%= response.share_code %><%= response.created_at.strftime('%Y-%m-%d %H:%M:%S') %>
<%= response.id %><%= response.profile.user_id %><%= response.share_code %><%= response.created_at.strftime('%Y-%m-%d %H:%M:%S') %> + <%= link_to 'View Survey', survey_response_path(response) %> +