Skip to content
Snippets Groups Projects
Verified Commit d6929fd4 authored by Eduardo Navarro's avatar Eduardo Navarro Committed by Andrej Shadura
Browse files

[api][ci] Move creation of user with fake pw to model

Add basic tests for the new method.

Cherry-picked from 51ac16ad
parent bd75ca5b
Branches
No related tags found
1 merge request!21OBS SSO implementation
......@@ -214,14 +214,10 @@ class Webui::WebuiController < ActionController::Base
# The user does not exist in our database, create her.
unless User.where(login: user_login).exists?
logger.debug "Creating user #{user_login}"
chars = ["A".."Z", "a".."z", "0".."9"].collect { |r| r.to_a }.join
fakepw = (1..24).collect { chars[rand(chars.size)] }.pack("a"*24)
User.create!(login: user_login,
User.create_user_with_fake_pw!(login: user_login,
email: request.env['HTTP_X_EMAIL'],
state: User.default_user_state,
realname: "#{request.env['HTTP_X_FIRSTNAME']} #{request.env['HTTP_X_LASTNAME']}".strip,
password: fakepw,
password_confirmation: fakepw)
realname: "#{request.env['HTTP_X_FIRSTNAME']} #{request.env['HTTP_X_LASTNAME']}".strip)
end
# The user exists, check if shes active and update the info
......
......@@ -217,6 +217,15 @@ class User < ActiveRecord::Base
end
end
def self.create_user_with_fake_pw!(attributes = {})
chars = ["A".."Z", "a".."z", "0".."9"].collect(&:to_a).join
fakepw = (1..24).collect { chars[rand(chars.size)] }.pack("a" * 24)
attributes[:password] = fakepw
create!(attributes)
end
# This static method tries to find a user with the given login and password
# in the database. Returns the user or nil if he could not be found
def self.find_with_credentials(login, password)
......
......@@ -131,4 +131,22 @@ RSpec.describe User do
expect(owned_packages[1]).to eq([project_with_package.packages.first.name, project_with_package.name])
end
end
describe 'create_user_with_fake_pw!' do
context 'with login and email' do
let(:user) { User.create_user_with_fake_pw!({ login: 'tux', email: 'some@email.com' }) }
it 'creates a user with a fake password' do
expect(user).to be_an(User)
expect(user.login).to eq('tux')
expect(user.email).to eq('some@email.com')
end
end
context 'without params' do
it 'throws an exception' do
expect{ User.create_user_with_fake_pw! }.to raise_error(ActiveRecord::RecordInvalid)
end
end
end
end
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment