Repository: Nerzal/gocloak
Pull request #392 (2022-12-13 11:03:45)

Add params to GetUsersByRoleName
Add params to list more than a 100 users with GetUsersByRoleName

Fixing https://github.com/Nerzal/gocloak/issues/391


upils: Hi @Nerzal, is this possible to check this and let workflows run?
Nerzal: Hi, if you merge the main branch the tests should be able to run now :) 
upils: Merge done, this should work now.
codecov[bot]: # [Codecov](https://codecov.io/gh/Nerzal/gocloak/pull/392?src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Nerzal) Report
> Merging [#392](https://codecov.io/gh/Nerzal/gocloak/pull/392?src=pr&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Nerzal) (ec92883) into [main](https://codecov.io/gh/Nerzal/gocloak/commit/94bb9404b0989b12ec89f0ea72f5de735f6d7fa1?el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Nerzal) (94bb940) will **decrease** coverage by `0.05%`.
> The diff coverage is `50.00%`.

```diff
@@            Coverage Diff             @@
##             main     #392      +/-   ##
==========================================
- Coverage   77.13%   77.08%   -0.06%     
==========================================
  Files           4        4              
  Lines        2152     2156       +4     
==========================================
+ Hits         1660     1662       +2     
- Misses        335      336       +1     
- Partials      157      158       +1     
```


| [Impacted Files](https://codecov.io/gh/Nerzal/gocloak/pull/392?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Nerzal) | Coverage Δ | |
|---|---|---|
| [client.go](https://codecov.io/gh/Nerzal/gocloak/pull/392?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Nerzal#diff-Y2xpZW50Lmdv) | `75.40% <50.00%> (-0.06%)` | :arrow_down: |

:mega: We’re building smart automated test selection to slash your CI/CD build times. [Learn more](https://about.codecov.io/iterative-testing/?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Nerzal)

upils: I added a test to improve coverage.

I noticed the docker image used was keycloak:latest, which now points to `20.0.2`. They seems to have reworked the way UserInfo can be accessed (see https://github.com/keycloak/keycloak-documentation/commit/533b730bd42aefff29d255d6d5f01cdbb9e0dfe8) so both `GetUserInfo` and `GetRawUserInfo` are broken in this keycloak version. So I have pinned the keycloak version to 19.0. I can remove this if you prefer.

Patch:
diff --git a/Dockerfile b/Dockerfile
index fe10a52..9b88331 100644
--- a/Dockerfile
+++ b/Dockerfile
@@ -1,4 +1,4 @@
-FROM quay.io/keycloak/keycloak:latest
+FROM quay.io/keycloak/keycloak:19.0
 COPY testdata data/import
 WORKDIR /opt/keycloak
 ENV KC_HOSTNAME=localhost
@@ -8,4 +8,4 @@ ENV KEYCLOAK_ADMIN=admin
 ENV KEYCLOAK_ADMIN_PASSWORD=secret
 ENV KC_FEATURES=account-api,account2,authorization,client-policies,impersonation,docker,scripts,upload_scripts
 RUN /opt/keycloak/bin/kc.sh import --file /data/import/gocloak-realm.json
-ENTRYPOINT ["/opt/keycloak/bin/kc.sh"]
\ No newline at end of file
+ENTRYPOINT ["/opt/keycloak/bin/kc.sh"]
diff --git a/client.go b/client.go
index 7e3d213..48919e9 100644
--- a/client.go
+++ b/client.go
@@ -2553,12 +2553,18 @@ func (g *GoCloak) GetUsers(ctx context.Context, token, realm string, params GetU
 }
 
 // GetUsersByRoleName returns all users have a given role
-func (g *GoCloak) GetUsersByRoleName(ctx context.Context, token, realm, roleName string) ([]*User, error) {
+func (g *GoCloak) GetUsersByRoleName(ctx context.Context, token, realm, roleName string, params GetUsersByRoleParams) ([]*User, error) {
 	const errMessage = "could not get users by role name"
 
 	var result []*User
+	queryParams, err := GetQueryParams(params)
+	if err != nil {
+		return nil, errors.Wrap(err, errMessage)
+	}
+
 	resp, err := g.getRequestWithBearerAuth(ctx, token).
 		SetResult(&result).
+		SetQueryParams(queryParams).
 		Get(g.getAdminRealmURL(realm, "roles", roleName, "users"))
 
 	if err := checkForError(resp, err, errMessage); err != nil {


Test patch:
diff --git a/client_test.go b/client_test.go
index 87d1464..82b248d 100644
--- a/client_test.go
+++ b/client_test.go
@@ -3450,10 +3450,20 @@ func Test_GetUsersByRoleName(t *testing.T) {
 		context.Background(),
 		token.AccessToken,
 		cfg.GoCloak.Realm,
-		roleName)
+		roleName,
+		gocloak.GetUsersByRoleParams{})
 	require.NoError(t, err)
 	require.NotEmpty(t, users)
 	require.Equal(t, userID, *users[0].ID)
+
+	users, err = client.GetUsersByRoleName(
+		context.Background(),
+		token.AccessToken,
+		cfg.GoCloak.Realm,
+		"unknown role",
+		gocloak.GetUsersByRoleParams{})
+	require.Error(t, err, "GetUsersByRoleName no error on unknown role")
+	require.Empty(t, users)
 }
 
 func Test_GetUsersByClientRoleName(t *testing.T) {