Commit f8dddf4a authored by Andy Soiron's avatar Andy Soiron

Merge branch 'led/move-state-machine-from-groupmember-to-member' into 'master'

Move state machine from GroupMember to Member

See merge request gitlab-org/gitlab!71257
parents f8b652b9 4ca0cee7
......@@ -8,10 +8,6 @@ module EE
prepended do
include UsageStatistics
STATE_CREATED = 0
STATE_AWAITING = 1
STATE_ACTIVE = 2
validate :sso_enforcement, if: :group
validate :group_domain_limitations, if: :group_has_domain_limitations?
......@@ -29,24 +25,6 @@ module EE
scope :guests, -> { where(access_level: ::Gitlab::Access::GUEST) }
scope :non_owners, -> { where("members.access_level < ?", ::Gitlab::Access::OWNER) }
scope :by_user_id, ->(user_id) { where(user_id: user_id) }
state_machine :state, initial: :created do
event :wait do
transition created: :awaiting
transition active: :awaiting
end
event :activate do
transition created: :active
transition awaiting: :active
end
state :created, value: STATE_CREATED
state :awaiting, value: STATE_AWAITING
state :active, value: STATE_ACTIVE
end
before_create :set_membership_activation
end
class_methods do
......@@ -97,12 +75,6 @@ module EE
execute_hooks_for(:destroy)
end
def set_membership_activation
return unless ::Feature.enabled?(:saas_user_caps, group, default_enabled: :yaml)
self.state = group.user_cap_reached? ? STATE_AWAITING : STATE_ACTIVE
end
def execute_hooks_for(event)
return unless self.source.feature_available?(:group_webhooks)
return unless GroupHook.where(group_id: self.source.self_and_ancestors).exists?
......
......@@ -6,6 +6,28 @@ module EE
extend ::Gitlab::Utils::Override
prepended do
STATE_CREATED = 0
STATE_AWAITING = 1
STATE_ACTIVE = 2
state_machine :state, initial: :created do
event :wait do
transition created: :awaiting
transition active: :awaiting
end
event :activate do
transition created: :active
transition awaiting: :active
end
state :created, value: STATE_CREATED
state :awaiting, value: STATE_AWAITING
state :active, value: STATE_ACTIVE
end
before_create :set_membership_activation
scope :with_csv_entity_associations, -> do
includes(:user, source: [:route, :parent])
end
......@@ -119,5 +141,11 @@ module EE
def email_not_verified
_("email '%{email}' is not a verified email." % { email: user.email })
end
def set_membership_activation
return unless group && ::Feature.enabled?(:saas_user_caps, group, default_enabled: :yaml)
self.state = group.user_cap_reached? ? STATE_AWAITING : STATE_ACTIVE
end
end
end
......@@ -279,53 +279,6 @@ RSpec.describe GroupMember do
end
end
context 'check if user cap has been reached' do
let_it_be(:group) { create(:group_with_plan, plan: :ultimate_plan) }
let!(:user) { create(:user) }
subject(:add_user_to_group) { group.add_developer(user) }
context 'when the :saas_user_caps feature flag is disabled' do
before do
stub_feature_flags(saas_user_caps: false)
end
it 'leaves the group member state to created' do
add_user_to_group
expect(user.group_members.last).to be_created
end
end
context 'when the :saas_user_caps feature flag is enabled' do
before do
stub_feature_flags(saas_user_caps: group)
allow(group).to receive(:user_cap_reached?).and_return(user_cap_reached)
end
context 'when the user cap for this group has not been reached' do
let(:user_cap_reached) { false }
it 'sets the group member to active' do
add_user_to_group
expect(user.group_members.last).to be_active
end
end
context 'when the user cap for this group has been reached' do
let(:user_cap_reached) { true }
it 'sets the group member to awaiting' do
add_user_to_group
expect(user.group_members.last).to be_awaiting
end
end
end
end
describe '#provisioned_by_this_group?' do
let_it_be(:group) { create(:group) }
......
......@@ -161,4 +161,82 @@ RSpec.describe Member, type: :model do
end
end
end
context 'check if user cap has been reached' do
let_it_be(:group) { create(:group_with_plan, plan: :ultimate_plan) }
let_it_be(:project) { create(:project, namespace: group)}
let!(:user) { create(:user) }
subject(:add_user_to_group) { group.add_developer(user) }
context 'when the :saas_user_caps feature flag is disabled' do
before do
stub_feature_flags(saas_user_caps: false)
end
it 'leaves the group member state to created' do
add_user_to_group
expect(user.group_members.last).to be_created
end
it 'leaves the project member state to created' do
project.add_developer(user)
expect(user.project_members.last).to be_created
end
end
context 'when the :saas_user_caps feature flag is enabled' do
before do
stub_feature_flags(saas_user_caps: group)
allow(project.group).to receive(:user_cap_reached?).and_return(user_cap_reached)
allow(group).to receive(:user_cap_reached?).and_return(user_cap_reached)
end
context 'when the user cap for this group has not been reached' do
let(:user_cap_reached) { false }
it 'sets the group member to active' do
add_user_to_group
expect(user.group_members.last).to be_active
end
it 'sets the project member to active' do
project.add_developer(user)
expect(user.project_members.last).to be_active
end
end
context 'when the user cap for this group has been reached' do
let(:user_cap_reached) { true }
it 'sets the group member to awaiting' do
add_user_to_group
expect(user.group_members.last).to be_awaiting
end
it 'sets the project member to awaiting' do
project.add_developer(user)
expect(user.project_members.last).to be_awaiting
end
end
end
context 'when user is added to a group-less project' do
let(:project) { create(:project) }
it 'adds project member and leaves the state to created' do
project.add_developer(user)
expect(user.project_members.last).to be_created
end
end
end
end
......@@ -1768,14 +1768,12 @@ RSpec.describe ProjectPolicy do
end
context 'when parent group has resource access token creation disabled' do
let(:parent) { create(:group_with_plan, plan: :bronze_plan) }
let(:resource_access_token_creation_allowed) { false }
let(:ns_for_parent) { create(:namespace_settings, resource_access_token_creation_allowed: resource_access_token_creation_allowed) }
let(:parent) { create(:group_with_plan, plan: :bronze_plan, namespace_settings: ns_for_parent) }
let(:group) { create(:group, parent: parent) }
let(:project) { create(:project, group: group) }
before do
parent.namespace_settings.update_column(:resource_access_token_creation_allowed, false)
end
context 'cannot create resource access tokens' do
it { is_expected.not_to be_allowed(:create_resource_access_tokens) }
end
......
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