-
Notifications
You must be signed in to change notification settings - Fork 70
/
Copy pathchange_requests_test.go
155 lines (151 loc) · 4.51 KB
/
change_requests_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
package hapi
import (
"context"
"errors"
"net/http"
"net/http/httptest"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestGetChangeRequest(t *testing.T) {
tests := map[string]struct {
request GetChangeRequest
responseStatus int
responseBody string
expectedPath string
expectedResponse *ChangeRequest
withError error
}{
"200 OK": {
request: GetChangeRequest{123},
responseStatus: http.StatusOK,
responseBody: `
{
"action": "EDIT",
"changeId": 123,
"edgeHostnames": [
{
"chinaCdn": {
"isChinaCdn": false
},
"dnsZone": "edgekey.net",
"edgeHostnameId": 112233,
"ipVersionBehavior": "IPV6_IPV4_DUALSTACK",
"map": "a;bcd.akamaiedge.net",
"productId": "DSA",
"recordName": "test123",
"securityType": "ENHANCED-TLS",
"serialNumber": 0,
"slotNumber": 1234,
"ttl": 21600,
"useDefaultMap": true,
"useDefaultTtl": true
}
],
"status": "PENDING",
"statusMessage": "File uploaded and awaiting validation",
"statusUpdateDate": "2023-09-04T09:21:38.000+00:00",
"submitDate": "2023-09-04T09:21:38.000+00:00",
"submitter": "nobody",
"submitterEmail": "[email protected]"
}
`,
expectedPath: "/hapi/v1/change-requests/123",
expectedResponse: &ChangeRequest{
Action: "EDIT",
ChangeID: 123,
EdgeHostnames: []EdgeHostname{
{
EdgeHostnameID: 112233,
RecordName: "test123",
DNSZone: "edgekey.net",
SecurityType: "ENHANCED-TLS",
UseDefaultTTL: true,
UseDefaultMap: true,
TTL: 21600,
Map: "a;bcd.akamaiedge.net",
SlotNumber: 1234,
IPVersionBehavior: "IPV6_IPV4_DUALSTACK",
Comments: "",
ChinaCDN: ChinaCDN{
IsChinaCDN: false,
},
ProductId: "DSA",
SerialNumber: 0,
},
},
Status: "PENDING",
StatusMessage: "File uploaded and awaiting validation",
StatusUpdateDate: "2023-09-04T09:21:38.000+00:00",
SubmitDate: "2023-09-04T09:21:38.000+00:00",
Submitter: "nobody",
SubmitterEmail: "[email protected]",
},
},
"403 Access Denied to Edge Hostname": {
request: GetChangeRequest{234},
responseStatus: http.StatusForbidden,
responseBody: `
{
"type": "/hapi/problems/access-denied-to-edge-hostname",
"title": "Access Denied to Edge Hostname",
"status": 403,
"detail": "You do not have access to this edge hostname",
"instance": "/hapi/error-instances/7a2c1b84-fe90-40f2-8391-aaaaaaaaaa",
"requestInstance": "http://cloud.akamaiapis.net/hapi/open/v1/change-requests/234#aaaaaaa",
"method": "GET",
"requestTime": "2023-09-04T09:26:16.561878149Z",
"errors": []
}`,
expectedPath: "/hapi/v1/change-requests/234",
withError: &Error{
Type: "/hapi/problems/access-denied-to-edge-hostname",
Title: "Access Denied to Edge Hostname",
Status: 403,
Detail: "You do not have access to this edge hostname",
Instance: "/hapi/error-instances/7a2c1b84-fe90-40f2-8391-aaaaaaaaaa",
RequestInstance: "http://cloud.akamaiapis.net/hapi/open/v1/change-requests/234#aaaaaaa",
Method: "GET",
RequestTime: "2023-09-04T09:26:16.561878149Z",
},
},
"500 internal server error": {
request: GetChangeRequest{123},
responseStatus: http.StatusInternalServerError,
responseBody: `
{
"type": "internal_error",
"title": "Internal Server Error",
"detail": "Error deleting activation",
"status": 500
}`,
expectedPath: "/hapi/v1/change-requests/123",
withError: &Error{
Type: "internal_error",
Title: "Internal Server Error",
Detail: "Error deleting activation",
Status: http.StatusInternalServerError,
},
},
}
for name, test := range tests {
t.Run(name, func(t *testing.T) {
mockServer := httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
assert.Equal(t, test.expectedPath, r.URL.String())
assert.Equal(t, http.MethodGet, r.Method)
w.WriteHeader(test.responseStatus)
_, err := w.Write([]byte(test.responseBody))
assert.NoError(t, err)
}))
client := mockAPIClient(t, mockServer)
result, err := client.GetChangeRequest(context.Background(), test.request)
if test.withError != nil {
assert.True(t, errors.Is(err, test.withError), "want: %s; got: %s", test.withError, err)
return
}
require.NoError(t, err)
assert.Equal(t, test.expectedResponse, result)
})
}
}