3296: send email when profile is created by user. add tests to profile mailer.
authorradhika <radhika@curoverse.com>
Sat, 9 Aug 2014 00:55:49 +0000 (20:55 -0400)
committerradhika <radhika@curoverse.com>
Sat, 9 Aug 2014 00:55:49 +0000 (20:55 -0400)
services/api/app/mailers/profile_notifier.rb
services/api/app/models/user.rb
services/api/config/application.default.yml
services/api/test/functional/arvados/v1/users_controller_test.rb

index 3863e8a6215e6618103f529fd8d925eb681c7400..13e3b343878c9fecde9f9f4ed04cb548d265a281 100644 (file)
@@ -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
index bcf4d2df5f59306654459657148313a35ee4fa11..7cd6ac40e5afb7840121106ddd7cc99d6d13aa2f 100644 (file)
@@ -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
index c32900cfaf01e73f9ab7cd542547020233528360..ddcaa57302b5ceec5437ab1b20cb09271b2f24ea 100644 (file)
@@ -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
index 28367831e18a48c9b954355626624905fb67eda8..299ae9e17f9d229f8117053a58f84c1966aca2f3 100644 (file)
@@ -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