-
Notifications
You must be signed in to change notification settings - Fork 3
/
plan_groups_test.go
110 lines (97 loc) · 2.74 KB
/
plan_groups_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
package chartmogul
import (
"net/http"
"net/http/httptest"
"testing"
"github.com/davecgh/go-spew/spew"
)
const onePlanGroupExample = `{
"name": "My plan group",
"uuid": "plg_b53fdbfc-c5eb-4a61-a589-85146cf8d0ab",
"plans_count": 2
}`
const listAllPlanGroupsExample = `{
"plan_groups": [
` + onePlanGroupExample + `
],
"current_page": 1,
"total_pages": 1
}`
func TestNewPlanGroupAllListing(t *testing.T) {
server := httptest.NewServer(
http.HandlerFunc(
func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
w.Write([]byte(listAllPlanGroupsExample)) //nolint
}))
defer server.Close()
SetURL(server.URL + "/v/%v")
tested := &API{
ApiKey: "token",
}
result, err := tested.ListPlanGroups(&Cursor{})
if err != nil {
spew.Dump(err)
t.Fatal("Not expected to fail")
}
if len(result.PlanGroups) != 1 ||
result.PlanGroups[0].UUID != "plg_b53fdbfc-c5eb-4a61-a589-85146cf8d0ab" ||
result.PlanGroups[0].PlansCount != 2 {
spew.Dump(result)
t.Fatal("Unexpected values")
}
}
func TestDeletePlanGroup(t *testing.T) {
server := httptest.NewServer(
http.HandlerFunc(
func(w http.ResponseWriter, r *http.Request) {
expected := "/v/plan_groups/plg_b53fdbfc-c5eb-4a61-a589-85146cf8d0ab"
path := r.URL.Path
if path != expected {
t.Errorf("Requested path expected: %v, actual: %v", expected, path)
w.WriteHeader(http.StatusNotFound)
}
w.WriteHeader(http.StatusNoContent)
}))
defer server.Close()
SetURL(server.URL + "/v/%v")
var tested IApi = &API{
ApiKey: "token",
}
err := tested.DeletePlanGroup("plg_b53fdbfc-c5eb-4a61-a589-85146cf8d0ab")
if err != nil {
spew.Dump(err)
t.Fatal("Not expected to fail")
}
}
func TestRetrievePlanGroup(t *testing.T) {
server := httptest.NewServer(
http.HandlerFunc(
func(w http.ResponseWriter, r *http.Request) {
expectedMethod := "GET"
if r.Method != expectedMethod {
t.Errorf("Requested method expected: %v, actual: %v", expectedMethod, r.Method)
}
expected := "/v/plan_groups/plg_b53fdbfc-c5eb-4a61-a589-85146cf8d0ab"
path := r.URL.Path
if path != expected {
t.Errorf("Requested path expected: %v, actual: %v", expected, path)
w.WriteHeader(http.StatusNotFound)
}
w.Write([]byte(onePlanGroupExample)) //nolint
}))
defer server.Close()
SetURL(server.URL + "/v/%v")
var tested IApi = &API{
ApiKey: "token",
}
planGroup, err := tested.RetrievePlanGroup("plg_b53fdbfc-c5eb-4a61-a589-85146cf8d0ab")
if planGroup.Name != "My plan group" || planGroup.PlansCount != 2 || planGroup.UUID != "plg_b53fdbfc-c5eb-4a61-a589-85146cf8d0ab" {
spew.Dump(planGroup)
t.Error("Unexpected plan group")
}
if err != nil {
spew.Dump(err)
t.Fatal("Not expected to fail")
}
}