forked from joefitzgerald/forecast
-
Notifications
You must be signed in to change notification settings - Fork 0
/
account_test.go
72 lines (61 loc) · 1.77 KB
/
account_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
package forecast_test
import (
"fmt"
"net/http"
"net/http/httptest"
. "github.com/joefitzgerald/forecast"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
)
var _ = Describe("Accounts", func() {
var (
server *httptest.Server
api *API
)
AfterEach(func() {
api = nil
if server == nil {
return
}
server.Close()
server = nil
})
Context("when a response is returned from the server", func() {
BeforeEach(func() {
server = httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
response := ReadFile("accounts.json")
fmt.Fprintf(w, "%s", response)
w.Header().Set("Content-Type", "application/json; charset=utf-8")
w.WriteHeader(http.StatusOK)
}))
api = New(server.URL, "test-token", "987654")
})
It("should return an account and a nil error", func() {
account, err := api.Account()
Ω(account).ShouldNot(BeNil())
Ω(account.ID).Should(Equal(987654))
Ω(account.Name).Should(Equal("Test"))
Ω(account.WeeklyCapacity).Should(Equal(144000))
Ω(len(account.ColorLabels)).Should(Equal(8))
Ω(account.HarvestName).Should(Equal("Test"))
Ω(account.HarvestSubdomain).Should(Equal("test"))
Ω(err).ShouldNot(HaveOccurred())
})
})
Context("when an error is returned from the server", func() {
BeforeEach(func() {
server = httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
response := "error"
fmt.Fprintf(w, "%s", response)
w.Header().Set("Content-Type", "application/json; charset=utf-8")
w.WriteHeader(http.StatusBadRequest)
}))
api = New(server.URL, "test-token", "987654")
})
It("should return an error", func() {
account, err := api.Account()
Ω(account).Should(BeNil())
Ω(err).Should(HaveOccurred())
})
})
})