github.com/akamai/AkamaiOPEN-edgegrid-golang/v8@v8.1.0/pkg/papi/property_test.go (about) 1 package papi 2 3 import ( 4 "context" 5 "errors" 6 "net/http" 7 "net/http/httptest" 8 "testing" 9 10 "github.com/akamai/AkamaiOPEN-edgegrid-golang/v8/pkg/tools" 11 "github.com/stretchr/testify/assert" 12 "github.com/stretchr/testify/require" 13 ) 14 15 func TestPapi_GetProperties(t *testing.T) { 16 tests := map[string]struct { 17 request GetPropertiesRequest 18 responseStatus int 19 responseBody string 20 expectedPath string 21 expectedResponse *GetPropertiesResponse 22 withError error 23 }{ 24 "200 OK": { 25 request: GetPropertiesRequest{ 26 ContractID: "ctr_1-1TJZFW", 27 GroupID: "grp_15166", 28 }, 29 responseStatus: http.StatusOK, 30 responseBody: ` 31 { 32 "properties": { 33 "items": [ 34 { 35 "accountId": "act_1-1TJZFB", 36 "contractId": "ctr_1-1TJZH5", 37 "groupId": "grp_15166", 38 "propertyId": "prp_175780", 39 "propertyName": "example.com", 40 "latestVersion": 2, 41 "stagingVersion": 1, 42 "productId": "prp_175780", 43 "productionVersion": null, 44 "assetId": "aid_101", 45 "note": "Notes about example.com" 46 } 47 ] 48 } 49 }`, 50 expectedPath: "/papi/v1/properties?contractId=ctr_1-1TJZFW&groupId=grp_15166", 51 expectedResponse: &GetPropertiesResponse{ 52 Properties: PropertiesItems{Items: []*Property{ 53 { 54 AccountID: "act_1-1TJZFB", 55 ContractID: "ctr_1-1TJZH5", 56 GroupID: "grp_15166", 57 PropertyID: "prp_175780", 58 ProductID: "prp_175780", 59 PropertyName: "example.com", 60 LatestVersion: 2, 61 StagingVersion: tools.IntPtr(1), 62 ProductionVersion: nil, 63 AssetID: "aid_101", 64 Note: "Notes about example.com", 65 }, 66 }}, 67 }, 68 }, 69 "500 internal server error": { 70 request: GetPropertiesRequest{ 71 ContractID: "ctr_1-1TJZFW", 72 GroupID: "grp_15166", 73 }, 74 responseStatus: http.StatusInternalServerError, 75 responseBody: ` 76 { 77 "type": "internal_error", 78 "title": "Internal Server Error", 79 "detail": "Error fetching properties", 80 "status": 500 81 }`, 82 expectedPath: "/papi/v1/properties?contractId=ctr_1-1TJZFW&groupId=grp_15166", 83 withError: &Error{ 84 Type: "internal_error", 85 Title: "Internal Server Error", 86 Detail: "Error fetching properties", 87 StatusCode: http.StatusInternalServerError, 88 }, 89 }, 90 "validation error": { 91 request: GetPropertiesRequest{ 92 GroupID: "grp_15166", 93 }, 94 responseStatus: http.StatusInternalServerError, 95 withError: ErrStructValidation, 96 }, 97 } 98 99 for name, test := range tests { 100 t.Run(name, func(t *testing.T) { 101 mockServer := httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { 102 assert.Equal(t, test.expectedPath, r.URL.String()) 103 assert.Equal(t, http.MethodGet, r.Method) 104 w.WriteHeader(test.responseStatus) 105 _, err := w.Write([]byte(test.responseBody)) 106 assert.NoError(t, err) 107 })) 108 client := mockAPIClient(t, mockServer) 109 result, err := client.GetProperties(context.Background(), test.request) 110 if test.withError != nil { 111 assert.True(t, errors.Is(err, test.withError), "want: %s; got: %s", test.withError, err) 112 return 113 } 114 require.NoError(t, err) 115 assert.Equal(t, test.expectedResponse, result) 116 }) 117 } 118 } 119 120 func TestPapi_GetProperty(t *testing.T) { 121 tests := map[string]struct { 122 request GetPropertyRequest 123 responseStatus int 124 responseBody string 125 expectedPath string 126 expectedResponse *GetPropertyResponse 127 withError error 128 }{ 129 "200 OK": { 130 request: GetPropertyRequest{ 131 ContractID: "ctr_1-1TJZFW", 132 GroupID: "grp_15166", 133 PropertyID: "prp_175780", 134 }, 135 responseStatus: http.StatusOK, 136 responseBody: ` 137 { 138 "properties": { 139 "items": [ 140 { 141 "accountId": "act_1-1TJZFB", 142 "contractId": "ctr_1-1TJZH5", 143 "groupId": "grp_15166", 144 "propertyId": "prp_175780", 145 "propertyName": "example.com", 146 "latestVersion": 2, 147 "stagingVersion": 1, 148 "productId": "prp_175780", 149 "productionVersion": null, 150 "assetId": "aid_101", 151 "note": "Notes about example.com" 152 } 153 ] 154 } 155 }`, 156 expectedPath: "/papi/v1/properties/prp_175780?contractId=ctr_1-1TJZFW&groupId=grp_15166", 157 expectedResponse: &GetPropertyResponse{ 158 Properties: PropertiesItems{Items: []*Property{ 159 { 160 AccountID: "act_1-1TJZFB", 161 ContractID: "ctr_1-1TJZH5", 162 GroupID: "grp_15166", 163 PropertyID: "prp_175780", 164 ProductID: "prp_175780", 165 PropertyName: "example.com", 166 LatestVersion: 2, 167 StagingVersion: tools.IntPtr(1), 168 ProductionVersion: nil, 169 AssetID: "aid_101", 170 Note: "Notes about example.com", 171 }, 172 }}, 173 Property: &Property{ 174 175 AccountID: "act_1-1TJZFB", 176 ContractID: "ctr_1-1TJZH5", 177 GroupID: "grp_15166", 178 PropertyID: "prp_175780", 179 ProductID: "prp_175780", 180 PropertyName: "example.com", 181 LatestVersion: 2, 182 StagingVersion: tools.IntPtr(1), 183 ProductionVersion: nil, 184 AssetID: "aid_101", 185 Note: "Notes about example.com", 186 }}, 187 }, 188 "Property not found": { 189 request: GetPropertyRequest{ 190 ContractID: "ctr_1-1TJZFW", 191 GroupID: "grp_15166", 192 PropertyID: "prp_175780", 193 }, 194 responseStatus: http.StatusOK, 195 responseBody: ` 196 { 197 "properties": { 198 "items": [ 199 ] 200 } 201 }`, 202 expectedPath: "/papi/v1/properties/prp_175780?contractId=ctr_1-1TJZFW&groupId=grp_15166", 203 withError: ErrNotFound, 204 }, 205 "500 internal server error": { 206 request: GetPropertyRequest{ 207 ContractID: "ctr_1-1TJZFW", 208 GroupID: "grp_15166", 209 PropertyID: "prp_175780", 210 }, 211 responseStatus: http.StatusInternalServerError, 212 responseBody: ` 213 { 214 "type": "internal_error", 215 "title": "Internal Server Error", 216 "detail": "Error fetching properties", 217 "status": 500 218 }`, 219 expectedPath: "/papi/v1/properties/prp_175780?contractId=ctr_1-1TJZFW&groupId=grp_15166", 220 withError: &Error{ 221 Type: "internal_error", 222 Title: "Internal Server Error", 223 Detail: "Error fetching properties", 224 StatusCode: http.StatusInternalServerError, 225 }, 226 }, 227 "validation error": { 228 request: GetPropertyRequest{ 229 ContractID: "ctr_1-1TJZFW", 230 GroupID: "grp_15166", 231 }, 232 withError: ErrStructValidation, 233 }, 234 } 235 236 for name, test := range tests { 237 t.Run(name, func(t *testing.T) { 238 mockServer := httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { 239 assert.Equal(t, test.expectedPath, r.URL.String()) 240 assert.Equal(t, http.MethodGet, r.Method) 241 w.WriteHeader(test.responseStatus) 242 _, err := w.Write([]byte(test.responseBody)) 243 assert.NoError(t, err) 244 })) 245 client := mockAPIClient(t, mockServer) 246 result, err := client.GetProperty(context.Background(), test.request) 247 if test.withError != nil { 248 assert.True(t, errors.Is(err, test.withError), "want: %s; got: %s", test.withError, err) 249 return 250 } 251 require.NoError(t, err) 252 assert.Equal(t, test.expectedResponse, result) 253 }) 254 } 255 } 256 257 func TestPapi_CreateProperty(t *testing.T) { 258 tests := map[string]struct { 259 request CreatePropertyRequest 260 responseStatus int 261 responseBody string 262 expectedPath string 263 expectedResponse *CreatePropertyResponse 264 withError error 265 }{ 266 "201 created": { 267 request: CreatePropertyRequest{ 268 ContractID: "ctr_1-1TJZFW", 269 GroupID: "grp_15166", 270 Property: PropertyCreate{ 271 ProductID: "prd_Alta", 272 PropertyName: "my.new.property.com", 273 CloneFrom: &PropertyCloneFrom{ 274 PropertyID: "prp_1234", 275 Version: 1, 276 }, 277 }, 278 }, 279 responseStatus: http.StatusCreated, 280 responseBody: ` 281 { 282 "propertyLink": "/papi/v1/properties/prp_173137?contractId=ctr_1-1TJZH5&groupId=grp_15225" 283 }`, 284 expectedPath: "/papi/v1/properties?contractId=ctr_1-1TJZFW&groupId=grp_15166", 285 expectedResponse: &CreatePropertyResponse{ 286 PropertyID: "prp_173137", 287 PropertyLink: "/papi/v1/properties/prp_173137?contractId=ctr_1-1TJZH5&groupId=grp_15225", 288 }, 289 }, 290 "500 internal server error": { 291 request: CreatePropertyRequest{ 292 ContractID: "ctr_1-1TJZFW", 293 GroupID: "grp_15166", 294 Property: PropertyCreate{ 295 ProductID: "prd_Alta", 296 PropertyName: "my.new.property.com", 297 }, 298 }, 299 responseStatus: http.StatusInternalServerError, 300 responseBody: ` 301 { 302 "type": "internal_error", 303 "title": "Internal Server Error", 304 "detail": "Error creating property", 305 "status": 500 306 }`, 307 expectedPath: "/papi/v1/properties?contractId=ctr_1-1TJZFW&groupId=grp_15166", 308 withError: &Error{ 309 Type: "internal_error", 310 Title: "Internal Server Error", 311 Detail: "Error creating property", 312 StatusCode: http.StatusInternalServerError, 313 }, 314 }, 315 "validation error": { 316 request: CreatePropertyRequest{ 317 ContractID: "ctr_1-1TJZFW", 318 GroupID: "grp_15166", 319 Property: PropertyCreate{ 320 ProductID: "prd_Alta", 321 }, 322 }, 323 withError: ErrStructValidation, 324 }, 325 } 326 327 for name, test := range tests { 328 t.Run(name, func(t *testing.T) { 329 mockServer := httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { 330 assert.Equal(t, test.expectedPath, r.URL.String()) 331 assert.Equal(t, http.MethodPost, r.Method) 332 w.WriteHeader(test.responseStatus) 333 _, err := w.Write([]byte(test.responseBody)) 334 assert.NoError(t, err) 335 })) 336 client := mockAPIClient(t, mockServer) 337 result, err := client.CreateProperty(context.Background(), test.request) 338 if test.withError != nil { 339 assert.True(t, errors.Is(err, test.withError), "want: %s; got: %s", test.withError, err) 340 return 341 } 342 require.NoError(t, err) 343 assert.Equal(t, test.expectedResponse, result) 344 }) 345 } 346 } 347 348 func TestPapi_RemoveProperty(t *testing.T) { 349 tests := map[string]struct { 350 request RemovePropertyRequest 351 responseStatus int 352 responseBody string 353 expectedPath string 354 expectedResponse *RemovePropertyResponse 355 withError error 356 }{ 357 "200 OK": { 358 request: RemovePropertyRequest{ 359 ContractID: "ctr_1-1TJZFW", 360 GroupID: "grp_15166", 361 PropertyID: "prp_175780", 362 }, 363 responseStatus: http.StatusOK, 364 responseBody: ` 365 { 366 "message": "Deletion Successful." 367 }`, 368 expectedPath: "/papi/v1/properties/prp_175780?contractId=ctr_1-1TJZFW&groupId=grp_15166", 369 expectedResponse: &RemovePropertyResponse{ 370 Message: "Deletion Successful.", 371 }, 372 }, 373 "500 internal server error": { 374 request: RemovePropertyRequest{ 375 ContractID: "ctr_1-1TJZFW", 376 GroupID: "grp_15166", 377 PropertyID: "prp_175780", 378 }, 379 responseStatus: http.StatusInternalServerError, 380 responseBody: ` 381 { 382 "type": "internal_error", 383 "title": "Internal Server Error", 384 "detail": "Error removing property", 385 "status": 500 386 }`, 387 expectedPath: "/papi/v1/properties/prp_175780?contractId=ctr_1-1TJZFW&groupId=grp_15166", 388 withError: &Error{ 389 Type: "internal_error", 390 Title: "Internal Server Error", 391 Detail: "Error removing property", 392 StatusCode: http.StatusInternalServerError, 393 }, 394 }, 395 "validation error": { 396 request: RemovePropertyRequest{ 397 ContractID: "ctr_1-1TJZFW", 398 GroupID: "grp_15166", 399 }, 400 withError: ErrStructValidation, 401 }, 402 } 403 404 for name, test := range tests { 405 t.Run(name, func(t *testing.T) { 406 mockServer := httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { 407 assert.Equal(t, test.expectedPath, r.URL.String()) 408 assert.Equal(t, http.MethodDelete, r.Method) 409 w.WriteHeader(test.responseStatus) 410 _, err := w.Write([]byte(test.responseBody)) 411 assert.NoError(t, err) 412 })) 413 client := mockAPIClient(t, mockServer) 414 result, err := client.RemoveProperty(context.Background(), test.request) 415 if test.withError != nil { 416 assert.True(t, errors.Is(err, test.withError), "want: %s; got: %s", test.withError, err) 417 return 418 } 419 require.NoError(t, err) 420 assert.Equal(t, test.expectedResponse, result) 421 }) 422 } 423 }