Merge branch '15318-ReqIds-on-api-errors'
[arvados.git] / services / api / test / unit / authorized_key_test.rb
1 # Copyright (C) The Arvados Authors. All rights reserved.
2 #
3 # SPDX-License-Identifier: AGPL-3.0
4
5 require 'test_helper'
6
7 class AuthorizedKeyTest < ActiveSupport::TestCase
8   TEST_KEY = "ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQCf5aTI55uyWr44TckP/ELUAyPsdnf5fTZDcSDN4qiMZYAL7TYV2ixwnbPObLObM0GmHSSFLV1KqsuFICUPgkyKoHbAH6XPgmtfOLU60VkGf1v5uxQ/kXCECRCJmPb3K9dIXGEw+1DXPdOV/xG7rJNvo4a9WK9iqqZr8p+VGKM6C017b8BDLk0tuEEjZ5jXcT/ka/hTScxWkKgF6auPOVQ79OA5+0VaYm4uQLzVUdgwVUPWQQecRrtnc08XYM1htpcLDIAbWfUNK7uE6XR3/OhtrJGf05FGbtGguPgi33F9W3Q3yw6saOK5Y3TfLbskgFaEdLgzqK/QSBRk2zBF49Tj test@localhost"
9
10   test 'create and update key' do
11     u1 = users(:active)
12     act_as_user u1 do
13       ak = AuthorizedKey.new(name: "foo", public_key: TEST_KEY, authorized_user_uuid: u1.uuid)
14       assert ak.save, ak.errors.full_messages.to_s
15       ak.name = "bar"
16       assert ak.valid?, ak.errors.full_messages.to_s
17       assert ak.save, ak.errors.full_messages.to_s
18     end
19   end
20
21   test 'duplicate key not permitted' do
22     u1 = users(:active)
23     act_as_user u1 do
24       ak = AuthorizedKey.new(name: "foo", public_key: TEST_KEY, authorized_user_uuid: u1.uuid)
25       assert ak.save
26     end
27     u2 = users(:spectator)
28     act_as_user u2 do
29       ak2 = AuthorizedKey.new(name: "bar", public_key: TEST_KEY, authorized_user_uuid: u2.uuid)
30       refute ak2.valid?
31       refute ak2.save
32       assert_match(/already exists/, ak2.errors.full_messages.to_s)
33     end
34   end
35
36   test 'attach key to wrong user account' do
37     act_as_user users(:active) do
38       ak = AuthorizedKey.new(name: "foo", public_key: TEST_KEY)
39       ak.authorized_user_uuid = users(:spectator).uuid
40       refute ak.save
41       ak.uuid = nil
42       ak.authorized_user_uuid = users(:admin).uuid
43       refute ak.save
44       ak.uuid = nil
45       ak.authorized_user_uuid = users(:active).uuid
46       assert ak.save, ak.errors.full_messages.to_s
47       ak.authorized_user_uuid = users(:admin).uuid
48       refute ak.save
49     end
50   end
51 end