From 14ad79466bd7e59e6db29cd9e5523591e33cbb8e Mon Sep 17 00:00:00 2001 From: Srinjoy Sen Chowdhury <116475469+AllMightLegend@users.noreply.github.com> Date: Thu, 15 Aug 2024 21:09:31 +0530 Subject: [PATCH] Create password_validation_test.go --- test/password_validation_test.go | 33 ++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 test/password_validation_test.go diff --git a/test/password_validation_test.go b/test/password_validation_test.go new file mode 100644 index 000000000..5be66f4eb --- /dev/null +++ b/test/password_validation_test.go @@ -0,0 +1,33 @@ +package main + +import "testing" + +func TestValidatePassword(t *testing.T) { + tests := []struct { + password string + config PasswordConfig + expectErr bool + }{ + // Test cases for length + {"short", defaultPasswordConfig, true}, + {"longEnoughPassword1!", defaultPasswordConfig, false}, + {string(make([]byte, maxPasswordLength+1)), defaultPasswordConfig, true}, + + // Test cases for character requirements + {"NoDigitsOrSpecialChars", defaultPasswordConfig, true}, + {"WithDigits1", defaultPasswordConfig, true}, + {"WithSpecialChars!", defaultPasswordConfig, true}, + {"ValidP@ssw0rd", defaultPasswordConfig, false}, + + // Custom config examples + {"NoSpecialChar123", PasswordConfig{minPasswordLength, maxPasswordLength, true, true, true, false}, false}, + {"alllowercase1!", PasswordConfig{minPasswordLength, maxPasswordLength, false, true, true, true}, false}, + } + + for _, tt := range tests { + err := ValidatePassword(tt.password, tt.config) + if (err != nil) != tt.expectErr { + t.Errorf("ValidatePassword(%s) = %v, expected error = %v", tt.password, err, tt.expectErr) + } + } +}