From e8b99010926e0d7bd1ebcbb17189f2d00df2ba67 Mon Sep 17 00:00:00 2001 From: Peter Amstutz Date: Wed, 20 Dec 2023 10:09:17 -0500 Subject: [PATCH] 21059: Testing email notification WIP Arvados-DCO-1.1-Signed-off-by: Peter Amstutz --- services/api/app/models/user.rb | 23 +++++++++++-------- .../api/test/integration/remote_user_test.rb | 15 ++++++++++++ 2 files changed, 29 insertions(+), 9 deletions(-) diff --git a/services/api/app/models/user.rb b/services/api/app/models/user.rb index dbacf9c017..a1b743cf51 100644 --- a/services/api/app/models/user.rb +++ b/services/api/app/models/user.rb @@ -264,8 +264,7 @@ SELECT target_uuid, perm_level def setup(repo_name: nil, vm_uuid: nil, send_notification_email: nil) newly_invited = Link.where(tail_uuid: self.uuid, head_uuid: all_users_group_uuid, - link_class: 'permission', - name: 'can_read').empty? + link_class: 'permission').empty? # Add can_read link from this user to "all users" which makes this # user "invited", and (depending on config) a link in the opposite @@ -606,13 +605,17 @@ SELECT target_uuid, perm_level def self.update_remote_user remote_user remote_user = remote_user.symbolize_keys + remote_user_prefix = remote_user[:uuid][0..4] + begin - user = User.find_or_create_by(uuid: remote_user[:uuid]) + user = User.create_with(email: remote_user[:email], + first_name: remote_user[:first_name], + last_name: remote_user[:last_name], + ).find_or_create_by(uuid: remote_user[:uuid]) rescue ActiveRecord::RecordNotUnique retry end - remote_user_prefix = user.uuid[0..4] user.with_lock do needupdate = {} [:email, :username, :first_name, :last_name, :prefs].each do |k| @@ -658,9 +661,10 @@ SELECT target_uuid, perm_level end end - if user.is_invited && remote_user[:is_invited] == false - # Remote user is not "invited" state, they should be unsetup, which - # also makes them inactive. + if user.is_invited && (remote_user[:is_invited] == false || remote_user[:is_active] == false) + # Remote user is not "invited" or "active" state on their home + # cluster, so they should be unsetup, which also makes them + # inactive. user.unsetup else if !user.is_invited && remote_user[:is_invited] and @@ -914,8 +918,9 @@ SELECT target_uuid, perm_level # Send admin notifications def send_admin_notifications - AdminNotifier.new_user(self).deliver_now - if not self.is_active then + if self.is_invited then + AdminNotifier.new_user(self).deliver_now + else AdminNotifier.new_inactive_user(self).deliver_now end end diff --git a/services/api/test/integration/remote_user_test.rb b/services/api/test/integration/remote_user_test.rb index b0d1320fdf..198f574ef2 100644 --- a/services/api/test/integration/remote_user_test.rb +++ b/services/api/test/integration/remote_user_test.rb @@ -106,6 +106,7 @@ class RemoteUsersTest < ActionDispatch::IntegrationTest } @stub_token_status = 200 @stub_token_scopes = ["all"] + ActionMailer::Base.deliveries = [] end teardown do @@ -366,6 +367,12 @@ class RemoteUsersTest < ActionDispatch::IntegrationTest test 'get user from Login cluster' do Rails.configuration.Login.LoginCluster = 'zbbbb' + email_dest = ActiveSupport::OrderedOptions.new + email_dest[:'arvados-admin@example.com'] = ActiveSupport::OrderedOptions.new + Rails.configuration.Users.UserNotifierEmailBcc = email_dest + Rails.configuration.Users.NewUserNotificationRecipients = email_dest + Rails.configuration.Users.NewInactiveUserNotificationRecipients = email_dest + get '/arvados/v1/users/current', params: {format: 'json'}, headers: auth(remote: 'zbbbb') @@ -375,6 +382,14 @@ class RemoteUsersTest < ActionDispatch::IntegrationTest assert_equal true, json_response['is_active'] assert_equal 'foo@example.com', json_response['email'] assert_equal 'barney', json_response['username'] + + ActionMailer::Base.deliveries.each do |d| + puts "--- delivery #{d.inspect}" + end + + assert_equal 2, ActionMailer::Base.deliveries.length + assert_equal "Welcome to Arvados - account enabled", ActionMailer::Base.deliveries[0].subject + assert_equal "[ARVADOS] New user created notification", ActionMailer::Base.deliveries[1].subject end [true, false].each do |trusted| -- 2.30.2