This repository was archived by the owner on May 26, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathauth_test.go
More file actions
60 lines (49 loc) · 1.46 KB
/
auth_test.go
File metadata and controls
60 lines (49 loc) · 1.46 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
package main
import (
"testing"
"time"
"github.com/google/uuid"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestAuthManager(t *testing.T) {
authManager := NewAuthManager()
require.NotNil(t, authManager)
// Generate a challenge
challenge, err := authManager.GenerateChallenge("addr")
require.NoError(t, err)
require.NotEmpty(t, challenge)
// Verify challenge exists
authManager.challengesMu.RLock()
savedChallenge, exists := authManager.challenges[challenge]
authManager.challengesMu.RUnlock()
require.True(t, exists)
assert.False(t, savedChallenge.Completed)
}
func TestAuthManagerSessionManagement(t *testing.T) {
am := &AuthManager{
challenges: make(map[uuid.UUID]*Challenge),
challengeTTL: 250 * time.Millisecond,
authSessions: make(map[string]time.Time),
sessionTTL: 500 * time.Millisecond,
cleanupTicker: time.NewTicker(10 * time.Minute),
maxChallenges: 1000,
}
// Add a test session
testAddr := "0x1234567890123456789012345678901234567890"
am.registerAuthSession(testAddr)
// Verify session is valid
valid := am.ValidateSession(testAddr)
assert.True(t, valid)
// Update session
time.Sleep(125 * time.Millisecond)
updated := am.UpdateSession(testAddr)
assert.True(t, updated)
// Verify still valid
valid = am.ValidateSession(testAddr)
assert.True(t, valid)
// Wait for session to expire
time.Sleep(500 * time.Millisecond)
valid = am.ValidateSession(testAddr)
assert.False(t, valid)
}