|
| 1 | +// Copyright (c) 2026 Keymaster Team |
| 2 | +// Keymaster - SSH key management system |
| 3 | +// This source code is licensed under the MIT license found in the LICENSE file. |
| 4 | + |
| 5 | +package db |
| 6 | + |
| 7 | +import ( |
| 8 | + "context" |
| 9 | + "strings" |
| 10 | + "testing" |
| 11 | + "time" |
| 12 | +) |
| 13 | + |
| 14 | +func TestAssignKeyToAccount_GlobalKey_ReturnsError(t *testing.T) { |
| 15 | + bStore, err := New("sqlite", ":memory:") |
| 16 | + if err != nil { |
| 17 | + t.Fatalf("New failed: %v", err) |
| 18 | + } |
| 19 | + bdb := bStore.BunDB() |
| 20 | + ctx := context.Background() |
| 21 | + |
| 22 | + // Create account |
| 23 | + _, err = ExecRaw(ctx, bdb, "INSERT INTO accounts(username, hostname, label) VALUES(?, ?, ?)", "user1", "host1.example.com", "Test1") |
| 24 | + if err != nil { |
| 25 | + t.Fatalf("insert account failed: %v", err) |
| 26 | + } |
| 27 | + acc, _ := GetAccountByIDBun(bdb, 1) |
| 28 | + if acc == nil { |
| 29 | + t.Fatal("account not found after insert") |
| 30 | + } |
| 31 | + |
| 32 | + // Create a GLOBAL key |
| 33 | + err = AddPublicKeyBun(bdb, "ssh-ed25519", "AAAAC3test", "global-key", true, time.Time{}) |
| 34 | + if err != nil { |
| 35 | + t.Fatalf("AddPublicKeyBun failed: %v", err) |
| 36 | + } |
| 37 | + pk, _ := GetPublicKeyByCommentBun(bdb, "global-key") |
| 38 | + if pk == nil { |
| 39 | + t.Fatal("key not found after insert") |
| 40 | + } |
| 41 | + |
| 42 | + // Try to assign the global key to an account - should fail |
| 43 | + err = AssignKeyToAccountBun(bdb, pk.ID, acc.ID) |
| 44 | + if err == nil { |
| 45 | + t.Fatal("expected error when assigning global key, got nil") |
| 46 | + } |
| 47 | + if !strings.Contains(err.Error(), "cannot assign global key") { |
| 48 | + t.Fatalf("expected error about global key, got: %v", err) |
| 49 | + } |
| 50 | + |
| 51 | + // Verify the key was NOT added to account_keys |
| 52 | + keys, err := GetKeysForAccountBun(bdb, acc.ID) |
| 53 | + if err != nil { |
| 54 | + t.Fatalf("GetKeysForAccountBun failed: %v", err) |
| 55 | + } |
| 56 | + if len(keys) != 0 { |
| 57 | + t.Fatalf("expected 0 keys in account_keys for account %d, got %d", acc.ID, len(keys)) |
| 58 | + } |
| 59 | +} |
| 60 | + |
| 61 | +func TestAssignKeyToAccount_NonGlobalKey_Succeeds(t *testing.T) { |
| 62 | + bStore, err := New("sqlite", ":memory:") |
| 63 | + if err != nil { |
| 64 | + t.Fatalf("New failed: %v", err) |
| 65 | + } |
| 66 | + bdb := bStore.BunDB() |
| 67 | + ctx := context.Background() |
| 68 | + |
| 69 | + // Create account |
| 70 | + _, err = ExecRaw(ctx, bdb, "INSERT INTO accounts(username, hostname, label) VALUES(?, ?, ?)", "user2", "host2.example.com", "Test2") |
| 71 | + if err != nil { |
| 72 | + t.Fatalf("insert account failed: %v", err) |
| 73 | + } |
| 74 | + acc, _ := GetAccountByIDBun(bdb, 1) |
| 75 | + if acc == nil { |
| 76 | + t.Fatal("account not found after insert") |
| 77 | + } |
| 78 | + |
| 79 | + // Create a NON-GLOBAL key |
| 80 | + err = AddPublicKeyBun(bdb, "ssh-ed25519", "AAAAC3test2", "regular-key", false, time.Time{}) |
| 81 | + if err != nil { |
| 82 | + t.Fatalf("AddPublicKeyBun failed: %v", err) |
| 83 | + } |
| 84 | + pk, _ := GetPublicKeyByCommentBun(bdb, "regular-key") |
| 85 | + if pk == nil { |
| 86 | + t.Fatal("key not found after insert") |
| 87 | + } |
| 88 | + |
| 89 | + // Assign the non-global key to an account - should succeed |
| 90 | + err = AssignKeyToAccountBun(bdb, pk.ID, acc.ID) |
| 91 | + if err != nil { |
| 92 | + t.Fatalf("expected no error when assigning non-global key, got: %v", err) |
| 93 | + } |
| 94 | + |
| 95 | + // Verify the key WAS added to account_keys |
| 96 | + keys, err := GetKeysForAccountBun(bdb, acc.ID) |
| 97 | + if err != nil { |
| 98 | + t.Fatalf("GetKeysForAccountBun failed: %v", err) |
| 99 | + } |
| 100 | + if len(keys) != 1 { |
| 101 | + t.Fatalf("expected 1 key in account_keys for account %d, got %d", acc.ID, len(keys)) |
| 102 | + } |
| 103 | + if keys[0].ID != pk.ID { |
| 104 | + t.Fatalf("expected key ID %d, got %d", pk.ID, keys[0].ID) |
| 105 | + } |
| 106 | +} |
0 commit comments