feat add tenant users

This commit is contained in:
2025-12-16 11:20:40 +08:00
parent 28ab17324d
commit d058b7ffda
17 changed files with 1780 additions and 32 deletions

View File

@@ -2,6 +2,7 @@ package services
import (
"database/sql"
"fmt"
"testing"
"quyun/v2/app/commands/testx"
@@ -9,6 +10,7 @@ import (
"quyun/v2/database/models"
"quyun/v2/pkg/consts"
"github.com/samber/lo"
. "github.com/smartystreets/goconvey/convey"
"github.com/stretchr/testify/suite"
@@ -94,3 +96,107 @@ func (t *UserTestSuite) Test_FindByUsername() {
})
})
}
// Test_Page
func (t *UserTestSuite) Test_Page() {
FocusConvey("test page", t.T(), func() {
Convey("filter username", func() {
database.Truncate(t.T().Context(), t.DB, models.TableNameUser)
username := "test-user"
m := &models.User{
Username: username,
Password: "test-password",
Roles: types.NewArray([]consts.Role{consts.RoleUser}),
Status: consts.UserStatusPendingVerify,
}
err := m.Create(t.T().Context())
So(err, ShouldBeNil)
pager, err := User.Page(t.T().Context(), &UserPageFilter{
Username: &username,
})
So(err, ShouldBeNil)
So(pager.Total, ShouldEqual, 1)
})
FocusConvey("filter tenant users", func() {
database.Truncate(
t.T().Context(),
t.DB,
models.TableNameUser,
models.TableNameTenant,
models.TableNameTenantUser,
)
username := "test-user"
m := &models.User{
Username: username,
Password: "test-password",
Roles: types.NewArray([]consts.Role{consts.RoleUser}),
Status: consts.UserStatusPendingVerify,
}
err := m.Create(t.T().Context())
So(err, ShouldBeNil)
m = &models.User{
Username: username + "02",
Password: "test-password",
Roles: types.NewArray([]consts.Role{consts.RoleUser}),
Status: consts.UserStatusPendingVerify,
}
err = m.Create(t.T().Context())
So(err, ShouldBeNil)
tenantModel := &models.Tenant{
UserID: 1,
Code: "abc",
UUID: types.NewUUIDv4(),
Name: "T01",
Status: consts.TenantStatusVerified,
}
err = tenantModel.Create(t.T().Context())
So(err, ShouldBeNil)
tenantModel = &models.Tenant{
UserID: 2,
Code: "abc01",
UUID: types.NewUUIDv4(),
Name: "T02",
Status: consts.TenantStatusVerified,
}
err = tenantModel.Create(t.T().Context())
So(err, ShouldBeNil)
count := 12
for i := 0; i < count; i++ {
m = &models.User{
Username: fmt.Sprintf("user_%d", i),
Password: "test-password",
Roles: types.NewArray([]consts.Role{consts.RoleUser}),
Status: consts.UserStatusPendingVerify,
}
err = m.Create(t.T().Context())
So(err, ShouldBeNil)
// create tenant user
err = Tenant.AddUser(t.T().Context(), int64(i%2+1), m.ID)
So(err, ShouldBeNil)
}
pager, err := User.Page(t.T().Context(), &UserPageFilter{
TenantID: lo.ToPtr(int64(1)),
})
So(err, ShouldBeNil)
So(pager.Total, ShouldEqual, 6)
})
})
}