-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathregistration_test.go
148 lines (122 loc) · 3.96 KB
/
registration_test.go
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
package webauthn
import (
"bytes"
"encoding/base64"
"encoding/json"
"fmt"
"net/http"
"net/http/httptest"
"testing"
"time"
"github.com/go-webauthn/webauthn/webauthn"
"github.com/gofiber/fiber/v2"
"github.com/stretchr/testify/assert"
)
func TestBeginRegistration(t *testing.T) {
// Setup
app := fiber.New()
config := Config{
RPDisplayName: "Test App",
RPID: "localhost",
RPOrigins: []string{"http://localhost"},
}
mw := New(config)
app.Post("/register/begin", mw.BeginRegistration("test-user", "testuser", "Test User"))
// Test case 1: Successful registration initiation
req := httptest.NewRequest("POST", "/register/begin", nil)
resp, err := app.Test(req)
assert.NoError(t, err)
assert.Equal(t, fiber.StatusOK, resp.StatusCode)
var result map[string]interface{}
err = json.NewDecoder(resp.Body).Decode(&result)
assert.NoError(t, err)
// Verify response contains required fields
assert.Contains(t, result, "publicKey")
publicKey := result["publicKey"].(map[string]interface{})
assert.Contains(t, publicKey, "challenge")
assert.Contains(t, publicKey, "rp")
assert.Contains(t, publicKey, "user")
}
func TestFinishRegistration(t *testing.T) {
// Setup
app := fiber.New()
config := Config{
RPDisplayName: "Test App",
RPID: "localhost",
RPOrigins: []string{"http://localhost"},
CredentialStore: &mockCredentialStore{}, // Add mock store
}
mw := New(config)
// Store test session
sessionID := "test-session"
sessionData := &webauthn.SessionData{
Challenge: "test-challenge",
UserID: []byte("test-user"),
}
err := mw.sessions.StoreSession(sessionID, &SessionData{
UserID: "test-user",
SessionData: *sessionData,
ExpiresAt: time.Now().Add(5 * time.Minute),
})
assert.NoError(t, err)
app.Post("/register/finish", mw.FinishRegistration())
// Create mock attestation response
mockResponse := createMockAttestationResponse(t, sessionData.Challenge)
// Test case 1: Successful registration completion
req := httptest.NewRequest("POST", "/register/finish", bytes.NewReader(mockResponse))
req.AddCookie(&http.Cookie{
Name: "webauthn_session",
Value: sessionID,
})
resp, err := app.Test(req)
assert.NoError(t, err)
assert.Equal(t, fiber.StatusOK, resp.StatusCode)
var result map[string]interface{}
err = json.NewDecoder(resp.Body).Decode(&result)
assert.NoError(t, err)
// Verify response structure
assert.Equal(t, "success", result["status"])
assert.Contains(t, result, "credential")
}
// Mock credential store for testing
type mockCredentialStore struct {
credentials map[string][]*Credential
}
func (m *mockCredentialStore) StoreCredential(userID string, cred *Credential) error {
if m.credentials == nil {
m.credentials = make(map[string][]*Credential)
}
m.credentials[userID] = append(m.credentials[userID], cred)
return nil
}
func (m *mockCredentialStore) GetCredential(credentialID []byte) (*Credential, error) {
// Implementation for testing
return nil, nil
}
func (m *mockCredentialStore) GetCredentialsByUser(userID string) ([]*Credential, error) {
return m.credentials[userID], nil
}
func (m *mockCredentialStore) UpdateCredential(cred *Credential) error {
return nil
}
// Helper function to create mock attestation response
func createMockAttestationResponse(t *testing.T, challenge string) []byte {
// Create a minimal mock response
// In real tests, this would be more complete
response := map[string]interface{}{
"id": base64.URLEncoding.EncodeToString([]byte("test-credential-id")),
"rawId": base64.URLEncoding.EncodeToString([]byte("test-credential-id")),
"type": "public-key",
"response": map[string]interface{}{
"attestationObject": base64.URLEncoding.EncodeToString([]byte("test-attestation")),
"clientDataJSON": base64.URLEncoding.EncodeToString([]byte(fmt.Sprintf(`{
"type": "webauthn.create",
"challenge": "%s",
"origin": "http://localhost"
}`, challenge))),
},
}
data, err := json.Marshal(response)
assert.NoError(t, err)
return data
}