vitess.io/vitess@v0.16.2/go/mysql/ldapauthserver/auth_server_ldap_test.go (about)

     1  /*
     2  Copyright 2019 The Vitess 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 ldapauthserver
    18  
    19  import (
    20  	"fmt"
    21  	"testing"
    22  
    23  	"github.com/stretchr/testify/require"
    24  	ldap "gopkg.in/ldap.v2"
    25  )
    26  
    27  type MockLdapClient struct{}
    28  
    29  func (mlc *MockLdapClient) Connect(network string, config *ServerConfig) error { return nil }
    30  func (mlc *MockLdapClient) Close()                                             {}
    31  func (mlc *MockLdapClient) Bind(username, password string) error {
    32  	if username != "testuser" || password != "testpass" {
    33  		return fmt.Errorf("invalid credentials: %s, %s", username, password)
    34  	}
    35  	return nil
    36  }
    37  func (mlc *MockLdapClient) Search(searchRequest *ldap.SearchRequest) (*ldap.SearchResult, error) {
    38  	return &ldap.SearchResult{}, nil
    39  }
    40  
    41  func TestValidateClearText(t *testing.T) {
    42  	asl := &AuthServerLdap{
    43  		Client:         &MockLdapClient{},
    44  		User:           "testuser",
    45  		Password:       "testpass",
    46  		UserDnPattern:  "%s",
    47  		RefreshSeconds: 1,
    48  	}
    49  	_, err := asl.validate("testuser", "testpass")
    50  	require.NoError(t, err, "AuthServerLdap failed to validate valid credentials. Got: %v", err)
    51  
    52  	_, err = asl.validate("invaliduser", "invalidpass")
    53  	require.Error(t, err, "AuthServerLdap validated invalid credentials.")
    54  
    55  }