summaryrefslogtreecommitdiff
path: root/validator_test.go
diff options
context:
space:
mode:
authorMarkus Krogh <markus@nordu.net>2018-06-08 08:01:06 +0200
committerMarkus Krogh <markus@nordu.net>2018-06-08 08:01:06 +0200
commitfc2455cdbf8c64c98a8f7104ae7e7acdcff1337c (patch)
treea80e94c6bbe7fbd768379dd940a269ae0920e744 /validator_test.go
parent16fe3880782e38a1adaa157f26b788049bcc3205 (diff)
Go impl of pwman, first draft
Diffstat (limited to 'validator_test.go')
-rw-r--r--validator_test.go66
1 files changed, 66 insertions, 0 deletions
diff --git a/validator_test.go b/validator_test.go
new file mode 100644
index 0000000..618692f
--- /dev/null
+++ b/validator_test.go
@@ -0,0 +1,66 @@
+package main
+
+import (
+ "strings"
+ "testing"
+)
+
+func TestValidatePasswordOk(t *testing.T) {
+ err := validatePassword("Testp4ssw0r_d")
+ if err != nil {
+ t.Error("Test password should pass validation")
+ }
+}
+
+func TestValidatePasswordLength(t *testing.T) {
+ err := validatePassword("test")
+ if err == nil {
+ t.Error("Password should fail validation")
+ }
+
+ if !strings.Contains(err.Error(), "least 10 characters") {
+ t.Error("Error should tell that the password is too short")
+ }
+ if !strings.Contains(err.Error(), "was 4 characters") {
+ t.Error("Error should contain the current password length")
+ }
+}
+
+func TestValidatePasswordUpperAndLower(t *testing.T) {
+
+ err := validatePassword("testtestfest")
+ if err == nil {
+ t.Error("All lowercase should fail uppercase validation")
+ }
+ if !strings.Contains(err.Error(), "uppercase") {
+ t.Error("Error message should contain uppercase")
+ }
+
+ err = validatePassword("TESTTESTFEST")
+ if err == nil {
+ t.Error("All uppercase should fail lowercase validation")
+ }
+ if !strings.Contains(err.Error(), "lowercase") {
+ t.Error("Error message should contain lowercase")
+ }
+}
+
+func TestValidatePasswordSpecialAndNumbers(t *testing.T) {
+ base_pass := "testTestFest"
+ bad_passwords := []string{"", "2", "#3"}
+
+ var err error
+ for _, chr := range bad_passwords {
+
+ err = validatePassword(base_pass + chr)
+ if err == nil {
+ t.Errorf("Password %s should fail 3 numbers and/or special", base_pass+chr)
+ }
+ if !strings.Contains(err.Error(), "special characters") {
+ t.Errorf("Error message should contain special characters: %s", base_pass+chr)
+ }
+ if !strings.Contains(err.Error(), "numbers") {
+ t.Errorf("Error message should contain 'numbers': %s", base_pass+chr)
+ }
+ }
+}