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

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

177 lines
5.2 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'
2020-07-28 23:09:34 +05:30
RSpec.describe Users::CreateService do
2017-08-17 22:00:37 +05:30
describe '#execute' do
2022-08-27 11:52:29 +05:30
let(:password) { User.random_password }
2017-08-17 22:00:37 +05:30
let(:admin_user) { create(:admin) }
context 'with an admin user' do
let(:service) { described_class.new(admin_user, params) }
2020-04-22 19:07:51 +05:30
let(:email) { 'jd@example.com' }
2017-08-17 22:00:37 +05:30
context 'when required parameters are provided' do
let(:params) do
2022-08-27 11:52:29 +05:30
{ name: 'John Doe', username: 'jduser', email: email, password: password }
2017-08-17 22:00:37 +05:30
end
it 'returns a persisted user' do
expect(service.execute).to be_persisted
end
it 'persists the given attributes' do
user = service.execute
user.reload
expect(user).to have_attributes(
name: params[:name],
username: params[:username],
email: params[:email],
password: params[:password],
created_by_id: admin_user.id
)
end
context 'when the current_user is not persisted' do
let(:admin_user) { build(:admin) }
it 'persists the given attributes and sets created_by_id to nil' do
user = service.execute
user.reload
expect(user).to have_attributes(
name: params[:name],
username: params[:username],
email: params[:email],
password: params[:password],
created_by_id: nil
)
end
end
it 'user is not confirmed if skip_confirmation param is not present' do
expect(service.execute).not_to be_confirmed
end
it 'logs the user creation' do
expect(service).to receive(:log_info).with("User \"John Doe\" (jd@example.com) was created")
service.execute
end
2021-02-22 17:27:13 +05:30
it 'executes system hooks' do
2017-08-17 22:00:37 +05:30
system_hook_service = spy(:system_hook_service)
expect(service).to receive(:system_hook_service).and_return(system_hook_service)
user = service.execute
expect(system_hook_service).to have_received(:execute_hooks_for).with(user, :create)
end
it 'does not send a notification email' do
notification_service = spy(:notification_service)
expect(service).not_to receive(:notification_service)
service.execute
expect(notification_service).not_to have_received(:new_user)
end
end
context 'when force_random_password parameter is true' do
let(:params) do
2022-08-27 11:52:29 +05:30
{ name: 'John Doe', username: 'jduser', email: 'jd@example.com', password: password, force_random_password: true }
2017-08-17 22:00:37 +05:30
end
it 'generates random password' do
user = service.execute
2022-08-27 11:52:29 +05:30
expect(user.password).not_to eq password
2017-08-17 22:00:37 +05:30
expect(user.password).to be_present
end
end
context 'when password_automatically_set parameter is true' do
let(:params) do
{
name: 'John Doe',
username: 'jduser',
email: 'jd@example.com',
2022-08-27 11:52:29 +05:30
password: password,
2017-08-17 22:00:37 +05:30
password_automatically_set: true
}
end
it 'persists the given attributes' do
user = service.execute
user.reload
expect(user).to have_attributes(
name: params[:name],
username: params[:username],
email: params[:email],
password: params[:password],
created_by_id: admin_user.id,
password_automatically_set: params[:password_automatically_set]
)
end
end
context 'when skip_confirmation parameter is true' do
let(:params) do
2022-08-27 11:52:29 +05:30
{ name: 'John Doe', username: 'jduser', email: 'jd@example.com', password: password, skip_confirmation: true }
2017-08-17 22:00:37 +05:30
end
it 'confirms the user' do
expect(service.execute).to be_confirmed
end
end
context 'when reset_password parameter is true' do
let(:params) do
2022-08-27 11:52:29 +05:30
{ name: 'John Doe', username: 'jduser', email: 'jd@example.com', password: password, reset_password: true }
2017-08-17 22:00:37 +05:30
end
it 'resets password even if a password parameter is given' do
expect(service.execute).to be_recently_sent_password_reset
end
it 'sends a notification email' do
notification_service = spy(:notification_service)
expect(service).to receive(:notification_service).and_return(notification_service)
user = service.execute
expect(notification_service).to have_received(:new_user).with(user, an_instance_of(String))
end
end
end
context 'with nil user' do
let(:params) do
2022-08-27 11:52:29 +05:30
{ name: 'John Doe', username: 'jduser', email: 'jd@example.com', password: password, skip_confirmation: true }
2017-08-17 22:00:37 +05:30
end
2020-10-24 23:57:45 +05:30
2017-08-17 22:00:37 +05:30
let(:service) { described_class.new(nil, params) }
it 'persists the given attributes' do
user = service.execute
user.reload
expect(user).to have_attributes(
name: params[:name],
username: params[:username],
email: params[:email],
password: params[:password],
created_by_id: nil,
admin: false
)
end
end
end
end