github.com/kyma-incubator/compass/components/director@v0.0.0-20230623144113-d764f56ff805/pkg/auth/service_account_token_provider_test.go (about)

     1  /*
     2   * Copyright 2020 The Compass Authors
     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 auth_test
    18  
    19  import (
    20  	"context"
    21  	"os"
    22  	"testing"
    23  
    24  	"github.com/kyma-incubator/compass/components/director/pkg/auth"
    25  	"github.com/stretchr/testify/suite"
    26  )
    27  
    28  func TestServiceAccountTokenAuthorizationProviderTestSuite(t *testing.T) {
    29  	suite.Run(t, new(ServiceAccountTokenAuthorizationProviderTestSuite))
    30  }
    31  
    32  type ServiceAccountTokenAuthorizationProviderTestSuite struct {
    33  	suite.Suite
    34  }
    35  
    36  func (suite *ServiceAccountTokenAuthorizationProviderTestSuite) TestServiceAccountTokenAuthorizationProvider_New() {
    37  	provider := auth.NewServiceAccountTokenAuthorizationProvider()
    38  	suite.Require().NotNil(provider)
    39  }
    40  
    41  func (suite *ServiceAccountTokenAuthorizationProviderTestSuite) TestServiceAccountTokenAuthorizationProvider_Name() {
    42  	provider := auth.NewServiceAccountTokenAuthorizationProvider()
    43  
    44  	name := provider.Name()
    45  
    46  	suite.Require().Equal(name, "ServiceAccountTokenAuthorizationProvider")
    47  }
    48  
    49  func (suite *ServiceAccountTokenAuthorizationProviderTestSuite) TestServiceAccountTokenAuthorizationProvider_Matches() {
    50  	provider := auth.NewServiceAccountTokenAuthorizationProvider()
    51  
    52  	matches := provider.Matches(context.TODO())
    53  	suite.Require().Equal(matches, true)
    54  }
    55  
    56  func (suite *ServiceAccountTokenAuthorizationProviderTestSuite) TestServiceAccountTokenAuthorizationProvider_DoesNotMatchWhenBasicCredentialsInContext() {
    57  	provider := auth.NewServiceAccountTokenAuthorizationProvider()
    58  
    59  	matches := provider.Matches(auth.SaveToContext(context.Background(), &auth.BasicCredentials{}))
    60  	suite.Require().Equal(matches, false)
    61  }
    62  
    63  func (suite *ServiceAccountTokenAuthorizationProviderTestSuite) TestServiceAccountTokenAuthorizationProvider_DoesNotMatchWhenOAuthCredentialsInContext() {
    64  	provider := auth.NewServiceAccountTokenAuthorizationProvider()
    65  
    66  	matches := provider.Matches(auth.SaveToContext(context.Background(), &auth.OAuthCredentials{}))
    67  	suite.Require().Equal(matches, false)
    68  }
    69  
    70  func (suite *ServiceAccountTokenAuthorizationProviderTestSuite) TestServiceAccountTokenAuthorizationProvider_GetAuthorization() {
    71  	tokenContent := "test-token"
    72  	tokenFileName := "token"
    73  	err := os.WriteFile(tokenFileName, []byte(tokenContent), os.ModePerm)
    74  	suite.Require().NoError(err)
    75  
    76  	defer func() {
    77  		err := os.Remove(tokenFileName)
    78  		suite.Require().NoError(err)
    79  	}()
    80  
    81  	provider := auth.NewServiceAccountTokenAuthorizationProviderWithPath(tokenFileName)
    82  
    83  	authorization, err := provider.GetAuthorization(context.TODO())
    84  
    85  	suite.Require().NoError(err)
    86  	suite.Require().NotEmpty(authorization)
    87  	suite.Require().Equal("Bearer "+tokenContent, authorization)
    88  }