github.com/wgh-/mattermost-server@v4.8.0-rc2+incompatible/model/user_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  	"fmt"
     8  	"net/http"
     9  	"strings"
    10  	"testing"
    11  )
    12  
    13  func TestPasswordHash(t *testing.T) {
    14  	hash := HashPassword("Test")
    15  
    16  	if !ComparePassword(hash, "Test") {
    17  		t.Fatal("Passwords don't match")
    18  	}
    19  
    20  	if ComparePassword(hash, "Test2") {
    21  		t.Fatal("Passwords should not have matched")
    22  	}
    23  }
    24  
    25  func TestUserJson(t *testing.T) {
    26  	user := User{Id: NewId(), Username: NewId()}
    27  	json := user.ToJson()
    28  	ruser := UserFromJson(strings.NewReader(json))
    29  
    30  	if user.Id != ruser.Id {
    31  		t.Fatal("Ids do not match")
    32  	}
    33  }
    34  
    35  func TestUserPreSave(t *testing.T) {
    36  	user := User{Password: "test"}
    37  	user.PreSave()
    38  	user.Etag(true, true)
    39  }
    40  
    41  func TestUserPreUpdate(t *testing.T) {
    42  	user := User{Password: "test"}
    43  	user.PreUpdate()
    44  }
    45  
    46  func TestUserUpdateMentionKeysFromUsername(t *testing.T) {
    47  	user := User{Username: "user"}
    48  	user.SetDefaultNotifications()
    49  
    50  	if user.NotifyProps["mention_keys"] != "user,@user" {
    51  		t.Fatalf("default mention keys are invalid: %v", user.NotifyProps["mention_keys"])
    52  	}
    53  
    54  	user.Username = "person"
    55  	user.UpdateMentionKeysFromUsername("user")
    56  	if user.NotifyProps["mention_keys"] != "person,@person" {
    57  		t.Fatalf("mention keys are invalid after changing username: %v", user.NotifyProps["mention_keys"])
    58  	}
    59  
    60  	user.NotifyProps["mention_keys"] += ",mention"
    61  	user.UpdateMentionKeysFromUsername("person")
    62  	if user.NotifyProps["mention_keys"] != "person,@person,mention" {
    63  		t.Fatalf("mention keys are invalid after adding extra mention keyword: %v", user.NotifyProps["mention_keys"])
    64  	}
    65  
    66  	user.Username = "user"
    67  	user.UpdateMentionKeysFromUsername("person")
    68  	if user.NotifyProps["mention_keys"] != "user,@user,mention" {
    69  		t.Fatalf("mention keys are invalid after changing username with extra mention keyword: %v", user.NotifyProps["mention_keys"])
    70  	}
    71  }
    72  
    73  func TestUserIsValid(t *testing.T) {
    74  	user := User{}
    75  
    76  	if err := user.IsValid(); !HasExpectedUserIsValidError(err, "id", "") {
    77  		t.Fatal(err)
    78  	}
    79  
    80  	user.Id = NewId()
    81  	if err := user.IsValid(); !HasExpectedUserIsValidError(err, "create_at", user.Id) {
    82  		t.Fatal()
    83  	}
    84  
    85  	user.CreateAt = GetMillis()
    86  	if err := user.IsValid(); !HasExpectedUserIsValidError(err, "update_at", user.Id) {
    87  		t.Fatal()
    88  	}
    89  
    90  	user.UpdateAt = GetMillis()
    91  	if err := user.IsValid(); !HasExpectedUserIsValidError(err, "username", user.Id) {
    92  		t.Fatal()
    93  	}
    94  
    95  	user.Username = NewId() + "^hello#"
    96  	if err := user.IsValid(); !HasExpectedUserIsValidError(err, "username", user.Id) {
    97  		t.Fatal()
    98  	}
    99  
   100  	user.Username = NewId()
   101  	user.Email = strings.Repeat("01234567890", 20)
   102  	if err := user.IsValid(); err == nil {
   103  		t.Fatal()
   104  	}
   105  
   106  	user.Email = strings.Repeat("a", 128)
   107  	user.Nickname = strings.Repeat("a", 65)
   108  	if err := user.IsValid(); !HasExpectedUserIsValidError(err, "nickname", user.Id) {
   109  		t.Fatal()
   110  	}
   111  
   112  	user.Nickname = strings.Repeat("a", 64)
   113  	if err := user.IsValid(); err != nil {
   114  		t.Fatal(err)
   115  	}
   116  
   117  	user.FirstName = ""
   118  	user.LastName = ""
   119  	if err := user.IsValid(); err != nil {
   120  		t.Fatal(err)
   121  	}
   122  
   123  	user.FirstName = strings.Repeat("a", 65)
   124  	if err := user.IsValid(); !HasExpectedUserIsValidError(err, "first_name", user.Id) {
   125  		t.Fatal(err)
   126  	}
   127  
   128  	user.FirstName = strings.Repeat("a", 64)
   129  	user.LastName = strings.Repeat("a", 65)
   130  	if err := user.IsValid(); !HasExpectedUserIsValidError(err, "last_name", user.Id) {
   131  		t.Fatal(err)
   132  	}
   133  
   134  	user.LastName = strings.Repeat("a", 64)
   135  	user.Position = strings.Repeat("a", 128)
   136  	if err := user.IsValid(); err != nil {
   137  		t.Fatal(err)
   138  	}
   139  
   140  	user.Position = strings.Repeat("a", 129)
   141  	if err := user.IsValid(); !HasExpectedUserIsValidError(err, "position", user.Id) {
   142  		t.Fatal(err)
   143  	}
   144  }
   145  
   146  func HasExpectedUserIsValidError(err *AppError, fieldName string, userId string) bool {
   147  	if err == nil {
   148  		return false
   149  	}
   150  
   151  	return err.Where == "User.IsValid" &&
   152  		err.Id == fmt.Sprintf("model.user.is_valid.%s.app_error", fieldName) &&
   153  		err.StatusCode == http.StatusBadRequest &&
   154  		(userId == "" || err.DetailedError == "user_id="+userId)
   155  }
   156  
   157  func TestUserGetFullName(t *testing.T) {
   158  	user := User{}
   159  
   160  	if fullName := user.GetFullName(); fullName != "" {
   161  		t.Fatal("Full name should be blank")
   162  	}
   163  
   164  	user.FirstName = "first"
   165  	if fullName := user.GetFullName(); fullName != "first" {
   166  		t.Fatal("Full name should be first name")
   167  	}
   168  
   169  	user.FirstName = ""
   170  	user.LastName = "last"
   171  	if fullName := user.GetFullName(); fullName != "last" {
   172  		t.Fatal("Full name should be last name")
   173  	}
   174  
   175  	user.FirstName = "first"
   176  	if fullName := user.GetFullName(); fullName != "first last" {
   177  		t.Fatal("Full name should be first name and last name")
   178  	}
   179  }
   180  
   181  func TestUserGetDisplayName(t *testing.T) {
   182  	user := User{Username: "username"}
   183  
   184  	if displayName := user.GetDisplayName(SHOW_FULLNAME); displayName != "username" {
   185  		t.Fatal("Display name should be username")
   186  	}
   187  
   188  	if displayName := user.GetDisplayName(SHOW_NICKNAME_FULLNAME); displayName != "username" {
   189  		t.Fatal("Display name should be username")
   190  	}
   191  
   192  	if displayName := user.GetDisplayName(SHOW_USERNAME); displayName != "username" {
   193  		t.Fatal("Display name should be username")
   194  	}
   195  
   196  	user.FirstName = "first"
   197  	user.LastName = "last"
   198  
   199  	if displayName := user.GetDisplayName(SHOW_FULLNAME); displayName != "first last" {
   200  		t.Fatal("Display name should be full name")
   201  	}
   202  
   203  	if displayName := user.GetDisplayName(SHOW_NICKNAME_FULLNAME); displayName != "first last" {
   204  		t.Fatal("Display name should be full name since there is no nickname")
   205  	}
   206  
   207  	if displayName := user.GetDisplayName(SHOW_USERNAME); displayName != "username" {
   208  		t.Fatal("Display name should be username")
   209  	}
   210  
   211  	user.Nickname = "nickname"
   212  	if displayName := user.GetDisplayName(SHOW_NICKNAME_FULLNAME); displayName != "nickname" {
   213  		t.Fatal("Display name should be nickname")
   214  	}
   215  }
   216  
   217  var usernames = []struct {
   218  	value    string
   219  	expected bool
   220  }{
   221  	{"spin-punch", true},
   222  	{"sp", true},
   223  	{"s", true},
   224  	{"1spin-punch", true},
   225  	{"-spin-punch", true},
   226  	{".spin-punch", true},
   227  	{"Spin-punch", false},
   228  	{"spin punch-", false},
   229  	{"spin_punch", true},
   230  	{"spin", true},
   231  	{"PUNCH", false},
   232  	{"spin.punch", true},
   233  	{"spin'punch", false},
   234  	{"spin*punch", false},
   235  	{"all", false},
   236  }
   237  
   238  func TestValidUsername(t *testing.T) {
   239  	for _, v := range usernames {
   240  		if IsValidUsername(v.value) != v.expected {
   241  			t.Errorf("expect %v as %v", v.value, v.expected)
   242  		}
   243  	}
   244  }
   245  
   246  func TestNormalizeUsername(t *testing.T) {
   247  	if NormalizeUsername("Spin-punch") != "spin-punch" {
   248  		t.Fatal("didn't normalize username properly")
   249  	}
   250  	if NormalizeUsername("PUNCH") != "punch" {
   251  		t.Fatal("didn't normalize username properly")
   252  	}
   253  	if NormalizeUsername("spin") != "spin" {
   254  		t.Fatal("didn't normalize username properly")
   255  	}
   256  }
   257  
   258  func TestNormalizeEmail(t *testing.T) {
   259  	if NormalizeEmail("TEST@EXAMPLE.COM") != "test@example.com" {
   260  		t.Fatal("didn't normalize email properly")
   261  	}
   262  	if NormalizeEmail("TEST2@example.com") != "test2@example.com" {
   263  		t.Fatal("didn't normalize email properly")
   264  	}
   265  	if NormalizeEmail("test3@example.com") != "test3@example.com" {
   266  		t.Fatal("didn't normalize email properly")
   267  	}
   268  }
   269  
   270  func TestCleanUsername(t *testing.T) {
   271  	if CleanUsername("Spin-punch") != "spin-punch" {
   272  		t.Fatal("didn't clean name properly")
   273  	}
   274  	if CleanUsername("PUNCH") != "punch" {
   275  		t.Fatal("didn't clean name properly")
   276  	}
   277  	if CleanUsername("spin'punch") != "spin-punch" {
   278  		t.Fatal("didn't clean name properly")
   279  	}
   280  	if CleanUsername("spin") != "spin" {
   281  		t.Fatal("didn't clean name properly")
   282  	}
   283  	if len(CleanUsername("all")) != 27 {
   284  		t.Fatal("didn't clean name properly")
   285  	}
   286  }
   287  
   288  func TestRoles(t *testing.T) {
   289  
   290  	if IsValidUserRoles("admin") {
   291  		t.Fatal()
   292  	}
   293  
   294  	if IsValidUserRoles("junk") {
   295  		t.Fatal()
   296  	}
   297  
   298  	if !IsValidUserRoles("system_user system_admin") {
   299  		t.Fatal()
   300  	}
   301  
   302  	if IsInRole("system_admin junk", "admin") {
   303  		t.Fatal()
   304  	}
   305  
   306  	if !IsInRole("system_admin junk", "system_admin") {
   307  		t.Fatal()
   308  	}
   309  
   310  	if IsInRole("admin", "system_admin") {
   311  		t.Fatal()
   312  	}
   313  }