summaryrefslogtreecommitdiffstats
path: root/model/user_test.go
blob: 286c92a666c4e94bedadf7e73eac1389b1894866 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
// Copyright (c) 2015 Mattermost, Inc. All Rights Reserved.
// See License.txt for license information.

package model

import (
	"strings"
	"testing"
)

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

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()
	user.Etag()
}

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

func TestUserIsValid(t *testing.T) {
	user := User{}

	if err := user.IsValid(); err == nil {
		t.Fatal()
	}

	user.Id = NewId()
	if err := user.IsValid(); err == nil {
		t.Fatal()
	}

	user.CreateAt = GetMillis()
	if err := user.IsValid(); err == nil {
		t.Fatal()
	}

	user.UpdateAt = GetMillis()
	if err := user.IsValid(); err == nil {
		t.Fatal()
	}

	user.Username = NewId() + "^hello#"
	if err := user.IsValid(); err == nil {
		t.Fatal()
	}

	user.Username = NewId()
	user.Email = strings.Repeat("01234567890", 20)
	if err := user.IsValid(); err == nil {
		t.Fatal()
	}

	user.Email = "test@nowhere.com"
	user.Nickname = strings.Repeat("01234567890", 20)
	if err := user.IsValid(); err == nil {
		t.Fatal()
	}

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

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

	user.FirstName = strings.Repeat("01234567890", 20)
	if err := user.IsValid(); err == nil {
		t.Fatal(err)
	}

	user.FirstName = ""
	user.LastName = strings.Repeat("01234567890", 20)
	if err := user.IsValid(); err == nil {
		t.Fatal(err)
	}
}

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

func TestUserGetDisplayName(t *testing.T) {
	user := User{Username: "user"}

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

	user.FirstName = "first"
	user.LastName = "last"
	if displayName := user.GetDisplayName(); displayName != "first last" {
		t.Fatal("Display name should be full name")
	}

	user.Nickname = "nickname"
	if displayName := user.GetDisplayName(); displayName != "nickname" {
		t.Fatal("Display name should be nickname")
	}
}

var usernames = []struct {
	value    string
	expected bool
}{
	{"spin-punch", true},
	{"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)
		}
	}
}

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

func TestRoles(t *testing.T) {

	if IsValidUserRoles("admin") {
		t.Fatal()
	}

	if IsValidUserRoles("junk") {
		t.Fatal()
	}

	if IsInRole("system_admin junk", "admin") {
		t.Fatal()
	}

	if !IsInRole("system_admin junk", "system_admin") {
		t.Fatal()
	}

	if IsInRole("admin", "system_admin") {
		t.Fatal()
	}
}