|
| 1 | +/* |
| 2 | + * Teleport |
| 3 | + * Copyright (C) 2025 Gravitational, Inc. |
| 4 | + * |
| 5 | + * This program is free software: you can redistribute it and/or modify |
| 6 | + * it under the terms of the GNU Affero General Public License as published by |
| 7 | + * the Free Software Foundation, either version 3 of the License, or |
| 8 | + * (at your option) any later version. |
| 9 | + * |
| 10 | + * This program is distributed in the hope that it will be useful, |
| 11 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | + * GNU Affero General Public License for more details. |
| 14 | + * |
| 15 | + * You should have received a copy of the GNU Affero General Public License |
| 16 | + * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 17 | + */ |
| 18 | + |
| 19 | +package web |
| 20 | + |
| 21 | +import ( |
| 22 | + "context" |
| 23 | + "encoding/json" |
| 24 | + "testing" |
| 25 | + |
| 26 | + "github.com/stretchr/testify/require" |
| 27 | + |
| 28 | + accessmonitoringrulesv1 "github.com/gravitational/teleport/api/gen/proto/go/teleport/accessmonitoringrules/v1" |
| 29 | + "github.com/gravitational/teleport/api/types" |
| 30 | +) |
| 31 | + |
| 32 | +const validAccessMonitoringRuleTerraform = `resource "teleport_access_monitoring_rule" "foo" { |
| 33 | + version = "v1" |
| 34 | +
|
| 35 | + metadata = { |
| 36 | + name = "foo" |
| 37 | + } |
| 38 | +
|
| 39 | + spec = { |
| 40 | + subjects = ["access_request"] |
| 41 | + condition = "some-condition" |
| 42 | + notification = { |
| 43 | + name = "mattermost" |
| 44 | + recipients = ["apple"] |
| 45 | + } |
| 46 | + } |
| 47 | +} |
| 48 | +` |
| 49 | + |
| 50 | +func TestTerraformStringify_Valid(t *testing.T) { |
| 51 | + t.Parallel() |
| 52 | + |
| 53 | + env := newWebPack(t, 1) |
| 54 | + proxy := env.proxies[0] |
| 55 | + pack := proxy.authPack(t, "test@example.com", nil) |
| 56 | + |
| 57 | + req := struct { |
| 58 | + Resource *accessmonitoringrulesv1.AccessMonitoringRule `json:"resource"` |
| 59 | + }{ |
| 60 | + Resource: getAccessMonitoringRuleResource(), |
| 61 | + } |
| 62 | + |
| 63 | + endpoint := pack.clt.Endpoint("webapi", "terraform", "stringify", types.KindAccessMonitoringRule) |
| 64 | + re, err := pack.clt.PostJSON(context.Background(), endpoint, req) |
| 65 | + require.NoError(t, err) |
| 66 | + |
| 67 | + var resp terraformStringifyResponse |
| 68 | + require.NoError(t, json.Unmarshal(re.Bytes(), &resp)) |
| 69 | + require.Equal(t, validAccessMonitoringRuleTerraform, resp.Terraform) |
| 70 | +} |
| 71 | + |
| 72 | +func TestTerraformStringify_Errors(t *testing.T) { |
| 73 | + t.Parallel() |
| 74 | + |
| 75 | + env := newWebPack(t, 1) |
| 76 | + proxy := env.proxies[0] |
| 77 | + pack := proxy.authPack(t, "test@example.com", nil) |
| 78 | + |
| 79 | + testCases := []struct { |
| 80 | + desc string |
| 81 | + kind string |
| 82 | + }{ |
| 83 | + { |
| 84 | + desc: "unsupported kind", |
| 85 | + kind: "something-random", |
| 86 | + }, |
| 87 | + { |
| 88 | + desc: "missing kind", |
| 89 | + kind: "", |
| 90 | + }, |
| 91 | + } |
| 92 | + |
| 93 | + for _, tc := range testCases { |
| 94 | + t.Run(tc.desc, func(t *testing.T) { |
| 95 | + endpoint := pack.clt.Endpoint("webapi", "terraform", "stringify", tc.kind) |
| 96 | + _, err := pack.clt.PostJSON(context.Background(), endpoint, struct { |
| 97 | + Resource *accessmonitoringrulesv1.AccessMonitoringRule `json:"resource"` |
| 98 | + }{ |
| 99 | + Resource: getAccessMonitoringRuleResource(), |
| 100 | + }) |
| 101 | + |
| 102 | + require.Error(t, err) |
| 103 | + }) |
| 104 | + } |
| 105 | +} |
0 commit comments