From 9daffdd3c6a5b91b2a090d9eef73c9356570803a Mon Sep 17 00:00:00 2001 From: Victor Cabezas Date: Mon, 19 Feb 2024 15:36:55 +0100 Subject: [PATCH] Match exact userID in GetUsers function --- e2e_suite_test.go | 1 + main.go | 16 ++++++++++++++-- 2 files changed, 15 insertions(+), 2 deletions(-) diff --git a/e2e_suite_test.go b/e2e_suite_test.go index 83d0ac4..ac378f4 100644 --- a/e2e_suite_test.go +++ b/e2e_suite_test.go @@ -545,6 +545,7 @@ var ( testUsers = []map[string]interface{}{ {"userId": "user1", "firstName": "user", "lastName": "one", "emailAddress": "user1@example.com", "status": "active", "roles": []string{"nx-anonymous"}, "password": "user1Password"}, + {"userId": "user12", "firstName": "user", "lastName": "one", "emailAddress": "user12@example.com", "status": "active", "roles": []string{"nx-anonymous"}, "password": "user12Password"}, } testRoles = []map[string]interface{}{ diff --git a/main.go b/main.go index 7a754a2..f547c40 100644 --- a/main.go +++ b/main.go @@ -315,11 +315,23 @@ func (p *ProxyState) GetUsers(userID *string) ([]NexusUser, error) { body, _ := ioutil.ReadAll(res.Body) return nil, fmt.Errorf("GET %s: %s - %s", &getUser, res.Status, string(body)) } - result := make([]NexusUser, 0, 1) - err = json.NewDecoder(res.Body).Decode(&result) + tmpResult := make([]NexusUser, 0, 1) + err = json.NewDecoder(res.Body).Decode(&tmpResult) if err != nil { return nil, err } + // Nexus API filters results by prefix instead of exact match + // This ensures that the user returned is the user requested by userID variable + result := make([]NexusUser, 0, 1) + if userID != nil { + for _, user := range tmpResult { + if user.UserID == *userID { + result = append(result, user) + } + } + } else { + result = tmpResult + } if len(result) == 0 { return nil, nil }