github.com/minio/console@v1.4.1/api/user_login_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 api
    18  
    19  import (
    20  	"context"
    21  	"errors"
    22  	"reflect"
    23  	"testing"
    24  
    25  	xoauth2 "golang.org/x/oauth2"
    26  
    27  	"github.com/minio/madmin-go/v3"
    28  
    29  	iampolicy "github.com/minio/pkg/v3/policy"
    30  
    31  	"github.com/minio/console/pkg/auth"
    32  
    33  	"github.com/minio/minio-go/v7/pkg/credentials"
    34  	"github.com/stretchr/testify/assert"
    35  )
    36  
    37  // Define a mock struct of ConsoleCredentialsI interface implementation
    38  type consoleCredentialsMock struct{}
    39  
    40  func (ac consoleCredentialsMock) GetActions() []string {
    41  	return []string{}
    42  }
    43  
    44  func (ac consoleCredentialsMock) GetAccountAccessKey() string {
    45  	return ""
    46  }
    47  
    48  // Common mocks
    49  var consoleCredentialsGetMock func() (credentials.Value, error)
    50  
    51  // mock function of Get()
    52  func (ac consoleCredentialsMock) Get() (credentials.Value, error) {
    53  	return consoleCredentialsGetMock()
    54  }
    55  
    56  func TestLogin(t *testing.T) {
    57  	funcAssert := assert.New(t)
    58  	consoleCredentials := consoleCredentialsMock{}
    59  	// Test Case 1: Valid consoleCredentials
    60  	consoleCredentialsGetMock = func() (credentials.Value, error) {
    61  		return credentials.Value{
    62  			AccessKeyID:     "fakeAccessKeyID",
    63  			SecretAccessKey: "fakeSecretAccessKey",
    64  			SessionToken:    "fakeSessionToken",
    65  			SignerType:      0,
    66  		}, nil
    67  	}
    68  	token, err := login(consoleCredentials, nil)
    69  	funcAssert.NotEmpty(token, "Token was returned empty")
    70  	funcAssert.Nil(err, "error creating a session")
    71  
    72  	// Test Case 2: Invalid credentials
    73  	consoleCredentialsGetMock = func() (credentials.Value, error) {
    74  		return credentials.Value{}, errors.New("")
    75  	}
    76  	_, err = login(consoleCredentials, nil)
    77  	funcAssert.NotNil(err, "not error returned creating a session")
    78  }
    79  
    80  type IdentityProviderMock struct{}
    81  
    82  var (
    83  	idpVerifyIdentityMock            func(ctx context.Context, code, state string) (*credentials.Credentials, error)
    84  	idpVerifyIdentityForOperatorMock func(ctx context.Context, code, state string) (*xoauth2.Token, error)
    85  	idpGenerateLoginURLMock          func() string
    86  )
    87  
    88  func (ac IdentityProviderMock) VerifyIdentity(ctx context.Context, code, state string) (*credentials.Credentials, error) {
    89  	return idpVerifyIdentityMock(ctx, code, state)
    90  }
    91  
    92  func (ac IdentityProviderMock) VerifyIdentityForOperator(ctx context.Context, code, state string) (*xoauth2.Token, error) {
    93  	return idpVerifyIdentityForOperatorMock(ctx, code, state)
    94  }
    95  
    96  func (ac IdentityProviderMock) GenerateLoginURL() string {
    97  	return idpGenerateLoginURLMock()
    98  }
    99  
   100  func Test_validateUserAgainstIDP(t *testing.T) {
   101  	provider := IdentityProviderMock{}
   102  	mockCode := "EAEAEAE"
   103  	mockState := "HUEHUEHUE"
   104  	type args struct {
   105  		ctx      context.Context
   106  		provider auth.IdentityProviderI
   107  		code     string
   108  		state    string
   109  	}
   110  	tests := []struct {
   111  		name     string
   112  		args     args
   113  		want     *credentials.Credentials
   114  		wantErr  bool
   115  		mockFunc func()
   116  	}{
   117  		{
   118  			name: "failed to verify user identity with idp",
   119  			args: args{
   120  				ctx:      context.Background(),
   121  				provider: provider,
   122  				code:     mockCode,
   123  				state:    mockState,
   124  			},
   125  			want:    nil,
   126  			wantErr: true,
   127  			mockFunc: func() {
   128  				idpVerifyIdentityMock = func(_ context.Context, _, _ string) (*credentials.Credentials, error) {
   129  					return nil, errors.New("something went wrong")
   130  				}
   131  			},
   132  		},
   133  	}
   134  	for _, tt := range tests {
   135  		t.Run(tt.name, func(_ *testing.T) {
   136  			if tt.mockFunc != nil {
   137  				tt.mockFunc()
   138  			}
   139  			got, err := verifyUserAgainstIDP(tt.args.ctx, tt.args.provider, tt.args.code, tt.args.state)
   140  			if (err != nil) != tt.wantErr {
   141  				t.Errorf("verifyUserAgainstIDP() error = %v, wantErr %v", err, tt.wantErr)
   142  				return
   143  			}
   144  			if !reflect.DeepEqual(got, tt.want) {
   145  				t.Errorf("verifyUserAgainstIDP() got = %v, want %v", got, tt.want)
   146  			}
   147  		})
   148  	}
   149  }
   150  
   151  func Test_getAccountInfo(t *testing.T) {
   152  	client := AdminClientMock{}
   153  	type args struct {
   154  		ctx    context.Context
   155  		client MinioAdmin
   156  	}
   157  	tests := []struct {
   158  		name     string
   159  		args     args
   160  		want     *iampolicy.Policy
   161  		wantErr  bool
   162  		mockFunc func()
   163  	}{
   164  		{
   165  			name: "error getting account info",
   166  			args: args{
   167  				ctx:    context.Background(),
   168  				client: client,
   169  			},
   170  			want:    nil,
   171  			wantErr: true,
   172  			mockFunc: func() {
   173  				minioAccountInfoMock = func(_ context.Context) (madmin.AccountInfo, error) {
   174  					return madmin.AccountInfo{}, errors.New("something went wrong")
   175  				}
   176  			},
   177  		},
   178  	}
   179  	for _, tt := range tests {
   180  		t.Run(tt.name, func(_ *testing.T) {
   181  			if tt.mockFunc != nil {
   182  				tt.mockFunc()
   183  			}
   184  			got, err := getAccountInfo(tt.args.ctx, tt.args.client)
   185  			if (err != nil) != tt.wantErr {
   186  				t.Errorf("getAccountInfo() error = %v, wantErr %v", err, tt.wantErr)
   187  				return
   188  			}
   189  			if tt.want != nil {
   190  				if !reflect.DeepEqual(got, tt.want) {
   191  					t.Errorf("getAccountInfo() got = %v, want %v", got, tt.want)
   192  				}
   193  			}
   194  		})
   195  	}
   196  }