-
Notifications
You must be signed in to change notification settings - Fork 1
/
multiuser_test.go
106 lines (104 loc) · 3.43 KB
/
multiuser_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
package minidb
import (
"io/ioutil"
"testing"
)
func TestMultiDB(t *testing.T) {
var db *MultiDB
tmpdir, err := ioutil.TempDir("", "multidb")
if err != nil {
t.Errorf(`could not create temporary directory for testing`)
}
db, err = NewMultiDB(tmpdir, "sqlite3")
if err != nil {
t.Errorf(`error creating MultiDB: %s`, err)
}
// some test users
p := DefaultParams()
salt := GenerateExternalSalt(p)
key := GenerateKey("a test password", salt, p)
user1, code, err := db.NewUser("John", "[email protected]", key)
if err != nil {
t.Errorf(`could not create new user "John", %s [errcode=%d]`, err, code)
}
salt2 := GenerateExternalSalt(p)
key2 := GenerateKey("another password", salt2, p)
user2, _, err := db.NewUser("Bob", "[email protected]", key2)
if err != nil {
t.Errorf(`could not create new user "Bob", %s`, err)
}
salt3 := GenerateExternalSalt(p)
key3 := GenerateKey("some password", salt3, p)
_, errcode, _ := db.NewUser("John", "[email protected]", key3)
if errcode != ErrUsernameInUse {
t.Errorf(`expected errcode=%d for NewUser on existing username, given %d`, ErrUsernameInUse, errcode)
}
// The following line is bad practice, you should never re-use salts and passwords.
_, errcode, _ = db.NewUser("Johnny", "[email protected]", key3)
if errcode != ErrEmailInUse {
t.Errorf(`expected errcode=%d for NewUser on existing email, given %d`, ErrEmailInUse, errcode)
}
// various methods test
if !db.ExistingUser(user1.Name()) {
t.Errorf(`MultiDB.ExistingUser() returned false for existing user`)
}
if !db.ExistingUser(user2.Name()) {
t.Errorf(`MultiDB.ExistingUser() returned false for existing user`)
}
// authentication tests
salt4, errcode, err := db.ExternalSalt("John")
if err != nil {
t.Errorf(`MultiDB.ExternalSalt() failed, errcode=%d: %s`, errcode, err)
}
key4 := GenerateKey("a test password", salt4, p)
_, errcode, err = db.Authenticate("John", key4)
if err != nil {
t.Errorf(`MultiDB.Authenticate() failed with errcode=%d: %s`, errcode, err)
}
// do something with the DB
user1db, reply, err := db.UserDB(user1)
if err != nil {
t.Errorf(`MultiDB.UserDB() failed with errcode=%d: %s`, reply, err)
}
user2db, reply, err := db.UserDB(user2)
if err != nil {
t.Errorf(`MultiDB.UserDB() failed with errcode=%d: %s`, reply, err)
}
tx, err := user1db.Begin()
if err != nil {
t.Errorf(`MultiDB user db transaction failed: %s`, err)
}
tx.SetStr(1, "test")
err = tx.Commit()
if err != nil {
t.Errorf(`MultiDB user db commit failed: %s`, err)
}
if s := user1db.GetStr(1); s != "test" {
t.Errorf(`simple get/set failed: %s`, err)
}
tx, err = user2db.Begin()
if err != nil {
t.Errorf(`MultiDB user db transaction failed: %s`, err)
}
tx.SetStr(1, "hello world")
err = tx.Commit()
if err != nil {
t.Errorf(`MultiDB user db commit failed: %s`, err)
}
if s := user2db.GetStr(1); s != "hello world" {
t.Errorf(`simple get/set failed: %s`, err)
}
if err != nil {
t.Errorf(`MultiDB user db commit failed: %s`, err)
}
if s, reply, err := db.UserEmail(user1); s != "[email protected]" {
t.Errorf(`test user email not stored correctly, errcode=%d, expected "[email protected]", given "%s": %s`, reply, s, err)
}
if s, reply, err := db.UserEmail(user2); s != "[email protected]" {
t.Errorf(`test user email not stored correctly, errcode=%d, expected "[email protected]", given "%s": %s`, reply, s, err)
}
// delete the DB
if reply, err := db.Delete(); err != nil {
t.Errorf(`error %d deleting the MultiDB: %s`, reply, err)
}
}