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
|
package service
import (
"context"
"errors"
"testing"
"time"
"codeberg.org/snonux/player/internal/clock"
"codeberg.org/snonux/player/internal/model"
"codeberg.org/snonux/player/internal/repository"
)
type fixedTokenManager struct {
plaintext string
hash string
}
func (m fixedTokenManager) Generate() (string, string, error) {
return m.plaintext, m.hash, nil
}
func (m fixedTokenManager) Hash(plaintext string) string {
if plaintext == m.plaintext {
return m.hash
}
return "unknown"
}
func TestAuthService_APITokenCRUD(t *testing.T) {
ctx := context.Background()
now := time.Date(2026, 5, 17, 12, 0, 0, 0, time.UTC)
expiresAt := now.Add(time.Hour)
clk := &clock.MockClock{T: now}
var created *model.APIToken
var deletedID int64
store := &repository.MockStore{
APITokenRepo: repository.MockAPITokenRepo{
CreateFunc: func(ctx context.Context, token *model.APIToken) (int64, error) {
created = token
return 7, nil
},
ListByUserFunc: func(ctx context.Context, userID int64) ([]model.APIToken, error) {
if userID != 42 {
return nil, nil
}
return []model.APIToken{{ID: 7, UserID: userID, Name: "automation"}}, nil
},
DeleteByIDFunc: func(ctx context.Context, id int64) error {
deletedID = id
return nil
},
},
}
svc := NewAuthService(store, clk, nil, nil, fixedTokenManager{plaintext: "plain", hash: "hashed"})
result, err := svc.CreateAPIToken(ctx, 42, "automation", &expiresAt)
if err != nil {
t.Fatalf("create api token: %v", err)
}
if result.Plaintext != "plain" || result.Token.ID != 7 {
t.Fatalf("unexpected result: %#v", result)
}
if created == nil || created.UserID != 42 || created.TokenHash != "hashed" || created.CreatedAt != now {
t.Fatalf("unexpected created token: %#v", created)
}
tokens, err := svc.ListAPITokens(ctx, 42)
if err != nil {
t.Fatalf("list api tokens: %v", err)
}
if len(tokens) != 1 || tokens[0].ID != 7 {
t.Fatalf("unexpected tokens: %#v", tokens)
}
if err := svc.RevokeAPIToken(ctx, 42, 7); err != nil {
t.Fatalf("revoke api token: %v", err)
}
if deletedID != 7 {
t.Fatalf("expected delete id 7, got %d", deletedID)
}
if err := svc.RevokeAPIToken(ctx, 42, 8); !errors.Is(err, ErrNotFound) {
t.Fatalf("expected ErrNotFound, got %v", err)
}
}
func TestAuthService_AuthenticateBearer(t *testing.T) {
ctx := context.Background()
now := time.Date(2026, 5, 17, 12, 0, 0, 0, time.UTC)
createdAt := now.Add(-time.Hour)
expiresAt := now.Add(time.Hour)
expiredAt := now.Add(-time.Second)
tests := []struct {
name string
token *model.APIToken
plaintext string
wantErr error
wantTouch bool
}{
{
name: "valid",
token: &model.APIToken{ID: 5, UserID: 42, CreatedAt: createdAt, ExpiresAt: &expiresAt},
plaintext: "plain",
wantTouch: true,
},
{
name: "revoked",
plaintext: "plain",
wantErr: ErrInvalidCredentials,
},
{
name: "expired",
token: &model.APIToken{ID: 5, UserID: 42, CreatedAt: createdAt, ExpiresAt: &expiredAt},
plaintext: "plain",
wantErr: ErrInvalidCredentials,
},
{
name: "empty",
wantErr: ErrInvalidCredentials,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
touched := false
store := &repository.MockStore{
APITokenRepo: repository.MockAPITokenRepo{
GetByHashFunc: func(ctx context.Context, tokenHash string) (*model.APIToken, error) {
if tokenHash != "hashed" {
t.Fatalf("unexpected hash: %q", tokenHash)
}
return tt.token, nil
},
TouchLastUsedFunc: func(ctx context.Context, id int64, lastUsedAt time.Time) error {
touched = true
if id != 5 || lastUsedAt != now {
t.Fatalf("unexpected touch: id=%d at=%v", id, lastUsedAt)
}
return nil
},
},
}
svc := NewAuthService(store, &clock.MockClock{T: now}, nil, nil, fixedTokenManager{plaintext: "plain", hash: "hashed"})
sess, err := svc.AuthenticateBearer(ctx, tt.plaintext)
if !errors.Is(err, tt.wantErr) {
t.Fatalf("expected error %v, got %v", tt.wantErr, err)
}
if touched != tt.wantTouch {
t.Fatalf("expected touched=%v, got %v", tt.wantTouch, touched)
}
if tt.wantErr != nil {
return
}
if sess == nil || sess.ID != "api-token:5" || sess.UserID != 42 || sess.ExpiresAt != expiresAt {
t.Fatalf("unexpected session: %#v", sess)
}
})
}
}
|