github.com/gigforks/mattermost-server@v4.9.1-0.20180619094218-800d97fa55d0+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()
   122  	}
   123  
   124  	user.CreateAt = GetMillis()
   125  	if err := user.IsValid(); !HasExpectedUserIsValidError(err, "update_at", user.Id) {
   126  		t.Fatal()
   127  	}
   128  
   129  	user.UpdateAt = GetMillis()
   130  	if err := user.IsValid(); !HasExpectedUserIsValidError(err, "username", user.Id) {
   131  		t.Fatal()
   132  	}
   133  
   134  	user.Username = NewId() + "^hello#"
   135  	if err := user.IsValid(); !HasExpectedUserIsValidError(err, "username", user.Id) {
   136  		t.Fatal()
   137  	}
   138  
   139  	user.Username = NewId()
   140  	user.Email = strings.Repeat("01234567890", 20)
   141  	if err := user.IsValid(); err == nil {
   142  		t.Fatal()
   143  	}
   144  
   145  	user.Email = strings.Repeat("a", 128)
   146  	user.Nickname = strings.Repeat("a", 65)
   147  	if err := user.IsValid(); !HasExpectedUserIsValidError(err, "nickname", user.Id) {
   148  		t.Fatal()
   149  	}
   150  
   151  	user.Nickname = strings.Repeat("a", 64)
   152  	if err := user.IsValid(); err != nil {
   153  		t.Fatal(err)
   154  	}
   155  
   156  	user.FirstName = ""
   157  	user.LastName = ""
   158  	if err := user.IsValid(); err != nil {
   159  		t.Fatal(err)
   160  	}
   161  
   162  	user.FirstName = strings.Repeat("a", 65)
   163  	if err := user.IsValid(); !HasExpectedUserIsValidError(err, "first_name", user.Id) {
   164  		t.Fatal(err)
   165  	}
   166  
   167  	user.FirstName = strings.Repeat("a", 64)
   168  	user.LastName = strings.Repeat("a", 65)
   169  	if err := user.IsValid(); !HasExpectedUserIsValidError(err, "last_name", user.Id) {
   170  		t.Fatal(err)
   171  	}
   172  
   173  	user.LastName = strings.Repeat("a", 64)
   174  	user.Position = strings.Repeat("a", 128)
   175  	if err := user.IsValid(); err != nil {
   176  		t.Fatal(err)
   177  	}
   178  
   179  	user.Position = strings.Repeat("a", 129)
   180  	if err := user.IsValid(); !HasExpectedUserIsValidError(err, "position", user.Id) {
   181  		t.Fatal(err)
   182  	}
   183  }
   184  
   185  func HasExpectedUserIsValidError(err *AppError, fieldName string, userId string) bool {
   186  	if err == nil {
   187  		return false
   188  	}
   189  
   190  	return err.Where == "User.IsValid" &&
   191  		err.Id == fmt.Sprintf("model.user.is_valid.%s.app_error", fieldName) &&
   192  		err.StatusCode == http.StatusBadRequest &&
   193  		(userId == "" || err.DetailedError == "user_id="+userId)
   194  }
   195  
   196  func TestUserGetFullName(t *testing.T) {
   197  	user := User{}
   198  
   199  	if fullName := user.GetFullName(); fullName != "" {
   200  		t.Fatal("Full name should be blank")
   201  	}
   202  
   203  	user.FirstName = "first"
   204  	if fullName := user.GetFullName(); fullName != "first" {
   205  		t.Fatal("Full name should be first name")
   206  	}
   207  
   208  	user.FirstName = ""
   209  	user.LastName = "last"
   210  	if fullName := user.GetFullName(); fullName != "last" {
   211  		t.Fatal("Full name should be last name")
   212  	}
   213  
   214  	user.FirstName = "first"
   215  	if fullName := user.GetFullName(); fullName != "first last" {
   216  		t.Fatal("Full name should be first name and last name")
   217  	}
   218  }
   219  
   220  func TestUserGetDisplayName(t *testing.T) {
   221  	user := User{Username: "username"}
   222  
   223  	if displayName := user.GetDisplayName(SHOW_FULLNAME); displayName != "username" {
   224  		t.Fatal("Display name should be username")
   225  	}
   226  
   227  	if displayName := user.GetDisplayName(SHOW_NICKNAME_FULLNAME); displayName != "username" {
   228  		t.Fatal("Display name should be username")
   229  	}
   230  
   231  	if displayName := user.GetDisplayName(SHOW_USERNAME); displayName != "username" {
   232  		t.Fatal("Display name should be username")
   233  	}
   234  
   235  	user.FirstName = "first"
   236  	user.LastName = "last"
   237  
   238  	if displayName := user.GetDisplayName(SHOW_FULLNAME); displayName != "first last" {
   239  		t.Fatal("Display name should be full name")
   240  	}
   241  
   242  	if displayName := user.GetDisplayName(SHOW_NICKNAME_FULLNAME); displayName != "first last" {
   243  		t.Fatal("Display name should be full name since there is no nickname")
   244  	}
   245  
   246  	if displayName := user.GetDisplayName(SHOW_USERNAME); displayName != "username" {
   247  		t.Fatal("Display name should be username")
   248  	}
   249  
   250  	user.Nickname = "nickname"
   251  	if displayName := user.GetDisplayName(SHOW_NICKNAME_FULLNAME); displayName != "nickname" {
   252  		t.Fatal("Display name should be nickname")
   253  	}
   254  }
   255  
   256  var usernames = []struct {
   257  	value    string
   258  	expected bool
   259  }{
   260  	{"spin-punch", true},
   261  	{"sp", true},
   262  	{"s", true},
   263  	{"1spin-punch", true},
   264  	{"-spin-punch", true},
   265  	{".spin-punch", true},
   266  	{"Spin-punch", false},
   267  	{"spin punch-", false},
   268  	{"spin_punch", true},
   269  	{"spin", true},
   270  	{"PUNCH", false},
   271  	{"spin.punch", true},
   272  	{"spin'punch", false},
   273  	{"spin*punch", false},
   274  	{"all", false},
   275  }
   276  
   277  func TestValidUsername(t *testing.T) {
   278  	for _, v := range usernames {
   279  		if IsValidUsername(v.value) != v.expected {
   280  			t.Errorf("expect %v as %v", v.value, v.expected)
   281  		}
   282  	}
   283  }
   284  
   285  func TestNormalizeUsername(t *testing.T) {
   286  	if NormalizeUsername("Spin-punch") != "spin-punch" {
   287  		t.Fatal("didn't normalize username properly")
   288  	}
   289  	if NormalizeUsername("PUNCH") != "punch" {
   290  		t.Fatal("didn't normalize username properly")
   291  	}
   292  	if NormalizeUsername("spin") != "spin" {
   293  		t.Fatal("didn't normalize username properly")
   294  	}
   295  }
   296  
   297  func TestNormalizeEmail(t *testing.T) {
   298  	if NormalizeEmail("TEST@EXAMPLE.COM") != "test@example.com" {
   299  		t.Fatal("didn't normalize email properly")
   300  	}
   301  	if NormalizeEmail("TEST2@example.com") != "test2@example.com" {
   302  		t.Fatal("didn't normalize email properly")
   303  	}
   304  	if NormalizeEmail("test3@example.com") != "test3@example.com" {
   305  		t.Fatal("didn't normalize email properly")
   306  	}
   307  }
   308  
   309  func TestCleanUsername(t *testing.T) {
   310  	if CleanUsername("Spin-punch") != "spin-punch" {
   311  		t.Fatal("didn't clean name properly")
   312  	}
   313  	if CleanUsername("PUNCH") != "punch" {
   314  		t.Fatal("didn't clean name properly")
   315  	}
   316  	if CleanUsername("spin'punch") != "spin-punch" {
   317  		t.Fatal("didn't clean name properly")
   318  	}
   319  	if CleanUsername("spin") != "spin" {
   320  		t.Fatal("didn't clean name properly")
   321  	}
   322  	if len(CleanUsername("all")) != 27 {
   323  		t.Fatal("didn't clean name properly")
   324  	}
   325  }
   326  
   327  func TestRoles(t *testing.T) {
   328  
   329  	if !IsValidUserRoles("team_user") {
   330  		t.Fatal()
   331  	}
   332  
   333  	if IsValidUserRoles("system_admin") {
   334  		t.Fatal()
   335  	}
   336  
   337  	if !IsValidUserRoles("system_user system_admin") {
   338  		t.Fatal()
   339  	}
   340  
   341  	if IsInRole("system_admin junk", "admin") {
   342  		t.Fatal()
   343  	}
   344  
   345  	if !IsInRole("system_admin junk", "system_admin") {
   346  		t.Fatal()
   347  	}
   348  
   349  	if IsInRole("admin", "system_admin") {
   350  		t.Fatal()
   351  	}
   352  }