|
| 1 | +package core |
| 2 | + |
| 3 | +import ( |
| 4 | + "path/filepath" |
| 5 | + "strings" |
| 6 | + "testing" |
| 7 | + "time" |
| 8 | + |
| 9 | + "github.com/AppsGanin/rospanel/internal/auth" |
| 10 | + "github.com/AppsGanin/rospanel/internal/model" |
| 11 | + "github.com/AppsGanin/rospanel/internal/store" |
| 12 | +) |
| 13 | + |
| 14 | +// rosterManager returns a manager whose panel is already owned, mirroring a real |
| 15 | +// install: bootstrap creates the owner, everyone else is added through the roster. |
| 16 | +func rosterManager(t *testing.T) (*Manager, int64) { |
| 17 | + t.Helper() |
| 18 | + st, err := store.Open(filepath.Join(t.TempDir(), "roster.db")) |
| 19 | + if err != nil { |
| 20 | + t.Fatalf("open store: %v", err) |
| 21 | + } |
| 22 | + t.Cleanup(func() { st.Close() }) |
| 23 | + |
| 24 | + hash, err := auth.HashPassword("owner-password") |
| 25 | + if err != nil { |
| 26 | + t.Fatalf("hash: %v", err) |
| 27 | + } |
| 28 | + ownerID, err := st.CreateAdmin("owner", hash, model.RoleOwner, false) |
| 29 | + if err != nil { |
| 30 | + t.Fatalf("create owner: %v", err) |
| 31 | + } |
| 32 | + return &Manager{store: st}, ownerID |
| 33 | +} |
| 34 | + |
| 35 | +func TestCreateAdminGatesTheAssignedPassword(t *testing.T) { |
| 36 | + m, _ := rosterManager(t) |
| 37 | + |
| 38 | + a, err := m.CreateAdmin("support", "temp-password", model.RoleOperator) |
| 39 | + if err != nil { |
| 40 | + t.Fatalf("create: %v", err) |
| 41 | + } |
| 42 | + if a.Role != model.RoleOperator { |
| 43 | + t.Errorf("role = %q, want %q", a.Role, model.RoleOperator) |
| 44 | + } |
| 45 | + // The owner picked this password and sent it over a chat window: it is a |
| 46 | + // bootstrap credential, and the account is useless until it's replaced. |
| 47 | + if !a.MustChangePassword { |
| 48 | + t.Error("a password chosen by someone else did not raise the change gate") |
| 49 | + } |
| 50 | + |
| 51 | + // And choosing their own lifts it. |
| 52 | + if err := m.ChangeAdminPassword(a.ID, "chosen-by-them"); err != nil { |
| 53 | + t.Fatalf("change password: %v", err) |
| 54 | + } |
| 55 | + got, err := m.store.GetAdmin(a.ID) |
| 56 | + if err != nil { |
| 57 | + t.Fatalf("get: %v", err) |
| 58 | + } |
| 59 | + if got.MustChangePassword { |
| 60 | + t.Error("gate still up after the admin picked their own password") |
| 61 | + } |
| 62 | +} |
| 63 | + |
| 64 | +func TestCreateAdminRejectsBadInput(t *testing.T) { |
| 65 | + m, _ := rosterManager(t) |
| 66 | + |
| 67 | + if _, err := m.CreateAdmin("support", "temp-password", model.RoleOperator); err != nil { |
| 68 | + t.Fatalf("seed: %v", err) |
| 69 | + } |
| 70 | + |
| 71 | + cases := []struct { |
| 72 | + name, username, password, role string |
| 73 | + }{ |
| 74 | + {"duplicate login", "support", "temp-password", model.RoleAdmin}, |
| 75 | + {"short password", "helper", "short", model.RoleAdmin}, |
| 76 | + {"login too short", "ab", "temp-password", model.RoleAdmin}, |
| 77 | + {"login with spaces", "the helper", "temp-password", model.RoleAdmin}, |
| 78 | + {"unknown role", "helper", "temp-password", "superuser"}, |
| 79 | + // The one that matters: no path may quietly produce a second owner. |
| 80 | + {"owner is not grantable", "helper", "temp-password", model.RoleOwner}, |
| 81 | + } |
| 82 | + for _, tc := range cases { |
| 83 | + t.Run(tc.name, func(t *testing.T) { |
| 84 | + if _, err := m.CreateAdmin(tc.username, tc.password, tc.role); err == nil { |
| 85 | + t.Fatal("accepted, want rejected") |
| 86 | + } |
| 87 | + }) |
| 88 | + } |
| 89 | + admins, err := m.ListAdmins() |
| 90 | + if err != nil { |
| 91 | + t.Fatalf("list: %v", err) |
| 92 | + } |
| 93 | + if len(admins) != 2 { // owner + support, nothing the rejected calls left behind |
| 94 | + t.Fatalf("roster grew to %d, want 2", len(admins)) |
| 95 | + } |
| 96 | +} |
| 97 | + |
| 98 | +// The two moves that would strand the panel: removing the only account that can |
| 99 | +// manage the roster, or the owner removing themselves. |
| 100 | +func TestRosterCannotStrandThePanel(t *testing.T) { |
| 101 | + m, ownerID := rosterManager(t) |
| 102 | + |
| 103 | + admin, err := m.CreateAdmin("colleague", "temp-password", model.RoleAdmin) |
| 104 | + if err != nil { |
| 105 | + t.Fatalf("create: %v", err) |
| 106 | + } |
| 107 | + |
| 108 | + if err := m.DeleteAdmin(ownerID, ownerID); err == nil { |
| 109 | + t.Error("the owner deleted themselves") |
| 110 | + } |
| 111 | + if err := m.DeleteAdmin(admin.ID, ownerID); err == nil { |
| 112 | + t.Error("an admin deleted the owner") |
| 113 | + } |
| 114 | + if err := m.SetAdminRole(ownerID, ownerID, model.RoleOperator); err == nil { |
| 115 | + t.Error("the owner demoted themselves to operator") |
| 116 | + } |
| 117 | + if err := m.ResetAdminPassword(ownerID, ownerID, "new-password"); err == nil { |
| 118 | + t.Error("the owner reset their own password through the roster (bypassing re-auth)") |
| 119 | + } |
| 120 | + |
| 121 | + // The owner survived all of it and is still the owner. |
| 122 | + owner, err := m.store.GetAdmin(ownerID) |
| 123 | + if err != nil { |
| 124 | + t.Fatalf("get owner: %v", err) |
| 125 | + } |
| 126 | + if owner.Role != model.RoleOwner { |
| 127 | + t.Fatalf("owner role = %q, want %q", owner.Role, model.RoleOwner) |
| 128 | + } |
| 129 | +} |
| 130 | + |
| 131 | +func TestDeleteAdminRejectsUnknownID(t *testing.T) { |
| 132 | + m, ownerID := rosterManager(t) |
| 133 | + err := m.DeleteAdmin(ownerID, 4242) |
| 134 | + if err == nil { |
| 135 | + t.Fatal("deleted an admin that does not exist") |
| 136 | + } |
| 137 | + if !strings.Contains(err.Error(), "не найден") { |
| 138 | + t.Errorf("err = %v, want a not-found message", err) |
| 139 | + } |
| 140 | +} |
| 141 | + |
| 142 | +// Resetting a colleague's password is what you do when they are locked out — or |
| 143 | +// when you no longer trust them. Either way every session they had must die with |
| 144 | +// the old password, or the reset achieves nothing against a stolen cookie. |
| 145 | +func TestResetAdminPasswordRevokesSessionsAndRegates(t *testing.T) { |
| 146 | + m, ownerID := rosterManager(t) |
| 147 | + |
| 148 | + admin, err := m.CreateAdmin("colleague", "temp-password", model.RoleAdmin) |
| 149 | + if err != nil { |
| 150 | + t.Fatalf("create: %v", err) |
| 151 | + } |
| 152 | + if err := m.ChangeAdminPassword(admin.ID, "chosen-by-them"); err != nil { |
| 153 | + t.Fatalf("change password: %v", err) |
| 154 | + } |
| 155 | + token, err := m.store.CreateSession(admin.ID, time.Hour) |
| 156 | + if err != nil { |
| 157 | + t.Fatalf("session: %v", err) |
| 158 | + } |
| 159 | + |
| 160 | + if err := m.ResetAdminPassword(ownerID, admin.ID, "reset-by-owner"); err != nil { |
| 161 | + t.Fatalf("reset: %v", err) |
| 162 | + } |
| 163 | + if _, ok := m.store.LookupSession(token); ok { |
| 164 | + t.Error("session survived a password reset") |
| 165 | + } |
| 166 | + got, err := m.store.GetAdmin(admin.ID) |
| 167 | + if err != nil { |
| 168 | + t.Fatalf("get: %v", err) |
| 169 | + } |
| 170 | + if !got.MustChangePassword { |
| 171 | + t.Error("an owner-assigned password did not raise the change gate") |
| 172 | + } |
| 173 | + if err := m.ResetAdminPassword(ownerID, admin.ID, "short"); err == nil { |
| 174 | + t.Error("accepted a password below the minimum length") |
| 175 | + } |
| 176 | +} |
0 commit comments