summaryrefslogtreecommitdiff
path: root/internal/util/validation_test.go
blob: dbac5e248c79c4502e7180d8607b4435a3d197f3 (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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
/*
   GoToSocial
   Copyright (C) 2021 GoToSocial Authors admin@gotosocial.org

   This program is free software: you can redistribute it and/or modify
   it under the terms of the GNU Affero General Public License as published by
   the Free Software Foundation, either version 3 of the License, or
   (at your option) any later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU Affero General Public License for more details.

   You should have received a copy of the GNU Affero General Public License
   along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

package util

import (
	"errors"
	"fmt"
	"testing"

	"github.com/stretchr/testify/assert"
	"github.com/stretchr/testify/suite"
)

type ValidationTestSuite struct {
	suite.Suite
}

func (suite *ValidationTestSuite) TestCheckPasswordStrength() {
	empty := ""
	terriblePassword := "password"
	weakPassword := "OKPassword"
	shortPassword := "Ok12"
	specialPassword := "Ok12%"
	longPassword := "thisisafuckinglongpasswordbutnospecialchars"
	tooLong := "Vestibulum ante ipsum primis in faucibus orci luctus et ultrices posuere cubilia curae; Quisque a enim nibh. Vestibulum bibendum leo ac porttitor auctor."
	strongPassword := "3dX5@Zc%mV*W2MBNEy$@"
	var err error

	err = ValidateNewPassword(empty)
	if assert.Error(suite.T(), err) {
		assert.Equal(suite.T(), errors.New("no password provided"), err)
	}

	err = ValidateNewPassword(terriblePassword)
	if assert.Error(suite.T(), err) {
		assert.Equal(suite.T(), errors.New("insecure password, try including more special characters, using uppercase letters, using numbers or using a longer password"), err)
	}

	err = ValidateNewPassword(weakPassword)
	if assert.Error(suite.T(), err) {
		assert.Equal(suite.T(), errors.New("insecure password, try including more special characters, using numbers or using a longer password"), err)
	}

	err = ValidateNewPassword(shortPassword)
	if assert.Error(suite.T(), err) {
		assert.Equal(suite.T(), errors.New("insecure password, try including more special characters or using a longer password"), err)
	}

	err = ValidateNewPassword(specialPassword)
	if assert.Error(suite.T(), err) {
		assert.Equal(suite.T(), errors.New("insecure password, try including more special characters or using a longer password"), err)
	}

	err = ValidateNewPassword(longPassword)
	if assert.NoError(suite.T(), err) {
		assert.Equal(suite.T(), nil, err)
	}

	err = ValidateNewPassword(tooLong)
	if assert.Error(suite.T(), err) {
		assert.Equal(suite.T(), errors.New("password should be no more than 64 chars"), err)
	}

	err = ValidateNewPassword(strongPassword)
	if assert.NoError(suite.T(), err) {
		assert.Equal(suite.T(), nil, err)
	}
}

func (suite *ValidationTestSuite) TestValidateUsername() {
	empty := ""
	tooLong := "holycrapthisisthelongestusernameiveeverseeninmylifethatstoomuchman"
	withSpaces := "this username has spaces in it"
	weirdChars := "thisusername&&&&&&&istooweird!!"
	leadingSpace := " see_that_leading_space"
	trailingSpace := "thisusername_ends_with_a_space "
	newlines := "this_is\n_almost_ok"
	goodUsername := "this_is_a_good_username"
	var err error

	err = ValidateUsername(empty)
	if assert.Error(suite.T(), err) {
		assert.Equal(suite.T(), errors.New("no username provided"), err)
	}

	err = ValidateUsername(tooLong)
	if assert.Error(suite.T(), err) {
		assert.Equal(suite.T(), fmt.Errorf("username should be no more than 64 chars but '%s' was 66", tooLong), err)
	}

	err = ValidateUsername(withSpaces)
	if assert.Error(suite.T(), err) {
		assert.Equal(suite.T(), fmt.Errorf("given username %s was invalid: must contain only lowercase letters, numbers, and underscores", withSpaces), err)
	}

	err = ValidateUsername(weirdChars)
	if assert.Error(suite.T(), err) {
		assert.Equal(suite.T(), fmt.Errorf("given username %s was invalid: must contain only lowercase letters, numbers, and underscores", weirdChars), err)
	}

	err = ValidateUsername(leadingSpace)
	if assert.Error(suite.T(), err) {
		assert.Equal(suite.T(), fmt.Errorf("given username %s was invalid: must contain only lowercase letters, numbers, and underscores", leadingSpace), err)
	}

	err = ValidateUsername(trailingSpace)
	if assert.Error(suite.T(), err) {
		assert.Equal(suite.T(), fmt.Errorf("given username %s was invalid: must contain only lowercase letters, numbers, and underscores", trailingSpace), err)
	}

	err = ValidateUsername(newlines)
	if assert.Error(suite.T(), err) {
		assert.Equal(suite.T(), fmt.Errorf("given username %s was invalid: must contain only lowercase letters, numbers, and underscores", newlines), err)
	}

	err = ValidateUsername(goodUsername)
	if assert.NoError(suite.T(), err) {
		assert.Equal(suite.T(), nil, err)
	}
}

func (suite *ValidationTestSuite) TestValidateEmail() {
	empty := ""
	notAnEmailAddress := "this-is-no-email-address!"
	almostAnEmailAddress := "@thisisalmostan@email.address"
	aWebsite := "https://thisisawebsite.com"
	tooLong := "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaahhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhggggggggggggggggggggggggggggggggggggggghhhhhhhhhhhhhhhhhggggggggggggggggggggghhhhhhhhhhhhhhhhhhhhhhhhhhhhhh@gmail.com"
	emailAddress := "thisis.actually@anemail.address"
	var err error

	err = ValidateEmail(empty)
	if assert.Error(suite.T(), err) {
		assert.Equal(suite.T(), errors.New("no email provided"), err)
	}

	err = ValidateEmail(notAnEmailAddress)
	if assert.Error(suite.T(), err) {
		assert.Equal(suite.T(), errors.New("mail: missing '@' or angle-addr"), err)
	}

	err = ValidateEmail(almostAnEmailAddress)
	if assert.Error(suite.T(), err) {
		assert.Equal(suite.T(), errors.New("mail: no angle-addr"), err)
	}

	err = ValidateEmail(aWebsite)
	if assert.Error(suite.T(), err) {
		assert.Equal(suite.T(), errors.New("mail: missing '@' or angle-addr"), err)
	}

	err = ValidateEmail(tooLong)
	if assert.Error(suite.T(), err) {
		assert.Equal(suite.T(), fmt.Errorf("email address should be no more than 256 chars but '%s' was 286", tooLong), err)
	}

	err = ValidateEmail(emailAddress)
	if assert.NoError(suite.T(), err) {
		assert.Equal(suite.T(), nil, err)
	}
}

func (suite *ValidationTestSuite) TestValidateLanguage() {
	empty := ""
	notALanguage := "this isn't a language at all!"
	english := "en"
	capitalEnglish := "EN"
	arabic3Letters := "ara"
	mixedCapsEnglish := "eN"
	englishUS := "en-us"
	dutch := "nl"
	german := "de"
	var err error

	err = ValidateLanguage(empty)
	if assert.Error(suite.T(), err) {
		assert.Equal(suite.T(), errors.New("no language provided"), err)
	}

	err = ValidateLanguage(notALanguage)
	if assert.Error(suite.T(), err) {
		assert.Equal(suite.T(), errors.New("language: tag is not well-formed"), err)
	}

	err = ValidateLanguage(english)
	if assert.NoError(suite.T(), err) {
		assert.Equal(suite.T(), nil, err)
	}

	err = ValidateLanguage(capitalEnglish)
	if assert.NoError(suite.T(), err) {
		assert.Equal(suite.T(), nil, err)
	}

	err = ValidateLanguage(arabic3Letters)
	if assert.NoError(suite.T(), err) {
		assert.Equal(suite.T(), nil, err)
	}

	err = ValidateLanguage(mixedCapsEnglish)
	if assert.NoError(suite.T(), err) {
		assert.Equal(suite.T(), nil, err)
	}

	err = ValidateLanguage(englishUS)
	if assert.Error(suite.T(), err) {
		assert.Equal(suite.T(), errors.New("language: tag is not well-formed"), err)
	}

	err = ValidateLanguage(dutch)
	if assert.NoError(suite.T(), err) {
		assert.Equal(suite.T(), nil, err)
	}

	err = ValidateLanguage(german)
	if assert.NoError(suite.T(), err) {
		assert.Equal(suite.T(), nil, err)
	}
}

func (suite *ValidationTestSuite) TestValidateReason() {
	empty := ""
	badReason := "because"
	goodReason := "to smash the state and destroy capitalism ultimately and completely"
	tooLong := "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Mauris auctor mollis viverra. Maecenas maximus mollis sem, nec fermentum velit consectetur non. Vestibulum ante ipsum primis in faucibus orci luctus et ultrices posuere cubilia curae; Quisque a enim nibh. Vestibulum bibendum leo ac porttitor auctor. Curabitur velit tellus, facilisis vitae lorem a, ullamcorper efficitur leo. Sed a auctor tortor. Sed ut finibus ante, sit amet laoreet sapien. Donec ullamcorper tellus a nibh sodales vulputate. Donec id dolor eu odio mollis bibendum. Pellentesque habitant morbi tristique senectus et netus at."
	var err error

	// check with no reason required
	err = ValidateSignUpReason(empty, false)
	if assert.NoError(suite.T(), err) {
		assert.Equal(suite.T(), nil, err)
	}

	err = ValidateSignUpReason(badReason, false)
	if assert.NoError(suite.T(), err) {
		assert.Equal(suite.T(), nil, err)
	}

	err = ValidateSignUpReason(tooLong, false)
	if assert.NoError(suite.T(), err) {
		assert.Equal(suite.T(), nil, err)
	}

	err = ValidateSignUpReason(goodReason, false)
	if assert.NoError(suite.T(), err) {
		assert.Equal(suite.T(), nil, err)
	}

	// check with reason required
	err = ValidateSignUpReason(empty, true)
	if assert.Error(suite.T(), err) {
		assert.Equal(suite.T(), errors.New("no reason provided"), err)
	}

	err = ValidateSignUpReason(badReason, true)
	if assert.Error(suite.T(), err) {
		assert.Equal(suite.T(), errors.New("reason should be at least 40 chars but 'because' was 7"), err)
	}

	err = ValidateSignUpReason(tooLong, true)
	if assert.Error(suite.T(), err) {
		assert.Equal(suite.T(), errors.New("reason should be no more than 500 chars but given reason was 600"), err)
	}

	err = ValidateSignUpReason(goodReason, true)
	if assert.NoError(suite.T(), err) {
		assert.Equal(suite.T(), nil, err)
	}
}

func TestValidationTestSuite(t *testing.T) {
	suite.Run(t, new(ValidationTestSuite))
}