|
| 1 | +package provider |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "testing" |
| 6 | + |
| 7 | + "github.com/google/go-cmp/cmp" |
| 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/stretchr/testify/assert" |
| 13 | + "google.golang.org/protobuf/testing/protocmp" |
| 14 | +) |
| 15 | + |
| 16 | +func TestJWTGroupsFilterFromPB(t *testing.T) { |
| 17 | + tests := []struct { |
| 18 | + name string |
| 19 | + input *pb.JwtGroupsFilter |
| 20 | + expected types.Object |
| 21 | + }{ |
| 22 | + { |
| 23 | + name: "nil input", |
| 24 | + input: nil, |
| 25 | + expected: types.ObjectNull(jwtGroupsFilterSchemaAttr), |
| 26 | + }, |
| 27 | + { |
| 28 | + name: "empty groups", |
| 29 | + input: &pb.JwtGroupsFilter{ |
| 30 | + Groups: []string{}, |
| 31 | + InferFromPpl: false, |
| 32 | + }, |
| 33 | + expected: types.ObjectValueMust(jwtGroupsFilterSchemaAttr, map[string]attr.Value{ |
| 34 | + "groups": types.SetValueMust(types.StringType, []attr.Value{}), |
| 35 | + "infer_from_ppl": types.BoolValue(false), |
| 36 | + }), |
| 37 | + }, |
| 38 | + { |
| 39 | + name: "with groups", |
| 40 | + input: &pb.JwtGroupsFilter{ |
| 41 | + Groups: []string{"group1", "group2"}, |
| 42 | + InferFromPpl: true, |
| 43 | + }, |
| 44 | + expected: types.ObjectValueMust(jwtGroupsFilterSchemaAttr, map[string]attr.Value{ |
| 45 | + "groups": types.SetValueMust(types.StringType, []attr.Value{ |
| 46 | + types.StringValue("group1"), |
| 47 | + types.StringValue("group2"), |
| 48 | + }), |
| 49 | + "infer_from_ppl": types.BoolValue(true), |
| 50 | + }), |
| 51 | + }, |
| 52 | + } |
| 53 | + |
| 54 | + for _, tc := range tests { |
| 55 | + t.Run(tc.name, func(t *testing.T) { |
| 56 | + var diags diag.Diagnostics |
| 57 | + var result types.Object |
| 58 | + JWTGroupsFilterFromPB(&result, tc.input, &diags) |
| 59 | + assert.False(t, diags.HasError()) |
| 60 | + diff := cmp.Diff(tc.expected, result) |
| 61 | + assert.Empty(t, diff) |
| 62 | + }) |
| 63 | + } |
| 64 | +} |
| 65 | + |
| 66 | +func TestJWTGroupsFilterToPB(t *testing.T) { |
| 67 | + ctx := context.Background() |
| 68 | + tests := []struct { |
| 69 | + name string |
| 70 | + input types.Object |
| 71 | + expected *pb.JwtGroupsFilter |
| 72 | + }{ |
| 73 | + { |
| 74 | + name: "null input", |
| 75 | + input: types.ObjectNull(jwtGroupsFilterSchemaAttr), |
| 76 | + expected: nil, |
| 77 | + }, |
| 78 | + { |
| 79 | + name: "empty groups", |
| 80 | + input: types.ObjectValueMust(jwtGroupsFilterSchemaAttr, map[string]attr.Value{ |
| 81 | + "groups": types.SetValueMust(types.StringType, []attr.Value{}), |
| 82 | + "infer_from_ppl": types.BoolValue(false), |
| 83 | + }), |
| 84 | + expected: &pb.JwtGroupsFilter{ |
| 85 | + Groups: []string{}, |
| 86 | + InferFromPpl: false, |
| 87 | + }, |
| 88 | + }, |
| 89 | + { |
| 90 | + name: "with groups", |
| 91 | + input: types.ObjectValueMust(jwtGroupsFilterSchemaAttr, map[string]attr.Value{ |
| 92 | + "groups": types.SetValueMust(types.StringType, []attr.Value{ |
| 93 | + types.StringValue("group1"), |
| 94 | + types.StringValue("group2"), |
| 95 | + }), |
| 96 | + "infer_from_ppl": types.BoolValue(true), |
| 97 | + }), |
| 98 | + expected: &pb.JwtGroupsFilter{ |
| 99 | + Groups: []string{"group1", "group2"}, |
| 100 | + InferFromPpl: true, |
| 101 | + }, |
| 102 | + }, |
| 103 | + } |
| 104 | + |
| 105 | + for _, tc := range tests { |
| 106 | + t.Run(tc.name, func(t *testing.T) { |
| 107 | + var diags diag.Diagnostics |
| 108 | + var result *pb.JwtGroupsFilter |
| 109 | + JWTGroupsFilterToPB(ctx, &result, tc.input, &diags) |
| 110 | + assert.False(t, diags.HasError()) |
| 111 | + diff := cmp.Diff(tc.expected, result, protocmp.Transform()) |
| 112 | + assert.Empty(t, diff) |
| 113 | + }) |
| 114 | + } |
| 115 | +} |
0 commit comments