[ARVADOS] updated: c09ec8260b02c4767c82bcb5b346899193329828

git at public.curoverse.com git at public.curoverse.com
Thu Mar 20 20:50:19 EDT 2014


Summary of changes:
 .../app/controllers/arvados/v1/users_controller.rb |   88 +++++--------
 services/api/config/routes.rb                      |    1 +
 services/api/script/setup-new-user.rb              |   36 ++++--
 .../functional/arvados/v1/users_controller_test.rb |  131 ++++++++++---------
 4 files changed, 131 insertions(+), 125 deletions(-)

       via  c09ec8260b02c4767c82bcb5b346899193329828 (commit)
       via  03aadd3864de6d5687e1e6c71815fafc4ec030af (commit)
       via  6849ec08498a596d0604c2876dcd605ce6c6aaeb (commit)
       via  4ad105bd22c9141e5f88139ad3bf334a52f7a5d1 (commit)
      from  1f5561a43693243ad3b6737dcd1020cbe0cc809f (commit)

Those revisions listed above that are new to this repository have
not appeared on any other notification email; so we list those
revisions in full, below.


commit c09ec8260b02c4767c82bcb5b346899193329828
Author: radhika chippada <radhika at radhika.curoverse>
Date:   Thu Mar 20 20:49:14 2014 -0400

    setup-new-user script using the setup method.

diff --git a/services/api/app/controllers/arvados/v1/users_controller.rb b/services/api/app/controllers/arvados/v1/users_controller.rb
index 92b84f4..adad309 100644
--- a/services/api/app/controllers/arvados/v1/users_controller.rb
+++ b/services/api/app/controllers/arvados/v1/users_controller.rb
@@ -153,14 +153,6 @@ class Arvados::V1::UsersController < ApplicationController
       if !@object[:email]
         return
       end
-=begin  
-        if found_objects.size > 1
-          logger.warn ("Found #{found_objects.size} users with email #{user_email}. Stop.")
-          raise ArgumentError.new "Found #{found_objects.size} users with email #{user_email}. Stop."
-        elsif found_objects.size == 1
-          found_object = found_objects.first
-        end
-=end
 
       found_objects = User.where('email=?', @object[:email])  
       found_object = found_objects.first
diff --git a/services/api/script/setup-new-user.rb b/services/api/script/setup-new-user.rb
index fbdb0b0..bc0fd4b 100755
--- a/services/api/script/setup-new-user.rb
+++ b/services/api/script/setup-new-user.rb
@@ -16,10 +16,6 @@ opts = Trollop::options do
   opt :debug, <<-eos
 Show debug messages.
   eos
-  opt :create, <<-eos
-Create a new user with the given email address if an existing user \
-is not found.
-  eos
   opt :openid_prefix, <<-eos, default: 'https://www.google.com/accounts/o8/id'
 If creating a new user record, require authentication from an OpenID \
 with this OpenID prefix *and* a matching email address in order to \
@@ -58,9 +54,9 @@ rescue Arvados::TransactionFailedError
              "with uuid or email #{user_arg.inspect}. Stop."
   else
     user = found.first
-    # Found user. Update ther user links
-    user = arv.user.setup(repo_name: user_repo_name, vm_uuid: vm_uuid, 
-        user: {email: user[:uuid]})
+    # Found user. Update the user links
+    user = arv.user.setup(user: {email: user[:uuid]}, repo_name: user_repo_name,
+        vm_uuid: vm_uuid, openid_prefix: opts.openid_prefix)
   end
 
   puts "USER = #{user.inspect}"
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 d4e25dd..319f515 100644
--- a/services/api/test/functional/arvados/v1/users_controller_test.rb
+++ b/services/api/test/functional/arvados/v1/users_controller_test.rb
@@ -101,9 +101,9 @@ class Arvados::V1::UsersControllerTest < ActionController::TestCase
     authorize_with :admin
 
     post :setup, {
+      user: {uuid: 'not_an_existing_uuid_and_not_email_format'},
       repo_name: 'test_repo',
-      vm_uuid: 'no_such_vm',
-      user: {uuid: 'not_an_existing_uuid_and_not_email_format'}
+      vm_uuid: 'no_such_vm'
     }
     response_body = JSON.parse(@response.body)
     response_errors = response_body['errors']
@@ -121,9 +121,9 @@ class Arvados::V1::UsersControllerTest < ActionController::TestCase
     authorize_with :admin
 
     post :setup, {
+      user: {uuid: inactive_user['uuid']},
       repo_name: 'test_repo',
-      vm_uuid: 'no_such_vm',
-      user: {uuid: inactive_user['uuid']}
+      vm_uuid: 'no_such_vm'
     }
 
     assert_response :success
@@ -184,9 +184,9 @@ class Arvados::V1::UsersControllerTest < ActionController::TestCase
     authorize_with :admin
 
     post :setup, {
-      repo_name: 'test_repo',
+      user: {email: 'abc at xyz.com'},
       vm_uuid: 'no_such_vm',
-      user: {email: 'abc at xyz.com'}
+      repo_name: 'test_repo'
     }
 
     assert_response :success
@@ -197,10 +197,9 @@ class Arvados::V1::UsersControllerTest < ActionController::TestCase
 
     # create again
     post :setup, {
-      user_param: 'abc at xyz.com',
       repo_name: 'test_repo',
       vm_uuid: 'no_such_vm',
-      user: {}
+      user: {email: 'abc at xyz.com'}
     }
 
     assert_response :success
@@ -279,13 +278,13 @@ class Arvados::V1::UsersControllerTest < ActionController::TestCase
     authorize_with :admin
 
     post :setup, {
-      repo_name: 'test_repo',
-      vm_uuid: @vm_uuid,
       user: {
         first_name: "in_create_test_first_name",
         last_name: "test_last_name",
         email: "test at abc.com"
-      }
+      },
+      repo_name: 'test_repo',
+      vm_uuid: @vm_uuid
     }
     assert_response :success
     created = JSON.parse(@response.body)

commit 03aadd3864de6d5687e1e6c71815fafc4ec030af
Author: radhika chippada <radhika at radhika.curoverse>
Date:   Thu Mar 20 20:06:07 2014 -0400

    Use setup method instead of create method to setup a new user

diff --git a/services/api/script/setup-new-user.rb b/services/api/script/setup-new-user.rb
index ced32bc..fbdb0b0 100755
--- a/services/api/script/setup-new-user.rb
+++ b/services/api/script/setup-new-user.rb
@@ -50,17 +50,19 @@ rescue Arvados::TransactionFailedError
                "does not look like an email address. Stop."
     end
            
-    user = arv.user.setup(repo_name: user_repo_name, vm_uuid: vm_uuid, user: {email: user_arg})
+    user = arv.user.setup(repo_name: user_repo_name, vm_uuid: vm_uuid, 
+        user: {email: user_arg})
     log.info { "created user: " + user[:uuid] }
   elsif found.count != 1
     abort "Found #{found.count} users " +
              "with uuid or email #{user_arg.inspect}. Stop."
-  else 
+  else
     user = found.first
     # Found user. Update ther user links
-    user = arv.user.setup(repo_name: user_repo_name, vm_uuid: vm_uuid, user: {email: user[:uuid]})
+    user = arv.user.setup(repo_name: user_repo_name, vm_uuid: vm_uuid, 
+        user: {email: user[:uuid]})
   end
-    
+
   puts "USER = #{user.inspect}"
   log.info { "user uuid: " + user[:uuid] }
 end

commit 6849ec08498a596d0604c2876dcd605ce6c6aaeb
Author: radhika chippada <radhika at radhika.curoverse>
Date:   Thu Mar 20 17:03:31 2014 -0400

    Update the functional tests to use the setup method

diff --git a/services/api/app/controllers/arvados/v1/users_controller.rb b/services/api/app/controllers/arvados/v1/users_controller.rb
index 1233e74..92b84f4 100644
--- a/services/api/app/controllers/arvados/v1/users_controller.rb
+++ b/services/api/app/controllers/arvados/v1/users_controller.rb
@@ -90,8 +90,8 @@ class Arvados::V1::UsersController < ApplicationController
 
   # create user object and all the needed links
   def setup
-puts "\n*************PARAMS = #{params.inspect}"
-    if params[:openid_prefix]   # check if default openid_prefix needs to be overridden
+    # check if default openid_prefix needs to be overridden
+    if params[:openid_prefix]
       openid_prefix = params[:openid_prefix]
     else 
       openid_prefix = Rails.configuration.openid_prefix
@@ -100,93 +100,14 @@ puts "\n*************PARAMS = #{params.inspect}"
 
     @object = model_class.new resource_attrs
 
-    # If user_param is passed, lookup for user. If exists, skip create and only create any missing links. 
-    if params[:user_param]
-      begin
-        @object_found = find_user_from_input params[:user_param], params[:user_param]
-      end
-      if !@object_found
-        @object = User.new    # when user_param is used, it will be used as user object
-        @object[:email] = params[:user_param]       
-        need_to_create = true
-      else
-        @object = @object_found
-      end
-    else    # need to create user for the given user data
-      @object_found = find_user_from_input @object[:uuid], @object[:email]
-      if !@object_found
-        need_to_create = true   # use the user object sent in to create with the user
-      else
-        @object = @object_found
-      end
-    end
+    # Lookup for user. If exists, only create any missing links
+    @object_found = find_user_from_input 
 
-    # create if need be, and then create or update the links as needed 
-    if need_to_create
-      if @object.save
-        oid_login_perm = Link.where(tail_uuid: @object[:email],
-                                    head_kind: 'arvados#user',
-                                    link_class: 'permission',
-                                    name: 'can_login')
-
-        if [] == oid_login_perm
-          # create openid login permission
-          oid_login_perm = Link.create(link_class: 'permission',
-                                       name: 'can_login',
-                                       tail_kind: 'email',
-                                       tail_uuid: @object[:email],
-                                       head_kind: 'arvados#user',
-                                       head_uuid: @object[:uuid],
-                                       properties: login_perm_props
-                                      )
-          logger.info { "openid login permission: " + oid_login_perm[:uuid] }
-        end
-      else
-        raise "Save failed"
+    if !@object_found
+      if !@object[:email]
+        raise "No email found in the input. Aborting user creation."
       end
-    end
 
-    # create links
-    create_user_repo_link params[:repo_name]
-    create_vm_login_permission_link params[:vm_uuid], params[:repo_name]
-    create_user_group_link 
-
-    show  end
-
-  # create user object and all the needed links
-  def create
-    if params[:openid_prefix]   # check if default openid_prefix needs to be overridden
-      openid_prefix = params[:openid_prefix]
-    else 
-      openid_prefix = Rails.configuration.openid_prefix
-    end
-    login_perm_props = {identity_url_prefix: openid_prefix}
-
-    @object = model_class.new resource_attrs
-
-    # If user_param is passed, lookup for user. If exists, skip create and only create any missing links. 
-    if params[:user_param]
-      begin
-        @object_found = find_user_from_input params[:user_param], params[:user_param]
-      end
-      if !@object_found
-        @object = User.new    # when user_param is used, it will be used as user object
-        @object[:email] = params[:user_param]       
-        need_to_create = true
-      else
-        @object = @object_found
-      end
-    else    # need to create user for the given user data
-      @object_found = find_user_from_input @object[:uuid], @object[:email]
-      if !@object_found
-        need_to_create = true   # use the user object sent in to create with the user
-      else
-        @object = @object_found
-      end
-    end
-
-    # create if need be, and then create or update the links as needed 
-    if need_to_create
       if @object.save
         oid_login_perm = Link.where(tail_uuid: @object[:email],
                                     head_kind: 'arvados#user',
@@ -208,43 +129,41 @@ puts "\n*************PARAMS = #{params.inspect}"
       else
         raise "Save failed"
       end
+    else
+      @object = @object_found
     end
-
+    
     # create links
     create_user_repo_link params[:repo_name]
     create_vm_login_permission_link params[:vm_uuid], params[:repo_name]
     create_user_group_link 
 
-    show
+    show  
   end
 
   protected 
 
   # find the user from the given user parameters
-  def find_user_from_input(user_uuid, user_email)
-    if user_uuid
-      found_object = User.find_by_uuid user_uuid
+  def find_user_from_input
+    if @object[:uuid]
+      found_object = User.find_by_uuid @object[:uuid]
     end
 
     if !found_object
-      begin
-        if !user_email
-          return
+      if !@object[:email]
+        return
+      end
+=begin  
+        if found_objects.size > 1
+          logger.warn ("Found #{found_objects.size} users with email #{user_email}. Stop.")
+          raise ArgumentError.new "Found #{found_objects.size} users with email #{user_email}. Stop."
+        elsif found_objects.size == 1
+          found_object = found_objects.first
         end
+=end
 
-        if !user_email.match(/\w\@\w+\.\w+/)
-          logger.warn ("Given user param is not valid email format: #{user_email}")
-          raise ArgumentError.new "User param is not of valid email format. Stop"
-        else
-          found_objects = User.where('email=?', user_email)  
-          if found_objects.size > 1
-            logger.warn ("Found #{found_objects.size} users with email #{user_email}. Stop.")
-            raise ArgumentError.new "Found #{found_objects.size} users with email #{user_email}. Stop."
-          elsif found_objects.size == 1
-            found_object = found_objects.first
-          end
-        end
-      end
+      found_objects = User.where('email=?', @object[:email])  
+      found_object = found_objects.first
     end
 
     return found_object
@@ -253,28 +172,30 @@ puts "\n*************PARAMS = #{params.inspect}"
   # link the repo_name passed
   def create_user_repo_link(repo_name)
     if not repo_name
-      logger.warn ("Repository name not given for #{@object[:uuid]}. Skip creating the link")
+      logger.warn ("Repository name not given for #{@object[:uuid]}.")
       return
     end
 
     # Check for an existing repository with the same name we're about to use.
-    repo = (repositories = Repository.where(name: repo_name)) != nil ? repositories.first : nil
+    repo = (repos = Repository.where(name: repo_name)) != nil ? repos.first : nil
     if repo
-      logger.warn "Repository already exists with name #{repo_name}: #{repo[:uuid]}. Will link to user."
+      logger.warn "Repository exists for #{repo_name}: #{repo[:uuid]}."
 
-      # Look for existing repository access (perhaps using a different repository/user name).
+      # Look for existing repository access for this repo
       repo_perms = Link.where(tail_uuid: @object[:uuid],
                               head_kind: 'arvados#repository',
                               head_uuid: repo[:uuid],
                               link_class: 'permission',
                               name: 'can_write')
       if [] != repo_perms
-        logger.warn "User already has repository access " + repo_perms.collect { |p| p[:uuid] }.inspect
+        logger.warn "User already has repository access " + 
+            repo_perms.collect { |p| p[:uuid] }.inspect
         return
       end
     end
 
-    repo ||= Repository.create(name: repo_name)   # create repo, if does not already exist
+    # create repo, if does not already exist
+    repo ||= Repository.create(name: repo_name)
     logger.info { "repo uuid: " + repo[:uuid] }
 
     repo_perm = Link.create(tail_kind: 'arvados#user',
@@ -292,7 +213,7 @@ puts "\n*************PARAMS = #{params.inspect}"
     begin
       vm = (vms = VirtualMachine.where(uuid: vm_uuid)) != nil ? vms.first : nil
       if not vm
-        logger.warn "Could not look up virtual machine with uuid #{vm_uuid.inspect}"
+        logger.warn "Could not find virtual machine for #{vm_uuid.inspect}"
         return
       end
 
@@ -324,7 +245,7 @@ puts "\n*************PARAMS = #{params.inspect}"
     end.first
 
     if not group
-      logger.warn "Could not look up the 'All users' group with uuid '*-*-fffffffffffffff'. Skip."
+      logger.warn "No 'All users' group with uuid '*-*-fffffffffffffff'."
       return
     else
       logger.info { "\"All users\" group uuid: " + group[:uuid] }
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 923949a..d4e25dd 100644
--- a/services/api/test/functional/arvados/v1/users_controller_test.rb
+++ b/services/api/test/functional/arvados/v1/users_controller_test.rb
@@ -69,7 +69,7 @@ class Arvados::V1::UsersControllerTest < ActionController::TestCase
       repo_name: repo_name,
       vm_uuid: 'no_such_vm',
       user: {
-        uuid: "is_this_correct",        
+        uuid: "this_is_agreeable",        
         first_name: "in_create_test_first_name",
         last_name: "test_last_name",
         email: "test at abc.com"
@@ -79,37 +79,40 @@ class Arvados::V1::UsersControllerTest < ActionController::TestCase
     created = JSON.parse(@response.body)
     assert_equal 'in_create_test_first_name', created['first_name']
     assert_not_nil created['uuid'], 'expected non-null uuid for the newly created user'
-    assert_equal 'is_this_correct', created['uuid']
+    assert_equal 'this_is_agreeable', created['uuid']
     assert_not_nil created['email'], 'since email was given, expected non-nil email'
-    assert_nil created['identity_url'], 'even though email isprovided, expected no identity_url since users_controller only creates user' 
+    assert_nil created['identity_url'], 'expected no identity_url' 
 
-    # since no such vm exists, expect only three new links: oid_login_perm, repo link and link add user to 'All users' group
+    # since no such vm exists, expect only three new links: 
+    # oid_login_perm, repo link and link add user to 'All users' group
     verify_num_links @all_links_at_start, 3
 
-    verify_link_exists_for_type 'User', 'permission', 'can_login', created['uuid'], created['email'], 'arvados#user', false
+    verify_link_exists_for_type 'User', 'permission', 'can_login', created['uuid'], 
+        created['email'], 'arvados#user', false
 
-    verify_link_exists_for_type 'Repository', 'permission', 'can_write', repo_name, created['uuid'], 'arvados#repository', true
+    verify_link_exists_for_type 'Repository', 'permission', 'can_write', repo_name, 
+        created['uuid'], 'arvados#repository', true
 
-    verify_link_exists_for_type 'Group', 'permission', 'can_read', 'All users', created['uuid'], 'arvados#group', true
+    verify_link_exists_for_type 'Group', 'permission', 'can_read', 'All users', 
+        created['uuid'], 'arvados#group', true
   end
 
-  test "create user with user_param, vm and repo as input" do
+  test "create user with bogus uuid, vm and repo as input" do
     authorize_with :admin
 
     post :setup, {
-      user_param: 'not_an_existing_uuid_and_not_email_format',
       repo_name: 'test_repo',
       vm_uuid: 'no_such_vm',
-      user: {}
+      user: {uuid: 'not_an_existing_uuid_and_not_email_format'}
     }
     response_body = JSON.parse(@response.body)
     response_errors = response_body['errors']
     assert_not_nil response_errors, 'Expected error in response'
-    incorrectly_formatted = response_errors.first.include?('ArgumentError: User param is not of valid email format')
+    incorrectly_formatted = response_errors.first.include?('No email found in the input')
     assert incorrectly_formatted, 'Expected not valid email format error'
   end
 
-  test "create user with existing uuid user_param, vm and repo as input" do
+  test "create user with existing uuid, vm and repo as input" do
     authorize_with :inactive
     get :current
     assert_response :success
@@ -118,10 +121,9 @@ class Arvados::V1::UsersControllerTest < ActionController::TestCase
     authorize_with :admin
 
     post :setup, {
-      user_param: inactive_user['uuid'],
       repo_name: 'test_repo',
       vm_uuid: 'no_such_vm',
-      user: {}
+      user: {uuid: inactive_user['uuid']}
     }
 
     assert_response :success
@@ -131,14 +133,13 @@ class Arvados::V1::UsersControllerTest < ActionController::TestCase
     assert_equal inactive_user['email'], response_object['email'], 'expecting inactive user email'
   end
 
-  test "create user with valid email user_param, vm and repo as input" do
+  test "create user with valid email, vm and repo as input" do
     authorize_with :admin
 
     post :setup, {
-      user_param: 'abc at xyz.com',
       repo_name: 'test_repo',
       vm_uuid: 'no_such_vm',
-      user: {}
+      user: {email: 'abc at xyz.com'}
     }
 
     assert_response :success
@@ -147,12 +148,11 @@ class Arvados::V1::UsersControllerTest < ActionController::TestCase
     assert_equal response_object['email'], 'abc at xyz.com', 'expecting given email'
   end
 
-  test "create user with valid email user_param, no vm and repo as input" do
+  test "create user with valid email, no vm and repo as input" do
     authorize_with :admin
 
     post :setup, {
-      user_param: 'abc at xyz.com',
-      user: {}
+      user: {email: 'abc at xyz.com'}
     }
 
     assert_response :success    
@@ -161,34 +161,32 @@ class Arvados::V1::UsersControllerTest < ActionController::TestCase
     assert_equal response_object['email'], 'abc at xyz.com', 'expecting given email'
   end
 
-  test "create user with user_param and non-empty user which will be ignored" do
+  test "create user with email, first name, repo name and vm uuid" do
     authorize_with :admin
 
     post :setup, {
-      user_param: 'abc at xyz.com',
       repo_name: 'test_repo',
       vm_uuid: 'no_such_vm',
       user: {
         first_name: 'test_first_name',
-        email: 'will_be_ignored at xyz.com'
+        email: 'abc at xyz.com'
       }
     }
 
     assert_response :success
     response_object = JSON.parse(@response.body)
     assert_not_nil response_object['uuid'], 'expected non-null uuid for the newly created user'
-    assert_equal response_object['email'], 'abc at xyz.com', 'expecting user_param as email'
-    assert_nil response_object['first_name'], 'expecting no first name since it will be reset when user_param is used'
+    assert_equal response_object['email'], 'abc at xyz.com', 'expecting given email'
+    assert_equal 'test_first_name', response_object['first_name'], 'expecting first name'
   end
 
   test "create user twice with user param and check links are not recreated" do
     authorize_with :admin
 
     post :setup, {
-      user_param: 'abc at xyz.com',
       repo_name: 'test_repo',
       vm_uuid: 'no_such_vm',
-      user: {}
+      user: {email: 'abc at xyz.com'}
     }
 
     assert_response :success
@@ -207,7 +205,8 @@ class Arvados::V1::UsersControllerTest < ActionController::TestCase
 
     assert_response :success
     response_object2 = JSON.parse(@response.body)
-    assert_equal response_object['uuid'], response_object2['uuid'], 'expected same uuid as first create operation'
+    assert_equal response_object['uuid'], response_object2['uuid'], 
+        'expected same uuid as first create operation'
     assert_equal response_object['email'], 'abc at xyz.com', 'expecting given email'
     verify_num_links @all_links_at_start, 3   # openid, group, and repo links. no vm link
   end
@@ -229,13 +228,14 @@ class Arvados::V1::UsersControllerTest < ActionController::TestCase
     verify_num_links @all_links_at_start, 3   # openid, group, and repo links. no vm link
 
     # create again
-    post :create, user: {
+    post :setup, user: {
       email: 'abc at xyz.com'
     }
 
     assert_response :success
     response_object2 = JSON.parse(@response.body)
-    assert_equal response_object['uuid'], response_object2['uuid'], 'expected same uuid as first create operation'
+    assert_equal response_object['uuid'], response_object2['uuid'], 
+        'expected same uuid as first create operation'
     assert_equal response_object['email'], 'abc at xyz.com', 'expecting given email'
     verify_num_links @all_links_at_start, 3   # openid, group, and repo links. no vm link
   end
@@ -258,17 +258,21 @@ class Arvados::V1::UsersControllerTest < ActionController::TestCase
     assert_equal 'in_create_test_first_name', created['first_name']
     assert_not_nil created['uuid'], 'expected non-null uuid for the newly created user'
     assert_not_nil created['email'], 'since email was given, expected non-nil email'
-    assert_nil created['identity_url'], 'even though email is provided, expected no identity_url since users_controller only creates user' 
+    assert_nil created['identity_url'], 'expected no identity_url' 
 
     # verify links
-    # expect 3 new links: oid_login_perm, repo link, and link add user to 'All users' group. No vm link since the vm_uuid passed in is not in system
+    # expect 3 new links: oid_login_perm, repo link, and link add user to 'All users' group. 
+    # No vm link since the vm_uuid passed in is not in system
     verify_num_links @all_links_at_start, 3
 
-    verify_link_exists_for_type 'User', 'permission', 'can_login', created['uuid'], created['email'], 'arvados#user', false
+    verify_link_exists_for_type 'User', 'permission', 'can_login', created['uuid'], 
+        created['email'], 'arvados#user', false
 
-    verify_link_exists_for_type 'Repository', 'permission', 'can_write', 'test_repo', created['uuid'], 'arvados#repository', true
+    verify_link_exists_for_type 'Repository', 'permission', 'can_write', 'test_repo', 
+        created['uuid'], 'arvados#repository', true
 
-    verify_link_exists_for_type 'Group', 'permission', 'can_read', 'All users', created['uuid'], 'arvados#group', true
+    verify_link_exists_for_type 'Group', 'permission', 'can_read', 'All users', 
+      created['uuid'], 'arvados#group', true
   end
 
   test "create user with user, vm and repo and verify links" do
@@ -288,18 +292,22 @@ class Arvados::V1::UsersControllerTest < ActionController::TestCase
     assert_equal 'in_create_test_first_name', created['first_name']
     assert_not_nil created['uuid'], 'expected non-null uuid for the newly created user'
     assert_not_nil created['email'], 'since email was given, expected non-nil email'
-    assert_nil created['identity_url'], 'even though email is provided, expected no identity_url since users_controller only creates user' 
+    assert_nil created['identity_url'], 'expected no identity_url' 
 
     # expect 4 new links: oid_login_perm, repo link, vm link and link add user to 'All users' group. 
     verify_num_links @all_links_at_start, 4
 
-    verify_link_exists_for_type 'User', 'permission', 'can_login', created['uuid'], created['email'], 'arvados#user', false
+    verify_link_exists_for_type 'User', 'permission', 'can_login', created['uuid'], 
+        created['email'], 'arvados#user', false
 
-    verify_link_exists_for_type 'Repository', 'permission', 'can_write', 'test_repo', created['uuid'], 'arvados#repository', true
+    verify_link_exists_for_type 'Repository', 'permission', 'can_write', 'test_repo', 
+        created['uuid'], 'arvados#repository', true
 
-    verify_link_exists_for_type 'Group', 'permission', 'can_read', 'All users', created['uuid'], 'arvados#group', true
+    verify_link_exists_for_type 'Group', 'permission', 'can_read', 'All users', 
+        created['uuid'], 'arvados#group', true
 
-    verify_link_exists_for_type 'VirtualMachine', 'permission', 'can_login', @vm_uuid, created['uuid'], 'arvados#virtualMachine', false
+    verify_link_exists_for_type 'VirtualMachine', 'permission', 'can_login', 
+        @vm_uuid, created['uuid'], 'arvados#virtualMachine', false
   end
 
   def verify_num_links (original_links, expected_num_additional_links)
@@ -308,7 +316,8 @@ class Arvados::V1::UsersControllerTest < ActionController::TestCase
               "Expected #{expected_num_additional_links.inspect} more links"
   end
 
-  def verify_link_exists_for_type(class_name, link_class, link_name, head_uuid, tail_uuid, head_kind, fetch_object)
+  def verify_link_exists_for_type(class_name, link_class, link_name, head_uuid, tail_uuid, 
+        head_kind, fetch_object)
     if fetch_object
       object = Object.const_get(class_name).where(name: head_uuid)
       assert [] != object, "expected a #{class_name.inspect} with the name #{head_uuid.inspect}"
@@ -320,7 +329,7 @@ class Arvados::V1::UsersControllerTest < ActionController::TestCase
                        tail_uuid: tail_uuid,
                        head_uuid: head_uuid,
                        head_kind: head_kind)
-    assert links.size > 0, "expected one or more links with the given criteria #{class_name} with #{head_uuid}"
+    assert links.size > 0, "expected one or more links for #{class_name} with #{head_uuid}"
   end
 
 end

commit 4ad105bd22c9141e5f88139ad3bf334a52f7a5d1
Author: radhika chippada <radhika at radhika.curoverse>
Date:   Thu Mar 20 16:07:48 2014 -0400

    Introduce a new setup method in the users controller

diff --git a/services/api/app/controllers/arvados/v1/users_controller.rb b/services/api/app/controllers/arvados/v1/users_controller.rb
index 367374a..1233e74 100644
--- a/services/api/app/controllers/arvados/v1/users_controller.rb
+++ b/services/api/app/controllers/arvados/v1/users_controller.rb
@@ -1,8 +1,8 @@
 class Arvados::V1::UsersController < ApplicationController
   skip_before_filter :find_object_by_uuid, only:
-    [:activate, :event_stream, :current, :system]
+    [:activate, :event_stream, :current, :system, :setup]
   skip_before_filter :render_404_if_no_object, only:
-    [:activate, :event_stream, :current, :system]
+    [:activate, :event_stream, :current, :system, :setup]
 
   def current
     @object = current_user
@@ -89,6 +89,71 @@ class Arvados::V1::UsersController < ApplicationController
   end
 
   # create user object and all the needed links
+  def setup
+puts "\n*************PARAMS = #{params.inspect}"
+    if params[:openid_prefix]   # check if default openid_prefix needs to be overridden
+      openid_prefix = params[:openid_prefix]
+    else 
+      openid_prefix = Rails.configuration.openid_prefix
+    end
+    login_perm_props = {identity_url_prefix: openid_prefix}
+
+    @object = model_class.new resource_attrs
+
+    # If user_param is passed, lookup for user. If exists, skip create and only create any missing links. 
+    if params[:user_param]
+      begin
+        @object_found = find_user_from_input params[:user_param], params[:user_param]
+      end
+      if !@object_found
+        @object = User.new    # when user_param is used, it will be used as user object
+        @object[:email] = params[:user_param]       
+        need_to_create = true
+      else
+        @object = @object_found
+      end
+    else    # need to create user for the given user data
+      @object_found = find_user_from_input @object[:uuid], @object[:email]
+      if !@object_found
+        need_to_create = true   # use the user object sent in to create with the user
+      else
+        @object = @object_found
+      end
+    end
+
+    # create if need be, and then create or update the links as needed 
+    if need_to_create
+      if @object.save
+        oid_login_perm = Link.where(tail_uuid: @object[:email],
+                                    head_kind: 'arvados#user',
+                                    link_class: 'permission',
+                                    name: 'can_login')
+
+        if [] == oid_login_perm
+          # create openid login permission
+          oid_login_perm = Link.create(link_class: 'permission',
+                                       name: 'can_login',
+                                       tail_kind: 'email',
+                                       tail_uuid: @object[:email],
+                                       head_kind: 'arvados#user',
+                                       head_uuid: @object[:uuid],
+                                       properties: login_perm_props
+                                      )
+          logger.info { "openid login permission: " + oid_login_perm[:uuid] }
+        end
+      else
+        raise "Save failed"
+      end
+    end
+
+    # create links
+    create_user_repo_link params[:repo_name]
+    create_vm_login_permission_link params[:vm_uuid], params[:repo_name]
+    create_user_group_link 
+
+    show  end
+
+  # create user object and all the needed links
   def create
     if params[:openid_prefix]   # check if default openid_prefix needs to be overridden
       openid_prefix = params[:openid_prefix]
diff --git a/services/api/config/routes.rb b/services/api/config/routes.rb
index dffae7f..044f0dc 100644
--- a/services/api/config/routes.rb
+++ b/services/api/config/routes.rb
@@ -90,6 +90,7 @@ Server::Application.routes.draw do
       post '/jobs/:uuid/cancel' => 'jobs#cancel'
       match '/users/:uuid/event_stream' => 'users#event_stream'
       post '/users/:uuid/activate' => 'users#activate'
+      post '/users/setup' => 'users#setup'
       match '/virtual_machines/get_all_logins' => 'virtual_machines#get_all_logins'
       match '/virtual_machines/:uuid/logins' => 'virtual_machines#logins'
       post '/api_client_authorizations/create_system_auth' => 'api_client_authorizations#create_system_auth'
diff --git a/services/api/script/setup-new-user.rb b/services/api/script/setup-new-user.rb
index 38c9903..ced32bc 100755
--- a/services/api/script/setup-new-user.rb
+++ b/services/api/script/setup-new-user.rb
@@ -4,6 +4,7 @@ abort 'Error: Ruby >= 1.9.3 required.' if RUBY_VERSION < '1.9.3'
 
 require 'logger'
 require 'trollop'
+
 log = Logger.new STDERR
 log.progname = $0.split('/').last
 
@@ -31,14 +32,35 @@ log.level = (ENV['DEBUG'] || opts.debug) ? Logger::DEBUG : Logger::WARN
 if ARGV.count != 3
   Trollop::die "required arguments are missing"
 end
+
 user_arg, user_repo_name, vm_uuid = ARGV
 
 require 'arvados'
 arv = Arvados.new(api_version: 'v1')
 
+# Look up the given user by uuid or, failing that, email address.
 begin
-  new_user = arv.user.create(user_param: user_arg, repo_name: user_repo_name, vm_uuid: vm_uuid, openid_prefix: opts.openid_prefix, user: {})
-  log.warn new_user
-rescue Exception => e #Arvados::TransactionFailedError
-  log.warn e.message
+  user = arv.user.get(uuid: user_arg)
+rescue Arvados::TransactionFailedError
+  found = arv.user.list(where: {email: ARGV[0]})[:items]
+         
+  if found.count == 0 
+    if !user_arg.match(/\w\@\w+\.\w+/)
+      abort "About to create new user, but #{user_arg.inspect} " +
+               "does not look like an email address. Stop."
+    end
+           
+    user = arv.user.setup(repo_name: user_repo_name, vm_uuid: vm_uuid, user: {email: user_arg})
+    log.info { "created user: " + user[:uuid] }
+  elsif found.count != 1
+    abort "Found #{found.count} users " +
+             "with uuid or email #{user_arg.inspect}. Stop."
+  else 
+    user = found.first
+    # Found user. Update ther user links
+    user = arv.user.setup(repo_name: user_repo_name, vm_uuid: vm_uuid, user: {email: user[:uuid]})
+  end
+    
+  puts "USER = #{user.inspect}"
+  log.info { "user uuid: " + user[:uuid] }
 end
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 748d281..923949a 100644
--- a/services/api/test/functional/arvados/v1/users_controller_test.rb
+++ b/services/api/test/functional/arvados/v1/users_controller_test.rb
@@ -65,7 +65,7 @@ class Arvados::V1::UsersControllerTest < ActionController::TestCase
     authorize_with :admin
     repo_name = 'test_repo'
 
-    post :create, {
+    post :setup, {
       repo_name: repo_name,
       vm_uuid: 'no_such_vm',
       user: {
@@ -96,13 +96,12 @@ class Arvados::V1::UsersControllerTest < ActionController::TestCase
   test "create user with user_param, vm and repo as input" do
     authorize_with :admin
 
-    post :create, {
+    post :setup, {
       user_param: 'not_an_existing_uuid_and_not_email_format',
       repo_name: 'test_repo',
       vm_uuid: 'no_such_vm',
       user: {}
     }
-
     response_body = JSON.parse(@response.body)
     response_errors = response_body['errors']
     assert_not_nil response_errors, 'Expected error in response'
@@ -118,7 +117,7 @@ class Arvados::V1::UsersControllerTest < ActionController::TestCase
     
     authorize_with :admin
 
-    post :create, {
+    post :setup, {
       user_param: inactive_user['uuid'],
       repo_name: 'test_repo',
       vm_uuid: 'no_such_vm',
@@ -135,7 +134,7 @@ class Arvados::V1::UsersControllerTest < ActionController::TestCase
   test "create user with valid email user_param, vm and repo as input" do
     authorize_with :admin
 
-    post :create, {
+    post :setup, {
       user_param: 'abc at xyz.com',
       repo_name: 'test_repo',
       vm_uuid: 'no_such_vm',
@@ -151,7 +150,7 @@ class Arvados::V1::UsersControllerTest < ActionController::TestCase
   test "create user with valid email user_param, no vm and repo as input" do
     authorize_with :admin
 
-    post :create, {
+    post :setup, {
       user_param: 'abc at xyz.com',
       user: {}
     }
@@ -165,7 +164,7 @@ class Arvados::V1::UsersControllerTest < ActionController::TestCase
   test "create user with user_param and non-empty user which will be ignored" do
     authorize_with :admin
 
-    post :create, {
+    post :setup, {
       user_param: 'abc at xyz.com',
       repo_name: 'test_repo',
       vm_uuid: 'no_such_vm',
@@ -185,7 +184,7 @@ class Arvados::V1::UsersControllerTest < ActionController::TestCase
   test "create user twice with user param and check links are not recreated" do
     authorize_with :admin
 
-    post :create, {
+    post :setup, {
       user_param: 'abc at xyz.com',
       repo_name: 'test_repo',
       vm_uuid: 'no_such_vm',
@@ -199,7 +198,7 @@ class Arvados::V1::UsersControllerTest < ActionController::TestCase
     verify_num_links @all_links_at_start, 3   # openid, group, and repo links. no vm link
 
     # create again
-    post :create, {
+    post :setup, {
       user_param: 'abc at xyz.com',
       repo_name: 'test_repo',
       vm_uuid: 'no_such_vm',
@@ -216,7 +215,7 @@ class Arvados::V1::UsersControllerTest < ActionController::TestCase
   test "create user twice with user object as input and check links are not recreated" do
     authorize_with :admin
 
-    post :create, {
+    post :setup, {
       repo_name: 'test_repo',
       user: {
         email: 'abc at xyz.com'
@@ -244,7 +243,7 @@ class Arvados::V1::UsersControllerTest < ActionController::TestCase
   test "create user with openid prefix" do
     authorize_with :admin
 
-    post :create, {
+    post :setup, {
       repo_name: 'test_repo',
       vm_uuid: 'no_such_vm',
       openid_prefix: 'http://www.xyz.com/account',
@@ -275,7 +274,7 @@ class Arvados::V1::UsersControllerTest < ActionController::TestCase
   test "create user with user, vm and repo and verify links" do
     authorize_with :admin
 
-    post :create, {
+    post :setup, {
       repo_name: 'test_repo',
       vm_uuid: @vm_uuid,
       user: {

-----------------------------------------------------------------------


hooks/post-receive
-- 




More information about the arvados-commits mailing list