-
Notifications
You must be signed in to change notification settings - Fork 1.5k
/
Copy pathaudit_test.go
52 lines (46 loc) · 1.26 KB
/
audit_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
// Copyright © 2022 Ory Corp
// SPDX-License-Identifier: Apache-2.0
package x
import (
"bytes"
"fmt"
"net/http"
"testing"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
"github.com/stretchr/testify/assert"
"github.com/ory/x/logrusx"
)
func TestLogAudit(t *testing.T) {
for k, tc := range []struct {
d string
message interface{}
expectContains []string
}{
{
d: "This should log \"access allowed\" because no errors are given",
message: nil,
expectContains: []string{"msg=access allowed"},
},
{
d: "This should log \"access denied\" because an error is given",
message: errors.New("asdf"),
expectContains: []string{"msg=access denied"},
},
} {
t.Run(fmt.Sprintf("case=%d/description=%s", k, tc.d), func(t *testing.T) {
r, err := http.NewRequest(http.MethodGet, "https://hydra/some/endpoint", nil)
if err != nil {
t.Fatal(err)
}
buf := bytes.NewBuffer([]byte{})
l := logrusx.NewAudit("", "", logrusx.ForceLevel(logrus.TraceLevel))
l.Logger.Out = buf
LogAudit(r, tc.message, l)
assert.Contains(t, buf.String(), "audience=audit")
for _, expectContain := range tc.expectContains {
assert.Contains(t, buf.String(), expectContain)
}
})
}
}