github.com/haalcala/mattermost-server-change-repo/v5@v5.33.2/audit/audit_test.go (about) 1 // Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved. 2 // See LICENSE.txt for license information. 3 4 package audit 5 6 import ( 7 "testing" 8 9 "github.com/mattermost/logr" 10 "github.com/mattermost/logr/format" 11 "github.com/stretchr/testify/require" 12 ) 13 14 func Test_sortAuditFields(t *testing.T) { 15 type args struct { 16 fields logr.Fields 17 } 18 tests := []struct { 19 name string 20 args args 21 want []format.ContextField 22 }{ 23 {name: "empty list", 24 args: args{fields: logr.Fields{}}, 25 want: []format.ContextField{}, 26 }, 27 {name: "partial list", 28 args: args{fields: logr.Fields{"zProp": "x", "xProp": "x", "yProp": "x", KeyClusterID: "x", KeyEvent: "x"}}, 29 want: []format.ContextField{ 30 {Key: KeyEvent, Val: "x"}, 31 {Key: "xProp", Val: "x"}, 32 {Key: "yProp", Val: "x"}, 33 {Key: "zProp", Val: "x"}, 34 {Key: KeyClusterID, Val: "x"}, 35 }, 36 }, 37 {name: "append/prepend only list", 38 args: args{fields: logr.Fields{KeyClusterID: "x", KeyEvent: "x", KeySessionID: "x", KeyIPAddress: "x", KeyClient: "x", 39 KeyUserID: "x", KeyStatus: "x"}}, 40 want: []format.ContextField{ 41 // prepend: KeyEvent, KeyStatus, KeyUserID, KeySessionID, KeyIPAddress 42 // append: KeyClusterID, KeyClient 43 {Key: KeyEvent, Val: "x"}, 44 {Key: KeyStatus, Val: "x"}, 45 {Key: KeyUserID, Val: "x"}, 46 {Key: KeySessionID, Val: "x"}, 47 {Key: KeyIPAddress, Val: "x"}, 48 {Key: KeyClusterID, Val: "x"}, 49 {Key: KeyClient, Val: "x"}, 50 }, 51 }, 52 {name: "sortables only list", 53 args: args{fields: logr.Fields{"zProp": "x", "xProp": "x", "yProp": "x"}}, 54 want: []format.ContextField{ 55 {Key: "xProp", Val: "x"}, 56 {Key: "yProp", Val: "x"}, 57 {Key: "zProp", Val: "x"}, 58 }, 59 }, 60 } 61 for _, tt := range tests { 62 t.Run(tt.name, func(t *testing.T) { 63 got := sortAuditFields(tt.args.fields) 64 require.Equal(t, tt.want, got) 65 }) 66 } 67 }