Merge branch 'master' into 2681-new-inactive-user-notification
authorWard Vandewege <ward@curoverse.com>
Fri, 30 May 2014 19:50:59 +0000 (15:50 -0400)
committerWard Vandewege <ward@curoverse.com>
Fri, 30 May 2014 19:50:59 +0000 (15:50 -0400)
services/api/app/mailers/admin_notifier.rb
services/api/app/models/user.rb
services/api/app/views/admin_notifier/after_create_user.text.erb [deleted file]
services/api/app/views/admin_notifier/new_inactive_user.text.erb [new file with mode: 0644]
services/api/app/views/admin_notifier/new_user.text.erb [new file with mode: 0644]
services/api/config/application.default.yml
services/api/test/unit/user_test.rb

index 5de70053dcf0265258244ad252df341936334154..e17f4a14dab2d05f16780b09a76844401ad3cbff 100644 (file)
@@ -1,34 +1,40 @@
 class AdminNotifier < ActionMailer::Base
+  include AbstractController::Callbacks
+
   default from: Rails.configuration.admin_notifier_email_from
+  before_filter :load_variables
 
-  def after_create(model, *args)
-    self.generic_callback('after_create', model, *args)
+  def new_user(user)
+    @user = user
+    if not Rails.configuration.new_user_notification_recipients.empty? then
+      @recipients = Rails.configuration.new_user_notification_recipients
+      logger.info "Sending mail to #{@recipients} about new user #{@user.uuid} (#{@user.full_name} <#{@user.email}>)"
+      mail(to: @recipients,
+           subject: "#{Rails.configuration.email_subject_prefix}New user notification"
+          )
+    end
   end
 
-  protected
-
-  def generic_callback(callback_type, model, *args)
-    model_specific_method = "#{callback_type}_#{model.class.to_s.underscore}".to_sym
-    if self.respond_to? model_specific_method
-      self.send model_specific_method, model, *args
+  def new_inactive_user(user)
+    @user = user
+    if not Rails.configuration.new_inactive_user_notification_recipients.empty? then
+      @recipients = Rails.configuration.new_inactive_user_notification_recipients
+      logger.info "Sending mail to #{@recipients} about new user #{@user.uuid} (#{@user.full_name} <#{@user.email}>)"
+      mail(to: @recipients,
+           subject: "#{Rails.configuration.email_subject_prefix}New inactive user notification"
+          )
     end
   end
 
-  def all_admin_emails()
-    User.
-      where(is_admin: true).
-      collect(&:email).
-      compact.
-      uniq.
-      select { |e| e.match /\@/ }
+private
+  def load_variables
+    if Rails.configuration.respond_to?('workbench_address') and
+       not Rails.configuration.workbench_address.nil? and
+       not Rails.configuration.workbench_address.empty? then
+      @wb_address = Rails.configuration.workbench_address.sub(/\/$/,'') + '/users'
+    else
+      @wb_address = ''
+    end
   end
 
-  def after_create_user(user, *args)
-    @new_user = user
-    logger.info "Sending mail to #{@recipients} about new user #{@new_user.uuid} (#{@new_user.full_name}, #{@new_user.email})"
-    mail({
-           to: self.all_admin_emails,
-           subject: "#{Rails.configuration.email_subject_prefix}New user: #{@new_user.full_name}, #{@new_user.email}"
-         })
-  end
 end
index d21991519553b9d889f742d1f137b11fc93e17bb..52dd8d79ff47014a9bfe0896a818b28c4d83395d 100644 (file)
@@ -12,7 +12,7 @@ class User < ArvadosModel
   before_update :prevent_inactive_admin
   before_create :check_auto_admin
   after_create :add_system_group_permission_link
-  after_create AdminNotifier
+  after_create :send_admin_notifications
 
   has_many :authorized_keys, :foreign_key => :authorized_user_uuid, :primary_key => :uuid
 
@@ -402,4 +402,12 @@ class User < ArvadosModel
                   head_uuid: self.uuid)
     end
   end
+
+  # Send admin notifications
+  def send_admin_notifications
+    AdminNotifier.new_user(self).deliver
+    if not self.is_active then
+      AdminNotifier.new_inactive_user(self).deliver
+    end
+  end
 end
diff --git a/services/api/app/views/admin_notifier/after_create_user.text.erb b/services/api/app/views/admin_notifier/after_create_user.text.erb
deleted file mode 100644 (file)
index 40bafd2..0000000
+++ /dev/null
@@ -1,7 +0,0 @@
-A user has logged in for the first time.
-
-<%= @new_user.uuid %> -- <%= @new_user.full_name %>, <%= @new_user.email %>
-
-View or activate the user:
-
-<%= users_url %>
diff --git a/services/api/app/views/admin_notifier/new_inactive_user.text.erb b/services/api/app/views/admin_notifier/new_inactive_user.text.erb
new file mode 100644 (file)
index 0000000..f5c9a07
--- /dev/null
@@ -0,0 +1,13 @@
+
+A new user landed on the inactive user page:
+
+  <%= @user.full_name %> <<%= @user.email %>>
+
+<% if not @wb_address.empty? -%>
+Please see workbench for more information:
+
+  <%= not @wb_address %>
+
+<% end -%>
+Thanks,
+Your friendly Arvados robot.
diff --git a/services/api/app/views/admin_notifier/new_user.text.erb b/services/api/app/views/admin_notifier/new_user.text.erb
new file mode 100644 (file)
index 0000000..374e46e
--- /dev/null
@@ -0,0 +1,16 @@
+
+A new user has been created:
+
+  <%= @user.full_name %> <<%= @user.email %>>
+
+This user is <%= @user.is_active ? '' : 'NOT ' %>active.
+
+<% if not @wb_address.empty? -%>
+Please see workbench for more information:
+
+  <%= @wb_address %>
+
+<% end -%>
+Thanks,
+Your friendly Arvados robot.
+
index a3ff6800be23bf336f8741a147c642000d1a69b9..848675cb55b5afa702502201c94624be8f3f32be 100644 (file)
@@ -87,6 +87,8 @@ common:
   admin_notifier_email_from: arvados@example.com
   email_subject_prefix: "[ARVADOS] "
   user_notifier_email_from: arvados@example.com
+  new_user_notification_recipients: [ ]
+  new_inactive_user_notification_recipients: [ ]
 
   # Visitors to the API server will be redirected to the workbench
   workbench_address: https://workbench.local:3001/
index 2d2db160cd3a0e971dd7ae56073b1de85216cfd0..0d556337249ad84a00289dd85e9150d8bea8fb17 100644 (file)
@@ -3,27 +3,8 @@ require 'test_helper'
 class UserTest < ActiveSupport::TestCase
   include CurrentApiClient
 
-  # 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
-
-    @all_users = User.find(:all)
-
-    @all_users.each do |user|
-      if user.uuid == system_user_uuid
-        @system_user = user
-      elsif user.is_admin && user.is_active
-        @admin_user = user
-      elsif user.is_active && !user.is_admin
-        @active_user = user
-      elsif !user.is_active && !user.is_invited
-        @uninvited_user = user
-      end
-    end
-  end
-
   test "check non-admin active user properties" do
+    @active_user = users(:active)     # get the active user
     assert !@active_user.is_admin, 'is_admin should not be set for a non-admin user'
     assert @active_user.is_active, 'user should be active'
     assert @active_user.is_invited, 'is_invited should be set'
@@ -35,12 +16,14 @@ class UserTest < ActiveSupport::TestCase
     assert @active_user.groups_i_can(:read).size > 0, "active user should be able read at least one group"
 
     # non-admin user cannot manage or write other user objects
+    @uninvited_user = users(:inactive_uninvited)     # get the uninvited user
     assert !(@active_user.can? :read=>"#{@uninvited_user.uuid}")
     assert !(@active_user.can? :write=>"#{@uninvited_user.uuid}")
     assert !(@active_user.can? :manage=>"#{@uninvited_user.uuid}")
   end
 
   test "check admin user properties" do
+    @admin_user = users(:admin)     # get the admin user
     assert @admin_user.is_admin, 'is_admin should be set for admin user'
     assert @admin_user.is_active, 'admin user cannot be inactive'
     assert @admin_user.is_invited, 'is_invited should be set'
@@ -56,12 +39,14 @@ class UserTest < ActiveSupport::TestCase
     assert @admin_user.groups_i_can(:manage).size > 0, "admin active user should be able manage at least one group"
 
     # admin user can also write or manage other users
+    @uninvited_user = users(:inactive_uninvited)     # get the uninvited user
     assert @admin_user.can? :read=>"#{@uninvited_user.uuid}"
     assert @admin_user.can? :write=>"#{@uninvited_user.uuid}"
     assert @admin_user.can? :manage=>"#{@uninvited_user.uuid}"
   end
 
   test "check inactive and uninvited user properties" do
+    @uninvited_user = users(:inactive_uninvited)     # get the uninvited user
     assert !@uninvited_user.is_admin, 'is_admin should not be set for a non-admin user'
     assert !@uninvited_user.is_active, 'user should be inactive'
     assert !@uninvited_user.is_invited, 'is_invited should not be set'
@@ -88,7 +73,7 @@ class UserTest < ActiveSupport::TestCase
   end
 
   test "full name should not contain spurious whitespace" do
-    Thread.current[:user] = @admin_user   # set admin user as the current user
+    set_user_from_auth :admin
 
     user = User.create ({uuid: 'zzzzz-tpzed-abcdefghijklmno', email: 'foo@example.com' })
 
@@ -101,14 +86,18 @@ class UserTest < ActiveSupport::TestCase
   end
 
   test "create new user" do
-    Thread.current[:user] = @admin_user   # set admin user as the current user
+    set_user_from_auth :admin
+
+    @all_users = User.find(:all)
 
     user = User.new
     user.first_name = "first_name_for_newly_created_user"
     user.save
 
     # verify there is one extra user in the db now
-    assert_equal @all_users.size+1, User.find(:all).size
+    # the API server also auto-creates the root system user after the first user
+    # is created, hence the test for the delta of 2.
+    assert_equal @all_users.size+2, User.find(:all).size
 
     user = User.find(user.id)   # get the user back
     assert_equal(user.first_name, 'first_name_for_newly_created_user')
@@ -122,8 +111,22 @@ class UserTest < ActiveSupport::TestCase
     assert_equal(user.first_name, 'first_name_for_newly_created_user_updated')
   end
 
+  test "create new user with notifications" do
+    set_user_from_auth :admin
+
+    user_notification_helper true, 'active-notify-address@example.com', 'inactive-notify-address@example.com'
+    user_notification_helper true, 'active-notify-address@example.com', []
+    user_notification_helper true, [], []
+    user_notification_helper false, 'active-notify-address@example.com', 'inactive-notify-address@example.com'
+    user_notification_helper false, [], 'inactive-notify-address@example.com'
+    user_notification_helper false, [], []
+  end
+
   test "update existing user" do
-    Thread.current[:user] = @active_user    # set active user as current user
+    set_user_from_auth :active    # set active user as current user
+
+    @active_user = users(:active)     # get the active user
+
     @active_user.first_name = "first_name_changed"
     @active_user.save
 
@@ -131,7 +134,7 @@ class UserTest < ActiveSupport::TestCase
     assert_equal(@active_user.first_name, 'first_name_changed')
 
     # admin user also should be able to update the "active" user info
-    Thread.current[:user] = @admin_user # set admin user as current user
+    set_user_from_auth :admin # set admin user as current user
     @active_user.first_name = "first_name_changed_by_admin_for_active_user"
     @active_user.save
 
@@ -140,9 +143,10 @@ class UserTest < ActiveSupport::TestCase
   end
 
   test "delete a user and verify" do
+    @active_user = users(:active)     # get the active user
     active_user_uuid = @active_user.uuid
 
-    Thread.current[:user] = @admin_user
+    set_user_from_auth :admin
     @active_user.delete
 
     found_deleted_user = false
@@ -157,7 +161,7 @@ class UserTest < ActiveSupport::TestCase
   end
 
   test "create new user as non-admin user" do
-    Thread.current[:user] = @active_user
+    set_user_from_auth :active
 
     begin
       user = User.new
@@ -169,7 +173,7 @@ class UserTest < ActiveSupport::TestCase
   end
 
   test "setup new user" do
-    Thread.current[:user] = @admin_user
+    set_user_from_auth :admin
 
     email = 'foo@example.com'
     openid_prefix = 'http://openid/prefix'
@@ -202,7 +206,7 @@ class UserTest < ActiveSupport::TestCase
   end
 
   test "setup new user with junk in database" do
-    Thread.current[:user] = @admin_user
+    set_user_from_auth :admin
 
     email = 'foo@example.com'
     openid_prefix = 'http://openid/prefix'
@@ -246,7 +250,7 @@ class UserTest < ActiveSupport::TestCase
 
 
   test "setup new user in multiple steps" do
-    Thread.current[:user] = @admin_user
+    set_user_from_auth :admin
 
     email = 'foo@example.com'
     openid_prefix = 'http://openid/prefix'
@@ -343,4 +347,55 @@ class UserTest < ActiveSupport::TestCase
     end
   end
 
+  def user_notification_helper (active, active_recipients, inactive_recipients)
+    Rails.configuration.new_user_notification_recipients = active_recipients
+    Rails.configuration.new_inactive_user_notification_recipients = inactive_recipients
+
+    assert_equal active_recipients, Rails.configuration.new_user_notification_recipients
+    assert_equal inactive_recipients, 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 = active
+    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 user notification" then
+        new_user_email = d
+      elsif d.subject == "#{Rails.configuration.email_subject_prefix}New inactive user notification" then
+        new_inactive_user_email = d
+      end
+    end
+
+    if not active
+      if not inactive_recipients.empty? then
+        assert_not_nil new_inactive_user_email, 'Expected new inactive user email after setup'
+        assert_equal Rails.configuration.user_notifier_email_from, new_inactive_user_email.from[0]
+        assert_equal inactive_recipients, new_inactive_user_email.to[0]
+        assert_equal "#{Rails.configuration.email_subject_prefix}New inactive user notification", new_inactive_user_email.subject
+      else
+        assert_nil new_inactive_user_email, 'Did not expect new inactive user email after setup'
+      end
+    end
+
+    if active
+      assert_nil new_inactive_user_email, 'Expected email after setup'
+      if not active_recipients.empty? then
+        assert_not_nil new_user_email, 'Expected new user email after setup'
+        assert_equal Rails.configuration.user_notifier_email_from, new_user_email.from[0]
+        assert_equal active_recipients, new_user_email.to[0]
+        assert_equal "#{Rails.configuration.email_subject_prefix}New user notification", new_user_email.subject
+      else
+        assert_nil new_user_email, 'Did not expect new user email after setup'
+      end
+    end
+    ActionMailer::Base.deliveries = []
+
+  end
+
 end