11package provider
22
33import (
4+ "encoding/json"
45 "fmt"
6+ "reflect"
57 "testing"
68
79 "github.com/hashicorp/terraform-plugin-sdk/v2/helper/acctest"
@@ -31,39 +33,114 @@ func TestAccKeycloakRealmClientPolicyProfile_basicWithExecutor(t *testing.T) {
3133 resourceName := "test-profile-with-executor"
3234 description := "Test description with executor"
3335 executorName := "pkce-enforcer"
36+ configuration := map [string ]interface {}{
37+ "auto-configure" : "true" ,
38+ }
3439
3540 resource .Test (t , resource.TestCase {
3641 ProviderFactories : testAccProviderFactories ,
3742 PreCheck : func () { testAccPreCheck (t ) },
3843 Steps : []resource.TestStep {
3944 {
40- Config : testKeycloakRealmClientPolicyProfile_basicWithExecutor (realmName , resourceName , description , executorName ),
45+ Config : testKeycloakRealmClientPolicyProfile_basicWithExecutor (realmName , resourceName , description , executorName , testKeycloakRealmClientPolicyProfile_mapConfig ( configuration ) ),
4146 Check : testAccCheckKeycloakRealmClientPolicyProfileWithExecutorExists (realmName , resourceName , executorName ),
4247 },
4348 },
4449 })
4550}
4651
52+ func TestAccKeycloakRealmClientPolicyProfile_basicWithExecutorAndJSON (t * testing.T ) {
53+ realmName := acctest .RandomWithPrefix ("tf-acc" )
54+ resourceName := "test-profile-with-executor-and-configuration"
55+ description := "Test description with executor and configuration"
56+ executorName := "secure-client-authenticator"
57+ configuration := map [string ]interface {}{
58+ "allowed-client-authenticators" : []string {"client-secret" , "client-secret-jwt" },
59+ "default-client-authenticator" : "client-secret" ,
60+ }
61+
62+ resource .Test (t , resource.TestCase {
63+ ProviderFactories : testAccProviderFactories ,
64+ PreCheck : func () { testAccPreCheck (t ) },
65+ Steps : []resource.TestStep {
66+ {
67+ Config : testKeycloakRealmClientPolicyProfile_basicWithExecutor (realmName , resourceName , description , executorName , testKeycloakRealmClientPolicyProfile_mapConfig (configuration )),
68+ Check : testAccCheckKeycloakRealmClientPolicyProfileWithExecutorMatches (realmName , resourceName , executorName , configuration ),
69+ },
70+ },
71+ })
72+ }
73+
4774func TestAccKeycloakRealmClientPolicyProfile_basicWithPolicy (t * testing.T ) {
4875 realmName := acctest .RandomWithPrefix ("tf-acc" )
4976 profileName := "test-profile"
5077 profileDescription := "Test profile description"
5178 policyName := "test-policy"
5279 policyDescription := "Test policy description"
5380 conditionName := "client-updater-source-roles"
81+ configuration := map [string ]interface {}{
82+ "is_negative_logic" : false ,
83+ "attributes" : []map [string ]string {
84+ {
85+ "key" : "test-key" ,
86+ "value" : "test-value" ,
87+ },
88+ },
89+ }
5490
5591 resource .Test (t , resource.TestCase {
5692 ProviderFactories : testAccProviderFactories ,
5793 PreCheck : func () { testAccPreCheck (t ) },
5894 Steps : []resource.TestStep {
5995 {
60- Config : testKeycloakRealmClientPolicyProfile_basicWithPolicy (realmName , profileName , profileDescription , policyName , policyDescription , conditionName ),
96+ Config : testKeycloakRealmClientPolicyProfile_basicWithPolicy (realmName , profileName , profileDescription , policyName , policyDescription , conditionName , testKeycloakRealmClientPolicyProfile_mapConfig ( configuration ) ),
6197 Check : testAccCheckKeycloakRealmClientPolicyProfilePolicyExists (realmName , policyName ),
6298 },
6399 },
64100 })
65101}
66102
103+ func TestAccKeycloakRealmClientPolicyProfile_basicWithPolicyAndJSON (t * testing.T ) {
104+ realmName := acctest .RandomWithPrefix ("tf-acc" )
105+ profileName := "test-profile"
106+ profileDescription := "Test profile description"
107+ policyName := "test-policy"
108+ policyDescription := "Test policy description"
109+ conditionName := "client-updater-context"
110+ configuration := map [string ]interface {}{
111+ "is_negative_logic" : false ,
112+ "update-client-source" : []string {"ByInitialAccessToken" , "ByRegistrationAccessToken" },
113+ }
114+
115+ resource .Test (t , resource.TestCase {
116+ ProviderFactories : testAccProviderFactories ,
117+ PreCheck : func () { testAccPreCheck (t ) },
118+ Steps : []resource.TestStep {
119+ {
120+ Config : testKeycloakRealmClientPolicyProfile_basicWithPolicy (realmName , profileName , profileDescription , policyName , policyDescription , conditionName , testKeycloakRealmClientPolicyProfile_mapConfig (configuration )),
121+ Check : testAccCheckKeycloakRealmClientPolicyProfilePolicyMatches (realmName , policyName , conditionName , configuration ),
122+ },
123+ },
124+ })
125+ }
126+
127+ func testKeycloakRealmClientPolicyProfile_mapConfig (configuration map [string ]interface {}) string {
128+ var s string = "{"
129+ for k , v := range configuration {
130+ switch reflect .TypeOf (v ).Kind () {
131+ case reflect .Map , reflect .Slice :
132+ jsonStr , _ := json .Marshal (v )
133+ s += fmt .Sprintf ("%s = jsonencode(%s)\n " , k , string (jsonStr ))
134+ case reflect .String :
135+ s += fmt .Sprintf ("%s = \" %v\" \n " , k , v )
136+ default :
137+ s += fmt .Sprintf ("%s = %v\n " , k , v )
138+ }
139+ }
140+ s += "}"
141+ return s
142+ }
143+
67144func testKeycloakRealmClientPolicyProfile_basic (realm string , name string , description string ) string {
68145 return fmt .Sprintf (`
69146resource "keycloak_realm" "realm" {
@@ -78,7 +155,7 @@ resource "keycloak_realm_client_policy_profile" "profile" {
78155 ` , realm , name , description )
79156}
80157
81- func testKeycloakRealmClientPolicyProfile_basicWithExecutor (realm string , name string , description string , executorName string ) string {
158+ func testKeycloakRealmClientPolicyProfile_basicWithExecutor (realm string , name string , description string , executorName string , configuration string ) string {
82159 return fmt .Sprintf (`
83160resource "keycloak_realm" "realm" {
84161 realm = "%s"
@@ -91,15 +168,13 @@ resource "keycloak_realm_client_policy_profile" "profile" {
91168
92169 executor {
93170 name = "%s"
94- configuration = {
95- auto-configure = "true"
96- }
171+ configuration = %s
97172 }
98173}
99- ` , realm , name , description , executorName )
174+ ` , realm , name , description , executorName , configuration )
100175}
101176
102- func testKeycloakRealmClientPolicyProfile_basicWithPolicy (realm string , profileName string , profileDescription string , policyName string , policyDescription string , conditionName string ) string {
177+ func testKeycloakRealmClientPolicyProfile_basicWithPolicy (realm string , profileName string , profileDescription string , policyName string , policyDescription string , conditionName string , configuration string ) string {
103178 return fmt .Sprintf (`
104179resource "keycloak_realm" "realm" {
105180 realm = "%s"
@@ -122,13 +197,10 @@ resource "keycloak_realm_client_policy_profile_policy" "policy" {
122197
123198 condition {
124199 name = "%s"
125- configuration = {
126- is_negative_logic = false
127- attributes = jsonencode([{"key": "test-key", "value": "test-value"}])
128- }
200+ configuration = %s
129201 }
130202}
131- ` , realm , profileName , profileDescription , policyName , policyDescription , conditionName )
203+ ` , realm , profileName , profileDescription , policyName , policyDescription , conditionName , configuration )
132204}
133205
134206func testAccCheckKeycloakRealmClientPolicyProfileExists (realm string , profileName string ) resource.TestCheckFunc {
@@ -157,6 +229,29 @@ func testAccCheckKeycloakRealmClientPolicyProfileWithExecutorExists(realm string
157229 }
158230}
159231
232+ func testAccCheckKeycloakRealmClientPolicyProfileWithExecutorMatches (realm string , profileName string , executorName string , configuration map [string ]interface {}) resource.TestCheckFunc {
233+ return func (s * terraform.State ) error {
234+ profile , err := keycloakClient .GetRealmClientPolicyProfileByName (testCtx , realm , profileName )
235+ if err != nil {
236+ return fmt .Errorf ("Client policy profile not found: %s" , profileName )
237+ }
238+
239+ if profile .Executors [0 ].Name != executorName {
240+ return fmt .Errorf ("Client policy profile executor not found: %s" , executorName )
241+ }
242+
243+ for k , got := range profile .Executors [0 ].Configuration {
244+ want := configuration [k ]
245+
246+ if ! equalsIgnoreType (got , want ) {
247+ return fmt .Errorf ("Client policy profile executor configuration does not match: want %v, got %v" , want , got )
248+ }
249+ }
250+
251+ return nil
252+ }
253+ }
254+
160255func testAccCheckKeycloakRealmClientPolicyProfilePolicyExists (realm string , policyName string ) resource.TestCheckFunc {
161256 return func (s * terraform.State ) error {
162257 _ , err := keycloakClient .GetRealmClientPolicyProfilePolicyByName (testCtx , realm , policyName )
@@ -167,3 +262,26 @@ func testAccCheckKeycloakRealmClientPolicyProfilePolicyExists(realm string, poli
167262 return nil
168263 }
169264}
265+
266+ func testAccCheckKeycloakRealmClientPolicyProfilePolicyMatches (realm string , policyName string , conditionName string , configuration map [string ]interface {}) resource.TestCheckFunc {
267+ return func (s * terraform.State ) error {
268+ policy , err := keycloakClient .GetRealmClientPolicyProfilePolicyByName (testCtx , realm , policyName )
269+ if err != nil {
270+ return fmt .Errorf ("Client policy profile policy not found: %s" , policyName )
271+ }
272+
273+ if policy .Conditions [0 ].Name != conditionName {
274+ return fmt .Errorf ("Client policy profile policy condition not found: %s" , conditionName )
275+ }
276+
277+ for k , got := range policy .Conditions [0 ].Configuration {
278+ want := configuration [k ]
279+
280+ if ! equalsIgnoreType (got , want ) {
281+ return fmt .Errorf ("Client policy profile policy condition configuration does not match: want %v, got %v" , want , got )
282+ }
283+ }
284+
285+ return nil
286+ }
287+ }
0 commit comments