github.com/cs3org/reva/v2@v2.27.7/pkg/password/password_policies_test.go (about)

     1  package password
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/stretchr/testify/assert"
     7  )
     8  
     9  func TestPolicies_Validate(t *testing.T) {
    10  	type fields struct {
    11  		minCharacters          int
    12  		minLowerCaseCharacters int
    13  		minUpperCaseCharacters int
    14  		minDigits              int
    15  		minSpecialCharacters   int
    16  		bannedPasswordsList    map[string]struct{}
    17  	}
    18  	tests := []struct {
    19  		name    string
    20  		fields  fields
    21  		args    string
    22  		wantErr bool
    23  		errMsg  string
    24  	}{
    25  		{
    26  			name: "all in one",
    27  			fields: fields{
    28  				minCharacters:          100,
    29  				minLowerCaseCharacters: 29,
    30  				minUpperCaseCharacters: 29,
    31  				minDigits:              10,
    32  				minSpecialCharacters:   32,
    33  			},
    34  			args: "1234567890abcdefghijklmnopqrstuvwxyzäöüABCDEFGHIJKLMNOPQRSTUVWXYZÄÖÜ !\"#$%&'()*+,-./:;<=>?@[\\]^_`{|}~",
    35  		},
    36  		{
    37  			name: "exactly",
    38  			fields: fields{
    39  				minCharacters:          19,
    40  				minLowerCaseCharacters: 7,
    41  				minUpperCaseCharacters: 7,
    42  				minDigits:              1,
    43  				minSpecialCharacters:   1,
    44  			},
    45  			args: "0äÖ-世界іІїЇЯяЙйßAaBb",
    46  		},
    47  		{
    48  			name: "error",
    49  			fields: fields{
    50  				minCharacters:          2,
    51  				minLowerCaseCharacters: 2,
    52  				minUpperCaseCharacters: 2,
    53  				minDigits:              2,
    54  				minSpecialCharacters:   2,
    55  			},
    56  			args:    "0äÖ-",
    57  			wantErr: true,
    58  			errMsg:  "at least 2 lowercase letters are required\nat least 2 uppercase letters are required\nat least 2 numbers are required\nat least 2 special characters are required  !\"#$%&'()*+,-./:;<=>?@[\\]^_`{|}~",
    59  		},
    60  		{
    61  			name: "banned list",
    62  			fields: fields{
    63  				minCharacters:          10,
    64  				minLowerCaseCharacters: 3,
    65  				minUpperCaseCharacters: 3,
    66  				minDigits:              3,
    67  				minSpecialCharacters:   1,
    68  				bannedPasswordsList:    map[string]struct{}{"123abcABC!": struct{}{}},
    69  			},
    70  			args:    "123abcABC!",
    71  			wantErr: true,
    72  			errMsg:  "unfortunately, your password is commonly used. please pick a harder-to-guess password for your safety",
    73  		},
    74  	}
    75  	for _, tt := range tests {
    76  		t.Run(tt.name, func(t *testing.T) {
    77  			s := NewPasswordPolicy(
    78  				tt.fields.minCharacters,
    79  				tt.fields.minLowerCaseCharacters,
    80  				tt.fields.minUpperCaseCharacters,
    81  				tt.fields.minDigits,
    82  				tt.fields.minSpecialCharacters,
    83  				tt.fields.bannedPasswordsList,
    84  			)
    85  			err := s.Validate(tt.args)
    86  			if tt.wantErr {
    87  				assert.EqualError(t, err, tt.errMsg)
    88  			} else {
    89  				assert.NoError(t, err)
    90  			}
    91  		})
    92  	}
    93  }
    94  
    95  func TestPasswordPolicies_Count(t *testing.T) {
    96  	type want struct {
    97  		wantCharacters          int
    98  		wantLowerCaseCharacters int
    99  		wantUpperCaseCharacters int
   100  		wantDigits              int
   101  		wantSpecialCharacters   int
   102  	}
   103  	tests := []struct {
   104  		name    string
   105  		fields  want
   106  		args    string
   107  		wantErr bool
   108  	}{
   109  		{
   110  			name: "all in one",
   111  			fields: want{
   112  				wantCharacters:          101,
   113  				wantLowerCaseCharacters: 29,
   114  				wantUpperCaseCharacters: 29,
   115  				wantDigits:              10,
   116  				wantSpecialCharacters:   33,
   117  			},
   118  			args: "1234567890abcdefghijklmnopqrstuvwxyzäöüABCDEFGHIJKLMNOPQRSTUVWXYZÄÖÜ !\"#$%&'()*+,-./:;<=>?@[\\]^_`{|}~",
   119  		},
   120  		{
   121  			name: "length only",
   122  			fields: want{
   123  				wantCharacters:          3,
   124  				wantLowerCaseCharacters: 0,
   125  				wantUpperCaseCharacters: 0,
   126  				wantDigits:              0,
   127  				wantSpecialCharacters:   0,
   128  			},
   129  			args: "世界ß",
   130  		},
   131  		{
   132  			name: "length only",
   133  			fields: want{
   134  				wantCharacters:          21,
   135  				wantLowerCaseCharacters: 7,
   136  				wantUpperCaseCharacters: 7,
   137  				wantDigits:              1,
   138  				wantSpecialCharacters:   3,
   139  			},
   140  			args: "0äÖ-世界 іІїЇЯяЙй ßAaBb",
   141  		},
   142  	}
   143  	for _, tt := range tests {
   144  		t.Run(tt.name, func(t *testing.T) {
   145  			i := NewPasswordPolicy(
   146  				tt.fields.wantCharacters,
   147  				tt.fields.wantLowerCaseCharacters,
   148  				tt.fields.wantUpperCaseCharacters,
   149  				tt.fields.wantDigits,
   150  				tt.fields.wantSpecialCharacters,
   151  				nil,
   152  			)
   153  			s := i.(*Policies)
   154  			if got := s.count(tt.args); got != tt.fields.wantCharacters {
   155  				t.Errorf("count() = %v, want %v", got, tt.fields.wantCharacters)
   156  			}
   157  			if got := s.countLowerCaseCharacters(tt.args); got != tt.fields.wantLowerCaseCharacters {
   158  				t.Errorf("countLowerCaseCharacters() = %v, want %v", got, tt.fields.wantLowerCaseCharacters)
   159  			}
   160  			if got := s.countUpperCaseCharacters(tt.args); got != tt.fields.wantUpperCaseCharacters {
   161  				t.Errorf("countUpperCaseCharacters() = %v, want %v", got, tt.fields.wantUpperCaseCharacters)
   162  			}
   163  			if got := s.countDigits(tt.args); got != tt.fields.wantDigits {
   164  				t.Errorf("countDigits() = %v, want %v", got, tt.fields.wantDigits)
   165  			}
   166  			if got := s.countSpecialCharacters(tt.args); got != tt.fields.wantSpecialCharacters {
   167  				t.Errorf("countSpecialCharacters() = %v, want %v", got, tt.fields.wantSpecialCharacters)
   168  			}
   169  		})
   170  	}
   171  }