github.com/1aal/kubeblocks@v0.0.0-20231107070852-e1c03e598921/pkg/cli/cmd/auth/authorize/cached_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  	"github.com/1aal/kubeblocks/pkg/cli/cmd/auth/authorize/authenticator"
    27  )
    28  
    29  type MockKeyring struct {
    30  	key   string
    31  	value []byte
    32  }
    33  
    34  func (m *MockKeyring) set(value []byte) error {
    35  	m.value = value
    36  	return nil
    37  }
    38  
    39  func (m *MockKeyring) get() ([]byte, error) {
    40  	return m.value, nil
    41  }
    42  
    43  func (m *MockKeyring) remove() error {
    44  	m.key = ""
    45  	m.value = nil
    46  	return nil
    47  }
    48  
    49  func (m *MockKeyring) isValid() bool {
    50  	return true
    51  }
    52  
    53  var _ = Describe("cache", func() {
    54  	var (
    55  		mockKeyring   KeyringProvider
    56  		cached        *KeyringCachedTokenProvider
    57  		tokenResponse authenticator.TokenResponse
    58  	)
    59  
    60  	BeforeEach(func() {
    61  		tokenResponse = authenticator.TokenResponse{
    62  			AccessToken:  "test",
    63  			RefreshToken: "test",
    64  		}
    65  		mockKeyring = &MockKeyring{}
    66  		cached = NewKeyringCachedTokenProvider(&mockKeyring)
    67  	})
    68  
    69  	AfterEach(func() {
    70  	})
    71  
    72  	Context("test cache", func() {
    73  		It("test cached token", func() {
    74  			ExpectWithOffset(1, func() error {
    75  				err := cached.cacheTokens(&tokenResponse)
    76  				return err
    77  			}()).To(BeNil())
    78  		})
    79  
    80  		It("test get token", func() {
    81  			ExpectWithOffset(1, func() error {
    82  				err := cached.cacheTokens(&tokenResponse)
    83  				return err
    84  			}()).To(BeNil())
    85  			ExpectWithOffset(1, func() error {
    86  				tokenResponse, err := cached.GetTokens()
    87  				Expect(tokenResponse.AccessToken).To(Equal("test"))
    88  				return err
    89  			}()).To(BeNil())
    90  		})
    91  
    92  		It("test fail to get token", func() {
    93  			ExpectWithOffset(1, func() error {
    94  				err := cached.cacheTokens(&tokenResponse)
    95  				return err
    96  			}()).To(BeNil())
    97  			ExpectWithOffset(1, func() error {
    98  				tokenResponse, err := cached.GetTokens()
    99  				Expect(tokenResponse.AccessToken).NotTo(Equal("cloud"))
   100  				return err
   101  			}()).To(BeNil())
   102  		})
   103  
   104  		It("test delete token", func() {
   105  			ExpectWithOffset(1, func() error {
   106  				err := cached.cacheTokens(&tokenResponse)
   107  				return err
   108  			}()).To(BeNil())
   109  
   110  			ExpectWithOffset(1, func() error {
   111  				err := cached.deleteTokens()
   112  				return err
   113  			}()).To(BeNil())
   114  
   115  			ExpectWithOffset(1, func() error {
   116  				_, err := cached.GetTokens()
   117  				return err
   118  			}()).NotTo(BeNil())
   119  		})
   120  	})
   121  })