-
Notifications
You must be signed in to change notification settings - Fork 5
/
account_test.go
85 lines (74 loc) · 2.12 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
73
74
75
76
77
78
79
80
81
82
83
84
85
package forecast_test
import (
"fmt"
"net/http"
"net/http/httptest"
"testing"
"github.com/joefitzgerald/forecast"
. "github.com/onsi/gomega"
"github.com/sclevine/spec"
"github.com/sclevine/spec/report"
)
func TestAccounts(t *testing.T) {
spec.Run(t, "Accounts", testAccounts, spec.Report(report.Terminal{}))
}
func testAccounts(t *testing.T, when spec.G, it spec.S) {
var (
server *httptest.Server
handler http.Handler
api *forecast.API
)
it.Before(func() {
RegisterTestingT(t)
server = httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if handler != nil {
handler.ServeHTTP(w, r)
}
}))
api = forecast.New(server.URL, "test-token", "987654")
})
it.After(func() {
api = nil
if server == nil {
return
}
server.Close()
server = nil
})
when("when a response is returned from the server", func() {
it.Before(func() {
handler = http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
response := ReadFile("accounts.json")
w.Header().Set("Content-Type", "application/json; charset=utf-8")
w.WriteHeader(http.StatusOK)
fmt.Fprintf(w, "%s", response)
})
})
it("should return an account and a nil error", func() {
account, err := api.Account()
Expect(account).ShouldNot(BeNil())
Expect(account.ID).Should(Equal(987654))
Expect(account.Name).Should(Equal("Test"))
Expect(account.WeeklyCapacity).Should(Equal(144000))
Expect(len(account.ColorLabels)).Should(Equal(8))
Expect(account.HarvestName).Should(Equal("Test"))
Expect(account.HarvestSubdomain).Should(Equal("test"))
Expect(err).ShouldNot(HaveOccurred())
})
})
when("when an error is returned from the server", func() {
it.Before(func() {
handler = http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
response := "error"
w.Header().Set("Content-Type", "application/json; charset=utf-8")
w.WriteHeader(http.StatusBadRequest)
fmt.Fprintf(w, "%s", response)
})
})
it("should return an error", func() {
account, err := api.Account()
Expect(account).Should(BeNil())
Expect(err).Should(HaveOccurred())
})
})
}