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
|
package auth
import (
"context"
"errors"
"testing"
"time"
"codeberg.org/snonux/player/internal/clock"
"codeberg.org/snonux/player/internal/model"
"codeberg.org/snonux/player/internal/repository"
)
func TestBCryptHasher_HashAndCompare(t *testing.T) {
h := NewBCryptHasher(4) // low cost for speed
tests := []struct {
name string
password string
}{
{"simple password", "hello"},
{"long password", "averylongpasswordthatexceeds32charactersormore"},
{"unicode password", "пароль密码🔐"},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
hash, err := h.Hash(tt.password)
if err != nil {
t.Fatalf("hash: %v", err)
}
if hash == "" {
t.Fatal("expected non-empty hash")
}
if err := h.Compare(hash, tt.password); err != nil {
t.Fatalf("compare correct password: %v", err)
}
if err := h.Compare(hash, tt.password+"x"); err == nil {
t.Fatal("expected error for wrong password")
}
// ensure same password yields different hash (salted)
hash2, _ := h.Hash(tt.password)
if hash == hash2 {
t.Fatal("expected different hashes for same password")
}
})
}
}
func TestSessionManager_CreateSession(t *testing.T) {
now := time.Date(2026, time.January, 1, 0, 0, 0, 0, time.UTC)
clk := &clock.MockClock{T: now}
var created *model.Session
mockRepo := repository.MockSessionRepo{
CreateSessionFunc: func(ctx context.Context, session *model.Session) error {
created = session
return nil
},
}
sm := NewSessionManager(&mockRepo, clk, time.Hour)
id, err := sm.CreateSession(context.Background(), 42)
if err != nil {
t.Fatalf("create session: %v", err)
}
if id == "" {
t.Fatal("expected non-empty session id")
}
if created == nil {
t.Fatal("expected session to be created")
}
if created.UserID != 42 {
t.Fatalf("expected user id 42, got %d", created.UserID)
}
if created.ExpiresAt != now.Add(time.Hour) {
t.Fatalf("unexpected expires at: %v", created.ExpiresAt)
}
}
func TestSessionManager_ValidateSession(t *testing.T) {
now := time.Date(2026, time.January, 1, 0, 0, 0, 0, time.UTC)
clk := &clock.MockClock{T: now}
tests := []struct {
name string
returns *model.Session
returnsErr error
expectNil bool
expectDel bool
}{
{
name: "valid session",
returns: &model.Session{
ID: "abc",
UserID: 1,
ExpiresAt: now.Add(time.Hour),
CreatedAt: now.Add(-time.Hour),
},
expectNil: false,
},
{
name: "session not found",
returns: nil,
expectNil: true,
},
{
name: "expired session",
returns: &model.Session{
ID: "old",
UserID: 1,
ExpiresAt: now.Add(-time.Minute),
CreatedAt: now.Add(-time.Hour),
},
expectNil: true,
expectDel: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
var deleted string
repo := repository.MockSessionRepo{
GetSessionByIDFunc: func(ctx context.Context, id string) (*model.Session, error) {
return tt.returns, tt.returnsErr
},
DeleteSessionFunc: func(ctx context.Context, id string) error {
deleted = id
return nil
},
}
sm := NewSessionManager(&repo, clk, time.Hour)
sess, err := sm.ValidateSession(context.Background(), "testID")
if err != nil {
t.Fatalf("validate: %v", err)
}
if (sess == nil) != tt.expectNil {
t.Fatalf("expected nil=%v, got %v", tt.expectNil, sess)
}
if tt.expectDel && deleted == "" {
t.Fatal("expected expired session to be deleted")
}
if !tt.expectDel && deleted != "" {
t.Fatal("unexpected delete")
}
})
}
}
func TestSessionManager_DeleteSession(t *testing.T) {
repo := repository.MockSessionRepo{
DeleteSessionFunc: func(ctx context.Context, id string) error {
if id != "abc" {
return errors.New("unexpected id")
}
return nil
},
}
sm := NewSessionManager(&repo, &clock.MockClock{T: time.Now()}, time.Hour)
if err := sm.DeleteSession(context.Background(), "abc"); err != nil {
t.Fatalf("delete: %v", err)
}
}
func TestSessionManager_Cleanup(t *testing.T) {
now := time.Date(2026, time.January, 1, 12, 0, 0, 0, time.UTC)
clk := &clock.MockClock{T: now}
var called time.Time
repo := repository.MockSessionRepo{
DeleteExpiredSessionsFunc: func(ctx context.Context, t time.Time) error {
called = t
return nil
},
}
sm := NewSessionManager(&repo, clk, time.Hour)
if err := sm.Cleanup(context.Background()); err != nil {
t.Fatalf("cleanup: %v", err)
}
if called != now {
t.Fatalf("expected cleanup called with %v, got %v", now, called)
}
}
|