|
| 1 | +package provider_test |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "testing" |
| 6 | + "time" |
| 7 | + |
| 8 | + "github.com/hashicorp/terraform-plugin-framework/attr" |
| 9 | + "github.com/hashicorp/terraform-plugin-framework/diag" |
| 10 | + "github.com/hashicorp/terraform-plugin-framework/types" |
| 11 | + "github.com/pomerium/enterprise-client-go/pb" |
| 12 | + "github.com/pomerium/enterprise-terraform-provider/internal/provider" |
| 13 | + "github.com/stretchr/testify/assert" |
| 14 | + "github.com/stretchr/testify/require" |
| 15 | + "google.golang.org/protobuf/types/known/durationpb" |
| 16 | +) |
| 17 | + |
| 18 | +func TestFromStringSlice(t *testing.T) { |
| 19 | + tests := []struct { |
| 20 | + name string |
| 21 | + input []string |
| 22 | + expected types.List |
| 23 | + }{ |
| 24 | + { |
| 25 | + name: "nil slice", |
| 26 | + input: nil, |
| 27 | + expected: types.ListNull(types.StringType), |
| 28 | + }, |
| 29 | + { |
| 30 | + name: "empty slice", |
| 31 | + input: []string{}, |
| 32 | + expected: types.ListValueMust(types.StringType, []attr.Value{}), |
| 33 | + }, |
| 34 | + { |
| 35 | + name: "normal slice", |
| 36 | + input: []string{"a", "b", "c"}, |
| 37 | + expected: types.ListValueMust(types.StringType, []attr.Value{ |
| 38 | + types.StringValue("a"), |
| 39 | + types.StringValue("b"), |
| 40 | + types.StringValue("c"), |
| 41 | + }), |
| 42 | + }, |
| 43 | + } |
| 44 | + |
| 45 | + for _, tt := range tests { |
| 46 | + t.Run(tt.name, func(t *testing.T) { |
| 47 | + result := provider.FromStringSlice(tt.input) |
| 48 | + assert.Equal(t, tt.expected, result) |
| 49 | + }) |
| 50 | + } |
| 51 | +} |
| 52 | + |
| 53 | +func TestFromDurationP(t *testing.T) { |
| 54 | + tests := []struct { |
| 55 | + name string |
| 56 | + input *durationpb.Duration |
| 57 | + expected types.String |
| 58 | + }{ |
| 59 | + { |
| 60 | + name: "nil duration", |
| 61 | + input: nil, |
| 62 | + expected: types.StringNull(), |
| 63 | + }, |
| 64 | + { |
| 65 | + name: "zero duration", |
| 66 | + input: durationpb.New(0), |
| 67 | + expected: types.StringValue("0s"), |
| 68 | + }, |
| 69 | + { |
| 70 | + name: "normal duration", |
| 71 | + input: durationpb.New(time.Hour + time.Minute), |
| 72 | + expected: types.StringValue("1h1m0s"), |
| 73 | + }, |
| 74 | + } |
| 75 | + |
| 76 | + for _, tt := range tests { |
| 77 | + t.Run(tt.name, func(t *testing.T) { |
| 78 | + result := provider.FromDuration(tt.input) |
| 79 | + assert.Equal(t, tt.expected, result) |
| 80 | + }) |
| 81 | + } |
| 82 | +} |
| 83 | + |
| 84 | +func TestToStringList(t *testing.T) { |
| 85 | + ctx := context.Background() |
| 86 | + tests := []struct { |
| 87 | + name string |
| 88 | + input types.List |
| 89 | + expectError bool |
| 90 | + validate func(*testing.T, *pb.Settings_StringList) |
| 91 | + }{ |
| 92 | + { |
| 93 | + name: "null list", |
| 94 | + input: types.ListNull(types.StringType), |
| 95 | + validate: func(t *testing.T, s *pb.Settings_StringList) { |
| 96 | + assert.Nil(t, s) |
| 97 | + }, |
| 98 | + }, |
| 99 | + { |
| 100 | + name: "empty list", |
| 101 | + input: types.ListValueMust(types.StringType, []attr.Value{}), |
| 102 | + validate: func(t *testing.T, s *pb.Settings_StringList) { |
| 103 | + require.NotNil(t, s) |
| 104 | + assert.Empty(t, s.Values) |
| 105 | + }, |
| 106 | + }, |
| 107 | + { |
| 108 | + name: "valid list", |
| 109 | + input: types.ListValueMust(types.StringType, []attr.Value{ |
| 110 | + types.StringValue("value1"), |
| 111 | + types.StringValue("value2"), |
| 112 | + }), |
| 113 | + validate: func(t *testing.T, s *pb.Settings_StringList) { |
| 114 | + require.NotNil(t, s) |
| 115 | + assert.Equal(t, []string{"value1", "value2"}, s.Values) |
| 116 | + }, |
| 117 | + }, |
| 118 | + } |
| 119 | + |
| 120 | + for _, tt := range tests { |
| 121 | + t.Run(tt.name, func(t *testing.T) { |
| 122 | + var result *pb.Settings_StringList |
| 123 | + diagnostics := diag.Diagnostics{} |
| 124 | + provider.ToStringList(ctx, &result, tt.input, &diagnostics) |
| 125 | + |
| 126 | + if tt.expectError { |
| 127 | + assert.True(t, diagnostics.HasError()) |
| 128 | + return |
| 129 | + } |
| 130 | + |
| 131 | + assert.False(t, diagnostics.HasError()) |
| 132 | + if tt.validate != nil { |
| 133 | + tt.validate(t, result) |
| 134 | + } |
| 135 | + }) |
| 136 | + } |
| 137 | +} |
0 commit comments