💾 Archived View for gemini.rmf-dev.com › repo › Vaati › Gemigit › files › 8c57daee4169161ba298a2f707… captured on 2023-09-08 at 16:28:33. Gemini links have been rewritten to link to archived content
⬅️ Previous capture (2023-05-24)
-=-=-=-=-=-=-
0 package db
1
2 import (
3 "testing"
4 "strings"
5 "strconv"
6 "gemigit/test"
7 )
8
9 const tooLongDescription =
10 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" +
11 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
12
13 var usersCount = 0
14 func createUserAndSession(t *testing.T) (User, string) {
15
16 usersCount += 1
17
18 username := test.FuncName(t) + strconv.Itoa(usersCount)
19 test.IsNil(t, Register(username, validPassword))
20
21 signature := username + "_signature"
22 user, err := FetchUser(username, signature)
23 test.IsNil(t, err)
24
25 test.IsNil(t, user.CreateSession(signature))
26
27 return user, signature
28 }
29
30 func TestGetUserID(t *testing.T) {
31
32 user, _ := createUserAndSession(t)
33
34 id, err := GetUserID(user.Name)
35 test.IsNil(t, err)
36
37 test.IsEqual(t, id, user.ID)
38
39 _, err = GetUserID(user.Name + "a")
40 test.IsNotNil(t, err, "GetUserID should return user not found")
41 }
42
43 func TestGetUser(t *testing.T) {
44
45 _, signature := createUserAndSession(t)
46
47 user, b := GetUser(signature)
48 test.IsEqual(t, b, true)
49 test.IsEqual(t, user.Name, user.Name)
50
51 user, b = GetUser(signature + "a")
52 test.IsEqual(t, b, false)
53
54 delete(users, signature)
55
56 user, b = GetUser(signature)
57 test.IsEqual(t, b, true)
58 test.IsEqual(t, user.Name, user.Name)
59 }
60
61 func TestGetPublicUser(t *testing.T) {
62
63 initDB(t)
64
65 username := test.FuncName(t)
66 test.IsNil(t, Register(username, validPassword))
67
68 _, err := GetPublicUser(username + "a")
69 test.IsNotNil(t, err, "should return user not found")
70
71 user, err := GetPublicUser(username)
72 test.IsNil(t, err)
73 test.IsEqual(t, user.Name, username)
74 }
75
76 func TestCheckAuth(t *testing.T) {
77
78 initDB(t)
79
80 username := test.FuncName(t)
81 test.IsNil(t, Register(username, validPassword))
82
83 /* Test credential */
84 test.IsNil(t, CheckAuth(username, validPassword))
85
86 test.IsNotNil(t, CheckAuth(username, validPassword + "a"),
87 "should return invalid credential")
88
89 test.IsNotNil(t, CheckAuth(username + "a", validPassword),
90 "should return invalid credential")
91 }
92
93 func TestChangePassword(t *testing.T) {
94
95 initDB(t)
96
97 user, signature := createUserAndSession(t)
98
99 test.IsNil(t, CheckAuth(user.Name, validPassword))
100 test.IsNotNil(t, user.ChangePassword(invalidPassword, signature),
101 "password should be invalid")
102 test.IsNotNil(t,
103 user.ChangePassword(validPassword + "a", signature + "a"),
104 "signature should be invalid")
105 test.IsNil(t, user.ChangePassword(validPassword + "a", signature))
106 test.IsNotNil(t, ChangePassword(user.Name+ "a", validPassword),
107 "username shoudl be invalid")
108 test.IsNotNil(t, CheckAuth(user.Name, validPassword),
109 "credential should be invalid")
110 test.IsNil(t, CheckAuth(user.Name, validPassword + "a"))
111 }
112
113 func TestRegistration(t *testing.T) {
114
115 initDB(t)
116
117 username := test.FuncName(t)
118 test.IsNil(t, Register(username, validPassword))
119
120 test.IsNotNil(t, Register(username, validPassword),
121 "should not be able to register the same username")
122
123 username = strings.ToLower(username)
124 test.IsNotNil(t, Register(username, validPassword),
125 "should detect case-insentive duplicates")
126
127 test.IsNotNil(t, Register(invalidUsername, validPassword),
128 "should not allow invalid username")
129 }
130
131 func TestDeleteUser(t *testing.T) {
132
133 initDB(t)
134
135 username := test.FuncName(t)
136 test.IsNil(t, Register(username, validPassword))
137 test.IsNil(t, CheckAuth(username, validPassword))
138
139 // Delete user
140 test.IsNotNil(t, DeleteUser(username + "a"),
141 "should return user not found")
142 test.IsNil(t, DeleteUser(username))
143 test.IsNotNil(t, CheckAuth(username, validPassword),
144 "should return invalid credential")
145
146 if err := CheckAuth(username, validPassword); err == nil {
147 t.Fatal("should return invalid credential")
148 }
149 }
150
151 func TestFetchUser(t *testing.T) {
152
153 initDB(t)
154
155 username := test.FuncName(t)
156 test.IsNil(t, Register(username, validPassword))
157
158 signature := username + "_signature"
159 user, err := FetchUser(username, signature)
160 test.IsNil(t, err)
161
162 test.IsEqual(t, user.Signature, signature)
163
164 _, err = FetchUser(username + "a", signature)
165 test.IsNotNil(t, err, "should return user not found")
166
167 }
168
169 func TestChangeDescription(t *testing.T) {
170
171 initDB(t)
172
173 user, signature := createUserAndSession(t)
174
175 test.IsNotNil(t, user.ChangeDescription(user.Name, "bad_signature"),
176 "should return invalid signature")
177
178 test.IsNil(t, user.ChangeDescription("my description", signature))
179
180 test.IsNotNil(t, user.ChangeDescription(tooLongDescription, signature),
181 "should return too long description")
182 }
183
184 func TestUpdateDescription(t *testing.T) {
185
186 initDB(t)
187
188 var u User
189 test.IsNotNil(t, u.UpdateDescription(), "should return sql error")
190
191 user, _ := createUserAndSession(t)
192
193 description := "testing"
194
195 _, err := db.Exec("UPDATE user SET description=? WHERE userID=?",
196 description, user.ID)
197 test.IsNil(t, err)
198
199 test.IsNil(t, user.UpdateDescription())
200 test.IsEqual(t, user.Description, description)
201 }
202
203 func TestSetSecret(t *testing.T) {
204
205 initDB(t)
206
207 user, _ := createUserAndSession(t)
208 test.IsNil(t, user.SetSecret("secret"))
209
210 user.ID = -1
211 test.IsNotNil(t, user.SetSecret("secret"),
212 "should return user not found")
213 }
214