-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhandler_verify_test.go
More file actions
230 lines (201 loc) · 4.71 KB
/
handler_verify_test.go
File metadata and controls
230 lines (201 loc) · 4.71 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
package caddyaltcha
import (
"net/http"
"net/http/httptest"
"testing"
"time"
"github.com/caddyserver/caddy/v2"
"github.com/caddyserver/caddy/v2/modules/caddyhttp"
)
func TestVerifyHandler_Provision(t *testing.T) {
h := &VerifyHandler{
HMACKey: "test-key-minimum-32-characters-long",
}
// Create a proper Caddy context
ctx := caddy.Context{}
err := h.Provision(ctx)
if err != nil {
t.Fatalf("Provision failed: %v", err)
}
// Check defaults were set
if h.SessionTTL == 0 {
t.Error("SessionTTL should have a default value")
}
if h.VerifyFieldName == "" {
t.Error("VerifyFieldName should have a default value")
}
if h.sessionBackend == nil {
t.Error("sessionBackend should be initialized")
}
}
func TestVerifyHandler_Validate(t *testing.T) {
tests := []struct {
name string
handler *VerifyHandler
wantErr bool
}{
{
name: "valid config",
handler: &VerifyHandler{
HMACKey: "test-key-minimum-32-characters-long",
SessionTTL: caddy.Duration(5 * time.Minute),
},
wantErr: false,
},
{
name: "missing hmac_key",
handler: &VerifyHandler{
SessionTTL: caddy.Duration(5 * time.Minute),
},
wantErr: true,
},
{
name: "session_ttl too short",
handler: &VerifyHandler{
HMACKey: "test-key-minimum-32-characters-long",
SessionTTL: caddy.Duration(30 * time.Second),
},
wantErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
// Provision first
ctx := caddy.Context{}
tt.handler.Provision(ctx)
err := tt.handler.Validate()
if (err != nil) != tt.wantErr {
t.Errorf("Validate() error = %v, wantErr %v", err, tt.wantErr)
}
})
}
}
func TestVerifyHandler_HasValidVerificationCookie(t *testing.T) {
h := &VerifyHandler{
HMACKey: "test-key-minimum-32-characters-long",
}
ctx := caddy.Context{}
h.Provision(ctx)
tests := []struct {
name string
setCookie bool
cookieName string
want bool
}{
{
name: "valid cookie",
setCookie: true,
cookieName: "altcha_verified",
want: true,
},
{
name: "no cookie",
setCookie: false,
want: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
req := httptest.NewRequest("GET", "/test", nil)
if tt.setCookie {
req.AddCookie(&http.Cookie{
Name: tt.cookieName,
Value: "test-value",
})
}
got := h.hasValidVerificationCookie(req)
if got != tt.want {
t.Errorf("hasValidVerificationCookie() = %v, want %v", got, tt.want)
}
})
}
}
func TestVerifyHandler_ExtractSolution(t *testing.T) {
h := &VerifyHandler{
VerifyFieldName: "altcha",
}
tests := []struct {
name string
setupReq func() *http.Request
want string
}{
{
name: "from query parameter",
setupReq: func() *http.Request {
return httptest.NewRequest("GET", "/test?altcha=solution123", nil)
},
want: "solution123",
},
{
name: "from header",
setupReq: func() *http.Request {
req := httptest.NewRequest("GET", "/test", nil)
req.Header.Set("X-Altcha-Solution", "solution456")
return req
},
want: "solution456",
},
{
name: "no solution",
setupReq: func() *http.Request {
return httptest.NewRequest("GET", "/test", nil)
},
want: "",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
req := tt.setupReq()
got := h.extractSolution(req)
if got != tt.want {
t.Errorf("extractSolution() = %v, want %v", got, tt.want)
}
})
}
}
func TestGenerateSessionID(t *testing.T) {
// Test that we can generate session IDs
id1, err := GenerateSessionID()
if err != nil {
t.Fatalf("GenerateSessionID() error = %v", err)
}
if len(id1) == 0 {
t.Error("GenerateSessionID() returned empty string")
}
// Generate another and ensure they're different
id2, err := GenerateSessionID()
if err != nil {
t.Fatalf("GenerateSessionID() error = %v", err)
}
if id1 == id2 {
t.Error("GenerateSessionID() returned duplicate IDs")
}
}
func TestGenerateCookieValue(t *testing.T) {
// Test that we can generate cookie values
val1, err := GenerateCookieValue()
if err != nil {
t.Fatalf("GenerateCookieValue() error = %v", err)
}
if len(val1) == 0 {
t.Error("GenerateCookieValue() returned empty string")
}
// Generate another and ensure they're different
val2, err := GenerateCookieValue()
if err != nil {
t.Fatalf("GenerateCookieValue() error = %v", err)
}
if val1 == val2 {
t.Error("GenerateCookieValue() returned duplicate values")
}
}
// Mock next handler for testing
type mockHandler struct {
called bool
}
func (m *mockHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) error {
m.called = true
w.WriteHeader(http.StatusOK)
return nil
}
var _ caddyhttp.Handler = (*mockHandler)(nil)