2019-04-11 12:17:24 +00:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2016-10-28 10:55:33 +00:00
|
|
|
require 'spec_helper'
|
|
|
|
|
2021-04-07 21:09:01 +00:00
|
|
|
RSpec.describe Members::CreateService, :aggregate_failures, :clean_gitlab_redis_shared_state, :sidekiq_inline do
|
2020-12-29 12:10:19 +00:00
|
|
|
let_it_be(:source) { create(:project) }
|
2020-12-16 00:09:58 +00:00
|
|
|
let_it_be(:user) { create(:user) }
|
2020-12-29 12:10:19 +00:00
|
|
|
let_it_be(:member) { create(:user) }
|
|
|
|
let_it_be(:user_ids) { member.id.to_s }
|
2020-12-16 00:09:58 +00:00
|
|
|
let_it_be(:access_level) { Gitlab::Access::GUEST }
|
2021-05-27 12:10:56 +00:00
|
|
|
let(:additional_params) { {} }
|
|
|
|
let(:params) { { user_ids: user_ids, access_level: access_level }.merge(additional_params) }
|
2020-12-16 00:09:58 +00:00
|
|
|
|
2021-04-07 21:09:01 +00:00
|
|
|
subject(:execute_service) { described_class.new(user, params.merge({ source: source })).execute }
|
2016-10-28 10:55:33 +00:00
|
|
|
|
2017-06-14 18:18:56 +00:00
|
|
|
before do
|
2021-01-11 12:10:41 +00:00
|
|
|
if source.is_a?(Project)
|
|
|
|
source.add_maintainer(user)
|
|
|
|
OnboardingProgress.onboard(source.namespace)
|
|
|
|
else
|
|
|
|
source.add_owner(user)
|
|
|
|
OnboardingProgress.onboard(source)
|
|
|
|
end
|
2017-06-14 18:18:56 +00:00
|
|
|
end
|
2016-10-28 10:55:33 +00:00
|
|
|
|
2020-12-16 00:09:58 +00:00
|
|
|
context 'when passing valid parameters' do
|
|
|
|
it 'adds a user to members' do
|
|
|
|
expect(execute_service[:status]).to eq(:success)
|
2020-12-29 12:10:19 +00:00
|
|
|
expect(source.users).to include member
|
2021-01-11 12:10:41 +00:00
|
|
|
expect(OnboardingProgress.completed?(source.namespace, :user_added)).to be(true)
|
2020-12-29 12:10:19 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
context 'when executing on a group' do
|
|
|
|
let_it_be(:source) { create(:group) }
|
|
|
|
|
|
|
|
it 'adds a user to members' do
|
|
|
|
expect(execute_service[:status]).to eq(:success)
|
|
|
|
expect(source.users).to include member
|
2021-01-11 12:10:41 +00:00
|
|
|
expect(OnboardingProgress.completed?(source, :user_added)).to be(true)
|
2020-12-29 12:10:19 +00:00
|
|
|
end
|
2020-12-16 00:09:58 +00:00
|
|
|
end
|
2016-10-28 10:55:33 +00:00
|
|
|
end
|
|
|
|
|
2020-12-16 00:09:58 +00:00
|
|
|
context 'when passing no user ids' do
|
|
|
|
let(:user_ids) { '' }
|
2016-10-28 10:55:33 +00:00
|
|
|
|
2020-12-16 00:09:58 +00:00
|
|
|
it 'does not add a member' do
|
|
|
|
expect(execute_service[:status]).to eq(:error)
|
|
|
|
expect(execute_service[:message]).to be_present
|
2020-12-29 12:10:19 +00:00
|
|
|
expect(source.users).not_to include member
|
2021-01-11 12:10:41 +00:00
|
|
|
expect(OnboardingProgress.completed?(source.namespace, :user_added)).to be(false)
|
2020-12-16 00:09:58 +00:00
|
|
|
end
|
2017-06-06 14:55:12 +00:00
|
|
|
end
|
|
|
|
|
2020-12-16 00:09:58 +00:00
|
|
|
context 'when passing many user ids' do
|
|
|
|
let(:user_ids) { 1.upto(101).to_a.join(',') }
|
2017-06-06 14:55:12 +00:00
|
|
|
|
2020-12-16 00:09:58 +00:00
|
|
|
it 'limits the number of users to 100' do
|
|
|
|
expect(execute_service[:status]).to eq(:error)
|
|
|
|
expect(execute_service[:message]).to be_present
|
2020-12-29 12:10:19 +00:00
|
|
|
expect(source.users).not_to include member
|
2021-01-11 12:10:41 +00:00
|
|
|
expect(OnboardingProgress.completed?(source.namespace, :user_added)).to be(false)
|
2020-12-16 00:09:58 +00:00
|
|
|
end
|
2016-10-28 10:55:33 +00:00
|
|
|
end
|
2019-01-31 09:51:29 +00:00
|
|
|
|
2020-12-16 00:09:58 +00:00
|
|
|
context 'when passing an invalid access level' do
|
|
|
|
let(:access_level) { -1 }
|
2019-01-31 09:51:29 +00:00
|
|
|
|
2020-12-16 00:09:58 +00:00
|
|
|
it 'does not add a member' do
|
|
|
|
expect(execute_service[:status]).to eq(:error)
|
2020-12-29 12:10:19 +00:00
|
|
|
expect(execute_service[:message]).to include("#{member.username}: Access level is not included in the list")
|
|
|
|
expect(source.users).not_to include member
|
2021-01-11 12:10:41 +00:00
|
|
|
expect(OnboardingProgress.completed?(source.namespace, :user_added)).to be(false)
|
2020-12-16 00:09:58 +00:00
|
|
|
end
|
2019-01-31 09:51:29 +00:00
|
|
|
end
|
2019-05-08 05:50:43 +00:00
|
|
|
|
2020-12-16 00:09:58 +00:00
|
|
|
context 'when passing an existing invite user id' do
|
2020-12-29 12:10:19 +00:00
|
|
|
let(:user_ids) { create(:project_member, :invited, project: source).invite_email }
|
2019-05-08 05:50:43 +00:00
|
|
|
|
2020-12-16 00:09:58 +00:00
|
|
|
it 'does not add a member' do
|
|
|
|
expect(execute_service[:status]).to eq(:error)
|
|
|
|
expect(execute_service[:message]).to eq('Invite email has already been taken')
|
2021-01-11 12:10:41 +00:00
|
|
|
expect(OnboardingProgress.completed?(source.namespace, :user_added)).to be(false)
|
2020-12-16 00:09:58 +00:00
|
|
|
end
|
2019-05-08 05:50:43 +00:00
|
|
|
end
|
2021-05-27 12:10:56 +00:00
|
|
|
|
|
|
|
context 'when tracking the invite source', :snowplow do
|
|
|
|
context 'when invite_source is not passed' do
|
|
|
|
it 'tracks the invite source as unknown' do
|
|
|
|
execute_service
|
|
|
|
|
|
|
|
expect_snowplow_event(
|
|
|
|
category: described_class.name,
|
|
|
|
action: 'create_member',
|
|
|
|
label: 'unknown',
|
|
|
|
property: 'existing_user'
|
|
|
|
)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when invite_source is not passed' do
|
|
|
|
let(:additional_params) { { invite_source: '_invite_source_' } }
|
|
|
|
|
|
|
|
it 'tracks the invite source from params' do
|
|
|
|
execute_service
|
|
|
|
|
|
|
|
expect_snowplow_event(
|
|
|
|
category: described_class.name,
|
|
|
|
action: 'create_member',
|
|
|
|
label: '_invite_source_',
|
|
|
|
property: 'existing_user'
|
|
|
|
)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when it is a net_new_user' do
|
|
|
|
let(:additional_params) { { user_ids: 'email@example.org' } }
|
|
|
|
|
|
|
|
it 'tracks the invite source from params' do
|
|
|
|
execute_service
|
|
|
|
|
|
|
|
expect_snowplow_event(
|
|
|
|
category: described_class.name,
|
|
|
|
action: 'create_member',
|
|
|
|
label: 'unknown',
|
|
|
|
property: 'net_new_user'
|
|
|
|
)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2016-10-28 10:55:33 +00:00
|
|
|
end
|