3 class UserTest < ActiveSupport::TestCase
4 include CurrentApiClient
6 # The fixture services/api/test/fixtures/users.yml serves as the input for this test case
8 # Make sure system_user exists before making "pre-test users" list
11 @all_users = User.find(:all)
13 @all_users.each do |user|
14 if user.uuid == system_user_uuid
16 elsif user.is_admin && user.is_active
18 elsif user.is_active && !user.is_admin
20 elsif !user.is_active && !user.is_invited
21 @uninvited_user = user
26 test "check non-admin active user properties" do
27 assert !@active_user.is_admin, 'is_admin should not be set for a non-admin user'
28 assert @active_user.is_active, 'user should be active'
29 assert @active_user.is_invited, 'is_invited should be set'
30 assert_not_nil @active_user.prefs, "user's preferences should be non-null, but may be size zero"
31 assert (@active_user.can? :read=>"#{@active_user.uuid}"), "user should be able to read own object"
32 assert (@active_user.can? :write=>"#{@active_user.uuid}"), "user should be able to write own object"
33 assert (@active_user.can? :manage=>"#{@active_user.uuid}"), "user should be able to manage own object"
35 assert @active_user.groups_i_can(:read).size > 0, "active user should be able read at least one group"
37 # non-admin user cannot manage or write other user objects
38 assert !(@active_user.can? :read=>"#{@uninvited_user.uuid}")
39 assert !(@active_user.can? :write=>"#{@uninvited_user.uuid}")
40 assert !(@active_user.can? :manage=>"#{@uninvited_user.uuid}")
43 test "check admin user properties" do
44 assert @admin_user.is_admin, 'is_admin should be set for admin user'
45 assert @admin_user.is_active, 'admin user cannot be inactive'
46 assert @admin_user.is_invited, 'is_invited should be set'
47 assert_not_nil @admin_user.uuid.size, "user's uuid should be non-null"
48 assert_not_nil @admin_user.prefs, "user's preferences should be non-null, but may be size zero"
49 assert @admin_user.identity_url.size > 0, "user's identity url is expected"
50 assert @admin_user.can? :read=>"#{@admin_user.uuid}"
51 assert @admin_user.can? :write=>"#{@admin_user.uuid}"
52 assert @admin_user.can? :manage=>"#{@admin_user.uuid}"
54 assert @admin_user.groups_i_can(:read).size > 0, "admin active user should be able read at least one group"
55 assert @admin_user.groups_i_can(:write).size > 0, "admin active user should be able write to at least one group"
56 assert @admin_user.groups_i_can(:manage).size > 0, "admin active user should be able manage at least one group"
58 # admin user can also write or manage other users
59 assert @admin_user.can? :read=>"#{@uninvited_user.uuid}"
60 assert @admin_user.can? :write=>"#{@uninvited_user.uuid}"
61 assert @admin_user.can? :manage=>"#{@uninvited_user.uuid}"
64 test "check inactive and uninvited user properties" do
65 assert !@uninvited_user.is_admin, 'is_admin should not be set for a non-admin user'
66 assert !@uninvited_user.is_active, 'user should be inactive'
67 assert !@uninvited_user.is_invited, 'is_invited should not be set'
68 assert @uninvited_user.can? :read=>"#{@uninvited_user.uuid}"
69 assert @uninvited_user.can? :write=>"#{@uninvited_user.uuid}"
70 assert @uninvited_user.can? :manage=>"#{@uninvited_user.uuid}"
72 assert @uninvited_user.groups_i_can(:read).size == 0, "inactive and uninvited user should not be able read any groups"
73 assert @uninvited_user.groups_i_can(:write).size == 0, "inactive and uninvited user should not be able write to any groups"
74 assert @uninvited_user.groups_i_can(:manage).size == 0, "inactive and uninvited user should not be able manage any groups"
77 test "find user method checks" do
78 User.find(:all).each do |user|
79 assert_not_nil user.uuid, "non-null uuid expected for " + user.full_name
82 user = users(:active) # get the active user
84 found_user = User.find(user.id) # find a user by the row id
86 assert_equal found_user.full_name, user.first_name + ' ' + user.last_name
87 assert_equal found_user.identity_url, user.identity_url
90 test "full name should not contain spurious whitespace" do
91 Thread.current[:user] = @admin_user # set admin user as the current user
93 user = User.create ({uuid: 'zzzzz-tpzed-abcdefghijklmno', email: 'foo@example.com' })
95 assert_equal '', user.full_name
97 user.first_name = 'John'
98 user.last_name = 'Smith'
100 assert_equal user.first_name + ' ' + user.last_name, user.full_name
103 test "create new user" do
104 Thread.current[:user] = @admin_user # set admin user as the current user
107 user.first_name = "first_name_for_newly_created_user"
110 # verify there is one extra user in the db now
111 assert_equal @all_users.size+1, User.find(:all).size
113 user = User.find(user.id) # get the user back
114 assert_equal(user.first_name, 'first_name_for_newly_created_user')
115 assert_not_nil user.uuid, 'uuid should be set for newly created user'
116 assert_nil user.email, 'email should be null for newly created user, because it was not passed in'
117 assert_nil user.identity_url, 'identity_url should be null for newly created user, because it was not passed in'
119 user.first_name = 'first_name_for_newly_created_user_updated'
121 user = User.find(user.id) # get the user back
122 assert_equal(user.first_name, 'first_name_for_newly_created_user_updated')
125 test "update existing user" do
126 Thread.current[:user] = @active_user # set active user as current user
127 @active_user.first_name = "first_name_changed"
130 @active_user = User.find(@active_user.id) # get the user back
131 assert_equal(@active_user.first_name, 'first_name_changed')
133 # admin user also should be able to update the "active" user info
134 Thread.current[:user] = @admin_user # set admin user as current user
135 @active_user.first_name = "first_name_changed_by_admin_for_active_user"
138 @active_user = User.find(@active_user.id) # get the user back
139 assert_equal(@active_user.first_name, 'first_name_changed_by_admin_for_active_user')
142 test "delete a user and verify" do
143 active_user_uuid = @active_user.uuid
145 Thread.current[:user] = @admin_user
148 found_deleted_user = false
149 User.find(:all).each do |user|
150 if user.uuid == active_user_uuid
151 found_deleted_user = true
155 assert !found_deleted_user, "found deleted user: "+active_user_uuid
159 test "create new user as non-admin user" do
160 Thread.current[:user] = @active_user
165 rescue ArvadosModel::PermissionDeniedError => e
167 assert (e.message.include? 'PermissionDeniedError'),
168 'Expected PermissionDeniedError'
171 test "setup new user" do
172 Thread.current[:user] = @admin_user
174 email = 'foo@example.com'
175 openid_prefix = 'http://openid/prefix'
177 user = User.create ({uuid: 'zzzzz-tpzed-abcdefghijklmno', email: email})
179 vm = VirtualMachine.create
181 response = User.setup user, openid_prefix, 'test_repo', vm.uuid
183 resp_user = find_obj_in_resp response, 'User'
184 verify_user resp_user, email
186 oid_login_perm = find_obj_in_resp response, 'Link', 'arvados#user'
188 verify_link oid_login_perm, 'permission', 'can_login', resp_user[:email],
191 assert_equal openid_prefix, oid_login_perm[:properties]['identity_url_prefix'],
192 'expected identity_url_prefix not found for oid_login_perm'
194 group_perm = find_obj_in_resp response, 'Link', 'arvados#group'
195 verify_link group_perm, 'permission', 'can_read', resp_user[:uuid], nil
197 repo_perm = find_obj_in_resp response, 'Link', 'arvados#repository'
198 verify_link repo_perm, 'permission', 'can_write', resp_user[:uuid], nil
200 vm_perm = find_obj_in_resp response, 'Link', 'arvados#virtualMachine'
201 verify_link vm_perm, 'permission', 'can_login', resp_user[:uuid], vm.uuid
204 test "setup new user with junk in database" do
205 Thread.current[:user] = @admin_user
207 email = 'foo@example.com'
208 openid_prefix = 'http://openid/prefix'
210 user = User.create ({uuid: 'zzzzz-tpzed-abcdefghijklmno', email: email})
212 vm = VirtualMachine.create
214 # Set up the bogus Link
215 bad_uuid = 'zzzzz-tpzed-xyzxyzxyzxyzxyz'
217 resp_link = Link.create ({tail_uuid: email, link_class: 'permission',
218 name: 'can_login', head_uuid: bad_uuid})
219 resp_link.save(validate: false)
221 verify_link resp_link, 'permission', 'can_login', email, bad_uuid
223 response = User.setup user, openid_prefix, 'test_repo', vm.uuid
225 resp_user = find_obj_in_resp response, 'User'
226 verify_user resp_user, email
228 oid_login_perm = find_obj_in_resp response, 'Link', 'arvados#user'
230 verify_link oid_login_perm, 'permission', 'can_login', resp_user[:email],
233 assert_equal openid_prefix, oid_login_perm[:properties]['identity_url_prefix'],
234 'expected identity_url_prefix not found for oid_login_perm'
236 group_perm = find_obj_in_resp response, 'Link', 'arvados#group'
237 verify_link group_perm, 'permission', 'can_read', resp_user[:uuid], nil
239 repo_perm = find_obj_in_resp response, 'Link', 'arvados#repository'
240 verify_link repo_perm, 'permission', 'can_write', resp_user[:uuid], nil
242 vm_perm = find_obj_in_resp response, 'Link', 'arvados#virtualMachine'
243 verify_link vm_perm, 'permission', 'can_login', resp_user[:uuid], vm.uuid
248 test "setup new user in multiple steps" do
249 Thread.current[:user] = @admin_user
251 email = 'foo@example.com'
252 openid_prefix = 'http://openid/prefix'
254 user = User.create ({uuid: 'zzzzz-tpzed-abcdefghijklmno', email: email})
256 response = User.setup user, openid_prefix
258 resp_user = find_obj_in_resp response, 'User'
259 verify_user resp_user, email
261 oid_login_perm = find_obj_in_resp response, 'Link', 'arvados#user'
262 verify_link oid_login_perm, 'permission', 'can_login', resp_user[:email],
264 assert_equal openid_prefix, oid_login_perm[:properties]['identity_url_prefix'],
265 'expected identity_url_prefix not found for oid_login_perm'
267 group_perm = find_obj_in_resp response, 'Link', 'arvados#group'
268 verify_link group_perm, 'permission', 'can_read', resp_user[:uuid], nil
270 # invoke setup again with repo_name
271 response = User.setup user, openid_prefix, 'test_repo'
272 resp_user = find_obj_in_resp response, 'User', nil
273 verify_user resp_user, email
274 assert_equal user.uuid, resp_user[:uuid], 'expected uuid not found'
276 group_perm = find_obj_in_resp response, 'Link', 'arvados#group'
277 verify_link group_perm, 'permission', 'can_read', resp_user[:uuid], nil
279 repo_perm = find_obj_in_resp response, 'Link', 'arvados#repository'
280 verify_link repo_perm, 'permission', 'can_write', resp_user[:uuid], nil
282 # invoke setup again with a vm_uuid
283 vm = VirtualMachine.create
285 response = User.setup user, openid_prefix, 'test_repo', vm.uuid
287 resp_user = find_obj_in_resp response, 'User', nil
288 verify_user resp_user, email
289 assert_equal user.uuid, resp_user[:uuid], 'expected uuid not found'
291 group_perm = find_obj_in_resp response, 'Link', 'arvados#group'
292 verify_link group_perm, 'permission', 'can_read', resp_user[:uuid], nil
294 repo_perm = find_obj_in_resp response, 'Link', 'arvados#repository'
295 verify_link repo_perm, 'permission', 'can_write', resp_user[:uuid], nil
297 vm_perm = find_obj_in_resp response, 'Link', 'arvados#virtualMachine'
298 verify_link vm_perm, 'permission', 'can_login', resp_user[:uuid], vm.uuid
301 def find_obj_in_resp (response_items, object_type, head_kind=nil)
303 response_items.each { |x|
308 if object_type == 'User'
309 if ArvadosModel::resource_class_for_uuid(x['uuid']) == User
313 else # looking for a link
314 if ArvadosModel::resource_class_for_uuid(x['head_uuid']).kind == head_kind
323 def verify_user (resp_user, email)
324 assert_not_nil resp_user, 'expected user object'
325 assert_not_nil resp_user['uuid'], 'expected user object'
326 assert_equal email, resp_user['email'], 'expected email not found'
330 def verify_link (link_object, link_class, link_name, tail_uuid, head_uuid)
331 assert_not_nil link_object, "expected link for #{link_class} #{link_name}"
332 assert_not_nil link_object[:uuid],
333 "expected non-nil uuid for link for #{link_class} #{link_name}"
334 assert_equal link_class, link_object[:link_class],
335 "expected link_class not found for #{link_class} #{link_name}"
336 assert_equal link_name, link_object[:name],
337 "expected link_name not found for #{link_class} #{link_name}"
338 assert_equal tail_uuid, link_object[:tail_uuid],
339 "expected tail_uuid not found for #{link_class} #{link_name}"
341 assert_equal head_uuid, link_object[:head_uuid],
342 "expected head_uuid not found for #{link_class} #{link_name}"