forked from trussworks/go-sample-api
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdogs_test.go
181 lines (149 loc) · 4.73 KB
/
dogs_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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
package integration
import (
"bytes"
"encoding/json"
"io/ioutil"
"net/http"
"net/url"
"path"
"time"
"github.com/google/uuid"
"bin/bork/pkg/models"
)
func (s IntegrationTestSuite) TestDogEndpoints() {
apiURL, err := url.Parse(s.server.URL)
s.NoError(err, "failed to parse URL")
apiURL.Path = path.Join(apiURL.Path, "/api/v1")
dogURL, err := url.Parse(apiURL.String())
s.NoError(err, "failed to parse URL")
dogURL.Path = path.Join(dogURL.Path, "/dog")
client := &http.Client{}
s.Run("POST will fail with no Authorization", func() {
req, err := http.NewRequest(http.MethodPost, dogURL.String(), bytes.NewBufferString(""))
s.NoError(err)
resp, err := client.Do(req)
s.NoError(err)
s.Equal(http.StatusUnauthorized, resp.StatusCode)
})
postDog := models.Dog{}
owner := "Owner"
s.Run("POST will succeed", func() {
body, err := json.Marshal(map[string]string{
"name": "Lola",
"breed": "Chihuahua",
"birthDate": s.clock.Now().Format(time.RFC3339),
})
s.NoError(err)
req, err := http.NewRequest(http.MethodPost, dogURL.String(), bytes.NewBuffer(body))
s.NoError(err)
req.Header.Set("Authorization", owner)
resp, err := client.Do(req)
s.NoError(err)
s.Equal(http.StatusOK, resp.StatusCode)
actualBody, err := ioutil.ReadAll(resp.Body)
s.NoError(err)
err = json.Unmarshal(actualBody, &postDog)
s.NoError(err)
s.NotZero(postDog.ID)
})
getURL, err := url.Parse(dogURL.String())
s.NoError(err, "failed to parse URL")
getURL.Path = path.Join(getURL.Path, postDog.ID.String())
s.Run("GET will fetch the dog just saved", func() {
req, err := http.NewRequest(http.MethodGet, getURL.String(), nil)
s.NoError(err)
req.Header.Set("Authorization", postDog.OwnerID)
resp, err := client.Do(req)
s.NoError(err)
defer resp.Body.Close()
s.Equal(http.StatusOK, resp.StatusCode)
actualBody, err := ioutil.ReadAll(resp.Body)
s.NoError(err)
getDog := models.Dog{}
err = json.Unmarshal(actualBody, &getDog)
s.NoError(err)
s.Equal(postDog.ID, getDog.ID)
s.Equal(postDog.Name, getDog.Name)
s.Equal(postDog.Breed, getDog.Breed)
s.Equal(postDog.OwnerID, getDog.OwnerID)
s.True(postDog.BirthDate.Equal(getDog.BirthDate))
})
s.Run("GET will fail with no Authorization", func() {
req, err := http.NewRequest(http.MethodGet, getURL.String(), bytes.NewBufferString(""))
s.NoError(err)
resp, err := client.Do(req)
s.NoError(err)
s.Equal(http.StatusUnauthorized, resp.StatusCode)
})
s.Run("GET will fail with wrong Owner", func() {
req, err := http.NewRequest(http.MethodGet, getURL.String(), bytes.NewBufferString(""))
s.NoError(err)
req.Header.Set("Authorization", "Other Owner")
resp, err := client.Do(req)
s.NoError(err)
s.Equal(http.StatusUnauthorized, resp.StatusCode)
})
s.Run("PUT will fail with no Authorization", func() {
req, err := http.NewRequest(http.MethodPut, dogURL.String(), bytes.NewBufferString(""))
s.NoError(err)
resp, err := client.Do(req)
s.NoError(err)
s.Equal(http.StatusUnauthorized, resp.StatusCode)
})
s.Run("PUT will succeed", func() {
postDog.Name = "Lolita"
body, err := json.Marshal(postDog)
s.NoError(err)
req, err := http.NewRequest(http.MethodPut, dogURL.String(), bytes.NewBuffer(body))
s.NoError(err)
req.Header.Set("Authorization", owner)
resp, err := client.Do(req)
s.NoError(err)
s.Equal(http.StatusOK, resp.StatusCode)
actualBody, err := ioutil.ReadAll(resp.Body)
s.NoError(err)
putDog := models.Dog{}
err = json.Unmarshal(actualBody, &putDog)
s.NoError(err)
s.Equal("Lolita", putDog.Name)
})
s.Run("PUT will fail on unknown dog", func() {
body, err := json.Marshal(map[string]string{
"id": uuid.New().String(),
"name": "Lola",
"breed": "Chihuahua",
"birthDate": s.clock.Now().Format(time.RFC3339),
})
s.NoError(err)
req, err := http.NewRequest(http.MethodPut, dogURL.String(), bytes.NewBuffer(body))
s.NoError(err)
req.Header.Set("Authorization", owner)
resp, err := client.Do(req)
s.NoError(err)
s.Equal(http.StatusNotFound, resp.StatusCode)
})
s.Run("GET all will fetch the dog saved", func() {
dogsURL, err := url.Parse(apiURL.String())
s.NoError(err, "failed to parse URL")
dogsURL.Path = path.Join(dogsURL.Path, "/dogs")
req, err := http.NewRequest(http.MethodGet, dogsURL.String(), nil)
s.NoError(err)
req.Header.Set("Authorization", postDog.OwnerID)
resp, err := client.Do(req)
s.NoError(err)
defer resp.Body.Close()
s.Equal(http.StatusOK, resp.StatusCode)
actualBody, err := ioutil.ReadAll(resp.Body)
s.NoError(err)
getDogs := models.Dogs{}
err = json.Unmarshal(actualBody, &getDogs)
s.NoError(err)
postDogCount := 0
for _, d := range getDogs {
if d.ID == postDog.ID {
postDogCount++
}
}
s.Equal(postDogCount, 1)
})
}