github.com/minio/console@v1.4.1/pkg/auth/idp/oauth2/provider_test.go (about)

     1  // This file is part of MinIO Console Server
     2  // Copyright (c) 2021 MinIO, Inc.
     3  //
     4  // This program is free software: you can redistribute it and/or modify
     5  // it under the terms of the GNU Affero General Public License as published by
     6  // the Free Software Foundation, either version 3 of the License, or
     7  // (at your option) any later version.
     8  //
     9  // This program is distributed in the hope that it will be useful,
    10  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    11  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    12  // GNU Affero General Public License for more details.
    13  //
    14  // You should have received a copy of the GNU Affero General Public License
    15  // along with this program.  If not, see <http://www.gnu.org/licenses/>.
    16  
    17  package oauth2
    18  
    19  import (
    20  	"context"
    21  	"net/http"
    22  	"testing"
    23  
    24  	"github.com/stretchr/testify/assert"
    25  	"golang.org/x/oauth2"
    26  )
    27  
    28  type Oauth2configMock struct{}
    29  
    30  var (
    31  	oauth2ConfigExchangeMock                 func(ctx context.Context, code string, opts ...oauth2.AuthCodeOption) (*oauth2.Token, error)
    32  	oauth2ConfigAuthCodeURLMock              func(state string, opts ...oauth2.AuthCodeOption) string
    33  	oauth2ConfigPasswordCredentialsTokenMock func(ctx context.Context, username, password string) (*oauth2.Token, error)
    34  	oauth2ConfigClientMock                   func(ctx context.Context, t *oauth2.Token) *http.Client
    35  	oauth2ConfigokenSourceMock               func(ctx context.Context, t *oauth2.Token) oauth2.TokenSource
    36  )
    37  
    38  func (ac Oauth2configMock) Exchange(ctx context.Context, code string, opts ...oauth2.AuthCodeOption) (*oauth2.Token, error) {
    39  	return oauth2ConfigExchangeMock(ctx, code, opts...)
    40  }
    41  
    42  func (ac Oauth2configMock) AuthCodeURL(state string, opts ...oauth2.AuthCodeOption) string {
    43  	return oauth2ConfigAuthCodeURLMock(state, opts...)
    44  }
    45  
    46  func (ac Oauth2configMock) PasswordCredentialsToken(ctx context.Context, username, password string) (*oauth2.Token, error) {
    47  	return oauth2ConfigPasswordCredentialsTokenMock(ctx, username, password)
    48  }
    49  
    50  func (ac Oauth2configMock) Client(ctx context.Context, t *oauth2.Token) *http.Client {
    51  	return oauth2ConfigClientMock(ctx, t)
    52  }
    53  
    54  func (ac Oauth2configMock) TokenSource(ctx context.Context, t *oauth2.Token) oauth2.TokenSource {
    55  	return oauth2ConfigokenSourceMock(ctx, t)
    56  }
    57  
    58  func TestGenerateLoginURL(t *testing.T) {
    59  	funcAssert := assert.New(t)
    60  	oauth2Provider := Provider{
    61  		oauth2Config: Oauth2configMock{},
    62  	}
    63  	// Test-1 : GenerateLoginURL() generates URL correctly with provided state
    64  	oauth2ConfigAuthCodeURLMock = func(state string, _ ...oauth2.AuthCodeOption) string {
    65  		// Internally we are testing the private method getRandomStateWithHMAC, this function should always returns
    66  		// a non-empty string
    67  		return state
    68  	}
    69  	url := oauth2Provider.GenerateLoginURL(DefaultDerivedKey, "testIDP")
    70  	funcAssert.NotEqual("", url)
    71  }