2017-04-12 08:27:57 -04:00
|
|
|
// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
|
2015-06-14 23:53:32 -08:00
|
|
|
// See License.txt for license information.
|
|
|
|
|
|
|
|
|
|
package model
|
|
|
|
|
|
|
|
|
|
import (
|
2017-04-17 16:45:51 +02:00
|
|
|
"fmt"
|
|
|
|
|
"net/http"
|
2015-06-14 23:53:32 -08:00
|
|
|
"strings"
|
|
|
|
|
"testing"
|
2018-04-20 08:44:18 -04:00
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
2015-06-14 23:53:32 -08:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
func TestPasswordHash(t *testing.T) {
|
|
|
|
|
hash := HashPassword("Test")
|
|
|
|
|
|
|
|
|
|
if !ComparePassword(hash, "Test") {
|
|
|
|
|
t.Fatal("Passwords don't match")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ComparePassword(hash, "Test2") {
|
|
|
|
|
t.Fatal("Passwords should not have matched")
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-04-20 08:44:18 -04:00
|
|
|
func TestUserDeepCopy(t *testing.T) {
|
|
|
|
|
id := NewId()
|
|
|
|
|
authData := "authdata"
|
|
|
|
|
mapKey := "key"
|
|
|
|
|
mapValue := "key"
|
|
|
|
|
|
|
|
|
|
user := &User{Id: id, AuthData: NewString(authData), Props: map[string]string{}, NotifyProps: map[string]string{}, Timezone: map[string]string{}}
|
|
|
|
|
user.Props[mapKey] = mapValue
|
|
|
|
|
user.NotifyProps[mapKey] = mapValue
|
|
|
|
|
user.Timezone[mapKey] = mapValue
|
|
|
|
|
|
|
|
|
|
copyUser := user.DeepCopy()
|
|
|
|
|
copyUser.Id = "someid"
|
|
|
|
|
*copyUser.AuthData = "changed"
|
|
|
|
|
copyUser.Props[mapKey] = "changed"
|
|
|
|
|
copyUser.NotifyProps[mapKey] = "changed"
|
|
|
|
|
copyUser.Timezone[mapKey] = "changed"
|
|
|
|
|
|
|
|
|
|
assert.Equal(t, id, user.Id)
|
|
|
|
|
assert.Equal(t, authData, *user.AuthData)
|
|
|
|
|
assert.Equal(t, mapValue, user.Props[mapKey])
|
|
|
|
|
assert.Equal(t, mapValue, user.NotifyProps[mapKey])
|
|
|
|
|
assert.Equal(t, mapValue, user.Timezone[mapKey])
|
|
|
|
|
|
|
|
|
|
user = &User{Id: id}
|
|
|
|
|
copyUser = user.DeepCopy()
|
|
|
|
|
|
|
|
|
|
assert.Equal(t, id, user.Id)
|
|
|
|
|
}
|
|
|
|
|
|
2015-06-14 23:53:32 -08:00
|
|
|
func TestUserJson(t *testing.T) {
|
|
|
|
|
user := User{Id: NewId(), Username: NewId()}
|
|
|
|
|
json := user.ToJson()
|
|
|
|
|
ruser := UserFromJson(strings.NewReader(json))
|
|
|
|
|
|
|
|
|
|
if user.Id != ruser.Id {
|
|
|
|
|
t.Fatal("Ids do not match")
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestUserPreSave(t *testing.T) {
|
|
|
|
|
user := User{Password: "test"}
|
|
|
|
|
user.PreSave()
|
2016-06-14 12:12:46 -04:00
|
|
|
user.Etag(true, true)
|
2018-03-29 14:58:18 -04:00
|
|
|
if user.Timezone == nil {
|
|
|
|
|
t.Fatal("Timezone is nil")
|
2018-03-29 20:50:24 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if user.Timezone["useAutomaticTimezone"] != "true" {
|
|
|
|
|
t.Fatal("Timezone is not set to default")
|
2018-03-29 14:58:18 -04:00
|
|
|
}
|
2015-06-14 23:53:32 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestUserPreUpdate(t *testing.T) {
|
|
|
|
|
user := User{Password: "test"}
|
|
|
|
|
user.PreUpdate()
|
|
|
|
|
}
|
|
|
|
|
|
2016-04-25 08:20:45 -04:00
|
|
|
func TestUserUpdateMentionKeysFromUsername(t *testing.T) {
|
|
|
|
|
user := User{Username: "user"}
|
|
|
|
|
user.SetDefaultNotifications()
|
|
|
|
|
|
|
|
|
|
if user.NotifyProps["mention_keys"] != "user,@user" {
|
2017-02-09 13:39:15 -08:00
|
|
|
t.Fatalf("default mention keys are invalid: %v", user.NotifyProps["mention_keys"])
|
2016-04-25 08:20:45 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
user.Username = "person"
|
|
|
|
|
user.UpdateMentionKeysFromUsername("user")
|
|
|
|
|
if user.NotifyProps["mention_keys"] != "person,@person" {
|
2017-02-09 13:39:15 -08:00
|
|
|
t.Fatalf("mention keys are invalid after changing username: %v", user.NotifyProps["mention_keys"])
|
2016-04-25 08:20:45 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
user.NotifyProps["mention_keys"] += ",mention"
|
|
|
|
|
user.UpdateMentionKeysFromUsername("person")
|
|
|
|
|
if user.NotifyProps["mention_keys"] != "person,@person,mention" {
|
2017-02-09 13:39:15 -08:00
|
|
|
t.Fatalf("mention keys are invalid after adding extra mention keyword: %v", user.NotifyProps["mention_keys"])
|
2016-04-25 08:20:45 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
user.Username = "user"
|
|
|
|
|
user.UpdateMentionKeysFromUsername("person")
|
|
|
|
|
if user.NotifyProps["mention_keys"] != "user,@user,mention" {
|
2017-02-09 13:39:15 -08:00
|
|
|
t.Fatalf("mention keys are invalid after changing username with extra mention keyword: %v", user.NotifyProps["mention_keys"])
|
2016-04-25 08:20:45 -04:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2015-06-14 23:53:32 -08:00
|
|
|
func TestUserIsValid(t *testing.T) {
|
|
|
|
|
user := User{}
|
|
|
|
|
|
2017-04-17 16:45:51 +02:00
|
|
|
if err := user.IsValid(); !HasExpectedUserIsValidError(err, "id", "") {
|
|
|
|
|
t.Fatal(err)
|
2015-06-14 23:53:32 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
user.Id = NewId()
|
2017-04-17 16:45:51 +02:00
|
|
|
if err := user.IsValid(); !HasExpectedUserIsValidError(err, "create_at", user.Id) {
|
2015-06-14 23:53:32 -08:00
|
|
|
t.Fatal()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
user.CreateAt = GetMillis()
|
2017-04-17 16:45:51 +02:00
|
|
|
if err := user.IsValid(); !HasExpectedUserIsValidError(err, "update_at", user.Id) {
|
2015-06-14 23:53:32 -08:00
|
|
|
t.Fatal()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
user.UpdateAt = GetMillis()
|
2017-04-17 16:45:51 +02:00
|
|
|
if err := user.IsValid(); !HasExpectedUserIsValidError(err, "username", user.Id) {
|
2015-06-14 23:53:32 -08:00
|
|
|
t.Fatal()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
user.Username = NewId() + "^hello#"
|
2017-04-17 16:45:51 +02:00
|
|
|
if err := user.IsValid(); !HasExpectedUserIsValidError(err, "username", user.Id) {
|
2015-06-14 23:53:32 -08:00
|
|
|
t.Fatal()
|
|
|
|
|
}
|
|
|
|
|
|
2017-04-18 17:37:25 -04:00
|
|
|
user.Username = NewId()
|
|
|
|
|
user.Email = strings.Repeat("01234567890", 20)
|
|
|
|
|
if err := user.IsValid(); err == nil {
|
2015-06-14 23:53:32 -08:00
|
|
|
t.Fatal()
|
|
|
|
|
}
|
|
|
|
|
|
2017-04-17 16:45:51 +02:00
|
|
|
user.Email = strings.Repeat("a", 128)
|
|
|
|
|
user.Nickname = strings.Repeat("a", 65)
|
|
|
|
|
if err := user.IsValid(); !HasExpectedUserIsValidError(err, "nickname", user.Id) {
|
2015-06-14 23:53:32 -08:00
|
|
|
t.Fatal()
|
|
|
|
|
}
|
|
|
|
|
|
2017-04-17 16:45:51 +02:00
|
|
|
user.Nickname = strings.Repeat("a", 64)
|
2015-06-14 23:53:32 -08:00
|
|
|
if err := user.IsValid(); err != nil {
|
|
|
|
|
t.Fatal(err)
|
|
|
|
|
}
|
2015-07-13 16:43:59 -04:00
|
|
|
|
|
|
|
|
user.FirstName = ""
|
|
|
|
|
user.LastName = ""
|
|
|
|
|
if err := user.IsValid(); err != nil {
|
|
|
|
|
t.Fatal(err)
|
|
|
|
|
}
|
|
|
|
|
|
2017-04-17 16:45:51 +02:00
|
|
|
user.FirstName = strings.Repeat("a", 65)
|
|
|
|
|
if err := user.IsValid(); !HasExpectedUserIsValidError(err, "first_name", user.Id) {
|
2015-07-13 16:43:59 -04:00
|
|
|
t.Fatal(err)
|
|
|
|
|
}
|
|
|
|
|
|
2017-04-17 16:45:51 +02:00
|
|
|
user.FirstName = strings.Repeat("a", 64)
|
|
|
|
|
user.LastName = strings.Repeat("a", 65)
|
|
|
|
|
if err := user.IsValid(); !HasExpectedUserIsValidError(err, "last_name", user.Id) {
|
2015-07-13 16:43:59 -04:00
|
|
|
t.Fatal(err)
|
|
|
|
|
}
|
2016-12-14 11:11:51 +00:00
|
|
|
|
2017-04-17 16:45:51 +02:00
|
|
|
user.LastName = strings.Repeat("a", 64)
|
2018-01-26 08:37:12 -05:00
|
|
|
user.Position = strings.Repeat("a", 128)
|
2016-12-14 11:11:51 +00:00
|
|
|
if err := user.IsValid(); err != nil {
|
|
|
|
|
t.Fatal(err)
|
|
|
|
|
}
|
|
|
|
|
|
2018-01-26 08:37:12 -05:00
|
|
|
user.Position = strings.Repeat("a", 129)
|
2017-04-17 16:45:51 +02:00
|
|
|
if err := user.IsValid(); !HasExpectedUserIsValidError(err, "position", user.Id) {
|
2016-12-14 11:11:51 +00:00
|
|
|
t.Fatal(err)
|
|
|
|
|
}
|
2015-06-14 23:53:32 -08:00
|
|
|
}
|
2015-07-13 17:47:57 -04:00
|
|
|
|
2017-04-17 16:45:51 +02:00
|
|
|
func HasExpectedUserIsValidError(err *AppError, fieldName string, userId string) bool {
|
|
|
|
|
if err == nil {
|
|
|
|
|
return false
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return err.Where == "User.IsValid" &&
|
|
|
|
|
err.Id == fmt.Sprintf("model.user.is_valid.%s.app_error", fieldName) &&
|
|
|
|
|
err.StatusCode == http.StatusBadRequest &&
|
|
|
|
|
(userId == "" || err.DetailedError == "user_id="+userId)
|
|
|
|
|
}
|
|
|
|
|
|
2015-07-14 17:07:19 -04:00
|
|
|
func TestUserGetFullName(t *testing.T) {
|
|
|
|
|
user := User{}
|
|
|
|
|
|
|
|
|
|
if fullName := user.GetFullName(); fullName != "" {
|
|
|
|
|
t.Fatal("Full name should be blank")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
user.FirstName = "first"
|
|
|
|
|
if fullName := user.GetFullName(); fullName != "first" {
|
|
|
|
|
t.Fatal("Full name should be first name")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
user.FirstName = ""
|
|
|
|
|
user.LastName = "last"
|
|
|
|
|
if fullName := user.GetFullName(); fullName != "last" {
|
|
|
|
|
t.Fatal("Full name should be last name")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
user.FirstName = "first"
|
|
|
|
|
if fullName := user.GetFullName(); fullName != "first last" {
|
|
|
|
|
t.Fatal("Full name should be first name and last name")
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2015-07-13 17:47:57 -04:00
|
|
|
func TestUserGetDisplayName(t *testing.T) {
|
2017-06-30 16:06:59 +08:00
|
|
|
user := User{Username: "username"}
|
|
|
|
|
|
|
|
|
|
if displayName := user.GetDisplayName(SHOW_FULLNAME); displayName != "username" {
|
|
|
|
|
t.Fatal("Display name should be username")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if displayName := user.GetDisplayName(SHOW_NICKNAME_FULLNAME); displayName != "username" {
|
|
|
|
|
t.Fatal("Display name should be username")
|
|
|
|
|
}
|
2015-07-13 17:47:57 -04:00
|
|
|
|
2017-06-30 16:06:59 +08:00
|
|
|
if displayName := user.GetDisplayName(SHOW_USERNAME); displayName != "username" {
|
2015-07-13 17:47:57 -04:00
|
|
|
t.Fatal("Display name should be username")
|
|
|
|
|
}
|
|
|
|
|
|
2015-07-14 17:07:19 -04:00
|
|
|
user.FirstName = "first"
|
|
|
|
|
user.LastName = "last"
|
2017-06-30 16:06:59 +08:00
|
|
|
|
|
|
|
|
if displayName := user.GetDisplayName(SHOW_FULLNAME); displayName != "first last" {
|
2015-07-14 17:07:19 -04:00
|
|
|
t.Fatal("Display name should be full name")
|
|
|
|
|
}
|
|
|
|
|
|
2017-06-30 16:06:59 +08:00
|
|
|
if displayName := user.GetDisplayName(SHOW_NICKNAME_FULLNAME); displayName != "first last" {
|
|
|
|
|
t.Fatal("Display name should be full name since there is no nickname")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if displayName := user.GetDisplayName(SHOW_USERNAME); displayName != "username" {
|
|
|
|
|
t.Fatal("Display name should be username")
|
|
|
|
|
}
|
|
|
|
|
|
2015-07-13 17:47:57 -04:00
|
|
|
user.Nickname = "nickname"
|
2017-06-30 16:06:59 +08:00
|
|
|
if displayName := user.GetDisplayName(SHOW_NICKNAME_FULLNAME); displayName != "nickname" {
|
2015-07-13 17:47:57 -04:00
|
|
|
t.Fatal("Display name should be nickname")
|
|
|
|
|
}
|
|
|
|
|
}
|
2015-08-28 08:37:55 -04:00
|
|
|
|
|
|
|
|
var usernames = []struct {
|
|
|
|
|
value string
|
|
|
|
|
expected bool
|
|
|
|
|
}{
|
|
|
|
|
{"spin-punch", true},
|
2017-03-17 12:42:23 -04:00
|
|
|
{"sp", true},
|
|
|
|
|
{"s", true},
|
2017-04-18 17:37:25 -04:00
|
|
|
{"1spin-punch", true},
|
|
|
|
|
{"-spin-punch", true},
|
|
|
|
|
{".spin-punch", true},
|
2015-08-28 08:37:55 -04:00
|
|
|
{"Spin-punch", false},
|
|
|
|
|
{"spin punch-", false},
|
|
|
|
|
{"spin_punch", true},
|
|
|
|
|
{"spin", true},
|
|
|
|
|
{"PUNCH", false},
|
|
|
|
|
{"spin.punch", true},
|
|
|
|
|
{"spin'punch", false},
|
|
|
|
|
{"spin*punch", false},
|
|
|
|
|
{"all", false},
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestValidUsername(t *testing.T) {
|
|
|
|
|
for _, v := range usernames {
|
|
|
|
|
if IsValidUsername(v.value) != v.expected {
|
|
|
|
|
t.Errorf("expect %v as %v", v.value, v.expected)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-02-05 14:56:01 -05:00
|
|
|
func TestNormalizeUsername(t *testing.T) {
|
|
|
|
|
if NormalizeUsername("Spin-punch") != "spin-punch" {
|
|
|
|
|
t.Fatal("didn't normalize username properly")
|
|
|
|
|
}
|
|
|
|
|
if NormalizeUsername("PUNCH") != "punch" {
|
|
|
|
|
t.Fatal("didn't normalize username properly")
|
|
|
|
|
}
|
|
|
|
|
if NormalizeUsername("spin") != "spin" {
|
|
|
|
|
t.Fatal("didn't normalize username properly")
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestNormalizeEmail(t *testing.T) {
|
|
|
|
|
if NormalizeEmail("TEST@EXAMPLE.COM") != "test@example.com" {
|
|
|
|
|
t.Fatal("didn't normalize email properly")
|
|
|
|
|
}
|
|
|
|
|
if NormalizeEmail("TEST2@example.com") != "test2@example.com" {
|
|
|
|
|
t.Fatal("didn't normalize email properly")
|
|
|
|
|
}
|
|
|
|
|
if NormalizeEmail("test3@example.com") != "test3@example.com" {
|
|
|
|
|
t.Fatal("didn't normalize email properly")
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2015-08-28 08:37:55 -04:00
|
|
|
func TestCleanUsername(t *testing.T) {
|
|
|
|
|
if CleanUsername("Spin-punch") != "spin-punch" {
|
|
|
|
|
t.Fatal("didn't clean name properly")
|
|
|
|
|
}
|
|
|
|
|
if CleanUsername("PUNCH") != "punch" {
|
|
|
|
|
t.Fatal("didn't clean name properly")
|
|
|
|
|
}
|
|
|
|
|
if CleanUsername("spin'punch") != "spin-punch" {
|
|
|
|
|
t.Fatal("didn't clean name properly")
|
|
|
|
|
}
|
|
|
|
|
if CleanUsername("spin") != "spin" {
|
|
|
|
|
t.Fatal("didn't clean name properly")
|
|
|
|
|
}
|
|
|
|
|
if len(CleanUsername("all")) != 27 {
|
|
|
|
|
t.Fatal("didn't clean name properly")
|
|
|
|
|
}
|
|
|
|
|
}
|
2015-09-04 11:59:10 -07:00
|
|
|
|
|
|
|
|
func TestRoles(t *testing.T) {
|
|
|
|
|
|
2018-02-06 15:34:08 +00:00
|
|
|
if !IsValidUserRoles("team_user") {
|
2015-09-04 11:59:10 -07:00
|
|
|
t.Fatal()
|
|
|
|
|
}
|
|
|
|
|
|
2018-02-06 15:34:08 +00:00
|
|
|
if IsValidUserRoles("system_admin") {
|
2015-09-04 16:56:18 -07:00
|
|
|
t.Fatal()
|
|
|
|
|
}
|
2015-09-04 11:59:10 -07:00
|
|
|
|
2016-09-13 12:42:48 -04:00
|
|
|
if !IsValidUserRoles("system_user system_admin") {
|
|
|
|
|
t.Fatal()
|
|
|
|
|
}
|
|
|
|
|
|
2015-09-04 16:56:18 -07:00
|
|
|
if IsInRole("system_admin junk", "admin") {
|
|
|
|
|
t.Fatal()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if !IsInRole("system_admin junk", "system_admin") {
|
|
|
|
|
t.Fatal()
|
|
|
|
|
}
|
2015-09-10 14:56:37 -07:00
|
|
|
|
|
|
|
|
if IsInRole("admin", "system_admin") {
|
|
|
|
|
t.Fatal()
|
|
|
|
|
}
|
2015-09-04 11:59:10 -07:00
|
|
|
}
|