|
| 1 | +package provider_test |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "testing" |
| 6 | + |
| 7 | + "github.com/hashicorp/terraform-plugin-framework/types" |
| 8 | + "github.com/pomerium/enterprise-client-go/pb" |
| 9 | + "github.com/pomerium/enterprise-terraform-provider/internal/provider" |
| 10 | + "github.com/stretchr/testify/assert" |
| 11 | + "github.com/stretchr/testify/require" |
| 12 | +) |
| 13 | + |
| 14 | +func TestConvertRouteFromPB(t *testing.T) { |
| 15 | + t.Run("jwt_issuer_format", func(t *testing.T) { |
| 16 | + testCases := []struct { |
| 17 | + name string |
| 18 | + input pb.IssuerFormat |
| 19 | + expected string |
| 20 | + isNull bool |
| 21 | + }{ |
| 22 | + { |
| 23 | + name: "host_only", |
| 24 | + input: pb.IssuerFormat_IssuerHostOnly, |
| 25 | + expected: "IssuerHostOnly", |
| 26 | + }, |
| 27 | + { |
| 28 | + name: "uri", |
| 29 | + input: pb.IssuerFormat_IssuerURI, |
| 30 | + expected: "IssuerURI", |
| 31 | + }, |
| 32 | + { |
| 33 | + name: "invalid value", |
| 34 | + input: pb.IssuerFormat(999), |
| 35 | + isNull: true, |
| 36 | + }, |
| 37 | + } |
| 38 | + |
| 39 | + for _, tc := range testCases { |
| 40 | + t.Run(tc.name, func(t *testing.T) { |
| 41 | + m := &provider.RouteModel{} |
| 42 | + r := &pb.Route{ |
| 43 | + JwtIssuerFormat: tc.input, |
| 44 | + } |
| 45 | + diags := provider.ConvertRouteFromPB(m, r) |
| 46 | + require.False(t, diags.HasError()) |
| 47 | + if tc.isNull { |
| 48 | + assert.True(t, m.JWTIssuerFormat.IsNull()) |
| 49 | + } else { |
| 50 | + assert.Equal(t, tc.expected, m.JWTIssuerFormat.ValueString()) |
| 51 | + } |
| 52 | + }) |
| 53 | + } |
| 54 | + }) |
| 55 | +} |
| 56 | + |
| 57 | +func TestConvertRouteToPB(t *testing.T) { |
| 58 | + t.Run("jwt_issuer_format", func(t *testing.T) { |
| 59 | + testCases := []struct { |
| 60 | + name string |
| 61 | + input string |
| 62 | + expected pb.IssuerFormat |
| 63 | + expectError bool |
| 64 | + }{ |
| 65 | + {"host_only", "IssuerHostOnly", pb.IssuerFormat_IssuerHostOnly, false}, |
| 66 | + {"uri", "IssuerURI", pb.IssuerFormat_IssuerURI, false}, |
| 67 | + {"invalid_value", "invalid_value", -1, true}, |
| 68 | + } |
| 69 | + |
| 70 | + for _, tc := range testCases { |
| 71 | + t.Run(tc.name, func(t *testing.T) { |
| 72 | + m := &provider.RouteModel{ |
| 73 | + JWTIssuerFormat: types.StringValue(tc.input), |
| 74 | + } |
| 75 | + r, diag := provider.ConvertRouteToPB(context.Background(), m) |
| 76 | + if tc.expectError { |
| 77 | + require.True(t, diag.HasError()) |
| 78 | + } else { |
| 79 | + require.False(t, diag.HasError()) |
| 80 | + assert.Equal(t, tc.expected, r.JwtIssuerFormat) |
| 81 | + } |
| 82 | + }) |
| 83 | + } |
| 84 | + }) |
| 85 | +} |
0 commit comments