/
tfe_test.go
150 lines (133 loc) · 4.74 KB
/
tfe_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
package tfe
import (
"bytes"
"encoding/json"
"fmt"
"os"
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
type tfeAPI struct {
ID string `jsonapi:"primary,tfe"`
Name string `jsonapi:"attr,name"`
CreatedAt time.Time `jsonapi:"attr,created-at,iso8601"`
Enalbed bool `jsonapi:"attr,enalbed"`
Emails []string `jsonapi:"attr,emails"`
Status tfeAPIStatus `jsonapi:"attr,status"`
StatusTimestamps tfeAPITimestamps `jsonapi:"attr,status-timestamps"`
DeliveryResponses []tfeAPIDeliveryResponse `jsonapi:"attr,delivery-responses"`
}
type tfeAPIDeliveryResponse struct {
Body string `jsonapi:"attr,body"`
Code int `jsonapi:"attr,code"`
}
type tfeAPIStatus string
type tfeAPITimestamps struct {
QueuedAt time.Time `jsonapi:"attr,queued-at,rfc3339"`
}
const (
tfeAPIStatusNormal tfeAPIStatus = "normal"
)
func Test_unmarshalResponse(t *testing.T) {
t.Run("unmarshal properly formatted json", func(t *testing.T) {
// This structure is intended to include multiple possible fields and
// formats that are valid for JSON:API
data := map[string]interface{}{
"data": map[string]interface{}{
"type": "tfe",
"id": "1",
"attributes": map[string]interface{}{
"name": "terraform",
"created-at": "2016-08-17T08:27:12Z",
"enabled": "true",
"status": tfeAPIStatusNormal,
"emails": []string{"test@hashicorp.com"},
"delivery-responses": []interface{}{
map[string]interface{}{
"body": "<html>",
"code": 200,
},
map[string]interface{}{
"body": "<body>",
"code": 300,
},
},
"status-timestamps": map[string]string{
"queued-at": "2020-03-16T23:15:59+00:00",
},
},
},
}
byteData, errMarshal := json.Marshal(data)
require.NoError(t, errMarshal)
responseBody := bytes.NewReader(byteData)
unmarshalledRequestBody := tfeAPI{}
err := unmarshalResponse(responseBody, &unmarshalledRequestBody)
require.NoError(t, err)
queuedParsedTime, err := time.Parse(time.RFC3339, "2020-03-16T23:15:59+00:00")
require.NoError(t, err)
assert.Equal(t, unmarshalledRequestBody.ID, "1")
assert.Equal(t, unmarshalledRequestBody.Name, "terraform")
assert.Equal(t, unmarshalledRequestBody.Status, tfeAPIStatusNormal)
assert.Equal(t, len(unmarshalledRequestBody.Emails), 1)
assert.Equal(t, unmarshalledRequestBody.Emails[0], "test@hashicorp.com")
assert.Equal(t, unmarshalledRequestBody.StatusTimestamps.QueuedAt, queuedParsedTime)
assert.NotEmpty(t, unmarshalledRequestBody.DeliveryResponses)
assert.Equal(t, len(unmarshalledRequestBody.DeliveryResponses), 2)
assert.Equal(t, unmarshalledRequestBody.DeliveryResponses[0].Body, "<html>")
assert.Equal(t, unmarshalledRequestBody.DeliveryResponses[0].Code, 200)
assert.Equal(t, unmarshalledRequestBody.DeliveryResponses[1].Body, "<body>")
assert.Equal(t, unmarshalledRequestBody.DeliveryResponses[1].Code, 300)
})
t.Run("can only unmarshal Items that are slices", func(t *testing.T) {
responseBody := bytes.NewReader([]byte(""))
malformattedItemStruct := struct {
*Pagination
Items int
}{
Items: 1,
}
err := unmarshalResponse(responseBody, &malformattedItemStruct)
require.Error(t, err)
assert.Equal(t, err, ErrItemsMustBeSlice)
})
t.Run("can only unmarshal a struct", func(t *testing.T) {
payload := "random"
responseBody := bytes.NewReader([]byte(payload))
notStruct := "not a struct"
err := unmarshalResponse(responseBody, notStruct)
assert.Error(t, err)
assert.EqualError(t, err, fmt.Sprintf("%v must be a struct or an io.Writer", notStruct))
})
}
func Test_EncodeQueryParams(t *testing.T) {
t.Run("with no listOptions and therefore no include field defined", func(t *testing.T) {
urlVals := map[string][]string{
"include": {},
}
requestURLquery := encodeQueryParams(urlVals)
assert.Equal(t, requestURLquery, "")
})
t.Run("with listOptions setting multiple include options", func(t *testing.T) {
urlVals := map[string][]string{
"include": {"workspace", "cost_estimate"},
}
requestURLquery := encodeQueryParams(urlVals)
assert.Equal(t, requestURLquery, "include=workspace%2Ccost_estimate")
})
}
func Test_RegistryBasePath(t *testing.T) {
client, err := NewClient(&Config{
Token: "foo",
})
require.NoError(t, err)
t.Run("ensures client creates a request with registry base path", func(t *testing.T) {
path := "/api/registry/some/path/to/resource"
req, err := client.NewRequest("GET", path, nil)
require.NoError(t, err)
expected := os.Getenv("TFE_ADDRESS") + path
assert.Equal(t, req.retryableRequest.URL.String(), expected)
})
}