Commit 53ef1de4 authored by Rémy Coutable's avatar Rémy Coutable

Fix production admin fixture to use the new `Users::CreateService`

Signed-off-by: default avatarRémy Coutable <remy@rymai.me>
parent 68aa43fd
...@@ -94,7 +94,7 @@ module Users ...@@ -94,7 +94,7 @@ module Users
def build_user_params def build_user_params
if current_user&.is_admin? if current_user&.is_admin?
user_params = params.slice(*admin_create_params) user_params = params.slice(*admin_create_params)
user_params[:created_by_id] = current_user.id user_params[:created_by_id] = current_user&.id
if params[:reset_password] if params[:reset_password]
user_params.merge!(force_random_password: true, password_expires_at: nil) user_params.merge!(force_random_password: true, password_expires_at: nil)
......
...@@ -12,10 +12,12 @@ else ...@@ -12,10 +12,12 @@ else
user_args[:password] = ENV['GITLAB_ROOT_PASSWORD'] user_args[:password] = ENV['GITLAB_ROOT_PASSWORD']
end end
user = User.new(user_args) # Only admins can create other admin users in Users::CreateService so to solve
user.skip_confirmation! # the chicken-and-egg problem, we pass a non-persisted admin user to the service.
transient_admin = User.new(admin: true)
user = Users::CreateService.new(transient_admin, user_args.merge!(skip_confirmation: true)).execute
if user.save if user.persisted?
puts "Administrator account created:".color(:green) puts "Administrator account created:".color(:green)
puts puts
puts "login: root".color(:green) puts "login: root".color(:green)
......
...@@ -61,6 +61,23 @@ describe Users::CreateService, services: true do ...@@ -61,6 +61,23 @@ describe Users::CreateService, services: true do
) )
end 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 it 'user is not confirmed if skip_confirmation param is not present' do
expect(service.execute).not_to be_confirmed expect(service.execute).not_to be_confirmed
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