user_test.go 9.24 KB
Newer Older
1
// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
=Corey Hulen's avatar
=Corey Hulen committed
2 3 4 5 6
// See License.txt for license information.

package model

import (
7 8
	"fmt"
	"net/http"
=Corey Hulen's avatar
=Corey Hulen committed
9 10
	"strings"
	"testing"
11 12

	"github.com/stretchr/testify/assert"
13
	"github.com/stretchr/testify/require"
=Corey Hulen's avatar
=Corey Hulen committed
14 15 16 17 18 19 20 21 22 23 24 25 26 27
)

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")
	}
}

28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
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()

55
	assert.Equal(t, id, copyUser.Id)
56 57
}

=Corey Hulen's avatar
=Corey Hulen committed
58 59 60 61 62 63 64 65 66 67 68 69 70
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()
71
	user.Etag(true, true)
72 73
	if user.Timezone == nil {
		t.Fatal("Timezone is nil")
74 75 76 77
	}

	if user.Timezone["useAutomaticTimezone"] != "true" {
		t.Fatal("Timezone is not set to default")
78
	}
=Corey Hulen's avatar
=Corey Hulen committed
79 80 81 82 83 84 85
}

func TestUserPreUpdate(t *testing.T) {
	user := User{Password: "test"}
	user.PreUpdate()
}

86 87 88 89 90
func TestUserUpdateMentionKeysFromUsername(t *testing.T) {
	user := User{Username: "user"}
	user.SetDefaultNotifications()

	if user.NotifyProps["mention_keys"] != "user,@user" {
91
		t.Fatalf("default mention keys are invalid: %v", user.NotifyProps["mention_keys"])
92 93 94 95 96
	}

	user.Username = "person"
	user.UpdateMentionKeysFromUsername("user")
	if user.NotifyProps["mention_keys"] != "person,@person" {
97
		t.Fatalf("mention keys are invalid after changing username: %v", user.NotifyProps["mention_keys"])
98 99 100 101 102
	}

	user.NotifyProps["mention_keys"] += ",mention"
	user.UpdateMentionKeysFromUsername("person")
	if user.NotifyProps["mention_keys"] != "person,@person,mention" {
103
		t.Fatalf("mention keys are invalid after adding extra mention keyword: %v", user.NotifyProps["mention_keys"])
104 105 106 107 108
	}

	user.Username = "user"
	user.UpdateMentionKeysFromUsername("person")
	if user.NotifyProps["mention_keys"] != "user,@user,mention" {
109
		t.Fatalf("mention keys are invalid after changing username with extra mention keyword: %v", user.NotifyProps["mention_keys"])
110 111 112
	}
}

=Corey Hulen's avatar
=Corey Hulen committed
113 114 115
func TestUserIsValid(t *testing.T) {
	user := User{}

116 117
	if err := user.IsValid(); !HasExpectedUserIsValidError(err, "id", "") {
		t.Fatal(err)
=Corey Hulen's avatar
=Corey Hulen committed
118 119 120
	}

	user.Id = NewId()
121 122
	err := user.IsValid()
	require.True(t, HasExpectedUserIsValidError(err, "create_at", user.Id), "expected user is valid error: %s", err.Error())
=Corey Hulen's avatar
=Corey Hulen committed
123 124

	user.CreateAt = GetMillis()
125 126
	err = user.IsValid()
	require.True(t, HasExpectedUserIsValidError(err, "update_at", user.Id), "expected user is valid error: %s", err.Error())
=Corey Hulen's avatar
=Corey Hulen committed
127 128

	user.UpdateAt = GetMillis()
129 130
	err = user.IsValid()
	require.True(t, HasExpectedUserIsValidError(err, "username", user.Id), "expected user is valid error: %s", err.Error())
=Corey Hulen's avatar
=Corey Hulen committed
131 132

	user.Username = NewId() + "^hello#"
133 134
	err = user.IsValid()
	require.True(t, HasExpectedUserIsValidError(err, "username", user.Id), "expected user is valid error: %s", err.Error())
=Corey Hulen's avatar
=Corey Hulen committed
135

136
	user.Username = NewId()
137 138
	err = user.IsValid()
	require.True(t, HasExpectedUserIsValidError(err, "email", user.Id), "expected user is valid error: %s", err.Error())
139

140
	user.Email = strings.Repeat("01234567890", 20)
141 142
	err = user.IsValid()
	require.True(t, HasExpectedUserIsValidError(err, "email", user.Id), "expected user is valid error: %s", err.Error())
=Corey Hulen's avatar
=Corey Hulen committed
143

144 145
	user.Email = "user@example.com"

146
	user.Nickname = strings.Repeat("a", 65)
147 148
	err = user.IsValid()
	require.True(t, HasExpectedUserIsValidError(err, "nickname", user.Id), "expected user is valid error: %s", err.Error())
=Corey Hulen's avatar
=Corey Hulen committed
149

150
	user.Nickname = strings.Repeat("a", 64)
=Corey Hulen's avatar
=Corey Hulen committed
151 152 153
	if err := user.IsValid(); err != nil {
		t.Fatal(err)
	}
154 155 156 157 158 159 160

	user.FirstName = ""
	user.LastName = ""
	if err := user.IsValid(); err != nil {
		t.Fatal(err)
	}

161 162
	user.FirstName = strings.Repeat("a", 65)
	if err := user.IsValid(); !HasExpectedUserIsValidError(err, "first_name", user.Id) {
163 164 165
		t.Fatal(err)
	}

166 167 168
	user.FirstName = strings.Repeat("a", 64)
	user.LastName = strings.Repeat("a", 65)
	if err := user.IsValid(); !HasExpectedUserIsValidError(err, "last_name", user.Id) {
169 170
		t.Fatal(err)
	}
171

172
	user.LastName = strings.Repeat("a", 64)
173
	user.Position = strings.Repeat("a", 128)
174 175 176 177
	if err := user.IsValid(); err != nil {
		t.Fatal(err)
	}

178
	user.Position = strings.Repeat("a", 129)
179
	if err := user.IsValid(); !HasExpectedUserIsValidError(err, "position", user.Id) {
180 181
		t.Fatal(err)
	}
=Corey Hulen's avatar
=Corey Hulen committed
182
}
183

184 185 186 187 188 189 190 191 192 193 194
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)
}

195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218
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")
	}
}

219
func TestUserGetDisplayName(t *testing.T) {
220 221 222 223 224 225 226 227 228
	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")
	}
229

230
	if displayName := user.GetDisplayName(SHOW_USERNAME); displayName != "username" {
231 232 233
		t.Fatal("Display name should be username")
	}

234 235
	user.FirstName = "first"
	user.LastName = "last"
236 237

	if displayName := user.GetDisplayName(SHOW_FULLNAME); displayName != "first last" {
238 239 240
		t.Fatal("Display name should be full name")
	}

241 242 243 244 245 246 247 248
	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")
	}

249
	user.Nickname = "nickname"
250
	if displayName := user.GetDisplayName(SHOW_NICKNAME_FULLNAME); displayName != "nickname" {
251 252 253
		t.Fatal("Display name should be nickname")
	}
}
254 255 256 257 258 259

var usernames = []struct {
	value    string
	expected bool
}{
	{"spin-punch", true},
260 261
	{"sp", true},
	{"s", true},
262 263 264
	{"1spin-punch", true},
	{"-spin-punch", true},
	{".spin-punch", true},
265 266 267 268 269 270 271 272 273
	{"Spin-punch", false},
	{"spin punch-", false},
	{"spin_punch", true},
	{"spin", true},
	{"PUNCH", false},
	{"spin.punch", true},
	{"spin'punch", false},
	{"spin*punch", false},
	{"all", false},
274
	{"system", false},
275 276 277 278 279 280 281 282 283 284
}

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)
		}
	}
}

285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308
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")
	}
}

309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325
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")
	}
}
=Corey Hulen's avatar
=Corey Hulen committed
326 327

func TestRoles(t *testing.T) {
328 329 330 331 332 333
	require.True(t, IsValidUserRoles("team_user"))
	require.False(t, IsValidUserRoles("system_admin"))
	require.True(t, IsValidUserRoles("system_user system_admin"))
	require.False(t, IsInRole("system_admin junk", "admin"))
	require.True(t, IsInRole("system_admin junk", "system_admin"))
	require.False(t, IsInRole("admin", "system_admin"))
=Corey Hulen's avatar
=Corey Hulen committed
334
}