Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

✅ Add unit test for audit #279

Merged
merged 1 commit into from
Jan 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
94 changes: 94 additions & 0 deletions pkg/dal/dao/audit_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
// Copyright 2023 sigma
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package dao_test

import (
"context"
"testing"

"github.com/rs/zerolog/log"
"github.com/spf13/viper"
"github.com/stretchr/testify/assert"

"github.com/go-sigma/sigma/pkg/dal"
"github.com/go-sigma/sigma/pkg/dal/dao"
"github.com/go-sigma/sigma/pkg/dal/models"
"github.com/go-sigma/sigma/pkg/dal/query"
"github.com/go-sigma/sigma/pkg/logger"
"github.com/go-sigma/sigma/pkg/tests"
"github.com/go-sigma/sigma/pkg/types/enums"
"github.com/go-sigma/sigma/pkg/utils/ptr"
)

func TestAuditServiceFactory(t *testing.T) {
f := dao.NewAuditServiceFactory()
artifactService := f.New()
assert.NotNil(t, artifactService)
artifactService = f.New(query.Q)
assert.NotNil(t, artifactService)
}

func TestAuditService(t *testing.T) {
viper.SetDefault("log.level", "debug")
logger.SetLevel("debug")
err := tests.Initialize(t)
assert.NoError(t, err)
err = tests.DB.Init()
assert.NoError(t, err)
defer func() {
conn, err := dal.DB.DB()
assert.NoError(t, err)
err = conn.Close()
assert.NoError(t, err)
err = tests.DB.DeInit()
assert.NoError(t, err)
}()

ctx := log.Logger.WithContext(context.Background())

auditServiceFactory := dao.NewAuditServiceFactory()
auditService := auditServiceFactory.New()
assert.NotNil(t, auditService)

namespaceServiceFactory := dao.NewNamespaceServiceFactory()
namespaceService := namespaceServiceFactory.New()
assert.NotNil(t, namespaceService)

namespaceObj1 := &models.Namespace{Name: "test"}
err = namespaceService.Create(ctx, namespaceObj1)
assert.NoError(t, err)

userServiceFactory := dao.NewUserServiceFactory()
userService := userServiceFactory.New()
assert.NotNil(t, userService)

userObj := &models.User{Username: "test-case", Password: ptr.Of("test-case"), Email: ptr.Of("email")}
err = userService.Create(ctx, userObj)
assert.NoError(t, err)

err = auditService.Create(ctx, &models.Audit{
UserID: userObj.ID,
NamespaceID: ptr.Of(namespaceObj1.ID),
Action: enums.AuditActionCreate,
ResourceType: enums.AuditResourceTypeNamespace,
Resource: namespaceObj1.Name,
})
assert.NoError(t, err)

hotNamespaceObjs, err := auditService.HotNamespace(ctx, userObj.ID, 3)
assert.NoError(t, err)
assert.Equal(t, len(hotNamespaceObjs), 1)
assert.Equal(t, hotNamespaceObjs[0].Name, namespaceObj1.Name)
}
4 changes: 2 additions & 2 deletions pkg/dal/dao/user_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,12 +55,12 @@ func TestUserGetByUsername(t *testing.T) {
assert.NoError(t, err)
}()

f := dao.NewUserServiceFactory()
userServiceFactory := dao.NewUserServiceFactory()

ctx := log.Logger.WithContext(context.Background())

err = query.Q.Transaction(func(tx *query.Query) error {
userService := f.New(tx)
userService := userServiceFactory.New(tx)
assert.NotNil(t, userService)
err := userService.Create(ctx, &models.User{Username: "test-case", Password: ptr.Of("test-case"), Email: ptr.Of("email")})
assert.NoError(t, err)
Expand Down