1
1
package backends_test
2
2
3
3
import (
4
+ "bytes"
4
5
"context"
6
+ "database/sql"
7
+ "encoding/json"
8
+ "net"
9
+ "net/http"
5
10
"testing"
11
+ "time"
6
12
7
13
"github.com/fatih/structs"
14
+ "github.com/google/uuid"
15
+ "github.com/sqlc-dev/pqtype"
16
+ "github.com/stretchr/testify/assert"
8
17
"github.com/stretchr/testify/require"
9
18
10
19
"cdr.dev/slog"
20
+ "cdr.dev/slog/sloggers/slogjson"
21
+ "github.com/coder/coder/v2/coderd/database"
11
22
"github.com/coder/coder/v2/enterprise/audit/audittest"
12
23
"github.com/coder/coder/v2/enterprise/audit/backends"
13
24
)
@@ -34,6 +45,54 @@ func TestSlogBackend(t *testing.T) {
34
45
require .Equal (t , sink .entries [0 ].Message , "audit_log" )
35
46
require .Len (t , sink .entries [0 ].Fields , len (structs .Fields (alog )))
36
47
})
48
+
49
+ t .Run ("FormatsCorrectly" , func (t * testing.T ) {
50
+ t .Parallel ()
51
+
52
+ var (
53
+ ctx , cancel = context .WithCancel (context .Background ())
54
+
55
+ buf = bytes .NewBuffer (nil )
56
+ logger = slog .Make (slogjson .Sink (buf ))
57
+ backend = backends .NewSlog (logger )
58
+
59
+ _ , inet , _ = net .ParseCIDR ("127.0.0.1/32" )
60
+ alog = database.AuditLog {
61
+ ID : uuid.UUID {1 },
62
+ Time : time .Unix (1257894000 , 0 ),
63
+ UserID : uuid.UUID {2 },
64
+ OrganizationID : uuid.UUID {3 },
65
+ Ip : pqtype.Inet {
66
+ IPNet : * inet ,
67
+ Valid : true ,
68
+ },
69
+ UserAgent : sql.NullString {String : "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/100.0.4896.127 Safari/537.36" , Valid : true },
70
+ ResourceType : database .ResourceTypeOrganization ,
71
+ ResourceID : uuid.UUID {4 },
72
+ ResourceTarget : "colin's organization" ,
73
+ ResourceIcon : "photo.png" ,
74
+ Action : database .AuditActionDelete ,
75
+ Diff : []byte (`{"1": 2}` ),
76
+ StatusCode : http .StatusNoContent ,
77
+ AdditionalFields : []byte (`{"name":"doug","species":"cat"}` ),
78
+ RequestID : uuid.UUID {5 },
79
+ }
80
+ )
81
+ defer cancel ()
82
+
83
+ err := backend .Export (ctx , alog )
84
+ require .NoError (t , err )
85
+ logger .Sync ()
86
+
87
+ s := struct {
88
+ Fields json.RawMessage `json:"fields"`
89
+ }{}
90
+ err = json .Unmarshal (buf .Bytes (), & s )
91
+ require .NoError (t , err )
92
+
93
+ expected := `{"ID":"01000000-0000-0000-0000-000000000000","Time":"2009-11-10T23:00:00Z","UserID":"02000000-0000-0000-0000-000000000000","OrganizationID":"03000000-0000-0000-0000-000000000000","Ip":"127.0.0.1","UserAgent":"Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/100.0.4896.127 Safari/537.36","ResourceType":"organization","ResourceID":"04000000-0000-0000-0000-000000000000","ResourceTarget":"colin's organization","Action":"delete","Diff":{"1":2},"StatusCode":204,"AdditionalFields":{"name":"doug","species":"cat"},"RequestID":"05000000-0000-0000-0000-000000000000","ResourceIcon":"photo.png"}`
94
+ assert .Equal (t , expected , string (s .Fields ))
95
+ })
37
96
}
38
97
39
98
type fakeSink struct {
0 commit comments