|
| 1 | +package hapi |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "errors" |
| 6 | + "net/http" |
| 7 | + "net/http/httptest" |
| 8 | + "testing" |
| 9 | + |
| 10 | + "github.com/stretchr/testify/assert" |
| 11 | + "github.com/stretchr/testify/require" |
| 12 | +) |
| 13 | + |
| 14 | +func TestGetChangeRequest(t *testing.T) { |
| 15 | + tests := map[string]struct { |
| 16 | + request GetChangeRequest |
| 17 | + responseStatus int |
| 18 | + responseBody string |
| 19 | + expectedPath string |
| 20 | + expectedResponse *ChangeRequest |
| 21 | + withError error |
| 22 | + }{ |
| 23 | + "200 OK": { |
| 24 | + request: GetChangeRequest{123}, |
| 25 | + responseStatus: http.StatusOK, |
| 26 | + responseBody: ` |
| 27 | +{ |
| 28 | + "action": "EDIT", |
| 29 | + "changeId": 123, |
| 30 | + "edgeHostnames": [ |
| 31 | + { |
| 32 | + "chinaCdn": { |
| 33 | + "isChinaCdn": false |
| 34 | + }, |
| 35 | + "dnsZone": "edgekey.net", |
| 36 | + "edgeHostnameId": 112233, |
| 37 | + "ipVersionBehavior": "IPV6_IPV4_DUALSTACK", |
| 38 | + "map": "a;bcd.akamaiedge.net", |
| 39 | + "productId": "DSA", |
| 40 | + "recordName": "test123", |
| 41 | + "securityType": "ENHANCED-TLS", |
| 42 | + "serialNumber": 0, |
| 43 | + "slotNumber": 1234, |
| 44 | + "ttl": 21600, |
| 45 | + "useDefaultMap": true, |
| 46 | + "useDefaultTtl": true |
| 47 | + } |
| 48 | + ], |
| 49 | + "status": "PENDING", |
| 50 | + "statusMessage": "File uploaded and awaiting validation", |
| 51 | + "statusUpdateDate": "2023-09-04T09:21:38.000+00:00", |
| 52 | + "submitDate": "2023-09-04T09:21:38.000+00:00", |
| 53 | + "submitter": "nobody", |
| 54 | + "submitterEmail": "nobody@nomail-akamai.com" |
| 55 | +} |
| 56 | +`, |
| 57 | + expectedPath: "/hapi/v1/change-requests/123", |
| 58 | + expectedResponse: &ChangeRequest{ |
| 59 | + Action: "EDIT", |
| 60 | + ChangeID: 123, |
| 61 | + EdgeHostnames: []EdgeHostname{ |
| 62 | + { |
| 63 | + EdgeHostnameID: 112233, |
| 64 | + RecordName: "test123", |
| 65 | + DNSZone: "edgekey.net", |
| 66 | + SecurityType: "ENHANCED-TLS", |
| 67 | + UseDefaultTTL: true, |
| 68 | + UseDefaultMap: true, |
| 69 | + TTL: 21600, |
| 70 | + Map: "a;bcd.akamaiedge.net", |
| 71 | + SlotNumber: 1234, |
| 72 | + IPVersionBehavior: "IPV6_IPV4_DUALSTACK", |
| 73 | + Comments: "", |
| 74 | + ChinaCDN: ChinaCDN{ |
| 75 | + IsChinaCDN: false, |
| 76 | + }, |
| 77 | + ProductId: "DSA", |
| 78 | + SerialNumber: 0, |
| 79 | + }, |
| 80 | + }, |
| 81 | + Status: "PENDING", |
| 82 | + StatusMessage: "File uploaded and awaiting validation", |
| 83 | + StatusUpdateDate: "2023-09-04T09:21:38.000+00:00", |
| 84 | + SubmitDate: "2023-09-04T09:21:38.000+00:00", |
| 85 | + Submitter: "nobody", |
| 86 | + SubmitterEmail: "nobody@nomail-akamai.com", |
| 87 | + }, |
| 88 | + }, |
| 89 | + "403 Access Denied to Edge Hostname": { |
| 90 | + request: GetChangeRequest{234}, |
| 91 | + responseStatus: http.StatusForbidden, |
| 92 | + responseBody: ` |
| 93 | +{ |
| 94 | + "type": "/hapi/problems/access-denied-to-edge-hostname", |
| 95 | + "title": "Access Denied to Edge Hostname", |
| 96 | + "status": 403, |
| 97 | + "detail": "You do not have access to this edge hostname", |
| 98 | + "instance": "/hapi/error-instances/7a2c1b84-fe90-40f2-8391-aaaaaaaaaa", |
| 99 | + "requestInstance": "http://cloud.akamaiapis.net/hapi/open/v1/change-requests/234#aaaaaaa", |
| 100 | + "method": "GET", |
| 101 | + "requestTime": "2023-09-04T09:26:16.561878149Z", |
| 102 | + "errors": [] |
| 103 | +}`, |
| 104 | + expectedPath: "/hapi/v1/change-requests/234", |
| 105 | + withError: &Error{ |
| 106 | + Type: "/hapi/problems/access-denied-to-edge-hostname", |
| 107 | + Title: "Access Denied to Edge Hostname", |
| 108 | + Status: 403, |
| 109 | + Detail: "You do not have access to this edge hostname", |
| 110 | + Instance: "/hapi/error-instances/7a2c1b84-fe90-40f2-8391-aaaaaaaaaa", |
| 111 | + RequestInstance: "http://cloud.akamaiapis.net/hapi/open/v1/change-requests/234#aaaaaaa", |
| 112 | + Method: "GET", |
| 113 | + RequestTime: "2023-09-04T09:26:16.561878149Z", |
| 114 | + }, |
| 115 | + }, |
| 116 | + "500 internal server error": { |
| 117 | + request: GetChangeRequest{123}, |
| 118 | + responseStatus: http.StatusInternalServerError, |
| 119 | + responseBody: ` |
| 120 | +{ |
| 121 | + "type": "internal_error", |
| 122 | + "title": "Internal Server Error", |
| 123 | + "detail": "Error deleting activation", |
| 124 | + "status": 500 |
| 125 | +}`, |
| 126 | + expectedPath: "/hapi/v1/change-requests/123", |
| 127 | + withError: &Error{ |
| 128 | + Type: "internal_error", |
| 129 | + Title: "Internal Server Error", |
| 130 | + Detail: "Error deleting activation", |
| 131 | + Status: http.StatusInternalServerError, |
| 132 | + }, |
| 133 | + }, |
| 134 | + } |
| 135 | + |
| 136 | + for name, test := range tests { |
| 137 | + t.Run(name, func(t *testing.T) { |
| 138 | + mockServer := httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 139 | + assert.Equal(t, test.expectedPath, r.URL.String()) |
| 140 | + assert.Equal(t, http.MethodGet, r.Method) |
| 141 | + w.WriteHeader(test.responseStatus) |
| 142 | + _, err := w.Write([]byte(test.responseBody)) |
| 143 | + assert.NoError(t, err) |
| 144 | + })) |
| 145 | + client := mockAPIClient(t, mockServer) |
| 146 | + result, err := client.GetChangeRequest(context.Background(), test.request) |
| 147 | + if test.withError != nil { |
| 148 | + assert.True(t, errors.Is(err, test.withError), "want: %s; got: %s", test.withError, err) |
| 149 | + return |
| 150 | + } |
| 151 | + require.NoError(t, err) |
| 152 | + assert.Equal(t, test.expectedResponse, result) |
| 153 | + }) |
| 154 | + } |
| 155 | +} |
0 commit comments