2019-07-31 22:56:46 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2017-08-17 22:00:37 +05:30
|
|
|
require 'spec_helper'
|
|
|
|
|
2020-07-28 23:09:34 +05:30
|
|
|
RSpec.describe Users::BuildService do
|
2021-03-11 19:13:27 +05:30
|
|
|
using RSpec::Parameterized::TableSyntax
|
|
|
|
|
2017-08-17 22:00:37 +05:30
|
|
|
describe '#execute' do
|
2021-06-08 01:23:25 +05:30
|
|
|
let_it_be(:current_user) { nil }
|
2021-01-03 14:25:43 +05:30
|
|
|
|
2021-06-08 01:23:25 +05:30
|
|
|
let(:params) { build_stubbed(:user).slice(:first_name, :last_name, :username, :email, :password) }
|
|
|
|
let(:service) { described_class.new(current_user, params) }
|
2017-08-17 22:00:37 +05:30
|
|
|
|
2021-06-08 01:23:25 +05:30
|
|
|
shared_examples_for 'common build items' do
|
|
|
|
it { is_expected.to be_valid }
|
2018-11-20 20:47:30 +05:30
|
|
|
|
2021-01-03 14:25:43 +05:30
|
|
|
it 'sets the created_by_id' do
|
2021-06-08 01:23:25 +05:30
|
|
|
expect(user.created_by_id).to eq(current_user&.id)
|
2021-01-03 14:25:43 +05:30
|
|
|
end
|
|
|
|
|
2021-06-08 01:23:25 +05:30
|
|
|
it 'calls UpdateCanonicalEmailService' do
|
|
|
|
expect(Users::UpdateCanonicalEmailService).to receive(:new).and_call_original
|
2020-04-22 19:07:51 +05:30
|
|
|
|
2021-06-08 01:23:25 +05:30
|
|
|
user
|
2020-04-22 19:07:51 +05:30
|
|
|
end
|
|
|
|
|
2021-06-08 01:23:25 +05:30
|
|
|
context 'when user_type is provided' do
|
|
|
|
context 'when project_bot' do
|
|
|
|
before do
|
|
|
|
params.merge!({ user_type: :project_bot })
|
|
|
|
end
|
2018-12-05 23:21:45 +05:30
|
|
|
|
2021-06-08 01:23:25 +05:30
|
|
|
it { expect(user.project_bot?).to be true }
|
|
|
|
end
|
2018-12-05 23:21:45 +05:30
|
|
|
|
2021-06-08 01:23:25 +05:30
|
|
|
context 'when not a project_bot' do
|
|
|
|
before do
|
|
|
|
params.merge!({ user_type: :alert_bot })
|
|
|
|
end
|
2018-12-05 23:21:45 +05:30
|
|
|
|
2021-06-08 01:23:25 +05:30
|
|
|
it { expect(user).to be_human }
|
2018-12-05 23:21:45 +05:30
|
|
|
end
|
|
|
|
end
|
2021-06-08 01:23:25 +05:30
|
|
|
end
|
2018-12-05 23:21:45 +05:30
|
|
|
|
2021-06-08 01:23:25 +05:30
|
|
|
shared_examples_for 'current user not admin' do
|
2018-11-20 20:47:30 +05:30
|
|
|
context 'with "user_default_external" application setting' do
|
|
|
|
where(:user_default_external, :external, :email, :user_default_internal_regex, :result) do
|
|
|
|
true | nil | 'fl@example.com' | nil | true
|
|
|
|
true | true | 'fl@example.com' | nil | true
|
2021-06-08 01:23:25 +05:30
|
|
|
true | false | 'fl@example.com' | nil | true # admin difference
|
2018-11-20 20:47:30 +05:30
|
|
|
|
|
|
|
true | nil | 'fl@example.com' | '' | true
|
|
|
|
true | true | 'fl@example.com' | '' | true
|
2021-06-08 01:23:25 +05:30
|
|
|
true | false | 'fl@example.com' | '' | true # admin difference
|
2018-11-20 20:47:30 +05:30
|
|
|
|
|
|
|
true | nil | 'fl@example.com' | '^(?:(?!\.ext@).)*$\r?' | false
|
2021-06-08 01:23:25 +05:30
|
|
|
true | true | 'fl@example.com' | '^(?:(?!\.ext@).)*$\r?' | false # admin difference
|
2018-11-20 20:47:30 +05:30
|
|
|
true | false | 'fl@example.com' | '^(?:(?!\.ext@).)*$\r?' | false
|
|
|
|
|
|
|
|
true | nil | 'tester.ext@domain.com' | '^(?:(?!\.ext@).)*$\r?' | true
|
|
|
|
true | true | 'tester.ext@domain.com' | '^(?:(?!\.ext@).)*$\r?' | true
|
2021-06-08 01:23:25 +05:30
|
|
|
true | false | 'tester.ext@domain.com' | '^(?:(?!\.ext@).)*$\r?' | true # admin difference
|
2018-11-20 20:47:30 +05:30
|
|
|
|
|
|
|
false | nil | 'fl@example.com' | nil | false
|
2021-06-08 01:23:25 +05:30
|
|
|
false | true | 'fl@example.com' | nil | false # admin difference
|
2018-11-20 20:47:30 +05:30
|
|
|
false | false | 'fl@example.com' | nil | false
|
|
|
|
|
|
|
|
false | nil | 'fl@example.com' | '' | false
|
2021-06-08 01:23:25 +05:30
|
|
|
false | true | 'fl@example.com' | '' | false # admin difference
|
2018-11-20 20:47:30 +05:30
|
|
|
false | false | 'fl@example.com' | '' | false
|
|
|
|
|
|
|
|
false | nil | 'fl@example.com' | '^(?:(?!\.ext@).)*$\r?' | false
|
2021-06-08 01:23:25 +05:30
|
|
|
false | true | 'fl@example.com' | '^(?:(?!\.ext@).)*$\r?' | false # admin difference
|
2018-11-20 20:47:30 +05:30
|
|
|
false | false | 'fl@example.com' | '^(?:(?!\.ext@).)*$\r?' | false
|
|
|
|
|
|
|
|
false | nil | 'tester.ext@domain.com' | '^(?:(?!\.ext@).)*$\r?' | false
|
2021-06-08 01:23:25 +05:30
|
|
|
false | true | 'tester.ext@domain.com' | '^(?:(?!\.ext@).)*$\r?' | false # admin difference
|
2018-11-20 20:47:30 +05:30
|
|
|
false | false | 'tester.ext@domain.com' | '^(?:(?!\.ext@).)*$\r?' | false
|
|
|
|
end
|
|
|
|
|
|
|
|
with_them do
|
|
|
|
before do
|
|
|
|
stub_application_setting(user_default_external: user_default_external)
|
|
|
|
stub_application_setting(user_default_internal_regex: user_default_internal_regex)
|
|
|
|
|
|
|
|
params.merge!({ external: external, email: email }.compact)
|
|
|
|
end
|
|
|
|
|
2021-06-08 01:23:25 +05:30
|
|
|
it 'sets the value of Gitlab::CurrentSettings.user_default_external' do
|
2018-11-20 20:47:30 +05:30
|
|
|
expect(user.external).to eq(result)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2017-08-17 22:00:37 +05:30
|
|
|
|
|
|
|
context 'when "send_user_confirmation_email" application setting is true' do
|
|
|
|
before do
|
|
|
|
stub_application_setting(send_user_confirmation_email: true, signup_enabled?: true)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'does not confirm the user' do
|
2021-06-08 01:23:25 +05:30
|
|
|
expect(user).not_to be_confirmed
|
2017-08-17 22:00:37 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when "send_user_confirmation_email" application setting is false' do
|
|
|
|
before do
|
|
|
|
stub_application_setting(send_user_confirmation_email: false, signup_enabled?: true)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'confirms the user' do
|
2021-06-08 01:23:25 +05:30
|
|
|
expect(user).to be_confirmed
|
2017-08-17 22:00:37 +05:30
|
|
|
end
|
|
|
|
end
|
2018-11-20 20:47:30 +05:30
|
|
|
|
2021-06-08 01:23:25 +05:30
|
|
|
context 'with allowed params' do
|
|
|
|
let(:params) do
|
|
|
|
{
|
|
|
|
email: 1,
|
|
|
|
name: 1,
|
|
|
|
password: 1,
|
|
|
|
password_automatically_set: 1,
|
|
|
|
username: 1,
|
|
|
|
user_type: 'project_bot'
|
|
|
|
}
|
|
|
|
end
|
2020-04-22 19:07:51 +05:30
|
|
|
|
2021-06-08 01:23:25 +05:30
|
|
|
it 'sets all allowed attributes' do
|
|
|
|
expect(User).to receive(:new).with(hash_including(params)).and_call_original
|
2020-04-22 19:07:51 +05:30
|
|
|
|
2021-06-08 01:23:25 +05:30
|
|
|
user
|
2020-04-22 19:07:51 +05:30
|
|
|
end
|
2021-06-08 01:23:25 +05:30
|
|
|
end
|
|
|
|
end
|
2020-04-22 19:07:51 +05:30
|
|
|
|
2021-06-08 01:23:25 +05:30
|
|
|
context 'with nil current_user' do
|
|
|
|
subject(:user) { service.execute }
|
|
|
|
|
|
|
|
it_behaves_like 'common build items'
|
|
|
|
it_behaves_like 'current user not admin'
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'with non admin current_user' do
|
|
|
|
let_it_be(:current_user) { create(:user) }
|
|
|
|
|
|
|
|
let(:service) { described_class.new(current_user, params) }
|
|
|
|
|
|
|
|
subject(:user) { service.execute(skip_authorization: true) }
|
|
|
|
|
|
|
|
it 'raises AccessDeniedError exception when authorization is not skipped' do
|
|
|
|
expect { service.execute }.to raise_error Gitlab::Access::AccessDeniedError
|
|
|
|
end
|
2020-04-22 19:07:51 +05:30
|
|
|
|
2021-06-08 01:23:25 +05:30
|
|
|
it_behaves_like 'common build items'
|
|
|
|
it_behaves_like 'current user not admin'
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'with an admin current_user' do
|
|
|
|
let_it_be(:current_user) { create(:admin) }
|
|
|
|
|
|
|
|
let(:params) { build_stubbed(:user).slice(:name, :username, :email, :password) }
|
|
|
|
let(:service) { described_class.new(current_user, ActionController::Parameters.new(params).permit!) }
|
|
|
|
|
|
|
|
subject(:user) { service.execute }
|
|
|
|
|
|
|
|
it_behaves_like 'common build items'
|
|
|
|
|
|
|
|
context 'with allowed params' do
|
|
|
|
let(:params) do
|
|
|
|
{
|
|
|
|
access_level: 1,
|
|
|
|
admin: 1,
|
|
|
|
avatar: anything,
|
|
|
|
bio: 1,
|
|
|
|
can_create_group: 1,
|
|
|
|
color_scheme_id: 1,
|
|
|
|
email: 1,
|
|
|
|
external: 1,
|
|
|
|
force_random_password: 1,
|
|
|
|
hide_no_password: 1,
|
|
|
|
hide_no_ssh_key: 1,
|
|
|
|
linkedin: 1,
|
|
|
|
name: 1,
|
|
|
|
password: 1,
|
|
|
|
password_automatically_set: 1,
|
|
|
|
password_expires_at: 1,
|
|
|
|
projects_limit: 1,
|
|
|
|
remember_me: 1,
|
|
|
|
skip_confirmation: 1,
|
|
|
|
skype: 1,
|
|
|
|
theme_id: 1,
|
|
|
|
twitter: 1,
|
|
|
|
username: 1,
|
|
|
|
website_url: 1,
|
|
|
|
private_profile: 1,
|
|
|
|
organization: 1,
|
|
|
|
location: 1,
|
|
|
|
public_email: 1,
|
|
|
|
user_type: 'project_bot',
|
|
|
|
note: 1,
|
|
|
|
view_diffs_file_by_file: 1
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'sets all allowed attributes' do
|
|
|
|
expect(User).to receive(:new).with(hash_including(params)).and_call_original
|
|
|
|
|
|
|
|
service.execute
|
2020-04-22 19:07:51 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-11-20 20:47:30 +05:30
|
|
|
context 'with "user_default_external" application setting' do
|
|
|
|
where(:user_default_external, :external, :email, :user_default_internal_regex, :result) do
|
|
|
|
true | nil | 'fl@example.com' | nil | true
|
|
|
|
true | true | 'fl@example.com' | nil | true
|
2021-06-08 01:23:25 +05:30
|
|
|
true | false | 'fl@example.com' | nil | false # admin difference
|
2018-11-20 20:47:30 +05:30
|
|
|
|
|
|
|
true | nil | 'fl@example.com' | '' | true
|
|
|
|
true | true | 'fl@example.com' | '' | true
|
2021-06-08 01:23:25 +05:30
|
|
|
true | false | 'fl@example.com' | '' | false # admin difference
|
2018-11-20 20:47:30 +05:30
|
|
|
|
2019-02-15 15:39:39 +05:30
|
|
|
true | nil | 'fl@example.com' | '^(?:(?!\.ext@).)*$\r?' | false
|
2021-06-08 01:23:25 +05:30
|
|
|
true | true | 'fl@example.com' | '^(?:(?!\.ext@).)*$\r?' | true # admin difference
|
2019-02-15 15:39:39 +05:30
|
|
|
true | false | 'fl@example.com' | '^(?:(?!\.ext@).)*$\r?' | false
|
2018-11-20 20:47:30 +05:30
|
|
|
|
|
|
|
true | nil | 'tester.ext@domain.com' | '^(?:(?!\.ext@).)*$\r?' | true
|
|
|
|
true | true | 'tester.ext@domain.com' | '^(?:(?!\.ext@).)*$\r?' | true
|
2021-06-08 01:23:25 +05:30
|
|
|
true | false | 'tester.ext@domain.com' | '^(?:(?!\.ext@).)*$\r?' | false # admin difference
|
2018-11-20 20:47:30 +05:30
|
|
|
|
|
|
|
false | nil | 'fl@example.com' | nil | false
|
2021-06-08 01:23:25 +05:30
|
|
|
false | true | 'fl@example.com' | nil | true # admin difference
|
2018-11-20 20:47:30 +05:30
|
|
|
false | false | 'fl@example.com' | nil | false
|
|
|
|
|
|
|
|
false | nil | 'fl@example.com' | '' | false
|
2021-06-08 01:23:25 +05:30
|
|
|
false | true | 'fl@example.com' | '' | true # admin difference
|
2018-11-20 20:47:30 +05:30
|
|
|
false | false | 'fl@example.com' | '' | false
|
|
|
|
|
|
|
|
false | nil | 'fl@example.com' | '^(?:(?!\.ext@).)*$\r?' | false
|
2021-06-08 01:23:25 +05:30
|
|
|
false | true | 'fl@example.com' | '^(?:(?!\.ext@).)*$\r?' | true # admin difference
|
2018-11-20 20:47:30 +05:30
|
|
|
false | false | 'fl@example.com' | '^(?:(?!\.ext@).)*$\r?' | false
|
|
|
|
|
|
|
|
false | nil | 'tester.ext@domain.com' | '^(?:(?!\.ext@).)*$\r?' | false
|
2021-06-08 01:23:25 +05:30
|
|
|
false | true | 'tester.ext@domain.com' | '^(?:(?!\.ext@).)*$\r?' | true # admin difference
|
2018-11-20 20:47:30 +05:30
|
|
|
false | false | 'tester.ext@domain.com' | '^(?:(?!\.ext@).)*$\r?' | false
|
|
|
|
end
|
|
|
|
|
|
|
|
with_them do
|
|
|
|
before do
|
|
|
|
stub_application_setting(user_default_external: user_default_external)
|
|
|
|
stub_application_setting(user_default_internal_regex: user_default_internal_regex)
|
|
|
|
|
|
|
|
params.merge!({ external: external, email: email }.compact)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'sets the value of Gitlab::CurrentSettings.user_default_external' do
|
|
|
|
expect(user.external).to eq(result)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2017-08-17 22:00:37 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|