X-Git-Url: https://git.arvados.org/arvados.git/blobdiff_plain/f580cf6d3d86f6591e68b4501279fc45cbf847e0..ebb166d65eb37f89edeccfc5be97014f3f2a73e2:/services/api/test/unit/user_test.rb diff --git a/services/api/test/unit/user_test.rb b/services/api/test/unit/user_test.rb index 6cee757ee1..e5352bf0fa 100644 --- a/services/api/test/unit/user_test.rb +++ b/services/api/test/unit/user_test.rb @@ -87,6 +87,19 @@ class UserTest < ActiveSupport::TestCase assert_equal found_user.identity_url, user.identity_url end + test "full name should not contain spurious whitespace" do + Thread.current[:user] = @admin_user # set admin user as the current user + + user = User.create ({uuid: 'zzzzz-tpzed-abcdefghijklmno', email: 'foo@example.com' }) + + assert_equal '', user.full_name + + user.first_name = 'John' + user.last_name = 'Smith' + + assert_equal user.first_name + ' ' + user.last_name, user.full_name + end + test "create new user" do Thread.current[:user] = @admin_user # set admin user as the current user @@ -109,6 +122,134 @@ class UserTest < ActiveSupport::TestCase assert_equal(user.first_name, 'first_name_for_newly_created_user_updated') end + test "create new inactive user with new_inactive_user_notification_recipients empty" do + Thread.current[:user] = @admin_user # set admin user as the current user + + Rails.configuration.new_inactive_user_notification_recipients = '' + + ActionMailer::Base.deliveries = [] + + user = User.new + user.first_name = "first_name_for_newly_created_user" + user.is_active = false + user.save + + assert_equal '', Rails.configuration.new_inactive_user_notification_recipients + + ActionMailer::Base.deliveries.each do |d| + assert_not_equal "#{Rails.configuration.email_subject_prefix}New inactive user notification", setup_email.subject + end + + end + + test "create new inactive user with new_user_notification_recipients empty" do + Thread.current[:user] = @admin_user # set admin user as the current user + + Rails.configuration.new_user_notification_recipients = '' + + ActionMailer::Base.deliveries = [] + + user = User.new + user.first_name = "first_name_for_newly_created_user" + user.is_active = false + user.save + + assert_equal '', Rails.configuration.new_user_notification_recipients + + ActionMailer::Base.deliveries.each do |d| + assert_not_equal "#{Rails.configuration.email_subject_prefix}New user notification", d.subject + end + + end + + test "create new inactive user with new_user_notification_recipients and new_inactive_user_notification_recipients set" do + Thread.current[:user] = @admin_user # set admin user as the current user + + Rails.configuration.new_user_notification_recipients = 'foo_new@example.com' + Rails.configuration.new_inactive_user_notification_recipients = 'foo_new_inactive@example.com' + + ActionMailer::Base.deliveries = [] + + user = User.new + user.first_name = "first_name_for_newly_created_user" + user.is_active = false + user.save + + new_user_email = nil + new_inactive_user_email = nil + ActionMailer::Base.deliveries.each do |d| + if d.subject == "#{Rails.configuration.email_subject_prefix}New inactive user notification" then + new_inactive_user_email = d + end + if d.subject == "#{Rails.configuration.email_subject_prefix}New user notification" then + new_user_email = d + end + end + + assert_not_nil new_inactive_user_email, 'Expected new inactive user email after setup' + assert_not_nil new_user_email, 'Expected new user email after setup' + + assert_equal 'foo_new@example.com', Rails.configuration.new_user_notification_recipients + assert_equal 'foo_new_inactive@example.com', Rails.configuration.new_inactive_user_notification_recipients + + assert_equal Rails.configuration.user_notifier_email_from, new_inactive_user_email.from[0] + assert_equal 'foo_new_inactive@example.com', new_inactive_user_email.to[0] + assert_equal "#{Rails.configuration.email_subject_prefix}New inactive user notification", new_inactive_user_email.subject + + assert_equal Rails.configuration.user_notifier_email_from, new_user_email.from[0] + assert_equal 'foo_new@example.com', new_user_email.to[0] + assert_equal "#{Rails.configuration.email_subject_prefix}New user notification", new_user_email.subject + end + + test "create new inactive user with new_user_notification_recipients set" do + Thread.current[:user] = @admin_user # set admin user as the current user + + Rails.configuration.new_user_notification_recipients = 'foo@example.com' + + user = User.new + user.first_name = "first_name_for_newly_created_user" + user.is_active = false + user.save + + new_user_email = nil + + ActionMailer::Base.deliveries.each do |d| + if d.subject == "#{Rails.configuration.email_subject_prefix}New user notification" then + new_user_email = d + break + end + end + + assert_not_nil new_user_email, 'Expected email after setup' + + assert_equal 'foo@example.com', Rails.configuration.new_user_notification_recipients + + assert_equal Rails.configuration.user_notifier_email_from, new_user_email.from[0] + assert_equal 'foo@example.com', new_user_email.to[0] + assert_equal "#{Rails.configuration.email_subject_prefix}New user notification", new_user_email.subject + end + + test "create new active user with new_inactive_user_notification_recipients set" do + Thread.current[:user] = @admin_user # set admin user as the current user + + Rails.configuration.new_inactive_user_notification_recipients = 'foo@example.com' + + ActionMailer::Base.deliveries = [] + + user = User.new + user.first_name = "first_name_for_newly_created_user" + user.is_active = true + user.save + + assert_equal 'foo@example.com', Rails.configuration.new_inactive_user_notification_recipients + + ActionMailer::Base.deliveries.each do |d| + assert_not_equal "#{Rails.configuration.email_subject_prefix}New inactive user notification", setup_email.subject + end + + end + + test "update existing user" do Thread.current[:user] = @active_user # set active user as current user @active_user.first_name = "first_name_changed" @@ -175,7 +316,49 @@ class UserTest < ActiveSupport::TestCase verify_link oid_login_perm, 'permission', 'can_login', resp_user[:email], resp_user[:uuid] - assert_equal openid_prefix, oid_login_perm[:properties][:identity_url_prefix], + assert_equal openid_prefix, oid_login_perm[:properties]['identity_url_prefix'], + 'expected identity_url_prefix not found for oid_login_perm' + + group_perm = find_obj_in_resp response, 'Link', 'arvados#group' + verify_link group_perm, 'permission', 'can_read', resp_user[:uuid], nil + + repo_perm = find_obj_in_resp response, 'Link', 'arvados#repository' + verify_link repo_perm, 'permission', 'can_write', resp_user[:uuid], nil + + vm_perm = find_obj_in_resp response, 'Link', 'arvados#virtualMachine' + verify_link vm_perm, 'permission', 'can_login', resp_user[:uuid], vm.uuid + end + + test "setup new user with junk in database" do + Thread.current[:user] = @admin_user + + email = 'foo@example.com' + openid_prefix = 'http://openid/prefix' + + user = User.create ({uuid: 'zzzzz-tpzed-abcdefghijklmno', email: email}) + + vm = VirtualMachine.create + + # Set up the bogus Link + bad_uuid = 'zzzzz-tpzed-xyzxyzxyzxyzxyz' + + resp_link = Link.create ({tail_uuid: email, link_class: 'permission', + name: 'can_login', head_uuid: bad_uuid}) + resp_link.save(validate: false) + + verify_link resp_link, 'permission', 'can_login', email, bad_uuid + + response = User.setup user, openid_prefix, 'test_repo', vm.uuid + + resp_user = find_obj_in_resp response, 'User' + verify_user resp_user, email + + oid_login_perm = find_obj_in_resp response, 'Link', 'arvados#user' + + verify_link oid_login_perm, 'permission', 'can_login', resp_user[:email], + resp_user[:uuid] + + assert_equal openid_prefix, oid_login_perm[:properties]['identity_url_prefix'], 'expected identity_url_prefix not found for oid_login_perm' group_perm = find_obj_in_resp response, 'Link', 'arvados#group' @@ -188,6 +371,8 @@ class UserTest < ActiveSupport::TestCase verify_link vm_perm, 'permission', 'can_login', resp_user[:uuid], vm.uuid end + + test "setup new user in multiple steps" do Thread.current[:user] = @admin_user @@ -204,7 +389,7 @@ class UserTest < ActiveSupport::TestCase oid_login_perm = find_obj_in_resp response, 'Link', 'arvados#user' verify_link oid_login_perm, 'permission', 'can_login', resp_user[:email], resp_user[:uuid] - assert_equal openid_prefix, oid_login_perm[:properties][:identity_url_prefix], + assert_equal openid_prefix, oid_login_perm[:properties]['identity_url_prefix'], 'expected identity_url_prefix not found for oid_login_perm' group_perm = find_obj_in_resp response, 'Link', 'arvados#group'