debian-mirror-gitlab/spec/services/members/create_service_spec.rb

86 lines
2.9 KiB
Ruby
Raw Normal View History

2019-07-31 22:56:46 +05:30
# frozen_string_literal: true
2017-08-17 22:00:37 +05:30
require 'spec_helper'
2021-04-29 21:17:54 +05:30
RSpec.describe Members::CreateService, :aggregate_failures, :clean_gitlab_redis_shared_state, :sidekiq_inline do
2021-03-08 18:12:59 +05:30
let_it_be(:source) { create(:project) }
2021-02-22 17:27:13 +05:30
let_it_be(:user) { create(:user) }
2021-03-08 18:12:59 +05:30
let_it_be(:member) { create(:user) }
let_it_be(:user_ids) { member.id.to_s }
2021-02-22 17:27:13 +05:30
let_it_be(:access_level) { Gitlab::Access::GUEST }
let(:params) { { user_ids: user_ids, access_level: access_level } }
2021-04-29 21:17:54 +05:30
subject(:execute_service) { described_class.new(user, params.merge({ source: source })).execute }
2017-08-17 22:00:37 +05:30
2017-09-10 17:25:29 +05:30
before do
2021-03-08 18:12:59 +05:30
if source.is_a?(Project)
source.add_maintainer(user)
OnboardingProgress.onboard(source.namespace)
else
source.add_owner(user)
OnboardingProgress.onboard(source)
end
2017-09-10 17:25:29 +05:30
end
2017-08-17 22:00:37 +05:30
2021-02-22 17:27:13 +05:30
context 'when passing valid parameters' do
it 'adds a user to members' do
expect(execute_service[:status]).to eq(:success)
2021-03-08 18:12:59 +05:30
expect(source.users).to include member
expect(OnboardingProgress.completed?(source.namespace, :user_added)).to be(true)
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
expect(OnboardingProgress.completed?(source, :user_added)).to be(true)
end
2021-02-22 17:27:13 +05:30
end
2017-08-17 22:00:37 +05:30
end
2021-02-22 17:27:13 +05:30
context 'when passing no user ids' do
let(:user_ids) { '' }
2017-08-17 22:00:37 +05:30
2021-02-22 17:27:13 +05:30
it 'does not add a member' do
expect(execute_service[:status]).to eq(:error)
expect(execute_service[:message]).to be_present
2021-03-08 18:12:59 +05:30
expect(source.users).not_to include member
expect(OnboardingProgress.completed?(source.namespace, :user_added)).to be(false)
2021-02-22 17:27:13 +05:30
end
2017-09-10 17:25:29 +05:30
end
2021-02-22 17:27:13 +05:30
context 'when passing many user ids' do
let(:user_ids) { 1.upto(101).to_a.join(',') }
2017-09-10 17:25:29 +05:30
2021-02-22 17:27:13 +05:30
it 'limits the number of users to 100' do
expect(execute_service[:status]).to eq(:error)
expect(execute_service[:message]).to be_present
2021-03-08 18:12:59 +05:30
expect(source.users).not_to include member
expect(OnboardingProgress.completed?(source.namespace, :user_added)).to be(false)
2021-02-22 17:27:13 +05:30
end
2017-08-17 22:00:37 +05:30
end
2019-03-02 22:35:43 +05:30
2021-02-22 17:27:13 +05:30
context 'when passing an invalid access level' do
let(:access_level) { -1 }
2019-03-02 22:35:43 +05:30
2021-02-22 17:27:13 +05:30
it 'does not add a member' do
expect(execute_service[:status]).to eq(:error)
2021-03-08 18:12:59 +05:30
expect(execute_service[:message]).to include("#{member.username}: Access level is not included in the list")
expect(source.users).not_to include member
expect(OnboardingProgress.completed?(source.namespace, :user_added)).to be(false)
2021-02-22 17:27:13 +05:30
end
2019-03-02 22:35:43 +05:30
end
2019-07-31 22:56:46 +05:30
2021-02-22 17:27:13 +05:30
context 'when passing an existing invite user id' do
2021-03-08 18:12:59 +05:30
let(:user_ids) { create(:project_member, :invited, project: source).invite_email }
2019-07-31 22:56:46 +05:30
2021-02-22 17:27:13 +05:30
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-03-08 18:12:59 +05:30
expect(OnboardingProgress.completed?(source.namespace, :user_added)).to be(false)
2021-02-22 17:27:13 +05:30
end
2019-07-31 22:56:46 +05:30
end
2017-08-17 22:00:37 +05:30
end