github.com/volatiletech/authboss@v2.4.1+incompatible/defaults/rules_test.go (about)

     1  package defaults
     2  
     3  import (
     4  	"regexp"
     5  	"testing"
     6  )
     7  
     8  func TestRules_Errors(t *testing.T) {
     9  	t.Parallel()
    10  
    11  	tests := []struct {
    12  		Rules Rules
    13  		In    string
    14  		Error string
    15  	}{
    16  		{
    17  			Rules{FieldName: "email", Required: true},
    18  			"",
    19  			"email: Cannot be blank",
    20  		},
    21  		{
    22  			Rules{FieldName: "email", Required: true},
    23  			"   \t\t\n   ",
    24  			"email: Cannot be blank",
    25  		},
    26  		{
    27  			Rules{FieldName: "email", MatchError: "Regexp must match!", MustMatch: regexp.MustCompile("abc")},
    28  			"hello",
    29  			"email: Regexp must match!",
    30  		},
    31  		{
    32  			Rules{FieldName: "email", MinLength: 5},
    33  			"hi",
    34  			"email: Must be at least 5 characters",
    35  		},
    36  		{
    37  			Rules{FieldName: "email", MinLength: 1},
    38  			"",
    39  			"email: Must be at least 1 character",
    40  		},
    41  		{
    42  			Rules{FieldName: "email", MaxLength: 3},
    43  			"hello",
    44  			"email: Must be at most 3 characters",
    45  		},
    46  		{
    47  			Rules{FieldName: "email", MinLength: 3, MaxLength: 5},
    48  			"hi",
    49  			"email: Must be between 3 and 5 characters",
    50  		},
    51  		{
    52  			Rules{FieldName: "email", MinUpper: 2, MinLower: 1},
    53  			"AA",
    54  			"email: Must contain at least 1 lowercase letter",
    55  		},
    56  		{
    57  			Rules{FieldName: "email", MinLetters: 5},
    58  			"13345",
    59  			"email: Must contain at least 5 letters",
    60  		},
    61  		{
    62  			Rules{FieldName: "email", MinUpper: 5},
    63  			"hi",
    64  			"email: Must contain at least 5 uppercase letters",
    65  		},
    66  		{
    67  			Rules{FieldName: "email", MinLower: 5},
    68  			"hi",
    69  			"email: Must contain at least 5 lowercase letters",
    70  		},
    71  		{
    72  			Rules{FieldName: "email", MinSymbols: 5},
    73  			"hi",
    74  			"email: Must contain at least 5 symbols",
    75  		},
    76  		{
    77  			Rules{FieldName: "email", MinNumeric: 5},
    78  			"hi",
    79  			"email: Must contain at least 5 numbers",
    80  		},
    81  		{
    82  			Rules{FieldName: "email"},
    83  			"hi whitespace",
    84  			"email: No whitespace permitted",
    85  		},
    86  	}
    87  
    88  	for i, test := range tests {
    89  		i = i + 1
    90  
    91  		err := test.Rules.Errors(test.In)
    92  		if err == nil {
    93  			t.Errorf("(%d) Wanted: %q", i, test.Error)
    94  			continue
    95  		}
    96  
    97  		if e := err.Error(); e != test.Error {
    98  			t.Errorf("(%d) The error was wrong: %q", i, e)
    99  		}
   100  	}
   101  }
   102  
   103  func TestRules_Rules(t *testing.T) {
   104  	t.Parallel()
   105  
   106  	r := Rules{
   107  		FieldName:       "email",
   108  		MatchError:      "Must adhere to this regexp",
   109  		MustMatch:       regexp.MustCompile(""),
   110  		MinLength:       1,
   111  		MaxLength:       2,
   112  		MinLetters:      3,
   113  		MinUpper:        4,
   114  		MinLower:        5,
   115  		MinNumeric:      6,
   116  		MinSymbols:      7,
   117  		AllowWhitespace: false,
   118  	}
   119  
   120  	rules := r.Rules()
   121  
   122  	mustFind := []string{
   123  		"Must adhere to this regexp",
   124  		"Must be between 1 and 2 characters",
   125  		"Must contain at least 3 letters",
   126  		"Must contain at least 4 uppercase letters",
   127  		"Must contain at least 5 lowercase letters",
   128  		"Must contain at least 6 numbers",
   129  		"Must contain at least 7 symbols",
   130  	}
   131  
   132  	for i, toFind := range mustFind {
   133  		if rules[i] != toFind {
   134  			t.Error("Expected:", toFind, "got:", rules[i])
   135  		}
   136  	}
   137  }
   138  
   139  func TestRules_IsValid(t *testing.T) {
   140  	t.Parallel()
   141  
   142  	r := Rules{FieldName: "email", Required: true}
   143  	if r.IsValid("") {
   144  		t.Error("It should not be valid.")
   145  	}
   146  
   147  	if !r.IsValid("joe@joe.com") {
   148  		t.Error("It should be valid.")
   149  	}
   150  }
   151  
   152  func TestTallyCharacters(t *testing.T) {
   153  	t.Parallel()
   154  
   155  	u, l, n, s, w := tallyCharacters("123abcDEF@#$%^*   ")
   156  	if u != 3 {
   157  		t.Error("Number of upper:", u)
   158  	}
   159  	if l != 3 {
   160  		t.Error("Number of lower:", l)
   161  	}
   162  	if n != 3 {
   163  		t.Error("Number of numerics:", n)
   164  	}
   165  	if s != 6 {
   166  		t.Error("Number of symbols:", s)
   167  	}
   168  	if w != 3 {
   169  		t.Error("Number of whitespace:", w)
   170  	}
   171  }