-
Notifications
You must be signed in to change notification settings - Fork 0
/
flags_test.go
68 lines (65 loc) · 1.84 KB
/
flags_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
package main
import (
"io/ioutil"
"testing"
. "github.com/smartystreets/goconvey/convey"
)
func Test_Flags(t *testing.T) {
Convey("a flag json file can be loaded and parsed", t, func() {
flagfile, err := ioutil.ReadFile("tests/data/flag1.json")
flag, err := LoadAndParseFlag(flagfile)
So(err, ShouldBeNil)
So(flag.Name, ShouldEqual, "red_button")
So(len(flag.Policies), ShouldEqual, 3)
for _, policy := range flag.Policies {
So(policy.ParsedExpr, ShouldNotBeNil)
}
Convey("Match zero policies", func() {
doc1 := map[string]interface{}{
"user_username": "nobody",
"user_id": 1,
}
So(flag.GetValue(doc1), ShouldEqual, false)
})
Convey("Match first policy", func() {
doc1 := map[string]interface{}{
"user_username": "foo",
"user_id": 1,
}
So(flag.GetValue(doc1), ShouldEqual, true)
})
Convey("Match second policy", func() {
doc1 := map[string]interface{}{
"user_username": "nobody",
"user_id": 10,
}
So(flag.GetValue(doc1), ShouldEqual, true)
})
Convey("Match third policy", func() {
doc1 := map[string]interface{}{
"user_username": "jessfraz",
"user_id": 1,
}
So(flag.GetValue(doc1), ShouldEqual, true)
})
Convey("useless document", func() {
doc1 := map[string]interface{}{}
So(flag.GetValue(doc1), ShouldEqual, false)
})
})
Convey("a flag json file can be loaded and not parsed", t, func() {
flagfile, err := ioutil.ReadFile("tests/data/flag1.json")
flag, err := LoadFlagJSON(flagfile)
So(err, ShouldBeNil)
So(flag.Name, ShouldEqual, "red_button")
for _, policy := range flag.Policies {
So(policy.ParsedExpr, ShouldBeNil)
}
So(len(flag.Policies), ShouldEqual, 3)
})
Convey("an invalid json raises errors", t, func() {
flagfile := []byte("{'garbage:")
_, err := LoadFlagJSON(flagfile)
So(err, ShouldNotBeNil)
})
}