From af0162f0dfcee57d4b572df5bb4d4a0b15887adc Mon Sep 17 00:00:00 2001 From: Burak Sekili Date: Thu, 8 Jul 2021 14:51:37 +0300 Subject: [PATCH] Fix fetching user members of an empty group (#1436) Signed-off-by: Burak Sekili --- users/postgres/users.go | 18 +++++++++++++----- users/postgres/users_test.go | 13 +++++++++++++ 2 files changed, 26 insertions(+), 5 deletions(-) diff --git a/users/postgres/users.go b/users/postgres/users.go index 3e4a168b..ebec64dc 100644 --- a/users/postgres/users.go +++ b/users/postgres/users.go @@ -168,13 +168,21 @@ func (ur userRepository) RetrieveAll(ctx context.Context, offset, limit uint64, if mq != "" { query = append(query, mq) } - if len(userIDs) > 0 { - query = append(query, fmt.Sprintf("id IN ('%s')", strings.Join(userIDs, "','"))) - } - if len(query) > 0 { - emq = fmt.Sprintf(" WHERE %s", strings.Join(query, " AND ")) + + if len(userIDs) == 0 { + return users.UserPage{ + Users: []users.User{}, + PageMetadata: users.PageMetadata{ + Total: 0, + Offset: offset, + Limit: limit, + }, + }, nil } + query = append(query, fmt.Sprintf("id IN ('%s')", strings.Join(userIDs, "','"))) + emq = fmt.Sprintf(" WHERE %s", strings.Join(query, " AND ")) + q := fmt.Sprintf(`SELECT id, email, metadata FROM users %s ORDER BY email LIMIT :limit OFFSET :offset;`, emq) params := map[string]interface{}{ "limit": limit, diff --git a/users/postgres/users_test.go b/users/postgres/users_test.go index 6c8d7e50..cf659be2 100644 --- a/users/postgres/users_test.go +++ b/users/postgres/users_test.go @@ -137,6 +137,7 @@ func TestRetrieveAll(t *testing.T) { limit: nUsers, size: nUsers, total: nUsers, + ids: ids, }, "retrieve all users by email with limit and offset": { email: "All", @@ -144,6 +145,7 @@ func TestRetrieveAll(t *testing.T) { limit: 5, size: 5, total: nUsers, + ids: ids, }, "retrieve all users by metadata": { email: "All", @@ -152,6 +154,7 @@ func TestRetrieveAll(t *testing.T) { size: metaNum, total: nUsers, metadata: meta, + ids: ids, }, "retrieve users by metadata and ids": { email: "All", @@ -169,6 +172,7 @@ func TestRetrieveAll(t *testing.T) { size: 0, total: nUsers, metadata: wrongMeta, + ids: ids, }, "retrieve users by wrong metadata and ids": { email: "All", @@ -196,6 +200,15 @@ func TestRetrieveAll(t *testing.T) { ids: ids[0:5], metadata: meta, }, + "retrieve all users from empty ids": { + email: "All", + offset: 1, + limit: 5, + size: 0, + total: nUsers, + ids: []string{}, + metadata: meta, + }, } for desc, tc := range cases { page, err := userRepo.RetrieveAll(context.Background(), tc.offset, tc.limit, tc.ids, tc.email, tc.metadata)