@@ -3,7 +3,6 @@ package federated_identity_provider
33import (
44 "context"
55 "testing"
6- "time"
76
87 "github.com/hashicorp/terraform-plugin-framework/attr"
98 "github.com/hashicorp/terraform-plugin-framework/types"
@@ -30,12 +29,11 @@ func assertionsListFromModels(t *testing.T, ctx context.Context, assertions []As
3029 return listValue
3130}
3231
32+ func ptrString (s string ) * string { return & s }
33+
3334func TestMapFields (t * testing.T ) {
3435 ctx := context .Background ()
3536
36- createdAt := time .Date (2026 , 1 , 2 , 3 , 4 , 5 , 0 , time .UTC )
37- updatedAt := time .Date (2026 , 2 , 3 , 4 , 5 , 6 , 0 , time .UTC )
38-
3937 tests := []struct {
4038 description string
4139 input * serviceaccount.FederatedIdentityProvider
@@ -50,10 +48,9 @@ func TestMapFields(t *testing.T) {
5048 projectID : "pid" ,
5149 serviceAccountEmail : "service-account@sa.stackit.cloud" ,
5250 input : & serviceaccount.FederatedIdentityProvider {
53- Name : "provider-name" ,
54- Issuer : "https://issuer.example.com" ,
55- CreatedAt : createdAt ,
56- UpdatedAt : updatedAt ,
51+ Id : ptrString ("fed-uuid-123" ),
52+ Name : "provider-name" ,
53+ Issuer : "https://issuer.example.com" ,
5754 Assertions : []serviceaccount.FederatedIdentityProviderAssertionsInner {
5855 {Item : "iss" , Operator : "EQUALS" , Value : "https://issuer.example.com" },
5956 {Item : "sub" , Operator : "EQUALS" , Value : "user@example.com" },
@@ -107,15 +104,12 @@ func TestMapFields(t *testing.T) {
107104 if model .Id .ValueString () != "pid,service-account@sa.stackit.cloud,provider-name" {
108105 t .Fatalf ("id mismatch: got %q" , model .Id .ValueString ())
109106 }
107+ if model .FederationId .ValueString () != "fed-uuid-123" {
108+ t .Fatalf ("federation_id mismatch: got %q" , model .FederationId .ValueString ())
109+ }
110110 if model .Issuer .ValueString () != "https://issuer.example.com" {
111111 t .Fatalf ("issuer mismatch: got %q" , model .Issuer .ValueString ())
112112 }
113- if model .CreatedAt .ValueString () != createdAt .Format (time .RFC3339 ) {
114- t .Fatalf ("created_at mismatch: got %q" , model .CreatedAt .ValueString ())
115- }
116- if model .UpdatedAt .ValueString () != updatedAt .Format (time .RFC3339 ) {
117- t .Fatalf ("updated_at mismatch: got %q" , model .UpdatedAt .ValueString ())
118- }
119113 }
120114
121115 if tt .expectAssertionsNull {
@@ -125,12 +119,6 @@ func TestMapFields(t *testing.T) {
125119 if ! model .Issuer .IsNull () {
126120 t .Fatalf ("expected issuer to be null" )
127121 }
128- if ! model .CreatedAt .IsNull () {
129- t .Fatalf ("expected created_at to be null" )
130- }
131- if ! model .UpdatedAt .IsNull () {
132- t .Fatalf ("expected updated_at to be null" )
133- }
134122 return
135123 }
136124
@@ -257,3 +245,115 @@ func TestToCreatePayload(t *testing.T) {
257245 })
258246 }
259247}
248+
249+ func TestToUpdatePayload (t * testing.T ) {
250+ ctx := context .Background ()
251+
252+ validAssertions := []AssertionModel {
253+ {Item : types .StringValue ("aud" ), Operator : types .StringValue ("equals" ), Value : types .StringValue ("https://example.com" )},
254+ {Item : types .StringValue ("sub" ), Operator : types .StringValue ("equals" ), Value : types .StringValue ("user@example.com" )},
255+ }
256+
257+ tests := []struct {
258+ description string
259+ model * Model
260+ expectError bool
261+ }{
262+ {
263+ description : "all_fields_set" ,
264+ model : & Model {
265+ Name : types .StringValue ("provider-name" ),
266+ Issuer : types .StringValue ("https://issuer.example.com" ),
267+ Assertions : assertionsListFromModels (t , ctx , validAssertions ),
268+ },
269+ },
270+ {
271+ description : "null_assertions_replaces_external" ,
272+ model : & Model {
273+ Name : types .StringValue ("provider-name" ),
274+ Issuer : types .StringValue ("https://issuer.example.com" ),
275+ Assertions : types .ListNull (types.ObjectType {
276+ AttrTypes : map [string ]attr.Type {
277+ "item" : types .StringType ,
278+ "operator" : types .StringType ,
279+ "value" : types .StringType ,
280+ },
281+ }),
282+ },
283+ },
284+ {
285+ description : "null_issuer_and_name" ,
286+ model : & Model {
287+ Name : types .StringNull (),
288+ Issuer : types .StringNull (),
289+ Assertions : assertionsListFromModels (t , ctx , validAssertions [:1 ]),
290+ },
291+ },
292+ {
293+ description : "invalid_assertions_type" ,
294+ model : & Model {
295+ Name : types .StringValue ("provider-name" ),
296+ Issuer : types .StringValue ("https://issuer.example.com" ),
297+ Assertions : types .ListValueMust (types .StringType , []attr.Value {types .StringValue ("not-an-object" )}),
298+ },
299+ expectError : true ,
300+ },
301+ }
302+
303+ for _ , tt := range tests {
304+ t .Run (tt .description , func (t * testing.T ) {
305+ payload , err := toUpdatePayload (ctx , tt .model )
306+ if tt .expectError {
307+ if err == nil {
308+ t .Fatalf ("expected error but got nil" )
309+ }
310+ if payload != nil {
311+ t .Fatalf ("expected nil payload on error" )
312+ }
313+ return
314+ }
315+ if err != nil {
316+ t .Fatalf ("unexpected error: %v" , err )
317+ }
318+
319+ switch tt .description {
320+ case "all_fields_set" :
321+ if payload .Name != "provider-name" {
322+ t .Fatalf ("name mismatch: got %q" , payload .Name )
323+ }
324+ if payload .Issuer != "https://issuer.example.com" {
325+ t .Fatalf ("issuer mismatch: got %q" , payload .Issuer )
326+ }
327+ if len (payload .Assertions ) != 2 {
328+ t .Fatalf ("assertions length mismatch: got %d, expected 2" , len (payload .Assertions ))
329+ }
330+ if payload .Assertions [0 ].Item == nil || * payload .Assertions [0 ].Item != "aud" {
331+ t .Fatalf ("assertions[0].item mismatch" )
332+ }
333+ if payload .Assertions [0 ].Operator == nil || * payload .Assertions [0 ].Operator != "equals" {
334+ t .Fatalf ("assertions[0].operator mismatch" )
335+ }
336+ if payload .Assertions [0 ].Value == nil || * payload .Assertions [0 ].Value != "https://example.com" {
337+ t .Fatalf ("assertions[0].value mismatch" )
338+ }
339+ if payload .Assertions [1 ].Item == nil || * payload .Assertions [1 ].Item != "sub" {
340+ t .Fatalf ("assertions[1].item mismatch" )
341+ }
342+ case "null_assertions_replaces_external" :
343+ if len (payload .Assertions ) != 0 {
344+ t .Fatalf ("expected assertions to be empty when null, got %d" , len (payload .Assertions ))
345+ }
346+ case "null_issuer_and_name" :
347+ if payload .Issuer != "" {
348+ t .Fatalf ("expected empty issuer for null, got %q" , payload .Issuer )
349+ }
350+ if payload .Name != "" {
351+ t .Fatalf ("expected empty name for null, got %q" , payload .Name )
352+ }
353+ if len (payload .Assertions ) != 1 {
354+ t .Fatalf ("assertions length mismatch: got %d, expected 1" , len (payload .Assertions ))
355+ }
356+ }
357+ })
358+ }
359+ }
0 commit comments