add IP to sessions, IP+hostname to clients

- Add ip column to sessions table (real client IP of session creator)
- Add ip and hostname columns to clients table (per-connection tracking)
- Update CreateSession, RegisterUser, LoginUser to store new fields
- Add GetClientHostInfo query method
- Update SessionHostInfo to include IP
- Extract executeCreateSession to fix funlen lint
- Add tests for session IP, client IP/hostname, login client tracking
- Update README with new field documentation
This commit is contained in:
user
2026-03-17 08:52:50 -07:00
parent c4652728b8
commit 0fef7929ad
8 changed files with 261 additions and 56 deletions

View File

@@ -34,7 +34,7 @@ func TestCreateSession(t *testing.T) {
ctx := t.Context()
sessionID, _, token, err := database.CreateSession(
ctx, "alice", "", "",
ctx, "alice", "", "", "",
)
if err != nil {
t.Fatal(err)
@@ -45,7 +45,7 @@ func TestCreateSession(t *testing.T) {
}
_, _, dupToken, dupErr := database.CreateSession(
ctx, "alice", "", "",
ctx, "alice", "", "", "",
)
if dupErr == nil {
t.Fatal("expected error for duplicate nick")
@@ -65,7 +65,7 @@ func assertSessionHostInfo(
t.Helper()
sessionID, _, _, err := database.CreateSession(
t.Context(), nick, inputUser, inputHost,
t.Context(), nick, inputUser, inputHost, "",
)
if err != nil {
t.Fatal(err)
@@ -118,6 +118,69 @@ func TestCreateSessionDefaultUsername(t *testing.T) {
)
}
func TestCreateSessionStoresIP(t *testing.T) {
t.Parallel()
database := setupTestDB(t)
ctx := t.Context()
sessionID, clientID, _, err := database.CreateSession(
ctx, "ipuser", "ident", "host.example.com",
"192.168.1.42",
)
if err != nil {
t.Fatal(err)
}
info, err := database.GetSessionHostInfo(
ctx, sessionID,
)
if err != nil {
t.Fatal(err)
}
if info.IP != "192.168.1.42" {
t.Fatalf(
"expected session IP 192.168.1.42, got %s",
info.IP,
)
}
clientInfo, err := database.GetClientHostInfo(
ctx, clientID,
)
if err != nil {
t.Fatal(err)
}
if clientInfo.IP != "192.168.1.42" {
t.Fatalf(
"expected client IP 192.168.1.42, got %s",
clientInfo.IP,
)
}
if clientInfo.Hostname != "host.example.com" {
t.Fatalf(
"expected client hostname host.example.com, got %s",
clientInfo.Hostname,
)
}
}
func TestGetClientHostInfoNotFound(t *testing.T) {
t.Parallel()
database := setupTestDB(t)
_, err := database.GetClientHostInfo(
t.Context(), 99999,
)
if err == nil {
t.Fatal("expected error for nonexistent client")
}
}
func TestGetSessionHostInfoNotFound(t *testing.T) {
t.Parallel()
@@ -183,7 +246,7 @@ func TestChannelMembersIncludeUserHost(t *testing.T) {
ctx := t.Context()
sid, _, _, err := database.CreateSession(
ctx, "memuser", "myuser", "myhost.net",
ctx, "memuser", "myuser", "myhost.net", "",
)
if err != nil {
t.Fatal(err)
@@ -233,7 +296,7 @@ func TestGetSessionByToken(t *testing.T) {
database := setupTestDB(t)
ctx := t.Context()
_, _, token, err := database.CreateSession(ctx, "bob", "", "")
_, _, token, err := database.CreateSession(ctx, "bob", "", "", "")
if err != nil {
t.Fatal(err)
}
@@ -266,7 +329,7 @@ func TestGetSessionByNick(t *testing.T) {
ctx := t.Context()
charlieID, charlieClientID, charlieToken, err :=
database.CreateSession(ctx, "charlie", "", "")
database.CreateSession(ctx, "charlie", "", "", "")
if err != nil {
t.Fatal(err)
}
@@ -323,7 +386,7 @@ func TestJoinAndPart(t *testing.T) {
database := setupTestDB(t)
ctx := t.Context()
sid, _, _, err := database.CreateSession(ctx, "user1", "", "")
sid, _, _, err := database.CreateSession(ctx, "user1", "", "", "")
if err != nil {
t.Fatal(err)
}
@@ -372,7 +435,7 @@ func TestDeleteChannelIfEmpty(t *testing.T) {
t.Fatal(err)
}
sid, _, _, err := database.CreateSession(ctx, "temp", "", "")
sid, _, _, err := database.CreateSession(ctx, "temp", "", "", "")
if err != nil {
t.Fatal(err)
}
@@ -407,7 +470,7 @@ func createSessionWithChannels(
ctx := t.Context()
sid, _, _, err := database.CreateSession(ctx, nick, "", "")
sid, _, _, err := database.CreateSession(ctx, nick, "", "", "")
if err != nil {
t.Fatal(err)
}
@@ -490,7 +553,7 @@ func TestChangeNick(t *testing.T) {
ctx := t.Context()
sid, _, token, err := database.CreateSession(
ctx, "old", "", "",
ctx, "old", "", "", "",
)
if err != nil {
t.Fatal(err)
@@ -574,7 +637,7 @@ func TestPollMessages(t *testing.T) {
ctx := t.Context()
sid, _, token, err := database.CreateSession(
ctx, "poller", "", "",
ctx, "poller", "", "", "",
)
if err != nil {
t.Fatal(err)
@@ -681,7 +744,7 @@ func TestDeleteSession(t *testing.T) {
ctx := t.Context()
sid, _, _, err := database.CreateSession(
ctx, "deleteme", "", "",
ctx, "deleteme", "", "", "",
)
if err != nil {
t.Fatal(err)
@@ -721,12 +784,12 @@ func TestChannelMembers(t *testing.T) {
database := setupTestDB(t)
ctx := t.Context()
sid1, _, _, err := database.CreateSession(ctx, "m1", "", "")
sid1, _, _, err := database.CreateSession(ctx, "m1", "", "", "")
if err != nil {
t.Fatal(err)
}
sid2, _, _, err := database.CreateSession(ctx, "m2", "", "")
sid2, _, _, err := database.CreateSession(ctx, "m2", "", "", "")
if err != nil {
t.Fatal(err)
}
@@ -784,7 +847,7 @@ func TestEnqueueToClient(t *testing.T) {
ctx := t.Context()
_, _, token, err := database.CreateSession(
ctx, "enqclient", "", "",
ctx, "enqclient", "", "", "",
)
if err != nil {
t.Fatal(err)