mirror of
https://github.com/optim-enterprises-bv/vault.git
synced 2025-11-03 20:17:59 +00:00
VAULT-24798: audit - improve error messages (#26312)
* audit: remove 'op' from error messages and do some clean up * Allow early error checking to be concerned with vault/Core vs. audit
This commit is contained in:
@@ -31,21 +31,21 @@ func TestAuditEvent_new(t *testing.T) {
|
||||
Subtype: subtype(""),
|
||||
Format: format(""),
|
||||
IsErrorExpected: true,
|
||||
ExpectedErrorMessage: "audit.NewEvent: audit.(AuditEvent).validate: audit.(subtype).validate: '' is not a valid event subtype: invalid parameter",
|
||||
ExpectedErrorMessage: "invalid event subtype \"\": invalid internal parameter",
|
||||
},
|
||||
"empty-Option": {
|
||||
Options: []Option{},
|
||||
Subtype: subtype(""),
|
||||
Format: format(""),
|
||||
IsErrorExpected: true,
|
||||
ExpectedErrorMessage: "audit.NewEvent: audit.(AuditEvent).validate: audit.(subtype).validate: '' is not a valid event subtype: invalid parameter",
|
||||
ExpectedErrorMessage: "invalid event subtype \"\": invalid internal parameter",
|
||||
},
|
||||
"bad-id": {
|
||||
Options: []Option{WithID("")},
|
||||
Subtype: ResponseType,
|
||||
Format: JSONFormat,
|
||||
IsErrorExpected: true,
|
||||
ExpectedErrorMessage: "audit.NewEvent: error applying options: id cannot be empty",
|
||||
ExpectedErrorMessage: "id cannot be empty",
|
||||
},
|
||||
"good": {
|
||||
Options: []Option{
|
||||
@@ -119,12 +119,12 @@ func TestAuditEvent_Validate(t *testing.T) {
|
||||
"nil": {
|
||||
Value: nil,
|
||||
IsErrorExpected: true,
|
||||
ExpectedErrorMessage: "audit.(AuditEvent).validate: event is nil: invalid parameter",
|
||||
ExpectedErrorMessage: "event is nil: invalid internal parameter",
|
||||
},
|
||||
"default": {
|
||||
Value: &AuditEvent{},
|
||||
IsErrorExpected: true,
|
||||
ExpectedErrorMessage: "audit.(AuditEvent).validate: missing ID: invalid parameter",
|
||||
ExpectedErrorMessage: "missing ID: invalid internal parameter",
|
||||
},
|
||||
"id-empty": {
|
||||
Value: &AuditEvent{
|
||||
@@ -135,7 +135,7 @@ func TestAuditEvent_Validate(t *testing.T) {
|
||||
Data: nil,
|
||||
},
|
||||
IsErrorExpected: true,
|
||||
ExpectedErrorMessage: "audit.(AuditEvent).validate: missing ID: invalid parameter",
|
||||
ExpectedErrorMessage: "missing ID: invalid internal parameter",
|
||||
},
|
||||
"version-fiddled": {
|
||||
Value: &AuditEvent{
|
||||
@@ -146,7 +146,7 @@ func TestAuditEvent_Validate(t *testing.T) {
|
||||
Data: nil,
|
||||
},
|
||||
IsErrorExpected: true,
|
||||
ExpectedErrorMessage: "audit.(AuditEvent).validate: event version unsupported: invalid parameter",
|
||||
ExpectedErrorMessage: "event version unsupported: invalid internal parameter",
|
||||
},
|
||||
"subtype-fiddled": {
|
||||
Value: &AuditEvent{
|
||||
@@ -157,7 +157,7 @@ func TestAuditEvent_Validate(t *testing.T) {
|
||||
Data: nil,
|
||||
},
|
||||
IsErrorExpected: true,
|
||||
ExpectedErrorMessage: "audit.(AuditEvent).validate: audit.(subtype).validate: 'moon' is not a valid event subtype: invalid parameter",
|
||||
ExpectedErrorMessage: "invalid event subtype \"moon\": invalid internal parameter",
|
||||
},
|
||||
"default-time": {
|
||||
Value: &AuditEvent{
|
||||
@@ -168,7 +168,7 @@ func TestAuditEvent_Validate(t *testing.T) {
|
||||
Data: nil,
|
||||
},
|
||||
IsErrorExpected: true,
|
||||
ExpectedErrorMessage: "audit.(AuditEvent).validate: event timestamp cannot be the zero time instant: invalid parameter",
|
||||
ExpectedErrorMessage: "event timestamp cannot be the zero time instant: invalid internal parameter",
|
||||
},
|
||||
"valid": {
|
||||
Value: &AuditEvent{
|
||||
@@ -212,12 +212,12 @@ func TestAuditEvent_Validate_Subtype(t *testing.T) {
|
||||
"empty": {
|
||||
Value: "",
|
||||
IsErrorExpected: true,
|
||||
ExpectedErrorMessage: "audit.(subtype).validate: '' is not a valid event subtype: invalid parameter",
|
||||
ExpectedErrorMessage: "invalid event subtype \"\": invalid internal parameter",
|
||||
},
|
||||
"unsupported": {
|
||||
Value: "foo",
|
||||
IsErrorExpected: true,
|
||||
ExpectedErrorMessage: "audit.(subtype).validate: 'foo' is not a valid event subtype: invalid parameter",
|
||||
ExpectedErrorMessage: "invalid event subtype \"foo\": invalid internal parameter",
|
||||
},
|
||||
"request": {
|
||||
Value: "AuditRequest",
|
||||
@@ -259,12 +259,12 @@ func TestAuditEvent_Validate_Format(t *testing.T) {
|
||||
"empty": {
|
||||
Value: "",
|
||||
IsErrorExpected: true,
|
||||
ExpectedErrorMessage: "audit.(format).validate: '' is not a valid format: invalid parameter",
|
||||
ExpectedErrorMessage: "invalid format \"\": invalid internal parameter",
|
||||
},
|
||||
"unsupported": {
|
||||
Value: "foo",
|
||||
IsErrorExpected: true,
|
||||
ExpectedErrorMessage: "audit.(format).validate: 'foo' is not a valid format: invalid parameter",
|
||||
ExpectedErrorMessage: "invalid format \"foo\": invalid internal parameter",
|
||||
},
|
||||
"json": {
|
||||
Value: "json",
|
||||
@@ -378,3 +378,69 @@ func TestAuditEvent_formattedTime(t *testing.T) {
|
||||
require.NotNil(t, a)
|
||||
require.Equal(t, "2024-03-22T10:00:05.00000001Z", a.formattedTime())
|
||||
}
|
||||
|
||||
// TestEvent_IsValidFormat ensures that we can correctly determine valid and
|
||||
// invalid formats.
|
||||
func TestEvent_IsValidFormat(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
tests := map[string]struct {
|
||||
input string
|
||||
expected bool
|
||||
}{
|
||||
"empty": {
|
||||
input: "",
|
||||
expected: false,
|
||||
},
|
||||
"whitespace": {
|
||||
input: " ",
|
||||
expected: false,
|
||||
},
|
||||
"invalid-test": {
|
||||
input: "test",
|
||||
expected: false,
|
||||
},
|
||||
"valid-json": {
|
||||
input: "json",
|
||||
expected: true,
|
||||
},
|
||||
"upper-json": {
|
||||
input: "JSON",
|
||||
expected: true,
|
||||
},
|
||||
"mixed-json": {
|
||||
input: "Json",
|
||||
expected: true,
|
||||
},
|
||||
"spacey-json": {
|
||||
input: " json ",
|
||||
expected: true,
|
||||
},
|
||||
"valid-jsonx": {
|
||||
input: "jsonx",
|
||||
expected: true,
|
||||
},
|
||||
"upper-jsonx": {
|
||||
input: "JSONX",
|
||||
expected: true,
|
||||
},
|
||||
"mixed-jsonx": {
|
||||
input: "JsonX",
|
||||
expected: true,
|
||||
},
|
||||
"spacey-jsonx": {
|
||||
input: " jsonx ",
|
||||
expected: true,
|
||||
},
|
||||
}
|
||||
|
||||
for name, tc := range tests {
|
||||
name := name
|
||||
tc := tc
|
||||
t.Run(name, func(t *testing.T) {
|
||||
t.Parallel()
|
||||
res := IsValidFormat(tc.input)
|
||||
require.Equal(t, tc.expected, res)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user