+ # The fixture services/api/test/fixtures/users.yml serves as the input for this test case
+ setup do
+ # Make sure system_user exists before making "pre-test users" list
+ system_user
+ end
+
+ %w(a aa a0 aA Aa AA A0).each do |username|
+ test "#{username.inspect} is a valid username" do
+ user = User.new(username: username)
+ assert(user.valid?)
+ end
+ end
+
+ test "username is not required" do
+ user = User.new(username: nil)
+ assert(user.valid?)
+ end
+
+ test "username beginning with numeral is invalid" do
+ user = User.new(username: "0a")
+ refute(user.valid?)
+ end
+
+ "\\.-_/!@#$%^&*()[]{}".each_char do |bad_char|
+ test "username containing #{bad_char.inspect} is invalid" do
+ user = User.new(username: "bad#{bad_char}username")
+ refute(user.valid?)
+ end
+ end
+
+ test "username must be unique" do
+ user = User.new(username: users(:active).username)
+ refute(user.valid?)
+ end
+
+ test "non-admin can't update username" do
+ set_user_from_auth :rominiadmin
+ user = User.find_by_uuid(users(:rominiadmin).uuid)
+ user.username = "selfupdate"
+ assert_not_allowed { user.save }
+ end
+
+ def check_admin_username_change(fixture_name)
+ set_user_from_auth :admin_trustedclient
+ user = User.find_by_uuid(users(fixture_name).uuid)
+ user.username = "newnamefromtest"
+ assert(user.save)
+ end
+
+ test "admin can set username" do
+ check_admin_username_change(:active_no_prefs)
+ end
+
+ test "admin can update username" do
+ check_admin_username_change(:active)
+ end
+
+ test "admin can update own username" do
+ check_admin_username_change(:admin)
+ end
+
+ def check_new_username_setting(email_name, expect_name)
+ set_user_from_auth :admin
+ user = User.create!(email: "#{email_name}@example.org")
+ assert_equal(expect_name, user.username)
+ end
+
+ test "new username set from e-mail" do
+ check_new_username_setting("dakota", "dakota")
+ end
+
+ test "new username set from e-mail with leading digits" do
+ check_new_username_setting("1dakota9", "dakota9")
+ end
+
+ test "new username set from e-mail with punctuation" do
+ check_new_username_setting("dakota.9", "dakota9")
+ end
+
+ test "new username set from e-mail with leading digits and punctuation" do
+ check_new_username_setting("1.dakota.z", "dakotaz")
+ end
+
+ test "new username set from e-mail with extra part" do
+ check_new_username_setting("dakota+arvados", "dakota")
+ end
+
+ test "new username set with deduplication" do
+ name = users(:active).username
+ check_new_username_setting(name, "#{name}2")
+ end
+
+ test "new username set avoiding blacklist" do
+ Rails.configuration.auto_setup_name_blacklist = ["root"]
+ check_new_username_setting("root", "root2")
+ end
+
+ test "no username set when no base available" do
+ check_new_username_setting("_", nil)
+ end
+
+ test "updating username updates repository names" do
+ set_user_from_auth :admin
+ user = users(:active)
+ user.username = "newtestname"
+ assert(user.save, "username update failed")
+ {foo: "newtestname/foo", repository2: "newtestname/foo2"}.
+ each_pair do |repo_sym, expect_name|
+ assert_equal(expect_name, repositories(repo_sym).name)
+ end
+ end
+
+ test "admin can clear username when user owns no repositories" do
+ set_user_from_auth :admin
+ user = users(:spectator)
+ user.username = nil
+ assert(user.save)
+ assert_nil(user.username)
+ end
+
+ test "admin can't clear username when user owns repositories" do
+ set_user_from_auth :admin
+ user = users(:active)
+ start_username = user.username
+ user.username = nil
+ assert_not_allowed { user.save }
+ refute_empty(user.errors[:username])
+ end
+
+ test "failed username update doesn't change repository names" do
+ set_user_from_auth :admin
+ user = users(:active)
+ user.username = users(:fuse).username
+ assert_not_allowed { user.save }
+ assert_equal("active/foo", repositories(:foo).name)
+ end
+
+ [[false, 'foo@example.com', true, nil],
+ [false, 'bar@example.com', nil, true],
+ [true, 'foo@example.com', true, nil],
+ [true, 'bar@example.com', true, true],
+ [false, false, nil, nil],
+ [true, false, true, nil]
+ ].each do |auto_admin_first_user_config, auto_admin_user_config, foo_should_be_admin, bar_should_be_admin|
+ # In each case, 'foo' is created first, then 'bar', then 'bar2', then 'baz'.
+ test "auto admin with auto_admin_first=#{auto_admin_first_user_config} auto_admin=#{auto_admin_user_config}" do
+
+ if auto_admin_first_user_config
+ # This test requires no admin users exist (except for the system user)
+ users(:admin).delete
+ @all_users = User.where("uuid not like '%-000000000000000'").where(:is_admin => true).find(:all)
+ assert_equal 0, @all_users.size, "No admin users should exist (except for the system user)"
+ end
+
+ Rails.configuration.auto_admin_first_user = auto_admin_first_user_config
+ Rails.configuration.auto_admin_user = auto_admin_user_config
+
+ # See if the foo user has is_admin
+ foo = User.new
+ foo.first_name = 'foo'
+ foo.email = 'foo@example.com'
+
+ act_as_system_user do
+ foo.save!
+ end
+
+ foo = User.find(foo.id) # get the user back
+ assert_equal foo_should_be_admin, foo.is_admin, "is_admin is wrong for user foo"
+ assert_equal 'foo', foo.first_name
+
+ # See if the bar user has is_admin
+ bar = User.new
+ bar.first_name = 'bar'
+ bar.email = 'bar@example.com'
+
+ act_as_system_user do
+ bar.save!
+ end
+
+ bar = User.find(bar.id) # get the user back
+ assert_equal bar_should_be_admin, bar.is_admin, "is_admin is wrong for user bar"
+ assert_equal 'bar', bar.first_name
+
+ # A subsequent user with the bar@example.com address should never be
+ # elevated to admin
+ bar2 = User.new
+ bar2.first_name = 'bar2'
+ bar2.email = 'bar@example.com'
+
+ act_as_system_user do
+ bar2.save!
+ end
+
+ bar2 = User.find(bar2.id) # get the user back
+ assert !bar2.is_admin, "is_admin is wrong for user bar2"
+ assert_equal 'bar2', bar2.first_name
+
+ # An ordinary new user should not be elevated to admin
+ baz = User.new
+ baz.first_name = 'baz'
+ baz.email = 'baz@example.com'
+
+ act_as_system_user do
+ baz.save!
+ end
+
+ baz = User.find(baz.id) # get the user back
+ assert !baz.is_admin
+ assert_equal 'baz', baz.first_name
+
+ end
+ end
+