github.com/gravitational/teleport/api@v0.0.0-20240507183017-3110591cbafc/types/appserver_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 types
    18  
    19  import (
    20  	"testing"
    21  
    22  	"github.com/stretchr/testify/require"
    23  
    24  	"github.com/gravitational/teleport/api"
    25  )
    26  
    27  func TestGetTunnelType(t *testing.T) {
    28  	t.Parallel()
    29  
    30  	tests := []struct {
    31  		name      string
    32  		appServer AppServer
    33  		expected  TunnelType
    34  	}{
    35  		{
    36  			name:      "default",
    37  			appServer: &AppServerV3{},
    38  			expected:  AppTunnel,
    39  		},
    40  		{
    41  			name: "okta",
    42  			appServer: &AppServerV3{
    43  				Metadata: Metadata{
    44  					Labels: map[string]string{
    45  						OriginLabel: OriginOkta,
    46  					},
    47  				},
    48  			},
    49  			expected: OktaTunnel,
    50  		},
    51  	}
    52  
    53  	for _, test := range tests {
    54  		t.Run(test.name, func(t *testing.T) {
    55  			require.Equal(t, test.expected, test.appServer.GetTunnelType())
    56  		})
    57  	}
    58  }
    59  
    60  func TestNewAppServerForAWSOIDCIntegration(t *testing.T) {
    61  	for _, tt := range []struct {
    62  		name           string
    63  		integratioName string
    64  		hostID         string
    65  		expectedApp    *AppServerV3
    66  		errCheck       require.ErrorAssertionFunc
    67  	}{
    68  		{
    69  			name:           "valid",
    70  			integratioName: "valid",
    71  			hostID:         "my-host-id",
    72  			expectedApp: &AppServerV3{
    73  				Kind:    KindAppServer,
    74  				Version: V3,
    75  				Metadata: Metadata{
    76  					Name:      "valid",
    77  					Namespace: "default",
    78  				},
    79  				Spec: AppServerSpecV3{
    80  					Version: api.Version,
    81  					HostID:  "my-host-id",
    82  					App: &AppV3{
    83  						Kind:    KindApp,
    84  						Version: V3,
    85  						Metadata: Metadata{
    86  							Name:      "valid",
    87  							Namespace: "default",
    88  						},
    89  						Spec: AppSpecV3{
    90  							URI:         "https://console.aws.amazon.com",
    91  							Cloud:       "AWS",
    92  							Integration: "valid",
    93  						},
    94  					},
    95  				},
    96  			},
    97  			errCheck: require.NoError,
    98  		},
    99  		{
   100  			name:           "error when HostID is missing",
   101  			integratioName: "invalid-missing-hostid",
   102  			errCheck:       require.Error,
   103  		},
   104  	} {
   105  		t.Run(tt.name, func(t *testing.T) {
   106  			app, err := NewAppServerForAWSOIDCIntegration(tt.integratioName, tt.hostID)
   107  			if tt.errCheck != nil {
   108  				tt.errCheck(t, err)
   109  			}
   110  			if tt.expectedApp != nil {
   111  				require.Equal(t, tt.expectedApp, app)
   112  			}
   113  		})
   114  	}
   115  }