diff --git a/app/models/invite_redeemer.rb b/app/models/invite_redeemer.rb index dae43a92bc9..e799caf45a9 100644 --- a/app/models/invite_redeemer.rb +++ b/app/models/invite_redeemer.rb @@ -112,8 +112,10 @@ InviteRedeemer = Struct.new(:invite, :username, :name) do end def notify_invitee - invite.invited_by.notifications.create(notification_type: Notification.types[:invitee_accepted], - data: {display_username: invited_user.username}.to_json) + if inviter = invite.invited_by + inviter.notifications.create(notification_type: Notification.types[:invitee_accepted], + data: {display_username: invited_user.username}.to_json) + end end def delete_duplicate_invites diff --git a/spec/models/invite_redeemer_spec.rb b/spec/models/invite_redeemer_spec.rb index a61819fd49c..4b5d1bcc447 100644 --- a/spec/models/invite_redeemer_spec.rb +++ b/spec/models/invite_redeemer_spec.rb @@ -2,8 +2,9 @@ require 'rails_helper' describe InviteRedeemer do - describe '#create_for_email' do + describe '#create_user_from_invite' do let(:user) { InviteRedeemer.create_user_from_invite(Fabricate(:invite, email: 'walter.white@email.com'), 'walter', 'Walter White') } + it "should be created correctly" do expect(user.username).to eq('walter') expect(user.name).to eq('Walter White') @@ -11,4 +12,32 @@ describe InviteRedeemer do expect(user.email).to eq('walter.white@email.com') end end + + describe "#redeem" do + let(:invite) { Fabricate(:invite) } + let(:name) { 'john snow' } + let(:username) { 'kingofthenorth' } + let(:invite_redeemer) { InviteRedeemer.new(invite, username, name) } + + it "should redeem the invite" do + inviter = invite.invited_by + user = invite_redeemer.redeem + + expect(user.name).to eq(name) + expect(user.username).to eq(username) + expect(user.invited_by).to eq(inviter) + expect(inviter.notifications.count).to eq(1) + end + + it "should not blow up if invited_by user has been removed" do + invite.invited_by.destroy! + invite.reload + + user = invite_redeemer.redeem + + expect(user.name).to eq(name) + expect(user.username).to eq(username) + expect(user.invited_by).to eq(nil) + end + end end