From: radhika Date: Sat, 9 Aug 2014 00:55:49 +0000 (-0400) Subject: 3296: send email when profile is created by user. add tests to profile mailer. X-Git-Tag: 1.1.0~2335^2~19 X-Git-Url: https://git.arvados.org/arvados.git/commitdiff_plain/a14f6bf083841a868c054e7e00149c6a1bf0b708 3296: send email when profile is created by user. add tests to profile mailer. --- diff --git a/services/api/app/mailers/profile_notifier.rb b/services/api/app/mailers/profile_notifier.rb index 3863e8a621..13e3b34387 100644 --- a/services/api/app/mailers/profile_notifier.rb +++ b/services/api/app/mailers/profile_notifier.rb @@ -3,6 +3,6 @@ class ProfileNotifier < ActionMailer::Base def profile_created(user, address) @user = user - mail(to: address, subject: 'Profile created') + mail(to: address, subject: "Profile created by #{@user.email}") end end diff --git a/services/api/app/models/user.rb b/services/api/app/models/user.rb index bcf4d2df5f..7cd6ac40e5 100644 --- a/services/api/app/models/user.rb +++ b/services/api/app/models/user.rb @@ -13,6 +13,8 @@ class User < ArvadosModel before_create :check_auto_admin after_create :add_system_group_permission_link after_create :send_admin_notifications + after_update :send_profile_created_notification + has_many :authorized_keys, :foreign_key => :authorized_user_uuid, :primary_key => :uuid @@ -442,4 +444,15 @@ class User < ArvadosModel AdminNotifier.new_inactive_user(self).deliver end end + + # Send notification if the user saved profile for the first time + def send_profile_created_notification + if self.changes.andand.include?(:prefs) + if !self.changes[:prefs][0].andand.keys.andand.any? + profile_notification_address = Rails.configuration.user_profile_notification_address + ProfileNotifier.profile_created(self, profile_notification_address).deliver if profile_notification_address + end + end + end + end diff --git a/services/api/config/application.default.yml b/services/api/config/application.default.yml index c32900cfaf..ddcaa57302 100644 --- a/services/api/config/application.default.yml +++ b/services/api/config/application.default.yml @@ -43,6 +43,9 @@ test: secret_token: <%= rand(2**512).to_s(36) %> blob_signing_key: zfhgfenhffzltr9dixws36j1yhksjoll2grmku38mi7yxd66h5j4q9w4jzanezacp8s6q0ro3hxakfye02152hncy6zml2ed0uc + # email address to which mail should be sent when the user creates profile for the first time + user_profile_notification_address: arvados@example.com + common: uuid_prefix: <%= Digest::MD5.hexdigest(`hostname`).to_i(16).to_s(36)[0..4] %> @@ -173,3 +176,6 @@ common: # to sign session tokens. IMPORTANT: This is a site secret. It # should be at least 50 characters. secret_token: ~ + + # email address to which mail should be sent when the user creates profile for the first time + user_profile_notification_address: false diff --git a/services/api/test/functional/arvados/v1/users_controller_test.rb b/services/api/test/functional/arvados/v1/users_controller_test.rb index 28367831e1..299ae9e17f 100644 --- a/services/api/test/functional/arvados/v1/users_controller_test.rb +++ b/services/api/test/functional/arvados/v1/users_controller_test.rb @@ -842,6 +842,49 @@ class Arvados::V1::UsersControllerTest < ActionController::TestCase check_active_users_index end + test "update inactive user profile and expect notification email" do + authorize_with :admin + + put :update, { + id: users(:inactive).uuid, + user: { + prefs: {:profile => {'organization' => 'Curoverse'}} + } + } + assert_response :success + + found_email = false + ActionMailer::Base.deliveries.andand.each do |email| + if email.subject == "Profile created by #{users(:inactive).email}" + found_email = true + break + end + end + assert_equal true, found_email, 'Expected email after creating profile' + end + + test "update active user profile and expect no notification email" do + authorize_with :admin + + put :update, { + id: users(:active).uuid, + user: { + prefs: {:profile => {'organization' => 'Curoverse'}} + } + } + assert_response :success + + found_email = false + ActionMailer::Base.deliveries.andand.each do |email| + if email.subject == "Profile created by #{users(:active).email}" + found_email = true + break + end + end + assert_equal false, found_email, 'Expected no email after updating profile' + end + + NON_ADMIN_USER_DATA = ["uuid", "kind", "is_active", "email", "first_name", "last_name"].sort