Skip to content

Commit 36ec6ea

Browse files
committed
[fix] Add Test for DeleteRoleScopeMapping
Signed-off-by: foch01 <[email protected]>
1 parent ad894de commit 36ec6ea

File tree

1 file changed

+96
-0
lines changed

1 file changed

+96
-0
lines changed

provider/resource_keycloak_generic_role_mapper_test.go

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -376,3 +376,99 @@ resource "keycloak_generic_role_mapper" "client-with-some-other-role" {
376376
}
377377
`, testAccRealm.Realm, clientName, someRoleName, someOtherRoleName)
378378
}
379+
380+
func TestAccKeycloakGenericClientRoleMapper_deleteRoleScopeMappingRealmRole(t *testing.T) {
381+
t.Parallel()
382+
383+
var role = &keycloak.Role{}
384+
var childClient = &keycloak.GenericClient{}
385+
386+
roleName := acctest.RandomWithPrefix("tf-acc")
387+
childClientName := acctest.RandomWithPrefix("tf-acc")
388+
389+
resource.Test(t, resource.TestCase{
390+
ProviderFactories: testAccProviderFactories,
391+
PreCheck: func() { testAccPreCheck(t) },
392+
Steps: []resource.TestStep{
393+
{
394+
Config: testKeycloakGenericClientRoleMapper_realmRole(roleName, childClientName),
395+
Check: resource.ComposeTestCheckFunc(
396+
testAccCheckKeycloakGenericClientRoleMapperExists("keycloak_generic_client_role_mapper.child-client-with-realm-role"),
397+
testAccCheckKeycloakRoleFetch("keycloak_role.realm-role", role),
398+
testAccCheckKeycloakGenericClientFetch("keycloak_openid_client.child-client", childClient),
399+
),
400+
},
401+
{
402+
PreConfig: func() {
403+
err := keycloakClient.DeleteRoleScopeMapping(testCtx, childClient.RealmId, childClient.Id, "", role)
404+
if err != nil {
405+
t.Fatalf("Error deleting realm role mapping: %s", err)
406+
}
407+
},
408+
Config: testKeycloakGenericClientRoleMapper_realmRole(roleName, childClientName),
409+
Check: testAccCheckKeycloakGenericClientRoleMapperExists("keycloak_generic_client_role_mapper.child-client-with-realm-role"),
410+
},
411+
},
412+
})
413+
}
414+
415+
func TestAccKeycloakGenericClientRoleMapper_deleteRoleScopeMappingClientRole(t *testing.T) {
416+
t.Parallel()
417+
418+
var role = &keycloak.Role{}
419+
var childClient = &keycloak.GenericClient{}
420+
421+
parentClientName := acctest.RandomWithPrefix("tf-acc")
422+
parentRoleName := acctest.RandomWithPrefix("tf-acc")
423+
childClientName := acctest.RandomWithPrefix("tf-acc")
424+
425+
resource.Test(t, resource.TestCase{
426+
ProviderFactories: testAccProviderFactories,
427+
PreCheck: func() { testAccPreCheck(t) },
428+
Steps: []resource.TestStep{
429+
{
430+
Config: testKeycloakGenericClientRoleMapper_basic(parentClientName, parentRoleName, childClientName),
431+
Check: resource.ComposeTestCheckFunc(
432+
testAccCheckKeycloakGenericClientRoleMapperExists("keycloak_generic_client_role_mapper.child-client-with-parent-client-role"),
433+
testAccCheckKeycloakRoleFetch("keycloak_role.parent-role", role),
434+
testAccCheckKeycloakGenericClientFetch("keycloak_openid_client.child-client", childClient),
435+
),
436+
},
437+
{
438+
PreConfig: func() {
439+
err := keycloakClient.DeleteRoleScopeMapping(testCtx, childClient.RealmId, childClient.Id, "", role)
440+
if err != nil {
441+
t.Fatalf("Error deleting client role mapping: %s", err)
442+
}
443+
},
444+
Config: testKeycloakGenericClientRoleMapper_basic(parentClientName, parentRoleName, childClientName),
445+
Check: testAccCheckKeycloakGenericClientRoleMapperExists("keycloak_generic_client_role_mapper.child-client-with-parent-client-role"),
446+
},
447+
},
448+
})
449+
}
450+
451+
func testKeycloakGenericClientRoleMapper_realmRole(roleName, childClientName string) string {
452+
return fmt.Sprintf(`
453+
data "keycloak_realm" "realm" {
454+
realm = "%s"
455+
}
456+
457+
resource "keycloak_role" "realm-role" {
458+
realm_id = data.keycloak_realm.realm.id
459+
name = "%s"
460+
}
461+
462+
resource "keycloak_openid_client" "child-client" {
463+
realm_id = data.keycloak_realm.realm.id
464+
client_id = "%s"
465+
access_type = "PUBLIC"
466+
}
467+
468+
resource "keycloak_generic_client_role_mapper" "child-client-with-realm-role" {
469+
realm_id = data.keycloak_realm.realm.id
470+
client_id = keycloak_openid_client.child-client.id
471+
role_id = keycloak_role.realm-role.id
472+
}
473+
`, testAccRealm.Realm, roleName, childClientName)
474+
}

0 commit comments

Comments
 (0)