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