-
Notifications
You must be signed in to change notification settings - Fork 67
/
group_test.go
135 lines (119 loc) · 3.54 KB
/
group_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
package chef
import (
"encoding/json"
"fmt"
"io"
"log"
"net/http"
"os"
"reflect"
"testing"
)
var (
testGroupJSON = "test/group.json"
)
func TestGroupFromJSONDecoder(t *testing.T) {
if file, err := os.Open(testGroupJSON); err != nil {
t.Error("Unexpected error '", err, "' during os.Open on", testGroupJSON)
} else {
dec := json.NewDecoder(file)
var g Group
if err := dec.Decode(&g); err == io.EOF {
log.Println(g)
} else if err != nil {
log.Fatal(err)
}
}
}
// TODO: Break out these method tests into separate test functions.
func TestGroupsService_Methods(t *testing.T) {
setup()
defer teardown()
// Set up our HTTP routes.
// FIXME: We should return HTTP response codes as defined by the Chef API so we can test for them.
mux.HandleFunc("/groups", func(w http.ResponseWriter, r *http.Request) {
switch {
case r.Method == "GET":
fmt.Fprintf(w, `{"group1": "https://url/for/group1", "group2": "https://url/for/group2"}`)
case r.Method == "POST":
fmt.Fprintf(w, `{ "uri": "http://localhost:4545/groups/group3" }`)
}
})
mux.HandleFunc("/groups/group3", func(w http.ResponseWriter, r *http.Request) {
switch {
// TODO: Add true test for PUT, updating an existing value.
case r.Method == "GET":
fmt.Fprintf(w, `{
"name": "group3",
"groupname": "group3",
"orgname": "Test Org, LLC",
"actors": ["tester"],
"clients": ["tester"],
"groups": ["nested-group"]
}`)
case r.Method == "PUT":
fmt.Fprintf(w, `{
"name": "group3",
"groupname": "group3",
"actors": {
"clients": [],
"groups": [],
"users": ["tester2"]
}
}`)
case r.Method == "DELETE":
}
})
// Test list
groups, err := client.Groups.List()
if err != nil {
t.Errorf("Groups.List returned error: %v", err)
}
listWant := map[string]string{"group1": "https://url/for/group1", "group2": "https://url/for/group2"}
if !reflect.DeepEqual(groups, listWant) {
t.Errorf("Groups.List returned %+v, want %+v", groups, listWant)
}
// test Get
group, err := client.Groups.Get("group3")
if err != nil {
t.Errorf("Groups.Get returned error: %v", err)
}
var wantGroup Group
wantGroup.Name = "group3"
wantGroup.GroupName = "group3"
wantGroup.OrgName = "Test Org, LLC"
wantGroup.Actors = []string{"tester"}
wantGroup.Clients = []string{"tester"}
wantGroup.Groups = []string{"nested-group"}
if !reflect.DeepEqual(group, wantGroup) {
t.Errorf("Groups.Get returned %+v, want %+v", group, wantGroup)
}
// test Create
res, err := client.Groups.Create(wantGroup)
if err != nil {
t.Errorf("Groups.Create returned error: %s", err.Error())
}
createResult := &GroupResult{"http://localhost:4545/groups/group3"}
if !reflect.DeepEqual(createResult, res) {
t.Errorf("Groups.Post returned %+v, want %+v", res, createResult)
}
// test Update
groupupdate := GroupUpdate{}
groupupdate.Name = "group3"
groupupdate.GroupName = "group3"
groupupdate.Actors.Clients = []string{}
groupupdate.Actors.Groups = []string{}
groupupdate.Actors.Users = []string{"tester2"}
updateRes, err := client.Groups.Update(groupupdate)
if err != nil {
t.Errorf("Groups Update returned error %v", err)
}
if !reflect.DeepEqual(updateRes, groupupdate) {
t.Errorf("Groups Update returned %+v, want %+v", updateRes, groupupdate)
}
// test Delete
err = client.Groups.Delete(group.Name)
if err != nil {
t.Errorf("Groups.Delete returned error: %v", err)
}
}