-
Notifications
You must be signed in to change notification settings - Fork 9
/
cachet_test.go
153 lines (144 loc) · 4.04 KB
/
cachet_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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
package main
import (
"io"
"net/http"
"net/http/httptest"
"testing"
"github.com/stretchr/testify/assert"
)
// the mock cachetHQ defines 1 component: "Component21"
func TestCachetListComponents(t *testing.T) {
var ts *httptest.Server
ts = httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.Method == "GET" && r.URL.Path == "/api/v1/components" {
w.Header().Set("Content-Type", "aplication/json")
w.WriteHeader(http.StatusOK)
io.WriteString(w, `{
"meta": {
"pagination": {
"total": 1,
"count": 1,
"per_page": 20,
"current_page": 1,
"total_pages": 1,
"links": {
"next_page": null,
"previous_page": null
}
}
},
"data": [
{
"id": 1,
"name": "API",
"description": "This is the Cachet API.",
"link": "",
"status": 1,
"order": 0,
"group_id": 0,
"created_at": "2015-07-24 14:42:10",
"updated_at": "2015-07-24 14:42:10",
"deleted_at": null,
"status_name": "Operational",
"tags": [
{"slug-of-tag": "Tag Name"}
]
}
]
}`)
} else if r.Method == "GET" && r.URL.Path == "/api/v1/incidents" {
w.Header().Set("Content-Type", "aplication/json")
w.WriteHeader(http.StatusOK)
io.WriteString(w, `{
"meta": {
"pagination": {
"total": 1,
"count": 1,
"per_page": "20",
"current_page": 1,
"total_pages": 1,
"links": {
"next_page": null,
"previous_page": null
}
}
},
"data": [
{
"id": 2,
"component_id": 1,
"name": "Incident Name",
"status": 1,
"visible": 1,
"message": "Incident Message",
"scheduled_at": "2015-08-01 12:00:00",
"created_at": "2015-08-01 12:00:00",
"updated_at": "2015-08-01 12:00:00",
"deleted_at": null,
"human_status": "Fixed"
}
]
}`)
} else if r.Method == "POST" && r.URL.Path == "/api/v1/incidents" {
w.Header().Set("Content-Type", "aplication/json")
w.WriteHeader(http.StatusOK)
io.WriteString(w, `{
"data": {
"id": 4,
"component_id": 1,
"name": "API down",
"status": 1,
"visible": 1,
"message": "API down",
"scheduled_at": "2015-08-01 12:00:00",
"created_at": "2015-08-01 12:00:00",
"updated_at": "2015-08-01 12:00:00",
"deleted_at": null,
"human_status": "Fixed"
}
}`)
} else if r.Method == "PUT" && r.URL.Path == "/api/v1/incidents/4" {
w.Header().Set("Content-Type", "aplication/json")
w.WriteHeader(http.StatusOK)
io.WriteString(w, `{
"data": {
"id": 1,
"component_id": 1,
"name": "Incident Name",
"status": 4,
"visible": 1,
"message": "Incident Message",
"scheduled_at": "2015-08-01 12:00:00",
"created_at": "2015-08-01 12:00:00",
"updated_at": "2015-08-01 12:00:00",
"deleted_at": null,
"human_status": "Fatal"
}
}`)
} else {
w.WriteHeader(http.StatusInternalServerError)
io.WriteString(w, `{"status":"fail"}`)
}
}))
defer ts.Close()
cachet := NewCachetImpl(ts.URL, "undefined", ts.Client())
// test list components
listComponents, err := cachet.ListComponents()
assert.Nil(t, err)
assert.Equal(t, 1, len(listComponents))
assert.Equal(t, 1, listComponents["API"])
// test search component
componentID, err := cachet.SearchComponent("API")
assert.Nil(t, err)
assert.Equal(t, 1, componentID)
// test list incidents
listIncidents, err := cachet.SearchIncidents(1)
assert.Nil(t, err)
assert.Equal(t, 1, len(listIncidents))
assert.Equal(t, 2, listIncidents[0].Id)
assert.Equal(t, 1, listIncidents[0].Status)
err = cachet.CreateIncident("API", 1, 1, 4)
assert.Nil(t, err)
err = cachet.UpdateIncident("API", 1, 4, 4, "message")
assert.Nil(t, err)
}