Commit 2ce025e8 authored by Stan Hu's avatar Stan Hu

Merge branch '330722-nomethoderror-undefined-method-username-for-nil-nilclass' into 'master'

Resolve "Execute member hooks only if an associated user is present"

See merge request gitlab-org/gitlab!62175
parents d8b30d79 37cbe23a
......@@ -166,10 +166,10 @@ class Member < ApplicationRecord
after_create :send_invite, if: :invite?, unless: :importing?
after_create :send_request, if: :request?, unless: :importing?
after_create :create_notification_setting, unless: [:pending?, :importing?]
after_create :post_create_hook, unless: [:pending?, :importing?]
after_update :post_update_hook, unless: [:pending?, :importing?]
after_create :post_create_hook, unless: [:pending?, :importing?], if: :hook_prerequisites_met?
after_update :post_update_hook, unless: [:pending?, :importing?], if: :hook_prerequisites_met?
after_destroy :destroy_notification_setting
after_destroy :post_destroy_hook, unless: :pending?
after_destroy :post_destroy_hook, unless: :pending?, if: :hook_prerequisites_met?
after_commit :refresh_member_authorized_projects
default_value_for :notification_level, NotificationSetting.levels[:global]
......@@ -383,6 +383,12 @@ class Member < ApplicationRecord
invite? || request?
end
def hook_prerequisites_met?
# It is essential that an associated user record exists
# so that we can successfully fire any member related hooks/notifications.
user.present?
end
def accept_request
return false unless request?
......
---
title: Execute member hooks only if an associated user is present
merge_request: 62175
author:
type: fixed
......@@ -778,10 +778,27 @@ RSpec.describe Member do
let(:invited_member) { create(:project_member, invite_email: "user@example.com", user: nil) }
let(:requester) { create(:project_member, requested_at: Time.current.utc) }
it { expect(invited_member).to be_invite }
it { expect(invited_member).to be_pending }
it { expect(requester).to be_pending }
end
describe '#hook_prerequisites_met?' do
let(:member) { create(:project_member) }
context 'when the member does not have an associated user' do
it 'returns false' do
member.update_column(:user_id, nil)
expect(member.reload.hook_prerequisites_met?).to eq(false)
end
end
context 'when the member has an associated user' do
it 'returns true' do
expect(member.hook_prerequisites_met?).to eq(true)
end
end
end
describe "#accept_invite!" do
let!(:member) { create(:project_member, invite_email: "user@example.com", user: nil) }
let(:user) { create(:user) }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment