Merge branch 'master' into 2681-new-inactive-user-notification
[arvados.git] / services / api / test / unit / user_test.rb
index 2d2db160cd3a0e971dd7ae56073b1de85216cfd0..e5352bf0faf30e06863b0d7c4aec5650f0420e69 100644 (file)
@@ -122,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"