Commit a02973bb authored by Doug Stull's avatar Doug Stull Committed by Arturo Herrero

Add daily limit logic to invite service

- bring inline with create_service.rb.
parent 2701da3d
...@@ -13,9 +13,9 @@ module Members ...@@ -13,9 +13,9 @@ module Members
end end
def execute(source) def execute(source)
@source = source
validate_emails! validate_emails!
@source = source
emails.each(&method(:process_email)) emails.each(&method(:process_email))
result result
rescue BlankEmailsError, TooManyEmailsError => e rescue BlankEmailsError, TooManyEmailsError => e
...@@ -96,3 +96,5 @@ module Members ...@@ -96,3 +96,5 @@ module Members
end end
end end
end end
Members::InviteService.prepend_if_ee('EE::Members::InviteService')
# frozen_string_literal: true
module EE
module Members
module InviteService
extend ::Gitlab::Utils::Override
private
override :validate_emails!
def validate_emails!
super
if invite_quota_exceeded?
raise ::Members::InviteService::TooManyEmailsError,
s_("AddMember|Invite limit of %{daily_invites} per day exceeded") %
{ daily_invites: source.actual_limits.daily_invites }
end
end
def invite_quota_exceeded?
return unless source.actual_limits.daily_invites
invite_count = ::Member.invite.created_today.in_hierarchy(source).count
source.actual_limits.exceeded?(:daily_invites, invite_count + emails.count)
end
end
end
end
---
title: Add daily quota limit to invitations API endpoint
merge_request: 56781
author:
type: other
# frozen_string_literal: true
require 'spec_helper'
RSpec.describe Members::InviteService, :aggregate_failures do
let_it_be(:user) { create(:user) }
let_it_be(:root_ancestor) { create(:group) }
let_it_be(:project, reload: true) { create(:project, group: root_ancestor) }
let_it_be(:subgroup) { create(:group, parent: root_ancestor) }
let_it_be(:subgroup_project) { create(:project, group: subgroup) }
let(:params) { { email: %w[email@example.org email2@example.org], access_level: Gitlab::Access::GUEST } }
subject(:result) { described_class.new(user, params).execute(project) }
before_all do
project.add_maintainer(user)
create(:project_member, :invited, project: subgroup_project, created_at: 2.days.ago)
create(:project_member, :invited, project: subgroup_project)
create(:group_member, :invited, group: subgroup, created_at: 2.days.ago)
create(:group_member, :invited, group: subgroup)
end
describe '#execute' do
context 'with group plan' do
let(:plan_limits) { create(:plan_limits, daily_invites: daily_invites) }
let(:plan) { create(:plan, limits: plan_limits) }
let!(:subscription) do
create(
:gitlab_subscription,
namespace: root_ancestor,
hosted_plan: plan
)
end
shared_examples 'quota limit exceeded' do |limit|
it 'limits the number of daily invites allowed' do
expect { result }.not_to change(ProjectMember, :count)
expect(result[:status]).to eq(:error)
expect(result[:message]).to eq("Invite limit of #{limit} per day exceeded")
end
end
context 'already exceeded invite quota limit' do
let(:daily_invites) { 2 }
it_behaves_like 'quota limit exceeded', 2
end
context 'will exceed invite quota limit' do
let(:daily_invites) { 3 }
it_behaves_like 'quota limit exceeded', 3
end
context 'within invite quota limit' do
let(:daily_invites) { 5 }
it 'successfully creates members' do
expect { result }.to change(ProjectMember, :count).by(2)
expect(result[:status]).to eq(:success)
end
end
context 'infinite invite quota limit' do
let(:daily_invites) { 0 }
it 'successfully creates members' do
expect { result }.to change(ProjectMember, :count).by(2)
expect(result[:status]).to eq(:success)
end
end
end
context 'without a plan' do
let(:plan) { nil }
it 'successfully creates members' do
expect { result }.to change(ProjectMember, :count).by(2)
expect(result[:status]).to eq(:success)
end
end
end
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