github.com/blend/go-sdk@v1.20220411.3/logger/audit_event_test.go (about) 1 /* 2 3 Copyright (c) 2022 - Present. Blend Labs, Inc. All rights reserved 4 Use of this source code is governed by a MIT license that can be found in the LICENSE file. 5 6 */ 7 8 package logger 9 10 import ( 11 "bytes" 12 "context" 13 "testing" 14 15 "github.com/blend/go-sdk/assert" 16 ) 17 18 func TestAuditEventMarshalJSON(t *testing.T) { 19 assert := assert.New(t) 20 21 ae := NewAuditEvent( 22 "example-string", 23 "pooped", 24 ) 25 26 contents := ae.Decompose() 27 assert.NotEmpty(contents) 28 assert.Equal("example-string", contents["principal"]) 29 assert.Equal("pooped", contents["verb"]) 30 } 31 32 func TestAuditEventOptions(t *testing.T) { 33 assert := assert.New(t) 34 35 ae := NewAuditEvent( 36 "example-string", 37 "pooped", 38 OptAuditContext("event context"), 39 OptAuditPrincipal("not example-string"), 40 OptAuditVerb("not pooped"), 41 OptAuditNoun("audit noun"), 42 OptAuditSubject("audit subject"), 43 OptAuditProperty("audit property"), 44 OptAuditRemoteAddress("remote address"), 45 OptAuditUserAgent("user agent"), 46 OptAuditExtra(map[string]string{"foo": "bar"}), 47 ) 48 49 assert.Equal("event context", ae.Context) 50 assert.Equal("not example-string", ae.Principal) 51 assert.Equal("not pooped", ae.Verb) 52 assert.Equal("audit noun", ae.Noun) 53 assert.Equal("audit subject", ae.Subject) 54 assert.Equal("audit property", ae.Property) 55 assert.Equal("remote address", ae.RemoteAddress) 56 assert.Equal("user agent", ae.UserAgent) 57 assert.Equal("bar", ae.Extra["foo"]) 58 } 59 60 func TestAuditEventWriteText(t *testing.T) { 61 assert := assert.New(t) 62 63 ae := NewAuditEvent( 64 "example-string", 65 "pooped", 66 OptAuditContext("event context"), 67 OptAuditPrincipal("not example-string"), 68 OptAuditVerb("not pooped"), 69 OptAuditNoun("audit noun"), 70 OptAuditSubject("audit subject"), 71 OptAuditProperty("audit property"), 72 OptAuditRemoteAddress("remote address"), 73 OptAuditUserAgent("user agent"), 74 OptAuditExtra(map[string]string{"foo": "bar"}), 75 ) 76 77 buf := new(bytes.Buffer) 78 noColor := TextOutputFormatter{ 79 NoColor: true, 80 } 81 82 ae.WriteText(noColor, buf) 83 84 assert.Equal("Context:event context Principal:not example-string Verb:not pooped Noun:audit noun Subject:audit subject Property:audit property Remote Addr:remote address UA:user agent foo:bar", buf.String()) 85 } 86 87 func TestAuditEventListener(t *testing.T) { 88 assert := assert.New(t) 89 90 ae := NewAuditEvent("example-string", "pooped") 91 92 var didCall bool 93 ml := NewAuditEventListener(func(ctx context.Context, ae AuditEvent) { 94 didCall = true 95 }) 96 97 ml(context.Background(), ae) 98 assert.True(didCall) 99 }