mirror of
https://dev.azure.com/schwarzit/schwarzit.stackit-public/_git/audit-go
synced 2026-02-08 00:57:24 +00:00
80 lines
2.6 KiB
Go
80 lines
2.6 KiB
Go
package api
|
|
|
|
import (
|
|
"context"
|
|
"strings"
|
|
"testing"
|
|
|
|
"buf.build/go/protovalidate"
|
|
"github.com/stretchr/testify/assert"
|
|
"github.com/stretchr/testify/mock"
|
|
"google.golang.org/protobuf/proto"
|
|
|
|
auditV1 "dev.azure.com/schwarzit/schwarzit.stackit-public/audit-go.git/gen/go/audit/v1"
|
|
internalAuditApi "dev.azure.com/schwarzit/schwarzit.stackit-public/audit-go.git/internal/audit/api"
|
|
pkgAuditCommon "dev.azure.com/schwarzit/schwarzit.stackit-public/audit-go.git/pkg/audit/common"
|
|
)
|
|
|
|
type ProtobufValidatorMock struct {
|
|
mock.Mock
|
|
}
|
|
|
|
func (m *ProtobufValidatorMock) Validate(msg proto.Message, options ...protovalidate.ValidationOption) error {
|
|
var args mock.Arguments
|
|
if len(options) > 0 {
|
|
args = m.Called(msg, options)
|
|
} else {
|
|
args = m.Called(msg)
|
|
}
|
|
return args.Error(0)
|
|
}
|
|
|
|
func TestMockAuditApi_Log(t *testing.T) {
|
|
|
|
auditApi, err := NewMockAuditApi()
|
|
assert.NoError(t, err)
|
|
|
|
// Instantiate test data
|
|
event, objectIdentifier := internalAuditApi.NewOrganizationAuditEvent(nil)
|
|
routableObjectIdentifier := pkgAuditCommon.NewRoutableIdentifier(objectIdentifier)
|
|
|
|
// Test
|
|
t.Run("Log", func(t *testing.T) {
|
|
assert.Nil(t, auditApi.Log(
|
|
context.Background(), event, auditV1.Visibility_VISIBILITY_PUBLIC, routableObjectIdentifier))
|
|
})
|
|
|
|
t.Run("reject data access event", func(t *testing.T) {
|
|
orgEvent, objIdentifier := internalAuditApi.NewOrganizationAuditEvent(nil)
|
|
orgEvent.LogName = strings.Replace(orgEvent.LogName, string(pkgAuditCommon.EventTypeAdminActivity), string(pkgAuditCommon.EventTypeDataAccess), 1)
|
|
rtIdentifier := pkgAuditCommon.NewRoutableIdentifier(objIdentifier)
|
|
|
|
assert.ErrorIs(t, auditApi.Log(
|
|
context.Background(), orgEvent, auditV1.Visibility_VISIBILITY_PUBLIC, rtIdentifier),
|
|
pkgAuditCommon.ErrUnsupportedEventTypeDataAccess)
|
|
})
|
|
|
|
t.Run("ValidateAndSerialize", func(t *testing.T) {
|
|
visibility := auditV1.Visibility_VISIBILITY_PUBLIC
|
|
cloudEvent, err := auditApi.ValidateAndSerialize(
|
|
context.Background(), event, visibility, routableObjectIdentifier)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
validateRoutableEventPayload(
|
|
t, cloudEvent.Data, objectIdentifier, event, event.ProtoPayload.OperationName, visibility)
|
|
})
|
|
|
|
t.Run("ValidateAndSerialize event nil", func(t *testing.T) {
|
|
visibility := auditV1.Visibility_VISIBILITY_PUBLIC
|
|
_, err := auditApi.ValidateAndSerialize(context.Background(), nil, visibility, routableObjectIdentifier)
|
|
|
|
assert.ErrorIs(t, err, pkgAuditCommon.ErrEventNil)
|
|
})
|
|
|
|
t.Run("Send", func(t *testing.T) {
|
|
var cloudEvent = pkgAuditCommon.CloudEvent{}
|
|
|
|
assert.Nil(t, auditApi.Send(context.Background(), routableObjectIdentifier, &cloudEvent))
|
|
})
|
|
}
|