forked from crawshaw/sqlite
-
Notifications
You must be signed in to change notification settings - Fork 0
/
auth_test.go
54 lines (48 loc) · 1.2 KB
/
auth_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
package sqlite_test
import (
"testing"
"github.com/makenotion/sqlite"
)
func TestSetAuthorizer(t *testing.T) {
c, err := sqlite.OpenConn(":memory:", 0)
if err != nil {
t.Fatal(err)
}
defer func() {
if err := c.Close(); err != nil {
t.Error(err)
}
}()
authResult := sqlite.AuthResult(0)
var lastAction sqlite.OpType
auth := sqlite.AuthorizeFunc(func(info sqlite.ActionInfo) sqlite.AuthResult {
lastAction = info.Action
return authResult
})
c.SetAuthorizer(auth)
t.Run("Allowed", func(t *testing.T) {
authResult = 0
stmt, _, err := c.PrepareTransient("SELECT 1;")
if err != nil {
t.Fatal(err)
}
stmt.Finalize()
if lastAction != sqlite.SQLITE_SELECT {
t.Errorf("action = %q; want SQLITE_SELECT", lastAction)
}
})
t.Run("Denied", func(t *testing.T) {
authResult = sqlite.SQLITE_DENY
stmt, _, err := c.PrepareTransient("SELECT 1;")
if err == nil {
stmt.Finalize()
t.Fatal("PrepareTransient did not return an error")
}
if got, want := sqlite.ErrCode(err), sqlite.SQLITE_AUTH; got != want {
t.Errorf("sqlite.ErrCode(err) = %v; want %v", got, want)
}
if lastAction != sqlite.SQLITE_SELECT {
t.Errorf("action = %q; want SQLITE_SELECT", lastAction)
}
})
}