Skip to content

Commit

Permalink
Fix Staff email invite
Browse files Browse the repository at this point in the history
There is an issue when inviting a staff user where the check for the
AnonymousSupportUser class doesn't work as expected.

Rather than check on class, we can use duck-typing and check for the
presence of an expected method.

This means the code is less coupled to a specific class and is more
explicit about the expectations for the code branch.
  • Loading branch information
felixclack committed Sep 12, 2023
1 parent f438556 commit b356af0
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 1 deletion.
2 changes: 1 addition & 1 deletion app/controllers/staff/invitations_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ class Staff::InvitationsController < Devise::InvitationsController
protected

def invite_resource(&block)
if current_inviter.is_a?(AnonymousSupportUser)
if !current_inviter.respond_to?(:primary_key)
resource_class.invite!(invite_params, &block)
else
super
Expand Down
75 changes: 75 additions & 0 deletions spec/system/support/staff_invite_as_staff_member_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
require 'rails_helper'

RSpec.feature 'Staff support', type: :system do
include CommonSteps

scenario "Staff user invites another Staff member" do
given_the_service_is_open
and_a_staff_member_exists
and_i_am_logged_in_as_a_staff_member
when_i_visit_the_staff_page
then_i_see_the_staff_index

when_i_click_on_invite
then_i_see_the_staff_invitation_form

when_i_fill_the_invitation_form
and_i_send_invitation
then_i_see_an_invitation_email
then_i_see_the_staff_index
then_i_see_the_invited_staff_user
end

private

def and_a_staff_member_exists
create(:staff, :confirmed)
end

def and_i_am_logged_in_as_a_staff_member
visit new_staff_session_path
fill_in 'Email', with: '[email protected]'
fill_in 'Password', with: 'password'
click_button 'Log in'
end

def when_i_visit_the_staff_page
visit support_interface_staff_index_path
end

def then_i_see_the_staff_index
expect(page).to have_current_path('/support/staff')
expect(page).to have_content('Staff')
end

def when_i_click_on_invite
click_link 'Invite staff user'
end

def then_i_see_the_staff_invitation_form
expect(page).to have_current_path('/staff/invitation/new')
expect(page).to have_content('Send invitation')
end

def when_i_fill_the_invitation_form
fill_in 'Email', with: '[email protected]'
end

def and_i_send_invitation
click_button 'Send an invitation'
end

def then_i_see_an_invitation_email
perform_enqueued_jobs
message = ActionMailer::Base.deliveries.first
expect(message).to_not be_nil

expect(message.subject).to eq("Invitation instructions")
expect(message.to).to include("[email protected]")
end

def then_i_see_the_invited_staff_user
expect(page).to have_content("[email protected]")
end
end

0 comments on commit b356af0

Please sign in to comment.