From 95f4ce9178497e8d567f9a5062f10e0b6f05df41 Mon Sep 17 00:00:00 2001 From: 61315 Date: Sun, 14 Apr 2024 17:35:09 -0500 Subject: [PATCH] chore: bounty hunt (coverage) --- rails_root/app/controllers/home_controller.rb | 18 +++++---- rails_root/app/views/home/index.html.erb | 10 ++--- .../spec/controllers/auth0_controller_spec.rb | 27 +++++++++++++ .../spec/controllers/home_controller_spec.rb | 39 +++++++++++++++++++ 4 files changed, 82 insertions(+), 12 deletions(-) create mode 100644 rails_root/spec/controllers/auth0_controller_spec.rb create mode 100644 rails_root/spec/controllers/home_controller_spec.rb diff --git a/rails_root/app/controllers/home_controller.rb b/rails_root/app/controllers/home_controller.rb index 8480c05..f2f9488 100644 --- a/rails_root/app/controllers/home_controller.rb +++ b/rails_root/app/controllers/home_controller.rb @@ -11,21 +11,25 @@ def index return if @survey_profile.nil? @survey_responses = fetch_survey_responses + @invitations = fetch_invitations end private def fetch_survey_responses - SurveyResponse.where(profile_id: @survey_profile.id).map do |response| - { response:, invited_by: fetch_invited_by(response) } + SurveyResponse.where(profile_id: @survey_profile.id) + end + + def fetch_invitations + @survey_responses.map do |response| + invitation = Invitation.find_by(response_id: response.id) + invitation ? fetch_invited_by(invitation) : 'N/A' end end - def fetch_invited_by(response) - invitation = Invitation.find_by(response_id: response.id) - parent_response = SurveyResponse.find(invitation.parent_response_id) if invitation + def fetch_invited_by(invitation) + parent_response = SurveyResponse.find(invitation.parent_response_id) profile = SurveyProfile.find(parent_response.profile_id) if parent_response - name = "#{profile.first_name} #{profile.last_name}" if profile - name || 'N/A' + "#{profile.first_name} #{profile.last_name}" if profile end end diff --git a/rails_root/app/views/home/index.html.erb b/rails_root/app/views/home/index.html.erb index 6ad4ac1..b454c2d 100644 --- a/rails_root/app/views/home/index.html.erb +++ b/rails_root/app/views/home/index.html.erb @@ -21,12 +21,12 @@ <% if !@survey_responses.nil?%> - <% @survey_responses.each do |response_hash| %> + <% @survey_responses.zip(@invitations).each do |response, invited_by| %> - <%= response_hash[:response].created_at %> - <%= response_hash[:invited_by] %> - <%= response_hash[:response].share_code %> - <%= link_to "Show", survey_response_path(response_hash[:response]) %> + <%= response.created_at %> + <%= invited_by %> + <%= response.share_code %> + <%= link_to "Show", survey_response_path(response) %> <% end %> diff --git a/rails_root/spec/controllers/auth0_controller_spec.rb b/rails_root/spec/controllers/auth0_controller_spec.rb new file mode 100644 index 0000000..4127fae --- /dev/null +++ b/rails_root/spec/controllers/auth0_controller_spec.rb @@ -0,0 +1,27 @@ +# frozen_string_literal: true + +require 'rails_helper' + +RSpec.describe Auth0Controller, type: :controller do + describe '#claim_invitation' do + before do + allow(controller).to receive(:redirect_to) + end + + context 'when the invitation does not exist or has expired' do + it 'deletes the invitation from the session and returns false' do + # Set up the session with an invitation that does not exist or has expired + session[:invitation] = { 'from' => 'nonexistent', 'expiration' => 1.hour.ago } + + # Call the method + result = controller.send(:claim_invitation) + + # Check that the invitation was deleted from the session + expect(session[:invitation]).to be_nil + + # Check that the method returned false + expect(result).to be(false) + end + end + end +end diff --git a/rails_root/spec/controllers/home_controller_spec.rb b/rails_root/spec/controllers/home_controller_spec.rb new file mode 100644 index 0000000..0617e9f --- /dev/null +++ b/rails_root/spec/controllers/home_controller_spec.rb @@ -0,0 +1,39 @@ +# frozen_string_literal: true + +# spec/controllers/home_controller_spec.rb +require 'rails_helper' + +RSpec.describe HomeController, type: :controller do + before do + @inviter_profile = SurveyProfile.create!(user_id: '1', first_name: 'Gary', last_name: 'Chalmers', campus_name: 'Springfield Elementary', district_name: 'Springfield', role: 'Superintendent') + invitee_profile = SurveyProfile.create!(user_id: '2', first_name: 'Seymour', last_name: 'Skinner', campus_name: 'Springfield Elementary', district_name: 'Springfield', role: 'Principal') + parent_survey_response = SurveyResponse.create!(share_code: 'SHARECODE', profile_id: @inviter_profile.id) + + @invitation = Invitation.create!(parent_response_id: parent_survey_response.id, visited: false, last_sent: Time.now) + sharecode_from_invitation = @invitation.parent_response.share_code + @new_response_to_fill = SurveyResponse.create(profile: invitee_profile, share_code: sharecode_from_invitation) + @invitation.update!(response_id: @new_response_to_fill.id, claimed_by_id: invitee_profile.id) + + session[:userinfo] = { 'sub' => invitee_profile.user_id } + end + + describe 'GET #index' do + it 'fetches survey responses' do + get :index + expect(assigns(:survey_responses)).to eq([@new_response_to_fill]) + end + + it 'fetches invitations' do + get :index + + parent_response = @invitation.parent_response + profile = parent_response.profile + + expected_invited_by = profile.nil? ? 'N/A' : "#{profile.first_name} #{profile.last_name}" + + actual_invitations = assigns(:invitations) + + expect(actual_invitations).to eq([expected_invited_by]) + end + end +end