github.com/1aal/kubeblocks@v0.0.0-20231107070852-e1c03e598921/pkg/cli/cmd/auth/authorize/token_provider_test.go (about)

     1  /*
     2  Copyright (C) 2022-2023 ApeCloud Co., Ltd
     3  
     4  This file is part of KubeBlocks project
     5  
     6  This program is free software: you can redistribute it and/or modify
     7  it under the terms of the GNU Affero General Public License as published by
     8  the Free Software Foundation, either version 3 of the License, or
     9  (at your option) any later version.
    10  
    11  This program is distributed in the hope that it will be useful
    12  but WITHOUT ANY WARRANTY; without even the implied warranty of
    13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    14  GNU Affero General Public License for more details.
    15  
    16  You should have received a copy of the GNU Affero General Public License
    17  along with this program.  If not, see <http://www.gnu.org/licenses/>.
    18  */
    19  
    20  package authorize
    21  
    22  import (
    23  	. "github.com/onsi/ginkgo/v2"
    24  	. "github.com/onsi/gomega"
    25  
    26  	"context"
    27  	"fmt"
    28  
    29  	"github.com/1aal/kubeblocks/pkg/cli/cmd/auth/authorize/authenticator"
    30  )
    31  
    32  type MockIssued struct {
    33  	tokenResponse    *authenticator.TokenResponse
    34  	userInfoResponse *authenticator.UserInfoResponse
    35  }
    36  
    37  var tokenResponse = authenticator.TokenResponse{
    38  	AccessToken:  "test_access_token",
    39  	RefreshToken: "test_refresh_token",
    40  	IDToken:      "test_id_token",
    41  	ExpiresIn:    3600000000000,
    42  }
    43  
    44  var userInfoResponse = authenticator.UserInfoResponse{
    45  	Name:    "test_name",
    46  	Email:   "test_email",
    47  	Locale:  "test_locale",
    48  	Subject: "test_subject",
    49  }
    50  
    51  func (m *MockIssued) authenticate(ctx context.Context) (*authenticator.TokenResponse, error) {
    52  	return m.tokenResponse, nil
    53  }
    54  
    55  func (m *MockIssued) refreshToken(refreshToken string) (*authenticator.TokenResponse, error) {
    56  	if refreshToken == m.tokenResponse.RefreshToken {
    57  		m.tokenResponse.AccessToken = "newAccessToken"
    58  		return m.tokenResponse, nil
    59  	}
    60  	return nil, fmt.Errorf("refresh token not match")
    61  }
    62  
    63  func (m *MockIssued) getUserInfo(token string) (*authenticator.UserInfoResponse, error) {
    64  	if token == m.tokenResponse.AccessToken {
    65  		return m.userInfoResponse, nil
    66  	}
    67  	return nil, fmt.Errorf("token not match")
    68  }
    69  
    70  func (m *MockIssued) logout(ctx context.Context, token string) error {
    71  	if token == m.tokenResponse.IDToken {
    72  		return nil
    73  	}
    74  	return fmt.Errorf("token not match")
    75  }
    76  
    77  type MockCached struct {
    78  	tokenResponse    *authenticator.TokenResponse
    79  	userInfoResponse *authenticator.UserInfoResponse
    80  }
    81  
    82  func (m *MockCached) cacheTokens(tokenResponse *authenticator.TokenResponse) error {
    83  	m.tokenResponse = tokenResponse
    84  	return nil
    85  }
    86  
    87  func (m *MockCached) deleteTokens() error {
    88  	m.tokenResponse = nil
    89  	return nil
    90  }
    91  
    92  func (m *MockCached) cacheUserInfo(userInfoResponse *authenticator.UserInfoResponse) error {
    93  	m.userInfoResponse = userInfoResponse
    94  	return nil
    95  }
    96  
    97  func (m *MockCached) GetTokens() (*authenticator.TokenResponse, error) {
    98  	return m.tokenResponse, nil
    99  }
   100  
   101  func (m *MockCached) getUserInfo() (*authenticator.UserInfoResponse, error) {
   102  	return m.userInfoResponse, nil
   103  }
   104  
   105  var _ = Describe("token provider", func() {
   106  	var (
   107  		mockIssued *MockIssued
   108  		mockCached *MockCached
   109  	)
   110  
   111  	BeforeEach(func() {
   112  		mockIssued = &MockIssued{
   113  			tokenResponse:    &tokenResponse,
   114  			userInfoResponse: &userInfoResponse,
   115  		}
   116  
   117  		mockCached = &MockCached{
   118  			tokenResponse:    &tokenResponse,
   119  			userInfoResponse: &userInfoResponse,
   120  		}
   121  	})
   122  
   123  	AfterEach(func() {
   124  	})
   125  
   126  	Context("test token provider", func() {
   127  		It("test login", func() {
   128  			tokenProvider := newTokenProvider(mockCached, mockIssued)
   129  			_, _, err := tokenProvider.Login(context.Background())
   130  			Expect(err).Should(HaveOccurred())
   131  		})
   132  
   133  		It("test logout", func() {
   134  			tokenProvider := newTokenProvider(mockCached, mockIssued)
   135  
   136  			ExpectWithOffset(1, func() error {
   137  				err := tokenProvider.Logout(context.Background())
   138  				return err
   139  			}()).To(BeNil())
   140  		})
   141  
   142  		It("test IsValidToken", func() {
   143  			Expect(IsValidToken("test_token")).To(BeFalse())
   144  		})
   145  
   146  		It("test getTokenFromCache", func() {
   147  			tokenProvider := &TokenProvider{
   148  				cached: mockCached,
   149  				issued: mockIssued,
   150  			}
   151  			isAccessTokenValid := func(tokenResponse authenticator.TokenResponse) bool { return IsValidToken(tokenResponse.AccessToken) }
   152  			_, err := tokenProvider.getTokenFromCache(isAccessTokenValid)
   153  			Expect(err).ShouldNot(HaveOccurred())
   154  		})
   155  
   156  		It("test getRefreshToken", func() {
   157  			tokenProvider := &TokenProvider{
   158  				cached: mockCached,
   159  				issued: mockIssued,
   160  			}
   161  			Expect(tokenProvider.getRefreshToken("")).Should(BeNil())
   162  		})
   163  	})
   164  })