From a6a3e20738092978da63b988bff4019df5084ed4 Mon Sep 17 00:00:00 2001 From: Abin Simon Date: Wed, 18 May 2022 15:13:54 +0530 Subject: [PATCH 1/2] Fix user listing --- internal/dao/common.go | 4 +++- internal/dao/user.go | 13 +++++++++---- 2 files changed, 12 insertions(+), 5 deletions(-) diff --git a/internal/dao/common.go b/internal/dao/common.go index de363ec..4b8e8c7 100644 --- a/internal/dao/common.go +++ b/internal/dao/common.go @@ -248,8 +248,10 @@ func ListFiltered(ctx context.Context, db bun.IDB, if orderBy != "" && order != "" { sq.Order(orderBy + " " + order) } - if limit != 0 || offset != 0 { + if limit > 0 { sq.Limit(limit) + } + if offset > 0 { sq.Offset(offset) } err := sq.Scan(ctx) diff --git a/internal/dao/user.go b/internal/dao/user.go index 2576a76..9669b2a 100644 --- a/internal/dao/user.go +++ b/internal/dao/user.go @@ -100,7 +100,10 @@ func GetQueryFilteredUsers(ctx context.Context, db bun.IDB, partner, org, group, if len(projects) != 0 { q.Where("project_id IN (?)", bun.In(projects)) } - q.Scan(ctx) + err := q.Scan(ctx) + if err != nil { + return nil, err + } acc := []uuid.UUID{} for _, a := range p { @@ -122,18 +125,20 @@ func ListFilteredUsers(ctx context.Context, db bun.IDB, users *[]models.KratosId if len(fusers) > 0 { // filter with precomputed users if we have any - q.Where("id IN (?)", bun.In(fusers)) + q.Where("identities.id IN (?)", bun.In(fusers)) } if query != "" { - q.Where("traits ->> 'email' ILIKE ?", "%"+query+"%") // XXX: ILIKE is not-standard + q.Where("traits ->> 'email' ILIKE ?", "%"+query+"%") // XXX: ILIKE is not-standard sql q.WhereOr("traits ->> 'first_name' ILIKE ?", "%"+query+"%") q.WhereOr("traits ->> 'last_name' ILIKE ?", "%"+query+"%") } if orderBy != "" && order != "" { q.Order("traits ->> '" + orderBy + "' " + order) } - if limit != 0 || offset != 0 { + if limit > 0 { q.Limit(limit) + } + if offset > 0 { q.Offset(offset) } err := q.Scan(ctx) From dd3a1fd4b177a03a4edf95968e9d1c79ef9bda4c Mon Sep 17 00:00:00 2001 From: Abin Simon Date: Thu, 19 May 2022 14:58:49 +0530 Subject: [PATCH 2/2] Fix tests for user list fetch --- pkg/service/user_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pkg/service/user_test.go b/pkg/service/user_test.go index 77411a2..a4b1731 100644 --- a/pkg/service/user_test.go +++ b/pkg/service/user_test.go @@ -507,7 +507,7 @@ func TestUserList(t *testing.T) { } if tc.role != "" || tc.group != "" || len(tc.projects) != 0 { addSentryLookupExpectation(mock, []string{uuuid1, uuuid2}, puuid, ouuid) - mock.ExpectQuery(`SELECT "identities"."id", .*WHERE .id IN .'` + uuuid1 + `', '` + uuuid2 + `'.. ` + q + order + `LIMIT ` + fmt.Sprint(tc.limit) + ` OFFSET ` + fmt.Sprint(tc.offset)). + mock.ExpectQuery(`SELECT "identities"."id", .*WHERE .identities.id IN .'` + uuuid1 + `', '` + uuuid2 + `'.. ` + q + order + `LIMIT ` + fmt.Sprint(tc.limit) + ` OFFSET ` + fmt.Sprint(tc.offset)). WithArgs().WillReturnRows(sqlmock.NewRows([]string{"id", "traits"}). AddRow(uuuid1, []byte(`{"email":"johndoe@provider.com", "first_name": "John", "last_name": "Doe", "organization_id": "`+ouuid+`", "partner_id": "`+puuid+`", "description": "My awesome user"}`)). AddRow(uuuid2, []byte(`{"email":"johndoe@provider.com", "first_name": "John", "last_name": "Doe", "organization_id": "`+ouuid+`", "partner_id": "`+puuid+`", "description": "My awesome user"}`)))