Merge branch '2375-log-table' into master
[arvados.git] / services / api / test / unit / user_test.rb
index 0fbf981ed459a189e1abd5b37d05f6ba87dd9fd1..16c05a718196f5a3fc3ad9518266be6e44dbb964 100644 (file)
@@ -1,17 +1,23 @@
 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.is_admin && user.is_active 
+      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 
+      elsif !user.is_active && !user.is_invited
         @uninvited_user = user
       end
     end
@@ -81,7 +87,7 @@ class UserTest < ActiveSupport::TestCase
     assert_equal found_user.identity_url, user.identity_url
   end
 
-  test "create new user" do 
+  test "create new user" do
     Thread.current[:user] = @admin_user   # set admin user as the current user
 
     user = User.new
@@ -89,7 +95,7 @@ class UserTest < ActiveSupport::TestCase
     user.save
 
     # verify there is one extra user in the db now
-    assert (User.find(:all).size == @all_users.size+1)
+    assert_equal @all_users.size+1, User.find(:all).size
 
     user = User.find(user.id)   # get the user back
     assert_equal(user.first_name, 'first_name_for_newly_created_user')
@@ -103,7 +109,7 @@ class UserTest < ActiveSupport::TestCase
     assert_equal(user.first_name, 'first_name_for_newly_created_user_updated')
   end
 
-  test "update existing user" do 
+  test "update existing user" do
     Thread.current[:user] = @active_user    # set active user as current user
     @active_user.first_name = "first_name_changed"
     @active_user.save
@@ -119,22 +125,22 @@ class UserTest < ActiveSupport::TestCase
     @active_user = User.find(@active_user.id)   # get the user back
     assert_equal(@active_user.first_name, 'first_name_changed_by_admin_for_active_user')
   end
-  
-  test "delete a user and verify" do 
+
+  test "delete a user and verify" do
     active_user_uuid = @active_user.uuid
 
-    Thread.current[:user] = @admin_user     
+    Thread.current[:user] = @admin_user
     @active_user.delete
 
     found_deleted_user = false
-    User.find(:all).each do |user| 
-      if user.uuid == active_user_uuid 
+    User.find(:all).each do |user|
+      if user.uuid == active_user_uuid
         found_deleted_user = true
         break
-      end     
+      end
     end
     assert !found_deleted_user, "found deleted user: "+active_user_uuid
-  
+
   end
 
   test "create new user as non-admin user" do
@@ -149,58 +155,15 @@ class UserTest < ActiveSupport::TestCase
         'Expected PermissionDeniedError'
   end
 
-  test "setup new user as non-admin user" do
-    Thread.current[:user] = @active_user
-
-    begin
-      user = User.new
-      user.email = 'abc@xyz.com'
-      
-      User.setup user, 'http://openid/prefix'
-    rescue ArvadosModel::PermissionDeniedError => e
-    end
-
-    assert (e.message.include? 'PermissionDeniedError'),
-        'Expected PermissionDeniedError'
-  end
-
-  test "setup new user with no email" do
+  test "setup new user" do
     Thread.current[:user] = @admin_user
 
-    begin
-      user = User.new
-      
-      User.setup user, 'http://openid/prefix'
-    rescue ArvadosModel::RuntimeError => e
-    end
-
-    assert (e.message.include? 'No email found'),
-        'Expected RuntimeError'
-  end
-
-  test "setup new user with email but no openid_prefix" do
-    Thread.current[:user] = @admin_user
-
-    begin
-      user = User.new
-      user.email = 'abc@xyz.com'
-      
-      User.setup user
-
-    rescue ArvadosModel::ArgumentError => e
-    end
-    assert (e.message.include? 'wrong number of arguments'),
-        'Expected ArgumentError'
-  end
-
-  test "setup new user with all input data" do
-    Thread.current[:user] = @admin_user
-
-    email = 'abc@xyz.com'
+    email = 'foo@example.com'
     openid_prefix = 'http://openid/prefix'
 
     user = User.new
     user.email = email
+    user.uuid = 'abcdefghijklmnop'
 
     vm = VirtualMachine.create
 
@@ -228,11 +191,12 @@ class UserTest < ActiveSupport::TestCase
   test "setup new user in multiple steps" do
     Thread.current[:user] = @admin_user
 
-    email = 'abc@xyz.com'
+    email = 'foo@example.com'
     openid_prefix = 'http://openid/prefix'
 
     user = User.new
     user.email = email
+    user.uuid = 'abcdefghijklmnop'
 
     response = User.setup user, openid_prefix
 
@@ -249,9 +213,6 @@ class UserTest < ActiveSupport::TestCase
     verify_link group_perm, 'permission', 'can_read', resp_user[:uuid], nil
 
     # invoke setup again with repo_name
-    user = User.new
-    user.uuid = resp_user[:uuid]
-
     response = User.setup user, openid_prefix, 'test_repo'
     resp_user = find_obj_in_resp response, 'User', nil
     verify_user resp_user, email
@@ -311,15 +272,15 @@ class UserTest < ActiveSupport::TestCase
     assert_not_nil link_object, 'expected link for #{link_class} #{link_name}'
     assert_not_nil link_object[:uuid],
         'expected non-nil uuid for link for #{link_class} #{link_name}'
-    assert_equal link_class, link_object[:link_class], 
+    assert_equal link_class, link_object[:link_class],
         'expected link_class not found for #{link_class} #{link_name}'
-    assert_equal link_name, link_object[:name], 
+    assert_equal link_name, link_object[:name],
         'expected link_name not found for #{link_class} #{link_name}'
-    assert_equal tail_uuid, link_object[:tail_uuid], 
-        'expected tail_uuid not found for group_perm'
+    assert_equal tail_uuid, link_object[:tail_uuid],
+        'expected tail_uuid not found for #{link_class} #{link_name}'
     if head_uuid
-      assert_equal tail_uuid, link_object[:tail_uuid], 
-          'expected tail_uuid not found for group_perm'
+      assert_equal head_uuid, link_object[:head_uuid],
+          'expected head_uuid not found for #{link_class} #{link_name}'
     end
   end