github.com/kubewharf/katalyst-core@v0.5.3/pkg/util/credential/basic_test.go (about)

     1  /*
     2  Copyright 2022 The Katalyst Authors.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package credential
    18  
    19  import (
    20  	"encoding/base64"
    21  	"net/http"
    22  	"reflect"
    23  	"testing"
    24  
    25  	"github.com/stretchr/testify/assert"
    26  
    27  	"github.com/kubewharf/katalyst-api/pkg/apis/config/v1alpha1"
    28  	"github.com/kubewharf/katalyst-core/pkg/config/agent/dynamic"
    29  	"github.com/kubewharf/katalyst-core/pkg/config/generic"
    30  )
    31  
    32  func makeBasicToken(user, password string) string {
    33  	rawString := user + ":" + password
    34  	return "Basic " + base64.StdEncoding.EncodeToString([]byte(rawString))
    35  }
    36  
    37  func makeCred(t *testing.T) Credential {
    38  	conf := generic.NewAuthConfiguration()
    39  
    40  	configuration := dynamic.NewDynamicAgentConfiguration()
    41  	dynamicConf := dynamic.NewConfiguration()
    42  	dynamicConf.UserPasswordPairs = []v1alpha1.UserPasswordPair{
    43  		{Username: "user-1", Password: base64.StdEncoding.EncodeToString([]byte("123456"))},
    44  		{Username: "user-2", Password: base64.StdEncoding.EncodeToString([]byte("abcdefg"))},
    45  	}
    46  	configuration.SetDynamicConfiguration(dynamicConf)
    47  
    48  	credential, err := NewBasicAuthCredential(conf, configuration)
    49  	assert.NoError(t, err)
    50  	assert.NotNil(t, credential)
    51  
    52  	basicAuth := credential.(*basicAuthCredential)
    53  	basicAuth.updateAuthPairFromDynamicConf()
    54  
    55  	return credential
    56  }
    57  
    58  func Test_basicAuthCredential_Auth(t *testing.T) {
    59  	t.Parallel()
    60  
    61  	cred := makeCred(t)
    62  	tests := []struct {
    63  		name     string
    64  		username string
    65  		password string
    66  		want     AuthInfo
    67  		wantErr  bool
    68  	}{
    69  		{
    70  			name:     "right password",
    71  			username: "user-1",
    72  			password: "123456",
    73  			want: BasicAuthInfo{
    74  				Username: "user-1",
    75  				Password: "123456",
    76  			},
    77  			wantErr: false,
    78  		},
    79  		{
    80  			name:     "wrong password",
    81  			username: "user-1",
    82  			password: "123456789",
    83  			want:     nil,
    84  			wantErr:  true,
    85  		},
    86  	}
    87  	for _, tt := range tests {
    88  		tt := tt
    89  		t.Run(tt.name, func(t *testing.T) {
    90  			t.Parallel()
    91  
    92  			hr := &http.Request{
    93  				Header: make(http.Header),
    94  			}
    95  			hr.SetBasicAuth(tt.username, tt.password)
    96  			got, err := cred.Auth(hr)
    97  			if (err != nil) != tt.wantErr {
    98  				t.Errorf("Auth() error = %v, wantErr %v", err, tt.wantErr)
    99  				return
   100  			}
   101  			if !reflect.DeepEqual(got, tt.want) {
   102  				t.Errorf("Auth() got = %v, want %v", got, tt.want)
   103  			}
   104  		})
   105  	}
   106  }
   107  
   108  func Test_basicAuthCredential_AuthToken(t *testing.T) {
   109  	t.Parallel()
   110  
   111  	cred := makeCred(t)
   112  	tests := []struct {
   113  		name     string
   114  		username string
   115  		password string
   116  		want     AuthInfo
   117  		wantErr  bool
   118  	}{
   119  		{
   120  			name:     "right password",
   121  			username: "user-1",
   122  			password: "123456",
   123  			want: BasicAuthInfo{
   124  				Username: "user-1",
   125  				Password: "123456",
   126  			},
   127  			wantErr: false,
   128  		},
   129  		{
   130  			name:     "wrong password",
   131  			username: "user-1",
   132  			password: "123456789",
   133  			want:     nil,
   134  			wantErr:  true,
   135  		},
   136  	}
   137  	for _, tt := range tests {
   138  		tt := tt
   139  		t.Run(tt.name, func(t *testing.T) {
   140  			t.Parallel()
   141  
   142  			got, err := cred.AuthToken(makeBasicToken(tt.username, tt.password))
   143  			if (err != nil) != tt.wantErr {
   144  				t.Errorf("Auth() error = %v, wantErr %v", err, tt.wantErr)
   145  				return
   146  			}
   147  			if !reflect.DeepEqual(got, tt.want) {
   148  				t.Errorf("Auth() got = %v, want %v", got, tt.want)
   149  			}
   150  		})
   151  	}
   152  }