This repository has been archived by the owner on Oct 19, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuser_test.go
129 lines (104 loc) · 1.99 KB
/
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
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
package auth
import (
"os"
"testing"
"gopkg.in/kwiscale/orm.v0"
_ "gopkg.in/kwiscale/orm.v0/dialects/sqlite"
)
func up() {
orm.Initialize("sqlite", "test.db")
Initialize()
}
func down() {
os.Remove("test.db")
}
func TestCreateUser(t *testing.T) {
up()
defer down()
u := User{}
u.Login = "Foo"
u.Password = "mypassword"
err := u.Create()
if err != nil {
t.Fatal(err)
}
}
func TestLogin(t *testing.T) {
up()
defer down()
u := User{}
u.Login = "Foo"
u.Password = "mypassword"
u.Create()
u2 := User{}
res := u2.Signin("Foo", "mypassword")
if !res {
t.Fatal("User should login")
}
u3 := User{}
res = u3.Signin("Foo", "badpassword")
if res {
t.Fatal("User should NOT login")
}
}
func TestUpdateUser(t *testing.T) {
defer down()
up()
u := User{}
u.Login = "Foo"
u.Password = "mypassword"
u.Create()
test := User{}
test.Signin("Foo", "mypassword")
test.Password = "newpass"
err := test.Save()
if err != nil {
t.Fatal("Password change error", err)
}
if !test.Signin("Foo", "newpass") {
t.Fatal("User should be able to login with new password")
}
test.Login = "Foo2"
err = test.Save()
if err == nil {
t.Fatal("User login cannot be changed")
}
}
func TestRemoveUser(t *testing.T) {
defer down()
up()
u := User{}
u.Login = "Foo"
u.Password = "mypassword"
u.Create()
err := u.Delete()
if err != nil {
t.Fatal("User deletion error", err)
}
}
func TestInvalidSignup(t *testing.T) {
u := User{}
u.Password = "mypass"
err := u.Create()
if err == nil {
t.Fatal("User shouldn't be able create without Login")
}
u.Login = "Foo"
u.Password = ""
err = u.Create()
if err == nil {
t.Fatal("User shouldn't be able create without password")
}
u.Login = "F"
u.Password = "mypass"
err = u.Create()
if err == nil {
t.Fatal("User shouldn't be able to create with a login length <", minlength)
}
u.Login = "Foo"
u.Password = "m"
err = u.Create()
if err == nil {
t.Fatal("User shouldn't be able to create with a password length too short")
}
}