github.com/gravitational/teleport/api@v0.0.0-20240507183017-3110591cbafc/accessrequest/access_request_test.go (about) 1 /* 2 Copyright 2023 Gravitational, Inc. 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 package accessrequest 18 19 import ( 20 "context" 21 "testing" 22 23 "github.com/stretchr/testify/require" 24 25 "github.com/gravitational/teleport/api/client/proto" 26 "github.com/gravitational/teleport/api/types" 27 ) 28 29 func newNode(t *testing.T, name, hostname string) types.Server { 30 t.Helper() 31 node, err := types.NewServer(name, types.KindNode, 32 types.ServerSpecV2{ 33 Hostname: hostname, 34 }) 35 require.NoError(t, err) 36 return node 37 } 38 39 func newApp(t *testing.T, name, description, origin string) types.Application { 40 t.Helper() 41 app, err := types.NewAppV3(types.Metadata{ 42 Name: name, 43 Description: description, 44 Labels: map[string]string{ 45 types.OriginLabel: origin, 46 }, 47 }, 48 types.AppSpecV3{ 49 URI: "https://some-addr.com", 50 PublicAddr: "https://some-addr.com", 51 }) 52 require.NoError(t, err) 53 return app 54 } 55 56 func newUserGroup(t *testing.T, name, description, origin string) types.UserGroup { 57 t.Helper() 58 userGroup, err := types.NewUserGroup(types.Metadata{ 59 Name: name, 60 Description: description, 61 Labels: map[string]string{ 62 types.OriginLabel: origin, 63 }, 64 }, types.UserGroupSpecV1{}) 65 require.NoError(t, err) 66 return userGroup 67 } 68 69 func newResourceID(clusterName, kind, name string) types.ResourceID { 70 return types.ResourceID{ 71 ClusterName: clusterName, 72 Kind: kind, 73 Name: name, 74 } 75 } 76 77 type mockResourceLister struct { 78 resources []types.ResourceWithLabels 79 } 80 81 func (m *mockResourceLister) ListResources(ctx context.Context, _ proto.ListResourcesRequest) (*types.ListResourcesResponse, error) { 82 return &types.ListResourcesResponse{ 83 Resources: m.resources, 84 }, nil 85 } 86 87 func TestGetResourceDetails(t *testing.T) { 88 clusterName := "cluster" 89 90 presence := &mockResourceLister{ 91 resources: []types.ResourceWithLabels{ 92 newNode(t, "node1", "hostname 1"), 93 newApp(t, "app1", "friendly app 1", types.OriginDynamic), 94 newApp(t, "app2", "friendly app 2", types.OriginDynamic), 95 newApp(t, "app3", "friendly app 3", types.OriginOkta), 96 newUserGroup(t, "group1", "friendly group 1", types.OriginOkta), 97 }, 98 } 99 resourceIDs := []types.ResourceID{ 100 newResourceID(clusterName, types.KindNode, "node1"), 101 newResourceID(clusterName, types.KindApp, "app1"), 102 newResourceID(clusterName, types.KindApp, "app2"), 103 newResourceID(clusterName, types.KindApp, "app3"), 104 newResourceID(clusterName, types.KindUserGroup, "group1"), 105 } 106 107 ctx := context.Background() 108 109 details, err := GetResourceDetails(ctx, clusterName, presence, resourceIDs) 110 require.NoError(t, err) 111 112 // Check the resource details to see if friendly names properly propagated. 113 114 // Node should be named for its hostname. 115 require.Equal(t, "hostname 1", details[types.ResourceIDToString(resourceIDs[0])].FriendlyName) 116 117 // app1 and app2 are expected to be empty because they're not Okta sourced resources. 118 require.Empty(t, details[types.ResourceIDToString(resourceIDs[1])].FriendlyName) 119 120 require.Empty(t, details[types.ResourceIDToString(resourceIDs[2])].FriendlyName) 121 122 // This Okta sourced app should have a friendly name. 123 require.Equal(t, "friendly app 3", details[types.ResourceIDToString(resourceIDs[3])].FriendlyName) 124 125 // This Okta sourced user group should have a friendly name. 126 require.Equal(t, "friendly group 1", details[types.ResourceIDToString(resourceIDs[4])].FriendlyName) 127 }