github.com/greenpau/go-authcrunch@v1.1.4/pkg/identity/password_test.go (about) 1 // Copyright 2022 Paul Greenberg greenpau@outlook.com 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package identity 16 17 import ( 18 "fmt" 19 "testing" 20 21 "github.com/greenpau/go-authcrunch/internal/tests" 22 "github.com/greenpau/go-authcrunch/pkg/errors" 23 ) 24 25 func TestNewPassword(t *testing.T) { 26 testcases := []struct { 27 name string 28 quick bool 29 purpose string 30 algorithm string 31 params map[string]interface{} 32 input string 33 password string 34 want map[string]interface{} 35 shouldErr bool 36 err error 37 }{ 38 { 39 name: "test password", 40 quick: true, 41 input: "foobar", 42 password: "foobar", 43 want: map[string]interface{}{ 44 "purpose": "generic", 45 "algorithm": "bcrypt", 46 "cost": 10, 47 "password_match": true, 48 }, 49 }, 50 { 51 name: "test password with options", 52 purpose: "generic", 53 algorithm: "bcrypt", 54 params: map[string]interface{}{ 55 "cost": 10, 56 }, 57 input: "foobar", 58 password: "foobar2", 59 want: map[string]interface{}{ 60 "purpose": "generic", 61 "algorithm": "bcrypt", 62 "cost": 10, 63 "password_match": false, 64 }, 65 }, 66 { 67 name: "test password with invalid bcrypt params", 68 purpose: "generic", 69 algorithm: "bcrypt", 70 params: map[string]interface{}{ 71 "cost": 10000, 72 }, 73 input: "foobar", 74 password: "foobar", 75 shouldErr: true, 76 err: errors.ErrPasswordGenerate.WithArgs("crypto/bcrypt: cost 10000 is outside allowed range (4,31)"), 77 }, 78 { 79 name: "test password with empty hash algorithm", 80 input: "foobar", 81 shouldErr: true, 82 err: errors.ErrPasswordEmptyAlgorithm, 83 }, 84 { 85 name: "test password with empty hash algorithm", 86 algorithm: "foobar", 87 input: "foobar", 88 shouldErr: true, 89 err: errors.ErrPasswordUnsupportedAlgorithm.WithArgs("foobar"), 90 }, 91 { 92 name: "test empty password", 93 input: " ", 94 shouldErr: true, 95 err: errors.ErrPasswordEmpty, 96 }, 97 } 98 for _, tc := range testcases { 99 t.Run(tc.name, func(t *testing.T) { 100 var entry *Password 101 var err error 102 msgs := []string{fmt.Sprintf("test name: %s", tc.name)} 103 if tc.quick { 104 entry, err = NewPassword(tc.input) 105 } else { 106 entry, err = NewPasswordWithOptions(tc.input, tc.purpose, tc.algorithm, tc.params) 107 } 108 if tests.EvalErrWithLog(t, err, "new password", tc.shouldErr, tc.err, msgs) { 109 return 110 } 111 got := make(map[string]interface{}) 112 got["purpose"] = entry.Purpose 113 got["algorithm"] = entry.Algorithm 114 got["cost"] = entry.Cost 115 got["password_match"] = entry.Match(tc.password) 116 tests.EvalObjectsWithLog(t, "eval", tc.want, got, msgs) 117 entry.Disable() 118 }) 119 } 120 }