github.com/polarismesh/polaris@v1.17.8/auth/defaultauth/utils_test.go (about)

     1  /**
     2   * Tencent is pleased to support the open source community by making Polaris available.
     3   *
     4   * Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved.
     5   *
     6   * Licensed under the BSD 3-Clause License (the "License");
     7   * you may not use this file except in compliance with the License.
     8   * You may obtain a copy of the License at
     9   *
    10   * https://opensource.org/licenses/BSD-3-Clause
    11   *
    12   * Unless required by applicable law or agreed to in writing, software distributed
    13   * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
    14   * CONDITIONS OF ANY KIND, either express or implied. See the License for the
    15   * specific language governing permissions and limitations under the License.
    16   */
    17  
    18  package defaultauth_test
    19  
    20  import (
    21  	"testing"
    22  
    23  	"github.com/golang/protobuf/ptypes/wrappers"
    24  
    25  	"github.com/polarismesh/polaris/auth/defaultauth"
    26  	"github.com/polarismesh/polaris/common/utils"
    27  )
    28  
    29  func Test_checkPassword(t *testing.T) {
    30  	type args struct {
    31  		password *wrappers.StringValue
    32  	}
    33  	tests := []struct {
    34  		name    string
    35  		args    args
    36  		wantErr bool
    37  	}{
    38  		{
    39  			args: args{
    40  				password: utils.NewStringValue("1234"),
    41  			},
    42  			wantErr: true,
    43  		},
    44  		{
    45  			args: args{
    46  				password: utils.NewStringValue("123456"),
    47  			},
    48  			wantErr: false,
    49  		},
    50  		{
    51  			args: args{
    52  				password: utils.NewStringValue("abc45"),
    53  			},
    54  			wantErr: true,
    55  		},
    56  		{
    57  			args: args{
    58  				password: utils.NewStringValue("Aabc456"),
    59  			},
    60  			wantErr: false,
    61  		},
    62  		{
    63  			args: args{
    64  				password: utils.NewStringValue("Aa@@bc456"),
    65  			},
    66  			wantErr: false,
    67  		},
    68  		{
    69  			args: args{
    70  				password: utils.NewStringValue("Aa@@bc45~6"),
    71  			},
    72  			wantErr: false,
    73  		},
    74  		{
    75  			args: args{
    76  				password: utils.NewStringValue("abc456abc4565612"),
    77  			},
    78  			wantErr: false,
    79  		},
    80  		{
    81  			args: args{
    82  				password: utils.NewStringValue("abc456abc456abc456"),
    83  			},
    84  			wantErr: true,
    85  		},
    86  		{
    87  			args: args{
    88  				password: utils.NewStringValue("123456123456123456"),
    89  			},
    90  			wantErr: true,
    91  		},
    92  		{
    93  			args: args{
    94  				password: utils.NewStringValue("abc456abc456abc45612"),
    95  			},
    96  			wantErr: true,
    97  		},
    98  	}
    99  	for _, tt := range tests {
   100  		t.Run(tt.name, func(t *testing.T) {
   101  			if err := defaultauth.TestCheckPassword(tt.args.password); (err != nil) != tt.wantErr {
   102  				t.Errorf("checkPassword() error = %v, wantErr %v, args = %#v", err, tt.wantErr, tt.args.password.GetValue())
   103  			}
   104  		})
   105  	}
   106  }
   107  
   108  func Test_checkName(t *testing.T) {
   109  	type args struct {
   110  		name *wrappers.StringValue
   111  	}
   112  	tests := []struct {
   113  		name    string
   114  		args    args
   115  		wantErr bool
   116  	}{
   117  		{
   118  			args: args{
   119  				name: utils.NewStringValue("123123"),
   120  			},
   121  			wantErr: false,
   122  		},
   123  		{
   124  			args: args{
   125  				name: utils.NewStringValue("测试鉴权策略-1"),
   126  			},
   127  			wantErr: false,
   128  		},
   129  		{
   130  			args: args{
   131  				name: utils.NewStringValue("测试鉴-权策略_1."),
   132  			},
   133  			wantErr: false,
   134  		},
   135  		{
   136  			args: args{
   137  				name: utils.NewStringValue("qweqwe-权qweqw策略_1"),
   138  			},
   139  			wantErr: false,
   140  		},
   141  		{
   142  			args: args{
   143  				name: utils.NewStringValue("测试鉴权策略&1"),
   144  			},
   145  			wantErr: true,
   146  		},
   147  	}
   148  	for _, tt := range tests {
   149  		t.Run(tt.name, func(t *testing.T) {
   150  			if err := defaultauth.TestCheckName(tt.args.name); (err != nil) != tt.wantErr {
   151  				t.Errorf("checkName() error = %v, wantErr %v", err, tt.wantErr)
   152  			}
   153  		})
   154  	}
   155  }