-
Notifications
You must be signed in to change notification settings - Fork 3
/
mongo_test.go
105 lines (95 loc) · 2.08 KB
/
mongo_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
// Copyright (c) 2013 Jason McVetta. This is Free Software, released under the
// terms of the GPL v3. See http://www.gnu.org/copyleft/gpl.html for details.
// Resist intellectual serfdom - the ownership of ideas is akin to slavery.
package o2pro
import (
"github.com/bmizerany/assert"
"labix.org/v2/mgo"
"log"
"testing"
"time"
)
func col(db *mgo.Database) *mgo.Collection {
return db.C("authorizations")
}
func testMongo(t *testing.T) (*Provider, *mgo.Database) {
log.SetFlags(log.Ltime | log.Lshortfile)
session, err := mgo.Dial("mongodb://127.0.0.1")
if err != nil {
t.Fatal(err)
}
db := session.DB("test_o2pro")
dur, err := time.ParseDuration(DefaultExpireAfter)
if err != nil {
t.Fatal(err)
}
stor := NewMongoStorage(db, dur)
if err != nil {
t.Fatal(err)
}
p := NewProvider(stor, kirkAuthenticator, GrantAll)
p.Scopes = testScopesAll
p.DefaultScopes = testScopesDefault
err = p.Initialize()
if err != nil {
t.Fatal(err)
}
err = p.Migrate()
if err != nil {
t.Fatal(err)
}
return p, db
}
func TestMgoNewAuth(t *testing.T) {
s, db := testMongo(t)
username := "jtkirk"
scopes := []string{"enterprise", "shuttlecraft"}
note := "foo bar baz"
auth, err := s.NewAuthz(username, note, scopes)
if err != nil {
t.Error(err)
}
c := col(db)
query := struct {
Token string
}{
Token: auth.Token,
}
q := c.Find(&query)
cnt, err := q.Count()
if err != nil {
t.Error(err)
}
assert.Equal(t, 1, cnt)
a := Authz{}
err = q.One(&a)
if err != nil {
t.Error(err)
}
assert.Equal(t, username, a.User)
sm := a.ScopesMap()
for _, scope := range scopes {
_, ok := sm[scope]
assert.T(t, ok, "Expected scope: ", scope)
}
}
func TestMgoAuthz(t *testing.T) {
p, _ := testMongo(t)
doTestAuthz(p, t)
}
func TestMgoExpiration(t *testing.T) {
p, _ := testMongo(t)
doTestExpiration(p, t)
}
func TestMgoPasswordRequest(t *testing.T) {
p, _ := testMongo(t)
doTestPasswordRequest(p, t)
}
func TestMgoRequireScope(t *testing.T) {
p, _ := testMongo(t)
doTestRequireScope(p, t)
}
func TestMgoTestRequireAuthc(t *testing.T) {
p, _ := testMongo(t)
doTestRequireAuthc(p, t)
}