Skip to content

Commit

Permalink
chore: bounty hunt (coverage)
Browse files Browse the repository at this point in the history
  • Loading branch information
61315 committed Apr 16, 2024
1 parent 58a5bf7 commit 95f4ce9
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 12 deletions.
18 changes: 11 additions & 7 deletions rails_root/app/controllers/home_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
10 changes: 5 additions & 5 deletions rails_root/app/views/home/index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,12 @@
</thead>
<% if !@survey_responses.nil?%>
<tbody>
<% @survey_responses.each do |response_hash| %>
<% @survey_responses.zip(@invitations).each do |response, invited_by| %>
<tr>
<td><%= response_hash[:response].created_at %></td>
<td><%= response_hash[:invited_by] %></td>
<td><%= response_hash[:response].share_code %></td>
<td><%= link_to "Show", survey_response_path(response_hash[:response]) %></td>
<td><%= response.created_at %></td>
<td><%= invited_by %></td>
<td><%= response.share_code %></td>
<td><%= link_to "Show", survey_response_path(response) %></td>
</tr>
<% end %>
</tbody>
Expand Down
27 changes: 27 additions & 0 deletions rails_root/spec/controllers/auth0_controller_spec.rb
Original file line number Diff line number Diff line change
@@ -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
39 changes: 39 additions & 0 deletions rails_root/spec/controllers/home_controller_spec.rb
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 95f4ce9

Please sign in to comment.