github.com/1aal/kubeblocks@v0.0.0-20231107070852-e1c03e598921/pkg/cli/cmd/auth/authorize/issued_provoder_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 "k8s.io/cli-runtime/pkg/genericiooptions" 30 31 "github.com/1aal/kubeblocks/pkg/cli/cmd/auth/authorize/authenticator" 32 ) 33 34 var authorizationRes = authenticator.AuthorizationResponse{ 35 Code: "test_code", 36 CallbackURL: "test_callback_url", 37 } 38 39 type MockAuthenticator struct { 40 userInfoResponse *authenticator.UserInfoResponse 41 tokenResponse *authenticator.TokenResponse 42 authorizationResponse *authenticator.AuthorizationResponse 43 } 44 45 func NewMockAuthenticator() *MockAuthenticator { 46 return &MockAuthenticator{ 47 userInfoResponse: &userInfoResponse, 48 tokenResponse: &tokenResponse, 49 authorizationResponse: &authorizationRes, 50 } 51 } 52 53 func (m *MockAuthenticator) GetAuthorization(ctx context.Context, openURLFunc func(URL string), states ...string) (interface{}, error) { 54 return m.authorizationResponse, nil 55 } 56 57 func (m *MockAuthenticator) GetToken(ctx context.Context, authorization interface{}) (*authenticator.TokenResponse, error) { 58 if response, ok := authorization.(*authenticator.AuthorizationResponse); ok { 59 if response.Code == m.authorizationResponse.Code { 60 return m.tokenResponse, nil 61 } 62 } 63 return nil, fmt.Errorf("authorization not match") 64 } 65 66 func (m *MockAuthenticator) GetUserInfo(ctx context.Context, token string) (*authenticator.UserInfoResponse, error) { 67 if token == m.tokenResponse.AccessToken { 68 return m.userInfoResponse, nil 69 } 70 return nil, fmt.Errorf("token not match") 71 } 72 73 func (m *MockAuthenticator) Logout(ctx context.Context, token string, openURLFunc func(URL string)) error { 74 if token == m.tokenResponse.AccessToken { 75 return nil 76 } 77 return fmt.Errorf("token not match") 78 } 79 80 func (m *MockAuthenticator) RefreshToken(ctx context.Context, refreshToken string) (*authenticator.TokenResponse, error) { 81 if refreshToken == m.tokenResponse.RefreshToken { 82 m.tokenResponse.AccessToken = "newAccessToken" 83 return m.tokenResponse, nil 84 } 85 return nil, fmt.Errorf("refresh token not match") 86 } 87 88 var _ = Describe("issued provider", func() { 89 var ( 90 mockAuthenticator *MockAuthenticator 91 issuedTokenProvider *CloudIssuedTokenProvider 92 tokenRes *authenticator.TokenResponse 93 o Options 94 streams genericiooptions.IOStreams 95 ) 96 97 BeforeEach(func() { 98 mockAuthenticator = NewMockAuthenticator() 99 streams, _, _, _ = genericiooptions.NewTestIOStreams() 100 o = Options{ 101 ClientID: "test_client_id", 102 AuthURL: "test_auth_url", 103 NoBrowser: true, 104 IOStreams: streams, 105 } 106 }) 107 108 AfterEach(func() { 109 }) 110 111 Context("test issued provider", func() { 112 It("test authenticate", func() { 113 ExpectWithOffset(1, func() error { 114 var err error 115 issuedTokenProvider, err = newIssuedTokenProvider(o, mockAuthenticator) 116 return err 117 }()).To(BeNil()) 118 119 ExpectWithOffset(1, func() error { 120 _, err := issuedTokenProvider.authenticate(context.Background()) 121 return err 122 }()).To(BeNil()) 123 }) 124 125 It("test getUserInfo", func() { 126 ExpectWithOffset(1, func() error { 127 var err error 128 issuedTokenProvider, err = newIssuedTokenProvider(o, mockAuthenticator) 129 return err 130 }()).To(BeNil()) 131 132 ExpectWithOffset(1, func() error { 133 var err error 134 tokenRes, err = issuedTokenProvider.authenticate(context.Background()) 135 return err 136 }()).To(BeNil()) 137 138 ExpectWithOffset(1, func() error { 139 _, err := issuedTokenProvider.getUserInfo(tokenRes.AccessToken) 140 return err 141 }()).To(BeNil()) 142 }) 143 144 It("test refreshToken", func() { 145 ExpectWithOffset(1, func() error { 146 var err error 147 issuedTokenProvider, err = newIssuedTokenProvider(o, mockAuthenticator) 148 return err 149 }()).To(BeNil()) 150 151 ExpectWithOffset(1, func() error { 152 var err error 153 tokenRes, err = issuedTokenProvider.authenticate(context.Background()) 154 return err 155 }()).To(BeNil()) 156 157 ExpectWithOffset(1, func() error { 158 _, err := issuedTokenProvider.refreshToken(tokenRes.RefreshToken) 159 return err 160 }()).To(BeNil()) 161 }) 162 163 It("test logout", func() { 164 ExpectWithOffset(1, func() error { 165 var err error 166 issuedTokenProvider, err = newIssuedTokenProvider(o, mockAuthenticator) 167 return err 168 }()).To(BeNil()) 169 170 ExpectWithOffset(1, func() error { 171 var err error 172 tokenRes, err = issuedTokenProvider.authenticate(context.Background()) 173 return err 174 }()).To(BeNil()) 175 176 ExpectWithOffset(1, func() error { 177 err := issuedTokenProvider.logout(context.Background(), tokenRes.AccessToken) 178 return err 179 }()).To(BeNil()) 180 }) 181 }) 182 })