summaryrefslogtreecommitdiff
path: root/validator_test.go
blob: 618692fb907cf2098b25dac6861f8de9b01676f5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
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)
		}
	}
}