Extract testutil helper function

This commit is contained in:
Joshua Casey
2024-11-01 13:55:29 -05:00
parent 9994e033b2
commit 09ca7920ea
3 changed files with 16 additions and 18 deletions

View File

@@ -1955,15 +1955,7 @@ func TestCallbackEndpoint(t *testing.T) {
}
if test.wantAuditLogs != nil {
var encodedStateParam stateparam.Encoded
if test.path != "" {
var path *url.URL
path, err = url.Parse(test.path)
require.NoError(t, err)
encodedStateParam = stateparam.Encoded(path.Query().Get("state"))
}
wantAuditLogs := test.wantAuditLogs(encodedStateParam, sessionID)
wantAuditLogs := test.wantAuditLogs(testutil.GetStateParam(t, test.path), sessionID)
testutil.CompareAuditLogs(t, wantAuditLogs, log.String())
}
})

View File

@@ -464,15 +464,7 @@ func TestLoginEndpoint(t *testing.T) {
require.Equal(t, test.wantBody, rsp.Body.String())
if test.wantAuditLogs != nil {
var encodedStateParam stateparam.Encoded
if test.path != "" {
var path *url.URL
path, err = url.Parse(test.path)
require.NoError(t, err)
encodedStateParam = stateparam.Encoded(path.Query().Get("state"))
}
wantAuditLogs := test.wantAuditLogs(encodedStateParam)
wantAuditLogs := test.wantAuditLogs(testutil.GetStateParam(t, test.path))
testutil.CompareAuditLogs(t, wantAuditLogs, log.String())
}
})

View File

@@ -6,10 +6,13 @@ package testutil
import (
"bytes"
"encoding/json"
"net/url"
"strings"
"testing"
"github.com/stretchr/testify/require"
"go.pinniped.dev/internal/federationdomain/stateparam"
)
func RequireLogLines(t *testing.T, wantLogs []string, log *bytes.Buffer) {
@@ -38,6 +41,17 @@ func WantAuditLog(message string, params map[string]any, auditID ...string) Want
return result
}
func GetStateParam(t *testing.T, fullURL string) stateparam.Encoded {
var encodedStateParam stateparam.Encoded
if fullURL != "" {
path, err := url.Parse(fullURL)
require.NoError(t, err)
encodedStateParam = stateparam.Encoded(path.Query().Get("state"))
}
return encodedStateParam
}
func CompareAuditLogs(t *testing.T, wantAuditLogs []WantedAuditLog, actualAuditLogsOneLiner string) {
t.Helper()