github.com/m3db/m3@v1.5.1-0.20231129193456-75a402aa583b/src/dbnode/auth/entities_test.go (about)

     1  // Copyright (c) 2023 Uber Technologies, Inc.
     2  //
     3  // Permission is hereby granted, free of charge, to any person obtaining a copy
     4  // of this software and associated documentation files (the "Software"), to deal
     5  // in the Software without restriction, including without limitation the rights
     6  // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
     7  // copies of the Software, and to permit persons to whom the Software is
     8  // furnished to do so, subject to the following conditions:
     9  //
    10  // The above copyright notice and this permission notice shall be included in
    11  // all copies or substantial portions of the Software.
    12  //
    13  // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    14  // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    15  // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
    16  // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
    17  // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
    18  // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
    19  // THE SOFTWARE.
    20  
    21  package auth
    22  
    23  import (
    24  	"testing"
    25  
    26  	"github.com/stretchr/testify/assert"
    27  )
    28  
    29  func TestInboundCredentials_Validate(t *testing.T) {
    30  	tests := []struct {
    31  		name     string
    32  		userName string
    33  		digest   string
    34  		credtype CredentialType
    35  		err      string
    36  	}{
    37  		{
    38  			name:     "no error",
    39  			userName: "abc",
    40  			digest:   "xyz",
    41  			credtype: CredentialClient,
    42  		}, {
    43  			name:   "username missing",
    44  			digest: "xyz",
    45  			err:    "username field is empty",
    46  		}, {
    47  			name:     "digest missing",
    48  			userName: "xyz",
    49  			credtype: CredentialClient,
    50  			err:      "digest field is empty for inbound",
    51  		}, {
    52  			name:     "cred type missing",
    53  			userName: "abc",
    54  			digest:   "xyz",
    55  			err:      "incorrect cred type field for inbound",
    56  		}, {
    57  			name:     "incorrect cred type",
    58  			userName: "abc",
    59  			digest:   "xyz",
    60  			credtype: CredentialEtcd,
    61  			err:      "incorrect cred type field for inbound",
    62  		},
    63  	}
    64  	for _, tt := range tests {
    65  		t.Run(tt.name, func(t *testing.T) {
    66  			newCreds := &InboundCredentials{Username: tt.userName, Digest: tt.digest, Type: tt.credtype}
    67  			err := newCreds.Validate()
    68  			if tt.err != "" {
    69  				assert.Contains(t, err.Error(), tt.err)
    70  			} else {
    71  				assert.NoError(t, err)
    72  			}
    73  		})
    74  	}
    75  }
    76  
    77  func TestOutboundCredentials_Validate(t *testing.T) {
    78  	tests := []struct {
    79  		name     string
    80  		userName string
    81  		password string
    82  		zone     string
    83  		credtype CredentialType
    84  		err      string
    85  	}{
    86  		{
    87  			name:     "no error",
    88  			userName: "abc",
    89  			password: "xyz",
    90  			zone:     "foo",
    91  			credtype: CredentialEtcd,
    92  		}, {
    93  			name:     "username missing",
    94  			password: "xyz",
    95  			zone:     "foo",
    96  			err:      "username field is empty",
    97  		}, {
    98  			name:     "password missing",
    99  			userName: "xyz",
   100  			zone:     "foo",
   101  			credtype: CredentialEtcd,
   102  			err:      "password field is empty",
   103  		}, {
   104  			name:     "zone missing",
   105  			userName: "abc",
   106  			password: "xyz",
   107  			credtype: CredentialPeer,
   108  			err:      "zone field is not set",
   109  		}, {
   110  			name:     "zone missing for dbnode - etcd",
   111  			userName: "abc",
   112  			password: "xyz",
   113  			credtype: CredentialEtcd,
   114  			err:      "zone field is not set",
   115  		}, {
   116  			name:     "cred type missing",
   117  			userName: "abc",
   118  			password: "xyz",
   119  			zone:     "foo",
   120  			err:      "incorrect cred type field for outbound",
   121  		},
   122  	}
   123  	for _, tt := range tests {
   124  		t.Run(tt.name, func(t *testing.T) {
   125  			newCreds := &OutboundCredentials{Username: tt.userName, Password: tt.password, Zone: tt.zone, Type: tt.credtype}
   126  			err := newCreds.Validate()
   127  			if tt.err != "" {
   128  				assert.Contains(t, err.Error(), tt.err)
   129  			} else {
   130  				assert.NoError(t, err)
   131  			}
   132  		})
   133  	}
   134  }