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