|
| 1 | +package blueprint_permissions |
| 2 | + |
| 3 | +import ( |
| 4 | + "testing" |
| 5 | + |
| 6 | + "github.com/hashicorp/terraform-plugin-framework/types" |
| 7 | + "github.com/port-labs/terraform-provider-port-labs/v2/internal/cli" |
| 8 | +) |
| 9 | + |
| 10 | +func TestRefreshBlueprintPermissionsStateWithNilUpdateMetadataProperties(t *testing.T) { |
| 11 | + state := &BlueprintPermissionsModel{ |
| 12 | + ID: types.StringValue("testBlueprint"), |
| 13 | + BlueprintIdentifier: types.StringValue("testBlueprint"), |
| 14 | + Entities: nil, |
| 15 | + } |
| 16 | + |
| 17 | + ownedByTeam := false |
| 18 | + apiResponse := &cli.BlueprintPermissions{ |
| 19 | + Entities: cli.BlueprintPermissionsEntities{ |
| 20 | + Register: cli.BlueprintPermissionsBlock{ |
| 21 | + Users: []string{}, |
| 22 | + Roles: []string{"Admin"}, |
| 23 | + Teams: []string{}, |
| 24 | + OwnedByTeam: &ownedByTeam, |
| 25 | + }, |
| 26 | + Unregister: cli.BlueprintPermissionsBlock{ |
| 27 | + Users: []string{}, |
| 28 | + Roles: []string{"Admin"}, |
| 29 | + Teams: []string{}, |
| 30 | + OwnedByTeam: &ownedByTeam, |
| 31 | + }, |
| 32 | + Update: cli.BlueprintPermissionsBlock{ |
| 33 | + Users: []string{}, |
| 34 | + Roles: []string{"Admin"}, |
| 35 | + Teams: []string{}, |
| 36 | + OwnedByTeam: &ownedByTeam, |
| 37 | + }, |
| 38 | + UpdateProperties: cli.BlueprintRolesOrPropertiesPermissionsBlock{ |
| 39 | + "$title": cli.BlueprintPermissionsBlock{ |
| 40 | + Users: []string{}, |
| 41 | + Roles: []string{"Member"}, |
| 42 | + Teams: []string{}, |
| 43 | + OwnedByTeam: &ownedByTeam, |
| 44 | + }, |
| 45 | + "$identifier": cli.BlueprintPermissionsBlock{ |
| 46 | + Users: []string{}, |
| 47 | + Roles: []string{"Member"}, |
| 48 | + Teams: []string{}, |
| 49 | + OwnedByTeam: &ownedByTeam, |
| 50 | + }, |
| 51 | + }, |
| 52 | + }, |
| 53 | + } |
| 54 | + |
| 55 | + err := refreshBlueprintPermissionsState(state, apiResponse, "testBlueprint") |
| 56 | + if err != nil { |
| 57 | + t.Fatalf("refreshBlueprintPermissionsState failed: %v", err) |
| 58 | + } |
| 59 | + if state.Entities == nil { |
| 60 | + t.Fatal("Entities should not be nil after refresh") |
| 61 | + } |
| 62 | + |
| 63 | + if state.Entities.UpdateMetadataProperties == nil { |
| 64 | + t.Fatal("UpdateMetadataProperties should not be nil after refresh") |
| 65 | + } |
| 66 | + |
| 67 | + if state.Entities.UpdateMetadataProperties.Title == nil { |
| 68 | + t.Fatal("UpdateMetadataProperties.Title should not be nil") |
| 69 | + } |
| 70 | + |
| 71 | + if state.Entities.UpdateMetadataProperties.Identifier == nil { |
| 72 | + t.Fatal("UpdateMetadataProperties.Identifier should not be nil") |
| 73 | + } |
| 74 | + |
| 75 | + if len(state.Entities.UpdateMetadataProperties.Title.Roles) != 1 { |
| 76 | + t.Errorf("Expected 1 role for Title, got %d", len(state.Entities.UpdateMetadataProperties.Title.Roles)) |
| 77 | + } |
| 78 | + |
| 79 | + if state.Entities.UpdateMetadataProperties.Title.Roles[0].ValueString() != "Member" { |
| 80 | + t.Errorf("Expected role 'Member' for Title, got %s", state.Entities.UpdateMetadataProperties.Title.Roles[0].ValueString()) |
| 81 | + } |
| 82 | +} |
| 83 | + |
| 84 | +func TestRefreshBlueprintPermissionsStateWithExistingUpdateMetadataProperties(t *testing.T) { |
| 85 | + ownedByTeam := false |
| 86 | + state := &BlueprintPermissionsModel{ |
| 87 | + ID: types.StringValue("testBlueprint"), |
| 88 | + BlueprintIdentifier: types.StringValue("testBlueprint"), |
| 89 | + Entities: &EntitiesBlueprintPermissionsModel{ |
| 90 | + UpdateMetadataProperties: &BlueprintMetadataPermissionsTFBlock{ |
| 91 | + Title: &BlueprintPermissionsTFBlock{ |
| 92 | + Users: []types.String{types.StringValue("user1@example.com")}, |
| 93 | + Roles: []types.String{types.StringValue("Admin")}, |
| 94 | + Teams: []types.String{}, |
| 95 | + OwnedByTeam: types.BoolValue(ownedByTeam), |
| 96 | + }, |
| 97 | + }, |
| 98 | + }, |
| 99 | + } |
| 100 | + |
| 101 | + apiResponse := &cli.BlueprintPermissions{ |
| 102 | + Entities: cli.BlueprintPermissionsEntities{ |
| 103 | + Register: cli.BlueprintPermissionsBlock{ |
| 104 | + Users: []string{}, |
| 105 | + Roles: []string{"Admin"}, |
| 106 | + Teams: []string{}, |
| 107 | + OwnedByTeam: &ownedByTeam, |
| 108 | + }, |
| 109 | + Unregister: cli.BlueprintPermissionsBlock{ |
| 110 | + Users: []string{}, |
| 111 | + Roles: []string{"Admin"}, |
| 112 | + Teams: []string{}, |
| 113 | + OwnedByTeam: &ownedByTeam, |
| 114 | + }, |
| 115 | + Update: cli.BlueprintPermissionsBlock{ |
| 116 | + Users: []string{}, |
| 117 | + Roles: []string{"Admin"}, |
| 118 | + Teams: []string{}, |
| 119 | + OwnedByTeam: &ownedByTeam, |
| 120 | + }, |
| 121 | + UpdateProperties: cli.BlueprintRolesOrPropertiesPermissionsBlock{ |
| 122 | + "$title": cli.BlueprintPermissionsBlock{ |
| 123 | + Users: []string{"user1@example.com", "user2@example.com"}, |
| 124 | + Roles: []string{"Member"}, |
| 125 | + Teams: []string{}, |
| 126 | + OwnedByTeam: &ownedByTeam, |
| 127 | + }, |
| 128 | + }, |
| 129 | + }, |
| 130 | + } |
| 131 | + |
| 132 | + err := refreshBlueprintPermissionsState(state, apiResponse, "testBlueprint") |
| 133 | + if err != nil { |
| 134 | + t.Fatalf("refreshBlueprintPermissionsState failed: %v", err) |
| 135 | + } |
| 136 | + |
| 137 | + if state.Entities.UpdateMetadataProperties.Title == nil { |
| 138 | + t.Fatal("UpdateMetadataProperties.Title should not be nil") |
| 139 | + } |
| 140 | + |
| 141 | + if len(state.Entities.UpdateMetadataProperties.Title.Users) != 2 { |
| 142 | + t.Errorf("Expected 2 users for Title, got %d", len(state.Entities.UpdateMetadataProperties.Title.Users)) |
| 143 | + } |
| 144 | +} |
0 commit comments