2026-04-01 11:37:55 +02:00
|
|
|
package db_test
|
|
|
|
|
|
|
|
|
|
import (
|
2026-07-23 17:03:52 +02:00
|
|
|
"context"
|
2026-04-01 11:37:55 +02:00
|
|
|
"testing"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
// ─── UpdateUserProfile tests ─────────────────────────────────────────────────
|
|
|
|
|
|
|
|
|
|
func TestUpdateUserProfile_UsernameAndAvatar(t *testing.T) {
|
|
|
|
|
database := newTestDB(t)
|
2026-07-23 17:03:52 +02:00
|
|
|
id, err := database.CreateUser(context.Background(), "profileuser", "hash", 4)
|
2026-04-01 11:37:55 +02:00
|
|
|
if err != nil {
|
|
|
|
|
t.Fatalf("CreateUser: %v", err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
avatar := "https://example.com/avatar.png"
|
2026-08-01 22:06:14 +02:00
|
|
|
if err := database.UpdateUserProfile(context.Background(), id, "newname", &avatar, nil, nil); err != nil {
|
2026-04-01 11:37:55 +02:00
|
|
|
t.Fatalf("UpdateUserProfile: %v", err)
|
|
|
|
|
}
|
|
|
|
|
|
2026-07-23 17:03:52 +02:00
|
|
|
user, err := database.GetUserByID(context.Background(), id)
|
2026-04-01 11:37:55 +02:00
|
|
|
if err != nil {
|
|
|
|
|
t.Fatalf("GetUserByID: %v", err)
|
|
|
|
|
}
|
|
|
|
|
if user.Username != "newname" {
|
|
|
|
|
t.Errorf("Username = %q, want %q", user.Username, "newname")
|
|
|
|
|
}
|
|
|
|
|
if user.Avatar == nil || *user.Avatar != avatar {
|
|
|
|
|
t.Errorf("Avatar = %v, want %q", user.Avatar, avatar)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestUpdateUserProfile_UsernameOnly(t *testing.T) {
|
|
|
|
|
database := newTestDB(t)
|
2026-07-23 17:03:52 +02:00
|
|
|
id, _ := database.CreateUser(context.Background(), "keepavatar", "hash", 4)
|
2026-04-01 11:37:55 +02:00
|
|
|
|
2026-08-01 22:06:14 +02:00
|
|
|
if err := database.UpdateUserProfile(context.Background(), id, "renamed", nil, nil, nil); err != nil {
|
2026-04-01 11:37:55 +02:00
|
|
|
t.Fatalf("UpdateUserProfile: %v", err)
|
|
|
|
|
}
|
|
|
|
|
|
2026-07-23 17:03:52 +02:00
|
|
|
user, _ := database.GetUserByID(context.Background(), id)
|
2026-04-01 11:37:55 +02:00
|
|
|
if user.Username != "renamed" {
|
|
|
|
|
t.Errorf("Username = %q, want %q", user.Username, "renamed")
|
|
|
|
|
}
|
|
|
|
|
if user.Avatar != nil {
|
|
|
|
|
t.Errorf("Avatar = %v, want nil", user.Avatar)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestUpdateUserProfile_DuplicateUsername(t *testing.T) {
|
|
|
|
|
database := newTestDB(t)
|
2026-07-23 17:03:52 +02:00
|
|
|
database.CreateUser(context.Background(), "existing", "hash", 4)
|
|
|
|
|
id2, _ := database.CreateUser(context.Background(), "changeme", "hash", 4)
|
2026-04-01 11:37:55 +02:00
|
|
|
|
2026-08-01 22:06:14 +02:00
|
|
|
err := database.UpdateUserProfile(context.Background(), id2, "existing", nil, nil, nil)
|
2026-04-01 11:37:55 +02:00
|
|
|
if err == nil {
|
|
|
|
|
t.Error("UpdateUserProfile with duplicate username should return error")
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestUpdateUserProfile_NonExistentUser(t *testing.T) {
|
|
|
|
|
database := newTestDB(t)
|
2026-08-01 22:06:14 +02:00
|
|
|
err := database.UpdateUserProfile(context.Background(), 99999, "ghost", nil, nil, nil)
|
2026-04-01 11:37:55 +02:00
|
|
|
if err == nil {
|
|
|
|
|
t.Error("UpdateUserProfile for non-existent user should return error")
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// ─── UpdateUserPassword tests ────────────────────────────────────────────────
|
|
|
|
|
|
|
|
|
|
func TestUpdateUserPassword_Success(t *testing.T) {
|
|
|
|
|
database := newTestDB(t)
|
2026-07-23 17:03:52 +02:00
|
|
|
id, _ := database.CreateUser(context.Background(), "pwuser", "oldhash", 4)
|
2026-04-01 11:37:55 +02:00
|
|
|
|
2026-07-23 17:03:52 +02:00
|
|
|
if err := database.UpdateUserPassword(context.Background(), id, "newhash"); err != nil {
|
2026-04-01 11:37:55 +02:00
|
|
|
t.Fatalf("UpdateUserPassword: %v", err)
|
|
|
|
|
}
|
|
|
|
|
|
2026-07-23 17:03:52 +02:00
|
|
|
user, _ := database.GetUserByID(context.Background(), id)
|
2026-04-01 11:37:55 +02:00
|
|
|
if user.PasswordHash != "newhash" {
|
|
|
|
|
t.Errorf("PasswordHash = %q, want %q", user.PasswordHash, "newhash")
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// ─── ListUserSessions tests ─────────────────────────────────────────────────
|
|
|
|
|
|
|
|
|
|
func TestListUserSessions_ReturnsSessions(t *testing.T) {
|
|
|
|
|
database := newTestDB(t)
|
2026-07-23 17:03:52 +02:00
|
|
|
uid, _ := database.CreateUser(context.Background(), "sessuser", "hash", 4)
|
2026-04-01 11:37:55 +02:00
|
|
|
|
2026-07-23 17:03:52 +02:00
|
|
|
database.CreateSession(context.Background(), uid, "tok1", "Chrome", "1.2.3.4")
|
|
|
|
|
database.CreateSession(context.Background(), uid, "tok2", "Firefox", "5.6.7.8")
|
2026-04-01 11:37:55 +02:00
|
|
|
|
2026-07-23 17:03:52 +02:00
|
|
|
sessions, err := database.ListUserSessions(context.Background(), uid)
|
2026-04-01 11:37:55 +02:00
|
|
|
if err != nil {
|
|
|
|
|
t.Fatalf("ListUserSessions: %v", err)
|
|
|
|
|
}
|
|
|
|
|
if len(sessions) != 2 {
|
|
|
|
|
t.Errorf("len(sessions) = %d, want 2", len(sessions))
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestListUserSessions_EmptyArray(t *testing.T) {
|
|
|
|
|
database := newTestDB(t)
|
2026-07-23 17:03:52 +02:00
|
|
|
uid, _ := database.CreateUser(context.Background(), "nosess", "hash", 4)
|
2026-04-01 11:37:55 +02:00
|
|
|
|
2026-07-23 17:03:52 +02:00
|
|
|
sessions, err := database.ListUserSessions(context.Background(), uid)
|
2026-04-01 11:37:55 +02:00
|
|
|
if err != nil {
|
|
|
|
|
t.Fatalf("ListUserSessions: %v", err)
|
|
|
|
|
}
|
|
|
|
|
if sessions == nil {
|
|
|
|
|
t.Error("ListUserSessions should return empty slice, not nil")
|
|
|
|
|
}
|
|
|
|
|
if len(sessions) != 0 {
|
|
|
|
|
t.Errorf("len(sessions) = %d, want 0", len(sessions))
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestListUserSessions_DoesNotReturnOtherUsers(t *testing.T) {
|
|
|
|
|
database := newTestDB(t)
|
2026-07-23 17:03:52 +02:00
|
|
|
uid1, _ := database.CreateUser(context.Background(), "user1", "hash", 4)
|
|
|
|
|
uid2, _ := database.CreateUser(context.Background(), "user2", "hash", 4)
|
2026-04-01 11:37:55 +02:00
|
|
|
|
2026-07-23 17:03:52 +02:00
|
|
|
database.CreateSession(context.Background(), uid1, "tok-u1", "Chrome", "1.2.3.4")
|
|
|
|
|
database.CreateSession(context.Background(), uid2, "tok-u2", "Firefox", "5.6.7.8")
|
2026-04-01 11:37:55 +02:00
|
|
|
|
2026-07-23 17:03:52 +02:00
|
|
|
sessions, _ := database.ListUserSessions(context.Background(), uid1)
|
2026-04-01 11:37:55 +02:00
|
|
|
if len(sessions) != 1 {
|
|
|
|
|
t.Errorf("len(sessions) = %d, want 1", len(sessions))
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// ─── DeleteSessionByID tests ─────────────────────────────────────────────────
|
|
|
|
|
|
|
|
|
|
func TestDeleteSessionByID_Success(t *testing.T) {
|
|
|
|
|
database := newTestDB(t)
|
2026-07-23 17:03:52 +02:00
|
|
|
uid, _ := database.CreateUser(context.Background(), "delsess", "hash", 4)
|
|
|
|
|
sessID, _ := database.CreateSession(context.Background(), uid, "deltok", "Chrome", "1.2.3.4")
|
2026-04-01 11:37:55 +02:00
|
|
|
|
2026-07-23 17:03:52 +02:00
|
|
|
err := database.DeleteSessionByID(context.Background(), sessID, uid)
|
2026-04-01 11:37:55 +02:00
|
|
|
if err != nil {
|
|
|
|
|
t.Fatalf("DeleteSessionByID: %v", err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Session should be gone.
|
2026-07-23 17:03:52 +02:00
|
|
|
sess, _ := database.GetSessionByTokenHash(context.Background(), "deltok")
|
2026-04-01 11:37:55 +02:00
|
|
|
if sess != nil {
|
|
|
|
|
t.Error("session should have been deleted")
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestDeleteSessionByID_WrongOwner(t *testing.T) {
|
|
|
|
|
database := newTestDB(t)
|
2026-07-23 17:03:52 +02:00
|
|
|
uid1, _ := database.CreateUser(context.Background(), "owner1", "hash", 4)
|
|
|
|
|
uid2, _ := database.CreateUser(context.Background(), "owner2", "hash", 4)
|
|
|
|
|
sessID, _ := database.CreateSession(context.Background(), uid1, "ownertok", "Chrome", "1.2.3.4")
|
2026-04-01 11:37:55 +02:00
|
|
|
|
2026-07-23 17:03:52 +02:00
|
|
|
err := database.DeleteSessionByID(context.Background(), sessID, uid2)
|
2026-04-01 11:37:55 +02:00
|
|
|
if err == nil {
|
|
|
|
|
t.Error("DeleteSessionByID should fail when user does not own the session")
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestDeleteSessionByID_NotFound(t *testing.T) {
|
|
|
|
|
database := newTestDB(t)
|
2026-07-23 17:03:52 +02:00
|
|
|
uid, _ := database.CreateUser(context.Background(), "delnf", "hash", 4)
|
2026-04-01 11:37:55 +02:00
|
|
|
|
2026-07-23 17:03:52 +02:00
|
|
|
err := database.DeleteSessionByID(context.Background(), 99999, uid)
|
2026-04-01 11:37:55 +02:00
|
|
|
if err == nil {
|
|
|
|
|
t.Error("DeleteSessionByID should fail for non-existent session")
|
|
|
|
|
}
|
|
|
|
|
}
|