This repository has been archived by the owner on Oct 14, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Retrieve users by name from the database. This is a new feature. It enables the user to retrieve users by name. Signed-off-by: rodneyosodo <[email protected]>
- Loading branch information
1 parent
004782d
commit 9618b1c
Showing
4 changed files
with
297 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -256,3 +256,211 @@ func TestIsPlatformAdmin(t *testing.T) { | |
assert.True(t, errors.Contains(err, tc.err), fmt.Sprintf("%s: expected %v got %v\n", tc.desc, tc.err, err)) | ||
} | ||
} | ||
|
||
func TestClientsRetrieveNames(t *testing.T) { | ||
t.Cleanup(func() { | ||
_, err := db.Exec("DELETE FROM clients") | ||
require.Nil(t, err, fmt.Sprintf("clean clients unexpected error: %s", err)) | ||
}) | ||
repo := cpostgres.NewRepository(database) | ||
|
||
nusers := 100 | ||
users := make([]mgclients.Client, nusers) | ||
|
||
name := namesgen.Generate() | ||
|
||
for i := 0; i < nusers; i++ { | ||
client := mgclients.Client{ | ||
ID: testsutil.GenerateUUID(t), | ||
Name: fmt.Sprintf("%s-%d", name, i), | ||
Credentials: mgclients.Credentials{ | ||
Identity: fmt.Sprintf("%s-%[email protected]", name, i), | ||
Secret: password, | ||
}, | ||
Metadata: mgclients.Metadata{}, | ||
Status: mgclients.EnabledStatus, | ||
} | ||
_, err := repo.Save(context.Background(), client) | ||
require.Nil(t, err, fmt.Sprintf("save client unexpected error: %s", err)) | ||
|
||
users[i] = mgclients.Client{ | ||
Name: client.Name, | ||
} | ||
} | ||
|
||
cases := []struct { | ||
desc string | ||
page mgclients.Page | ||
response mgclients.ClientsPage | ||
err error | ||
}{ | ||
{ | ||
desc: "retrieve all clients", | ||
page: mgclients.Page{ | ||
Offset: 0, | ||
Limit: uint64(nusers), | ||
}, | ||
response: mgclients.ClientsPage{ | ||
Clients: removeIdentities(users), | ||
Page: mgclients.Page{ | ||
Total: uint64(nusers), | ||
Offset: 0, | ||
Limit: uint64(nusers), | ||
}, | ||
}, | ||
err: nil, | ||
}, | ||
{ | ||
desc: "retrieve all clients with offset", | ||
page: mgclients.Page{ | ||
Offset: 10, | ||
Limit: uint64(nusers), | ||
}, | ||
response: mgclients.ClientsPage{ | ||
Clients: removeIdentities(users[10:]), | ||
Page: mgclients.Page{ | ||
Total: uint64(nusers), | ||
Offset: 10, | ||
Limit: uint64(nusers), | ||
}, | ||
}, | ||
err: nil, | ||
}, | ||
{ | ||
desc: "retrieve all clients with limit", | ||
page: mgclients.Page{ | ||
Offset: 0, | ||
Limit: 10, | ||
}, | ||
response: mgclients.ClientsPage{ | ||
Clients: removeIdentities(users[:10]), | ||
Page: mgclients.Page{ | ||
Total: uint64(nusers), | ||
Offset: 0, | ||
Limit: 10, | ||
}, | ||
}, | ||
err: nil, | ||
}, | ||
{ | ||
desc: "retrieve all clients with offset and limit", | ||
page: mgclients.Page{ | ||
Offset: 10, | ||
Limit: 10, | ||
}, | ||
response: mgclients.ClientsPage{ | ||
Clients: removeIdentities(users[10:20]), | ||
Page: mgclients.Page{ | ||
Total: uint64(nusers), | ||
Offset: 10, | ||
Limit: 10, | ||
}, | ||
}, | ||
err: nil, | ||
}, | ||
{ | ||
desc: "retrieve all clients with name", | ||
page: mgclients.Page{ | ||
Name: users[0].Name[:1], | ||
Offset: 0, | ||
Limit: 10, | ||
}, | ||
response: mgclients.ClientsPage{ | ||
Clients: findClient(users, users[0].Name[:1], true, false, 0, 10), | ||
Page: mgclients.Page{ | ||
Total: uint64(nusers), | ||
Offset: 0, | ||
Limit: 10, | ||
}, | ||
}, | ||
err: nil, | ||
}, | ||
{ | ||
desc: "retrieve all clients with name", | ||
page: mgclients.Page{ | ||
Name: users[0].Name[:4], | ||
Offset: 0, | ||
Limit: 10, | ||
}, | ||
response: mgclients.ClientsPage{ | ||
Clients: findClient(users, users[0].Name[:4], true, false, 0, 10), | ||
Page: mgclients.Page{ | ||
Total: uint64(nusers), | ||
Offset: 0, | ||
Limit: 10, | ||
}, | ||
}, | ||
err: nil, | ||
}, | ||
{ | ||
desc: "retrieve all clients unknown name", | ||
page: mgclients.Page{ | ||
Name: "unknown", | ||
Offset: 0, | ||
Limit: 10, | ||
}, | ||
response: mgclients.ClientsPage{ | ||
Clients: []mgclients.Client(nil), | ||
Page: mgclients.Page{ | ||
Total: 0, | ||
Offset: 0, | ||
Limit: 10, | ||
}, | ||
}, | ||
err: nil, | ||
}, | ||
{ | ||
desc: "retrieve all clients unknown identity", | ||
page: mgclients.Page{ | ||
Identity: "unknown", | ||
Offset: 0, | ||
Limit: 10, | ||
}, | ||
response: mgclients.ClientsPage{ | ||
Clients: []mgclients.Client(nil), | ||
Page: mgclients.Page{ | ||
Total: 0, | ||
Offset: 0, | ||
Limit: 10, | ||
}, | ||
}, | ||
err: nil, | ||
}, | ||
} | ||
for _, tc := range cases { | ||
resp, err := repo.RetrieveNames(context.Background(), tc.page) | ||
assert.True(t, errors.Contains(err, tc.err), fmt.Sprintf("%s: expected %s got %s\n", tc.desc, tc.err, err)) | ||
if err == nil { | ||
assert.Equal(t, tc.response, resp, fmt.Sprintf("%s: expected %v got %v\n", tc.desc, tc.response, resp)) | ||
} | ||
} | ||
} | ||
|
||
func findClient(clients []mgclients.Client, query string, name, email bool, offset, limit uint64) []mgclients.Client { | ||
clis := []mgclients.Client{} | ||
for _, client := range clients { | ||
if name && strings.Contains(client.Name, query) { | ||
clis = append(clis, client) | ||
} | ||
if email && strings.Contains(client.Credentials.Identity, query) { | ||
clis = append(clis, client) | ||
} | ||
} | ||
|
||
if offset > uint64(len(clis)) { | ||
return []mgclients.Client{} | ||
} | ||
|
||
if limit > uint64(len(clis)) { | ||
return clis[offset:] | ||
} | ||
|
||
return clis[offset:limit] | ||
} | ||
|
||
func removeIdentities(clients []mgclients.Client) []mgclients.Client { | ||
for i := range clients { | ||
clients[i].Credentials.Identity = "" | ||
} | ||
return clients | ||
} |