Skip to content

Commit 9faa363

Browse files
committed
More unit tests and html coverage report
also fixes a limit bug on listquery
1 parent e4e70ae commit 9faa363

File tree

19 files changed

+479
-183
lines changed

19 files changed

+479
-183
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ bin/*
88
backend/config.json
99
backend/embed/assets
1010
backend/.task
11+
backend/coverage.html
1112
test/node_modules
1213
*/node_modules
1314
docs/.vuepress/dist

backend/internal/api/middleware/auth.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ func Enforce(permission string) func(http.Handler) http.Handler {
4949
}
5050

5151
userID := uint(claims["uid"].(float64))
52-
_, enabled := user.IsEnabled(userID)
52+
_, enabled, _ := user.IsEnabled(userID)
5353
if token == nil || !enabled {
5454
h.ResultErrorJSON(w, r, http.StatusUnauthorized, "Unauthorised", nil)
5555
return

backend/internal/api/middleware/sse_auth.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ func SSEAuth(next http.Handler) http.Handler {
3232
}
3333

3434
userID := uint(claims["uid"].(float64))
35-
_, enabled := user.IsEnabled(userID)
35+
_, enabled, _ := user.IsEnabled(userID)
3636
if token == nil || !enabled {
3737
h.ResultErrorJSON(w, r, http.StatusUnauthorized, "Unauthorised", nil)
3838
return

backend/internal/entity/accesslist/methods.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,10 @@ func List(pageInfo model.PageInfo, filters []model.Filter) (entity.ListResponse,
3030
}
3131

3232
// Get rows
33+
dbo = entity.AddOffsetLimitToList(dbo, &pageInfo)
34+
dbo = entity.AddOrderToList(dbo, pageInfo.Sort, defaultSort)
3335
items := make([]Model, 0)
34-
if res := entity.AddOrderToList(dbo, &pageInfo, defaultSort).Find(&items); res.Error != nil {
36+
if res := dbo.Find(&items); res.Error != nil {
3537
return result, res.Error
3638
}
3739

backend/internal/entity/certificate/methods.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,10 @@ func List(pageInfo model.PageInfo, filters []model.Filter, expand []string) (ent
4646
}
4747

4848
// Get rows
49+
dbo = entity.AddOffsetLimitToList(dbo, &pageInfo)
50+
dbo = entity.AddOrderToList(dbo, pageInfo.Sort, defaultSort)
4951
items := make([]Model, 0)
50-
if res := entity.AddOrderToList(dbo, &pageInfo, defaultSort).Find(&items); res.Error != nil {
52+
if res := dbo.Find(&items); res.Error != nil {
5153
return result, res.Error
5254
}
5355

backend/internal/entity/certificateauthority/methods.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,10 @@ func List(pageInfo model.PageInfo, filters []model.Filter) (entity.ListResponse,
3030
}
3131

3232
// Get rows
33+
dbo = entity.AddOffsetLimitToList(dbo, &pageInfo)
34+
dbo = entity.AddOrderToList(dbo, pageInfo.Sort, defaultSort)
3335
items := make([]Model, 0)
34-
if res := entity.AddOrderToList(dbo, &pageInfo, defaultSort).Find(&items); res.Error != nil {
36+
if res := dbo.Find(&items); res.Error != nil {
3537
return result, res.Error
3638
}
3739

backend/internal/entity/dnsprovider/methods.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,10 @@ func List(pageInfo model.PageInfo, filters []model.Filter) (entity.ListResponse,
3030
}
3131

3232
// Get rows
33+
dbo = entity.AddOffsetLimitToList(dbo, &pageInfo)
34+
dbo = entity.AddOrderToList(dbo, pageInfo.Sort, defaultSort)
3335
items := make([]Model, 0)
34-
if res := entity.AddOrderToList(dbo, &pageInfo, defaultSort).Find(&items); res.Error != nil {
36+
if res := dbo.Find(&items); res.Error != nil {
3537
return result, res.Error
3638
}
3739

backend/internal/entity/host/methods.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,10 @@ func List(pageInfo model.PageInfo, filters []model.Filter, expand []string) (ent
3232
}
3333

3434
// Get rows
35+
dbo = entity.AddOffsetLimitToList(dbo, &pageInfo)
36+
dbo = entity.AddOrderToList(dbo, pageInfo.Sort, defaultSort)
3537
items := make([]Model, 0)
36-
if res := entity.AddOrderToList(dbo, &pageInfo, defaultSort).Find(&items); res.Error != nil {
38+
if res := dbo.Find(&items); res.Error != nil {
3739
return result, res.Error
3840
}
3941

backend/internal/entity/lists.go

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,6 @@ import (
77
"gorm.io/gorm"
88
)
99

10-
// ListableModel is a interface for common use
11-
type ListableModel interface {
12-
TableName() string
13-
}
14-
1510
// ListResponse is the JSON response for users list
1611
type ListResponse struct {
1712
Total int64 `json:"total"`
@@ -29,7 +24,6 @@ func ListQueryBuilder(
2924
filterMap map[string]model.FilterMapValue,
3025
) *gorm.DB {
3126
scopes := make([]func(*gorm.DB) *gorm.DB, 0)
32-
scopes = append(scopes, ScopeOffsetLimit(pageInfo))
3327
scopes = append(scopes, ScopeFilters(filters, filterMap))
3428
return database.GetDB().Scopes(scopes...)
3529
}
@@ -38,8 +32,16 @@ func ListQueryBuilder(
3832
// Postgres in particular doesn't like count(*) when ordering at the same time
3933
func AddOrderToList(
4034
dbo *gorm.DB,
41-
pageInfo *model.PageInfo,
35+
sort []model.Sort,
4236
defaultSort model.Sort,
4337
) *gorm.DB {
44-
return dbo.Scopes(ScopeOrderBy(pageInfo, defaultSort))
38+
return dbo.Scopes(ScopeOrderBy(sort, defaultSort))
39+
}
40+
41+
// AddOffsetLimitToList is used after query above is used for pagination
42+
func AddOffsetLimitToList(
43+
dbo *gorm.DB,
44+
pageInfo *model.PageInfo,
45+
) *gorm.DB {
46+
return dbo.Scopes(ScopeOffsetLimit(pageInfo))
4547
}

backend/internal/entity/nginxtemplate/methods.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,10 @@ func List(pageInfo model.PageInfo, filters []model.Filter) (entity.ListResponse,
3030
}
3131

3232
// Get rows
33+
dbo = entity.AddOffsetLimitToList(dbo, &pageInfo)
34+
dbo = entity.AddOrderToList(dbo, pageInfo.Sort, defaultSort)
3335
items := make([]Model, 0)
34-
if res := entity.AddOrderToList(dbo, &pageInfo, defaultSort).Find(&items); res.Error != nil {
36+
if res := dbo.Find(&items); res.Error != nil {
3537
return result, res.Error
3638
}
3739

0 commit comments

Comments
 (0)