-
Notifications
You must be signed in to change notification settings - Fork 0
/
models.user_test.go
87 lines (64 loc) · 1.54 KB
/
models.user_test.go
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
// models.user_test.go
package main
import "testing"
// Test the validity of different combinations of username/password
func TestUserValidity(t *testing.T) {
if !isUserValid("user1", "pass1") {
t.Fail()
}
if isUserValid("user2", "pass1") {
t.Fail()
}
if isUserValid("user1", "") {
t.Fail()
}
if isUserValid("", "pass1") {
t.Fail()
}
if isUserValid("User1", "pass1") {
t.Fail()
}
}
// Test if a new user can be registered with valid username/password
func TestValidUserRegistration(t *testing.T) {
saveLists()
u, err := registerNewUser("newuser", "newpass")
if err != nil || u.Username == "" {
t.Fail()
}
restoreLists()
}
// Test that a new user cannot be registered with invalid username/password
func TestInvalidUserRegistration(t *testing.T) {
saveLists()
// Try to register a user with a used username
u, err := registerNewUser("user1", "pass1")
if err == nil || u != nil {
t.Fail()
}
// Try to register with a blank password
u, err = registerNewUser("newuser", "")
if err == nil || u != nil {
t.Fail()
}
restoreLists()
}
// Test the function that checks for username availability
func TestUsernameAvailability(t *testing.T) {
saveLists()
// This username should be available
if !isUsernameAvailable("newuser1") {
t.Fail()
}
// This username should not be available
if isUsernameAvailable("user1") {
t.Fail()
}
// Register a new user
registerNewUser("newuser", "newpass")
// This newly registered username should not be available
if isUsernameAvailable("newuser") {
t.Fail()
}
restoreLists()
}