-
Notifications
You must be signed in to change notification settings - Fork 0
/
repo_test.go
159 lines (132 loc) · 3.07 KB
/
repo_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
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
package users
import (
"os"
"testing"
)
var dbname = "./testfoo.db"
func TestPGRepo(t *testing.T) {
dsn := os.Getenv("TESTDB")
if dsn == "" {
t.Skip("TESDB env not set skipping Postgres test")
}
db, err := openPG(dsn)
if err != nil {
t.Error(err)
return
}
testRepo(t, newSQLRepo(db))
}
func TestSQLiteRepo(t *testing.T) {
os.Remove(dbname)
db, err := openSQlite(dbname)
if err != nil {
t.Error(err)
return
}
testRepo(t, newSQLRepo(db))
os.Remove(dbname)
}
func TestNewWithSQLite(t *testing.T) {
os.Remove(dbname)
if _, _, err := NewWithSQLite(dbname); err != nil {
t.Error(err)
}
os.Remove(dbname)
}
func TestNewWithPG(t *testing.T) {
dsn := os.Getenv("TESTDB")
if dsn == "" {
t.Skip("TESTDB env not set skipping New test for Postgres")
}
if _, _, err := NewWithPG(dsn); err != nil {
t.Error(err)
}
}
func testRepo(t *testing.T, r Repository) {
defer r.Close()
// test Get yields error on random query
_, err := r.Get("obi-wan")
if err == nil {
t.Errorf("Get does not fail on random query")
}
// testing Put fails on mepty user struct
err = r.Put(User{})
if err == nil {
t.Errorf("repo Puts empty user")
}
u := User{
Username: "testuser",
Secret: BcryptHash("testpassword"), // using hash here to check Authenticate later
Enabled: true,
Attrs: map[string]string{
"attr1": "hello",
},
}
// testing we can put and get back
err = r.Put(u)
if err != nil {
t.Error(err)
}
u2, err := r.Get("testuser")
if err != nil {
t.Error(err)
}
if u2.Username != u.Username || u.Secret != u2.Secret ||
u2.Enabled != u.Enabled {
t.Errorf("got something useless from repo")
}
for k, v := range u.Attrs {
v2, ok := u2.Attrs[k]
if v2 != v || !ok {
t.Errorf("Attrs maps are not equal")
}
}
u2.Attrs["added"] = "newvalue"
u2.Enabled = false
// testing we can update
if err = r.Upd(u2); err != nil {
t.Error(err)
}
u2, err = r.Get("testuser")
if err != nil {
t.Error(err)
}
if v, ok := u2.Attrs["added"]; v != "newvalue" || !ok {
t.Errorf("attributes did not update")
}
if u2.Enabled {
t.Errorf("enabled did not update")
}
// test Update fails on invalid username
err = r.Upd(User{Username: "randomname", Secret: "randomhash"})
if err == nil {
t.Errorf("Upd does not fail on random username")
}
// test Upd fails on empty user struct
err = r.Upd(User{})
if err == nil {
t.Errorf("Upd does not fail on empty User struct")
}
// testing rename works
if err = r.Rename("testuser", "newtestuser"); err != nil {
t.Error(err)
}
u3, err := r.Get("newtestuser")
if err != nil {
t.Error(err)
}
if u3.Enabled != u2.Enabled || u3.Secret != u2.Secret {
t.Errorf("rename didn't work")
}
auth := newBcryptAuth(r)
// testing Authenticate works
if ok, err := auth.Authenticate("newtestuser", "testpassword"); !ok || err != nil {
t.Errorf("Authenticate fails on valid case")
} else if ok, err := auth.Authenticate("newtestuser", "randomstuff"); ok || err == nil {
t.Errorf("Authenticate doesn't fail on invalid case")
}
// testing del works
if err = r.Del("newtestuser"); err != nil {
t.Error(err)
}
}