-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathinitial_admin_test.go
More file actions
231 lines (192 loc) · 6.82 KB
/
Copy pathinitial_admin_test.go
File metadata and controls
231 lines (192 loc) · 6.82 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
package main
import (
"bytes"
"log"
"os"
"path/filepath"
"strings"
"testing"
"necore/dao"
"necore/database"
)
// setupEmptyUserEnv prepares an isolated working directory with an empty user
// database (all other databases are also migrated so ConnectSqlite does not
// fail) and chdir into it. Cleanup restores the previous working directory and
// closes all opened SQLite connections so the temp dir can be removed.
func setupEmptyUserEnv(t *testing.T) string {
t.Helper()
tmpDir := t.TempDir()
must(t, os.MkdirAll(filepath.Join(tmpDir, "data"), 0o755))
must(t, os.MkdirAll(filepath.Join(tmpDir, "contents"), 0o755))
envContent := "SECRET=unit-test-secret\nBOT_LOG_BUFFER_SIZE=100\n"
must(t, os.WriteFile(filepath.Join(tmpDir, ".env"), []byte(envContent), 0o600))
oldWd, err := os.Getwd()
must(t, err)
must(t, os.Chdir(tmpDir))
t.Cleanup(func() {
_ = os.Chdir(oldWd)
})
t.Setenv("SECRET", "unit-test-secret")
t.Setenv("BOT_LOG_BUFFER_SIZE", "100")
database.ConnectSqlite()
setGormLoggerSilent(database.GetUserDatabase())
setGormLoggerSilent(database.GetArticleDatabase())
setGormLoggerSilent(database.GetServerDatabase())
setGormLoggerSilent(database.GetDocumentDatabase())
setGormLoggerSilent(database.GetBotTokenDatabase())
t.Cleanup(func() {
closeGormDB(t, database.GetUserDatabase())
closeGormDB(t, database.GetArticleDatabase())
closeGormDB(t, database.GetServerDatabase())
closeGormDB(t, database.GetDocumentDatabase())
closeGormDB(t, database.GetBotTokenDatabase())
})
return tmpDir
}
// captureLogOutput redirects the standard logger output to a buffer for the
// duration of fn. Go test functions run sequentially by default (none of the
// tests in this file call t.Parallel), so the global swap is safe.
func captureLogOutput(t *testing.T, fn func()) string {
t.Helper()
var buf bytes.Buffer
oldOutput := log.Writer()
oldFlags := log.Flags()
log.SetOutput(&buf)
log.SetFlags(0)
defer func() {
log.SetOutput(oldOutput)
log.SetFlags(oldFlags)
}()
fn()
return buf.String()
}
// TestEnsureInitialAdmin_CreatesAdminWhenEmpty verifies that on a freshly
// initialized user database, EnsureInitialAdmin creates exactly one "admin"
// user with the admin group, prints the credentials once, and is a no-op on
// subsequent invocations.
func TestEnsureInitialAdmin_CreatesAdminWhenEmpty(t *testing.T) {
setupEmptyUserEnv(t)
count, err := dao.GetUserCount()
must(t, err)
if count != 0 {
t.Fatalf("expected empty user database, got count=%d", count)
}
output := captureLogOutput(t, func() {
must(t, dao.EnsureInitialAdmin())
})
if !strings.Contains(output, "admin") {
t.Fatalf("expected log output to mention admin, got %q", output)
}
if !strings.Contains(output, "password") {
t.Fatalf("expected log output to mention password, got %q", output)
}
// Running again should be a no-op: no additional user, no log output.
output2 := captureLogOutput(t, func() {
must(t, dao.EnsureInitialAdmin())
})
if output2 != "" {
t.Fatalf("expected no log output on repeated call, got %q", output2)
}
users, err := dao.GetAllUsers()
must(t, err)
if len(users) != 1 {
t.Fatalf("expected exactly 1 user, got %d", len(users))
}
if users[0].Username != "admin" {
t.Fatalf("expected admin user, got %q", users[0].Username)
}
if !dao.ContainsGroup(users[0].Group, "admin") {
t.Fatalf("expected admin group, got %q", users[0].Group)
}
if users[0].Password == "" {
t.Fatalf("admin password hash is empty")
}
}
// TestEnsureInitialAdmin_NoOpWhenPopulated verifies EnsureInitialAdmin does
// nothing when at least one active user already exists.
func TestEnsureInitialAdmin_NoOpWhenPopulated(t *testing.T) {
setupEmptyUserEnv(t)
must(t, dao.AddUserByUsername("someone", "some-pass"))
output := captureLogOutput(t, func() {
must(t, dao.EnsureInitialAdmin())
})
if output != "" {
t.Fatalf("expected no log output when users exist, got %q", output)
}
users, err := dao.GetAllUsers()
must(t, err)
if len(users) != 1 {
t.Fatalf("expected exactly 1 user, got %d", len(users))
}
if users[0].Username != "someone" {
t.Fatalf("expected only the pre-existing user, got %q", users[0].Username)
}
}
// TestEnsureInitialAdmin_RevivesSoftDeletedAdmin documents that when an admin
// was previously soft-deleted (active count is 0), EnsureInitialAdmin falls
// into the creation path, where AddAdminUser revives the row, increments
// token_version, and restores the admin group.
func TestEnsureInitialAdmin_RevivesSoftDeletedAdmin(t *testing.T) {
setupEmptyUserEnv(t)
must(t, dao.AddAdminUser("admin", "first-pass"))
must(t, dao.DeleteUserByUsername("admin"))
output := captureLogOutput(t, func() {
must(t, dao.EnsureInitialAdmin())
})
if !strings.Contains(output, "admin") {
t.Fatalf("expected log output to mention admin, got %q", output)
}
users, err := dao.GetAllUsers()
must(t, err)
if len(users) != 1 {
t.Fatalf("expected exactly 1 active user, got %d", len(users))
}
if users[0].Username != "admin" {
t.Fatalf("expected admin user, got %q", users[0].Username)
}
if !dao.ContainsGroup(users[0].Group, "admin") {
t.Fatalf("expected admin group, got %q", users[0].Group)
}
if users[0].TokenVersion != 2 {
t.Fatalf("expected token_version=2 after revival, got %d", users[0].TokenVersion)
}
}
// TestAddAdminUser_DuplicateActive verifies AddAdminUser rejects creating an
// admin that already exists as an active user.
func TestAddAdminUser_DuplicateActive(t *testing.T) {
setupEmptyUserEnv(t)
must(t, dao.AddAdminUser("admin", "first-pass"))
if err := dao.AddAdminUser("admin", "second-pass"); err == nil {
t.Fatalf("expected error creating duplicate admin, got nil")
}
}
// TestAddAdminUser_EmptyInputs verifies basic input validation.
func TestAddAdminUser_EmptyInputs(t *testing.T) {
setupEmptyUserEnv(t)
if err := dao.AddAdminUser("", "pass"); err == nil {
t.Fatalf("expected error for empty username, got nil")
}
if err := dao.AddAdminUser("admin", ""); err == nil {
t.Fatalf("expected error for empty password, got nil")
}
}
// TestGetUserCount_ExcludesSoftDeleted verifies Count reflects only active
// (non-soft-deleted) users, which is what EnsureInitialAdmin relies on.
func TestGetUserCount_ExcludesSoftDeleted(t *testing.T) {
setupEmptyUserEnv(t)
if count, err := dao.GetUserCount(); err != nil || count != 0 {
t.Fatalf("expected 0 users, got %d (err=%v)", count, err)
}
must(t, dao.AddUserByUsername("a", "p1"))
if count, err := dao.GetUserCount(); err != nil || count != 1 {
t.Fatalf("expected 1 user, got %d (err=%v)", count, err)
}
must(t, dao.AddUserByUsername("b", "p2"))
if count, err := dao.GetUserCount(); err != nil || count != 2 {
t.Fatalf("expected 2 users, got %d (err=%v)", count, err)
}
must(t, dao.DeleteUserByUsername("a"))
if count, err := dao.GetUserCount(); err != nil || count != 1 {
t.Fatalf("expected 1 user after soft delete, got %d (err=%v)", count, err)
}
}