forked from joefitzgerald/forecast
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapi_test.go
69 lines (60 loc) · 1.75 KB
/
api_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
package forecast_test
import (
"fmt"
"net/http"
"net/http/httptest"
. "github.com/joefitzgerald/forecast"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
)
var _ = Describe("API", func() {
var (
server *httptest.Server
api *API
)
AfterEach(func() {
api = nil
if server == nil {
return
}
server.Close()
server = nil
})
Context("when the access token is invalid", func() {
BeforeEach(func() {
server = httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
response := ReadFile("non-existent-token.json")
w.Header().Set("Content-Type", "application/json; charset=utf-8")
w.WriteHeader(http.StatusUnauthorized)
fmt.Fprintf(w, "%s", response)
}))
api = New(server.URL, "test-token", "000000")
})
It("should return an unauthorized error", func() {
user, err := api.WhoAmI()
Ω(user).Should(BeNil())
Ω(err).Should(HaveOccurred())
Ω(err.Error()).Should(Equal("401 Unauthorized: " + ReadFile("non-existent-token.json")))
})
})
Context("when the access token is valid", func() {
BeforeEach(func() {
server = httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
response := ReadFile("whoami.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", "000000")
})
It("should not return an unauthorized error", func() {
user, err := api.WhoAmI()
Ω(user).ShouldNot(BeNil())
Ω(user.ID).Should(Equal(123456))
Ω(len(user.AccountIds)).Should(Equal(2))
Ω(user.AccountIds[0]).Should(Equal(111111))
Ω(user.AccountIds[1]).Should(Equal(222222))
Ω(err).ShouldNot(HaveOccurred())
})
})
})