github.com/wgh-/mattermost-server@v4.8.0-rc2+incompatible/model/utils_test.go (about)

     1  // Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
     2  // See License.txt for license information.
     3  
     4  package model
     5  
     6  import (
     7  	"net/http"
     8  	"strings"
     9  	"testing"
    10  )
    11  
    12  func TestNewId(t *testing.T) {
    13  	for i := 0; i < 1000; i++ {
    14  		id := NewId()
    15  		if len(id) > 26 {
    16  			t.Fatal("ids shouldn't be longer than 26 chars")
    17  		}
    18  	}
    19  }
    20  
    21  func TestRandomString(t *testing.T) {
    22  	for i := 0; i < 1000; i++ {
    23  		r := NewRandomString(32)
    24  		if len(r) != 32 {
    25  			t.Fatal("should be 32 chars")
    26  		}
    27  	}
    28  }
    29  
    30  func TestAppError(t *testing.T) {
    31  	err := NewAppError("TestAppError", "message", nil, "", http.StatusInternalServerError)
    32  	json := err.ToJson()
    33  	rerr := AppErrorFromJson(strings.NewReader(json))
    34  	if err.Message != rerr.Message {
    35  		t.Fatal()
    36  	}
    37  
    38  	t.Log(err.Error())
    39  }
    40  
    41  func TestAppErrorJunk(t *testing.T) {
    42  	rerr := AppErrorFromJson(strings.NewReader("<html><body>This is a broken test</body></html>"))
    43  	if "body: <html><body>This is a broken test</body></html>" != rerr.DetailedError {
    44  		t.Fatal()
    45  	}
    46  }
    47  
    48  func TestMapJson(t *testing.T) {
    49  
    50  	m := make(map[string]string)
    51  	m["id"] = "test_id"
    52  	json := MapToJson(m)
    53  
    54  	rm := MapFromJson(strings.NewReader(json))
    55  
    56  	if rm["id"] != "test_id" {
    57  		t.Fatal("map should be valid")
    58  	}
    59  
    60  	rm2 := MapFromJson(strings.NewReader(""))
    61  	if len(rm2) > 0 {
    62  		t.Fatal("make should be ivalid")
    63  	}
    64  }
    65  
    66  func TestValidEmail(t *testing.T) {
    67  	if !IsValidEmail("corey+test@hulen.com") {
    68  		t.Error("email should be valid")
    69  	}
    70  
    71  	if IsValidEmail("@corey+test@hulen.com") {
    72  		t.Error("should be invalid")
    73  	}
    74  }
    75  
    76  func TestValidLower(t *testing.T) {
    77  	if !IsLower("corey+test@hulen.com") {
    78  		t.Error("should be valid")
    79  	}
    80  
    81  	if IsLower("Corey+test@hulen.com") {
    82  		t.Error("should be invalid")
    83  	}
    84  }
    85  
    86  func TestEtag(t *testing.T) {
    87  	etag := Etag("hello", 24)
    88  	if len(etag) <= 0 {
    89  		t.Fatal()
    90  	}
    91  }
    92  
    93  var hashtags = map[string]string{
    94  	"#test":           "#test",
    95  	"test":            "",
    96  	"#test123":        "#test123",
    97  	"#123test123":     "",
    98  	"#test-test":      "#test-test",
    99  	"#test?":          "#test",
   100  	"hi #there":       "#there",
   101  	"#bug #idea":      "#bug #idea",
   102  	"#bug or #gif!":   "#bug #gif",
   103  	"#hüllo":          "#hüllo",
   104  	"#?test":          "",
   105  	"#-test":          "",
   106  	"#yo_yo":          "#yo_yo",
   107  	"(#brakets)":      "#brakets",
   108  	")#stekarb(":      "#stekarb",
   109  	"<#less_than<":    "#less_than",
   110  	">#greater_than>": "#greater_than",
   111  	"-#minus-":        "#minus",
   112  	"_#under_":        "#under",
   113  	"+#plus+":         "#plus",
   114  	"=#equals=":       "#equals",
   115  	"%#pct%":          "#pct",
   116  	"&#and&":          "#and",
   117  	"^#hat^":          "#hat",
   118  	"##brown#":        "#brown",
   119  	"*#star*":         "#star",
   120  	"|#pipe|":         "#pipe",
   121  	":#colon:":        "#colon",
   122  	";#semi;":         "#semi",
   123  	"#Mötley;":        "#Mötley",
   124  	".#period.":       "#period",
   125  	"¿#upside¿":       "#upside",
   126  	"\"#quote\"":      "#quote",
   127  	"/#slash/":        "#slash",
   128  	"\\#backslash\\":  "#backslash",
   129  	"#a":              "",
   130  	"#1":              "",
   131  	"foo#bar":         "",
   132  }
   133  
   134  func TestParseHashtags(t *testing.T) {
   135  	for input, output := range hashtags {
   136  		if o, _ := ParseHashtags(input); o != output {
   137  			t.Fatal("failed to parse hashtags from input=" + input + " expected=" + output + " actual=" + o)
   138  		}
   139  	}
   140  }
   141  
   142  func TestIsValidAlphaNum(t *testing.T) {
   143  	cases := []struct {
   144  		Input  string
   145  		Result bool
   146  	}{
   147  		{
   148  			Input:  "test",
   149  			Result: true,
   150  		},
   151  		{
   152  			Input:  "test-name",
   153  			Result: true,
   154  		},
   155  		{
   156  			Input:  "test--name",
   157  			Result: true,
   158  		},
   159  		{
   160  			Input:  "test__name",
   161  			Result: true,
   162  		},
   163  		{
   164  			Input:  "-",
   165  			Result: false,
   166  		},
   167  		{
   168  			Input:  "__",
   169  			Result: false,
   170  		},
   171  		{
   172  			Input:  "test-",
   173  			Result: false,
   174  		},
   175  		{
   176  			Input:  "test--",
   177  			Result: false,
   178  		},
   179  		{
   180  			Input:  "test__",
   181  			Result: false,
   182  		},
   183  		{
   184  			Input:  "test:name",
   185  			Result: false,
   186  		},
   187  	}
   188  
   189  	for _, tc := range cases {
   190  		actual := IsValidAlphaNum(tc.Input)
   191  		if actual != tc.Result {
   192  			t.Fatalf("case: %v\tshould returned: %#v", tc, tc.Result)
   193  		}
   194  	}
   195  }
   196  
   197  func TestGetServerIpAddress(t *testing.T) {
   198  	if len(GetServerIpAddress()) == 0 {
   199  		t.Fatal("Should find local ip address")
   200  	}
   201  }
   202  
   203  func TestIsValidAlphaNumHyphenUnderscore(t *testing.T) {
   204  	casesWithFormat := []struct {
   205  		Input  string
   206  		Result bool
   207  	}{
   208  		{
   209  			Input:  "test",
   210  			Result: true,
   211  		},
   212  		{
   213  			Input:  "test-name",
   214  			Result: true,
   215  		},
   216  		{
   217  			Input:  "test--name",
   218  			Result: true,
   219  		},
   220  		{
   221  			Input:  "test__name",
   222  			Result: true,
   223  		},
   224  		{
   225  			Input:  "test_name",
   226  			Result: true,
   227  		},
   228  		{
   229  			Input:  "test_-name",
   230  			Result: true,
   231  		},
   232  		{
   233  			Input:  "-",
   234  			Result: false,
   235  		},
   236  		{
   237  			Input:  "__",
   238  			Result: false,
   239  		},
   240  		{
   241  			Input:  "test-",
   242  			Result: false,
   243  		},
   244  		{
   245  			Input:  "test--",
   246  			Result: false,
   247  		},
   248  		{
   249  			Input:  "test__",
   250  			Result: false,
   251  		},
   252  		{
   253  			Input:  "test:name",
   254  			Result: false,
   255  		},
   256  	}
   257  
   258  	for _, tc := range casesWithFormat {
   259  		actual := IsValidAlphaNumHyphenUnderscore(tc.Input, true)
   260  		if actual != tc.Result {
   261  			t.Fatalf("case: %v\tshould returned: %#v", tc, tc.Result)
   262  		}
   263  	}
   264  
   265  	casesWithoutFormat := []struct {
   266  		Input  string
   267  		Result bool
   268  	}{
   269  		{
   270  			Input:  "test",
   271  			Result: true,
   272  		},
   273  		{
   274  			Input:  "test-name",
   275  			Result: true,
   276  		},
   277  		{
   278  			Input:  "test--name",
   279  			Result: true,
   280  		},
   281  		{
   282  			Input:  "test__name",
   283  			Result: true,
   284  		},
   285  		{
   286  			Input:  "test_name",
   287  			Result: true,
   288  		},
   289  		{
   290  			Input:  "test_-name",
   291  			Result: true,
   292  		},
   293  		{
   294  			Input:  "-",
   295  			Result: true,
   296  		},
   297  		{
   298  			Input:  "_",
   299  			Result: true,
   300  		},
   301  		{
   302  			Input:  "test-",
   303  			Result: true,
   304  		},
   305  		{
   306  			Input:  "test--",
   307  			Result: true,
   308  		},
   309  		{
   310  			Input:  "test__",
   311  			Result: true,
   312  		},
   313  		{
   314  			Input:  ".",
   315  			Result: false,
   316  		},
   317  
   318  		{
   319  			Input:  "test,",
   320  			Result: false,
   321  		},
   322  		{
   323  			Input:  "test:name",
   324  			Result: false,
   325  		},
   326  	}
   327  
   328  	for _, tc := range casesWithoutFormat {
   329  		actual := IsValidAlphaNumHyphenUnderscore(tc.Input, false)
   330  		if actual != tc.Result {
   331  			t.Fatalf("case: '%v'\tshould returned: %#v", tc.Input, tc.Result)
   332  		}
   333  	}
   334  }
   335  
   336  func TestIsValidId(t *testing.T) {
   337  	cases := []struct {
   338  		Input  string
   339  		Result bool
   340  	}{
   341  		{
   342  			Input:  NewId(),
   343  			Result: true,
   344  		},
   345  		{
   346  			Input:  "",
   347  			Result: false,
   348  		},
   349  		{
   350  			Input:  "junk",
   351  			Result: false,
   352  		},
   353  		{
   354  			Input:  "qwertyuiop1234567890asdfg{",
   355  			Result: false,
   356  		},
   357  		{
   358  			Input:  NewId() + "}",
   359  			Result: false,
   360  		},
   361  	}
   362  
   363  	for _, tc := range cases {
   364  		actual := IsValidId(tc.Input)
   365  		if actual != tc.Result {
   366  			t.Fatalf("case: %v\tshould returned: %#v", tc, tc.Result)
   367  		}
   368  	}
   369  }