github.com/akamai/AkamaiOPEN-edgegrid-golang/v8@v8.1.0/pkg/imaging/policyset_test.go (about)

     1  package imaging
     2  
     3  import (
     4  	"context"
     5  	"errors"
     6  	"io/ioutil"
     7  	"net/http"
     8  	"net/http/httptest"
     9  	"testing"
    10  
    11  	"github.com/stretchr/testify/assert"
    12  	"github.com/stretchr/testify/require"
    13  )
    14  
    15  func TestListPolicySets(t *testing.T) {
    16  	tests := map[string]struct {
    17  		params           ListPolicySetsRequest
    18  		responseStatus   int
    19  		responseBody     string
    20  		expectedPath     string
    21  		expectedResponse []PolicySet
    22  		withError        error
    23  	}{
    24  		"200 OK - both networks": {
    25  			params: ListPolicySetsRequest{
    26  				ContractID: "3-WNKXX1",
    27  			},
    28  			responseStatus: http.StatusOK,
    29  			responseBody: `
    30  [
    31      {
    32          "name": "terraform_beta_v2",
    33          "id": "570e9090-5dbe-11ec-8a0a-71665789c1d8",
    34          "type": "IMAGE",
    35          "region": "US",
    36          "properties": [
    37              "jsmith_sqa2"
    38          ],
    39          "user": "jsmith",
    40          "lastModified": "2021-12-15 15:47:42+0000"
    41      },
    42      {
    43          "name": "my_example_token",
    44          "id": "57c73ae0-5204-11ec-8a0a-71665789c1d8",
    45          "type": "IMAGE",
    46          "region": "US",
    47          "properties": [],
    48          "user": "jsmith",
    49          "lastModified": "2021-11-30 17:38:35+0000"
    50      },
    51      {
    52          "name": "terraform_demo-1104268",
    53          "id": "terraform_demo-1104268",
    54          "type": "IMAGE",
    55          "region": "US",
    56          "properties": [
    57              "jsmith_sqa2"
    58          ],
    59          "user": "System",
    60          "lastModified": "2021-12-15 15:51:54+0000"
    61      }
    62  ]`,
    63  			expectedPath: "/imaging/v2/policysets/",
    64  			expectedResponse: []PolicySet{
    65  				{
    66  					Name:         "terraform_beta_v2",
    67  					ID:           "570e9090-5dbe-11ec-8a0a-71665789c1d8",
    68  					Type:         "IMAGE",
    69  					Region:       "US",
    70  					Properties:   []string{"jsmith_sqa2"},
    71  					User:         "jsmith",
    72  					LastModified: "2021-12-15 15:47:42+0000",
    73  				},
    74  				{
    75  					Name:         "my_example_token",
    76  					ID:           "57c73ae0-5204-11ec-8a0a-71665789c1d8",
    77  					Type:         "IMAGE",
    78  					Region:       "US",
    79  					Properties:   []string{},
    80  					User:         "jsmith",
    81  					LastModified: "2021-11-30 17:38:35+0000",
    82  				},
    83  				{
    84  					Name:         "terraform_demo-1104268",
    85  					ID:           "terraform_demo-1104268",
    86  					Type:         "IMAGE",
    87  					Region:       "US",
    88  					Properties:   []string{"jsmith_sqa2"},
    89  					User:         "System",
    90  					LastModified: "2021-12-15 15:51:54+0000",
    91  				},
    92  			},
    93  		},
    94  		"200 OK - staging network": {
    95  			params: ListPolicySetsRequest{
    96  				ContractID: "3-WNKXX1",
    97  				Network:    NetworkStaging,
    98  			},
    99  			responseStatus: http.StatusOK,
   100  			responseBody: `
   101  [
   102      {
   103          "name": "terraform_beta_v2",
   104          "id": "570e9090-5dbe-11ec-8a0a-71665789c1d8",
   105          "type": "IMAGE",
   106          "region": "US",
   107          "properties": [
   108              "jsmith_sqa2"
   109          ],
   110          "user": "jsmith",
   111          "lastModified": "2021-12-15 15:47:42+0000"
   112      },
   113      {
   114          "name": "terraform_demo-1104268",
   115          "id": "terraform_demo-1104268",
   116          "type": "IMAGE",
   117          "region": "US",
   118          "properties": [
   119              "jsmith_sqa2"
   120          ],
   121          "user": "System",
   122          "lastModified": "2021-12-15 15:51:54+0000"
   123      }
   124  ]`,
   125  			expectedPath: "/imaging/v2/network/staging/policysets/",
   126  			expectedResponse: []PolicySet{
   127  				{
   128  					Name:         "terraform_beta_v2",
   129  					ID:           "570e9090-5dbe-11ec-8a0a-71665789c1d8",
   130  					Type:         "IMAGE",
   131  					Region:       "US",
   132  					Properties:   []string{"jsmith_sqa2"},
   133  					User:         "jsmith",
   134  					LastModified: "2021-12-15 15:47:42+0000",
   135  				},
   136  				{
   137  					Name:         "terraform_demo-1104268",
   138  					ID:           "terraform_demo-1104268",
   139  					Type:         "IMAGE",
   140  					Region:       "US",
   141  					Properties:   []string{"jsmith_sqa2"},
   142  					User:         "System",
   143  					LastModified: "2021-12-15 15:51:54+0000",
   144  				},
   145  			},
   146  		},
   147  		"400 Bad request": {
   148  			params: ListPolicySetsRequest{
   149  				ContractID: "3-WNKXX1",
   150  			},
   151  			responseStatus: http.StatusInternalServerError,
   152  			responseBody: `{
   153  "type": "https://problems.luna.akamaiapis.net/image-policy-manager/IVM_1004",
   154  "title": "Bad Request",
   155  "instance": "52a21f40-9861-4d35-95d0-a603c85cb2ad",
   156  "status": 400,
   157  "detail": "A contract must be specified using the ContractID header.",
   158  "problemId": "52a21f40-9861-4d35-95d0-a603c85cb2ad"
   159  }`,
   160  			expectedPath: "/imaging/v2/policysets/",
   161  			withError: &Error{
   162  				Type:      "https://problems.luna.akamaiapis.net/image-policy-manager/IVM_1004",
   163  				Title:     "Bad Request",
   164  				Instance:  "52a21f40-9861-4d35-95d0-a603c85cb2ad",
   165  				Status:    400,
   166  				Detail:    "A contract must be specified using the ContractID header.",
   167  				ProblemID: "52a21f40-9861-4d35-95d0-a603c85cb2ad",
   168  			},
   169  		},
   170  		"401 Not authorized": {
   171  			params: ListPolicySetsRequest{
   172  				ContractID: "3-WNKXX1",
   173  			},
   174  			responseStatus: http.StatusInternalServerError,
   175  			responseBody: `{
   176  "type": "https://problems.luna-dev.akamaiapis.net/-/pep-authn/deny",
   177  "title": "Not authorized",
   178  "status": 401,
   179  "detail": "Inactive client token",
   180  "instance": "https://akaa-mgfkwp3rw4k2whym-eyn4wdjeur5lz37c.luna-dev.akamaiapis.net/imaging/v2/network/staging/policysets/",
   181  "method": "GET",
   182  "serverIp": "104.81.220.242",
   183  "clientIp": "22.22.22.22",
   184  "requestId": "124cc33c",
   185  "requestTime": "2022-01-12T16:53:44Z"
   186  }`,
   187  			expectedPath: "/imaging/v2/policysets/",
   188  			withError: &Error{
   189  				Type:        "https://problems.luna-dev.akamaiapis.net/-/pep-authn/deny",
   190  				Title:       "Not authorized",
   191  				Status:      401,
   192  				Detail:      "Inactive client token",
   193  				Instance:    "https://akaa-mgfkwp3rw4k2whym-eyn4wdjeur5lz37c.luna-dev.akamaiapis.net/imaging/v2/network/staging/policysets/",
   194  				Method:      "GET",
   195  				ServerIP:    "104.81.220.242",
   196  				ClientIP:    "22.22.22.22",
   197  				RequestID:   "124cc33c",
   198  				RequestTime: "2022-01-12T16:53:44Z",
   199  			},
   200  		},
   201  		"403 Forbidden": {
   202  			params: ListPolicySetsRequest{
   203  				ContractID: "3-WNKXX1",
   204  			},
   205  			responseStatus: http.StatusForbidden,
   206  			responseBody: `{
   207      "type": "https://problems.luna-dev.akamaiapis.net/-/pep-authz/deny",
   208      "title": "Forbidden",
   209      "status": 403,
   210      "detail": "The client does not have the grant needed for the request",
   211      "instance": "https://akaa-75xqbs7cot5jts7q-yjgmpc4nakckpt44.luna-dev.akamaiapis.net/imaging/v2/network/staging/policysets/",
   212      "authzRealm": "xlnausb2pov4jvuo.kh6prvchvniqn5zp",
   213      "method": "GET",
   214      "serverIp": "104.81.220.242",
   215      "clientIp": "22.22.22.22",
   216      "requestId": "1254027a",
   217      "requestTime": "2022-01-12T16:56:56Z"
   218  }`,
   219  			expectedPath: "/imaging/v2/policysets/",
   220  			withError: &Error{
   221  				Type:        "https://problems.luna-dev.akamaiapis.net/-/pep-authz/deny",
   222  				Title:       "Forbidden",
   223  				Status:      403,
   224  				Detail:      "The client does not have the grant needed for the request",
   225  				Instance:    "https://akaa-75xqbs7cot5jts7q-yjgmpc4nakckpt44.luna-dev.akamaiapis.net/imaging/v2/network/staging/policysets/",
   226  				AuthzRealm:  "xlnausb2pov4jvuo.kh6prvchvniqn5zp",
   227  				Method:      "GET",
   228  				ServerIP:    "104.81.220.242",
   229  				ClientIP:    "22.22.22.22",
   230  				RequestID:   "1254027a",
   231  				RequestTime: "2022-01-12T16:56:56Z",
   232  			},
   233  		},
   234  		// 500
   235  		"invalid network": {
   236  			params: ListPolicySetsRequest{
   237  				ContractID: "3-WNKXX1",
   238  				Network:    "foo",
   239  			},
   240  			withError: ErrStructValidation,
   241  		},
   242  		"missing contract": {
   243  			params: ListPolicySetsRequest{
   244  				Network: NetworkProduction,
   245  			},
   246  			withError: ErrStructValidation,
   247  		},
   248  	}
   249  
   250  	for name, test := range tests {
   251  		t.Run(name, func(t *testing.T) {
   252  			mockServer := httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
   253  				assert.Equal(t, test.expectedPath, r.URL.String())
   254  				assert.Equal(t, http.MethodGet, r.Method)
   255  				w.WriteHeader(test.responseStatus)
   256  				_, err := w.Write([]byte(test.responseBody))
   257  				assert.NoError(t, err)
   258  			}))
   259  			client := mockAPIClient(t, mockServer)
   260  			result, err := client.ListPolicySets(context.Background(), test.params)
   261  			if test.withError != nil {
   262  				assert.True(t, errors.Is(err, test.withError), "want: %s; got: %s", test.withError, err)
   263  				return
   264  			}
   265  			require.NoError(t, err)
   266  			assert.Equal(t, test.expectedResponse, result)
   267  		})
   268  	}
   269  }
   270  
   271  func TestGetPolicySet(t *testing.T) {
   272  	tests := map[string]struct {
   273  		params           GetPolicySetRequest
   274  		responseStatus   int
   275  		responseBody     string
   276  		expectedPath     string
   277  		expectedResponse *PolicySet
   278  		withError        error
   279  	}{
   280  		"200 OK for both networks": {
   281  			params: GetPolicySetRequest{
   282  				PolicySetID: "570f9090-5dbe-11ec-8a0a-71665789c1d8",
   283  				ContractID:  "3-WNKXX1",
   284  			},
   285  			responseStatus: http.StatusOK,
   286  			responseBody: `{
   287  				"id": "570f9090-5dbe-11ec-8a0a-71665789c1d8",
   288  				"name": "terraform_beta_v2",
   289  				"region": "US",
   290  				"type": "IMAGE",
   291  				"properties": [
   292  					"jsmith_sqa2"
   293  				],
   294  				"user": "jsmith",
   295  				"lastModified": "2021-12-15 15:47:42+0000"
   296  			}`,
   297  			expectedPath: "/imaging/v2/policysets/570f9090-5dbe-11ec-8a0a-71665789c1d8",
   298  			expectedResponse: &PolicySet{
   299  				ID:           "570f9090-5dbe-11ec-8a0a-71665789c1d8",
   300  				Name:         "terraform_beta_v2",
   301  				Region:       "US",
   302  				Type:         "IMAGE",
   303  				Properties:   []string{"jsmith_sqa2"},
   304  				User:         "jsmith",
   305  				LastModified: "2021-12-15 15:47:42+0000",
   306  			},
   307  		},
   308  		"200 OK for both staging network": {
   309  			params: GetPolicySetRequest{
   310  				PolicySetID: "570f9090-5dbe-11ec-8a0a-71665789c1d8",
   311  				ContractID:  "3-WNKXX1",
   312  				Network:     NetworkStaging,
   313  			},
   314  			responseStatus: http.StatusOK,
   315  			responseBody: `{
   316  				"id": "570f9090-5dbe-11ec-8a0a-71665789c1d8",
   317  				"name": "terraform_beta_v2",
   318  				"region": "US",
   319  				"type": "IMAGE",
   320  				"properties": [
   321  					"jsmith_sqa2"
   322  				],
   323  				"user": "jsmith",
   324  				"lastModified": "2021-12-15 15:47:42+0000"
   325  			}`,
   326  			expectedPath: "/imaging/v2/network/staging/policysets/570f9090-5dbe-11ec-8a0a-71665789c1d8",
   327  			expectedResponse: &PolicySet{
   328  				ID:           "570f9090-5dbe-11ec-8a0a-71665789c1d8",
   329  				Name:         "terraform_beta_v2",
   330  				Region:       "US",
   331  				Type:         "IMAGE",
   332  				Properties:   []string{"jsmith_sqa2"},
   333  				User:         "jsmith",
   334  				LastModified: "2021-12-15 15:47:42+0000",
   335  			},
   336  		},
   337  		"404 Not found": {
   338  			params: GetPolicySetRequest{
   339  				PolicySetID: "570f9090-5dbe-11ec-8a0a",
   340  				ContractID:  "3-WNKXX1",
   341  			},
   342  			responseStatus: http.StatusNotFound,
   343  			responseBody: `
   344  {
   345      "type": "https://problems.luna.akamaiapis.net/image-policy-manager/IVM_9000",
   346      "title": "Not Found",
   347      "instance": "21bde25b-c9a5-4987-b1d5-0c3b92f77b2e",
   348      "status": 404,
   349      "detail": "Policy set does not exist.",
   350      "extensionFields": {
   351          "requestId": "5f94ea1284bf1800"
   352      },
   353      "problemId": "21bde25b-c9a5-4987-b1d5-0c3b92f77b2e",
   354      "requestId": "5f94ea1284bf1800"
   355  }`,
   356  			expectedPath: "/imaging/v2/policysets/570f9090-5dbe-11ec-8a0a",
   357  			withError: &Error{
   358  				Type:     "https://problems.luna.akamaiapis.net/image-policy-manager/IVM_9000",
   359  				Title:    "Not Found",
   360  				Instance: "21bde25b-c9a5-4987-b1d5-0c3b92f77b2e",
   361  				Status:   404,
   362  				Detail:   "Policy set does not exist.",
   363  				ExtensionFields: map[string]string{
   364  					"requestId": "5f94ea1284bf1800",
   365  				},
   366  				ProblemID: "21bde25b-c9a5-4987-b1d5-0c3b92f77b2e",
   367  				RequestID: "5f94ea1284bf1800",
   368  			},
   369  		},
   370  		// 500
   371  		"missing Policy set Id": {
   372  			params: GetPolicySetRequest{
   373  				ContractID: "3-WNKXX1",
   374  				Network:    NetworkProduction,
   375  			},
   376  			withError: ErrStructValidation,
   377  		},
   378  		"invalid network": {
   379  			params: GetPolicySetRequest{
   380  				PolicySetID: "570f9090-5dbe-11ec-8a0a-71665789c1d8",
   381  				ContractID:  "3-WNKXX1",
   382  				Network:     "foo",
   383  			},
   384  			withError: ErrStructValidation,
   385  		},
   386  		"missing contract": {
   387  			params: GetPolicySetRequest{
   388  				PolicySetID: "570f9090-5dbe-11ec-8a0a-71665789c1d8",
   389  				Network:     NetworkProduction,
   390  			},
   391  			withError: ErrStructValidation,
   392  		},
   393  	}
   394  
   395  	for name, test := range tests {
   396  		t.Run(name, func(t *testing.T) {
   397  			mockServer := httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
   398  				assert.Equal(t, test.expectedPath, r.URL.String())
   399  				assert.Equal(t, http.MethodGet, r.Method)
   400  				w.WriteHeader(test.responseStatus)
   401  				_, err := w.Write([]byte(test.responseBody))
   402  				assert.NoError(t, err)
   403  			}))
   404  			client := mockAPIClient(t, mockServer)
   405  			result, err := client.GetPolicySet(context.Background(), test.params)
   406  			if test.withError != nil {
   407  				assert.True(t, errors.Is(err, test.withError), "want: %s; got: %s", test.withError, err)
   408  				return
   409  			}
   410  			require.NoError(t, err)
   411  			assert.Equal(t, test.expectedResponse, result)
   412  		})
   413  	}
   414  }
   415  
   416  func TestCreatePolicySet(t *testing.T) {
   417  	tests := map[string]struct {
   418  		params              CreatePolicySetRequest
   419  		expectedRequestBody string
   420  		responseStatus      int
   421  		responseBody        string
   422  		expectedPath        string
   423  		expectedResponse    *PolicySet
   424  		withError           error
   425  	}{
   426  		"201 created": {
   427  			params: CreatePolicySetRequest{
   428  				ContractID: "3-WNKXX1",
   429  				CreatePolicySet: CreatePolicySet{
   430  					Name:   "my_example_token",
   431  					Type:   "IMAGE",
   432  					Region: "US",
   433  				},
   434  			},
   435  			expectedRequestBody: `{"name":"my_example_token","region":"US","type":"IMAGE"}`,
   436  			responseStatus:      http.StatusCreated,
   437  			responseBody: `{
   438  				"name": "my_example_token",
   439  				"id": "29467ef0-751d-11ec-7a0a-71665789c1d8",
   440  				"type": "IMAGE",
   441  				"region": "US",
   442  				"properties": [],
   443  				"user": "ftzgvvigljhoq5ia",
   444  				"lastModified": "2022-01-14 09:34:25+0000"
   445  			}`,
   446  			expectedPath: "/imaging/v2/policysets/",
   447  			expectedResponse: &PolicySet{
   448  				Name:         "my_example_token",
   449  				ID:           "29467ef0-751d-11ec-7a0a-71665789c1d8",
   450  				Type:         "IMAGE",
   451  				Region:       "US",
   452  				Properties:   []string{},
   453  				User:         "ftzgvvigljhoq5ia",
   454  				LastModified: "2022-01-14 09:34:25+0000",
   455  			},
   456  		},
   457  		"201 created with default policy": {
   458  			params: CreatePolicySetRequest{
   459  				ContractID: "3-WNKXX1",
   460  				CreatePolicySet: CreatePolicySet{
   461  					Name:   "my_example_token",
   462  					Type:   "IMAGE",
   463  					Region: "US",
   464  					DefaultPolicy: &PolicyInputVideo{
   465  						Output: &OutputVideo{
   466  							PerceptualQuality: &OutputVideoPerceptualQualityVariableInline{
   467  								Value: OutputVideoPerceptualQualityPtr(OutputVideoPerceptualQualityMediumHigh),
   468  							},
   469  						},
   470  					},
   471  				},
   472  			},
   473  			expectedRequestBody: `{"name":"my_example_token","region":"US","type":"IMAGE","defaultPolicy":{"output":{"perceptualQuality":"mediumHigh"}}}`,
   474  			responseStatus:      http.StatusCreated,
   475  			responseBody: `{
   476  				"name": "my_example_token",
   477  				"id": "29467ef0-751d-11ec-7a0a-71665789c1d8",
   478  				"type": "IMAGE",
   479  				"region": "US",
   480  				"properties": [],
   481  				"user": "ftzgvvigljhoq5ia",
   482  				"lastModified": "2022-01-14 09:34:25+0000"
   483  			}`,
   484  			expectedPath: "/imaging/v2/policysets/",
   485  			expectedResponse: &PolicySet{
   486  				Name:         "my_example_token",
   487  				ID:           "29467ef0-751d-11ec-7a0a-71665789c1d8",
   488  				Type:         "IMAGE",
   489  				Region:       "US",
   490  				Properties:   []string{},
   491  				User:         "ftzgvvigljhoq5ia",
   492  				LastModified: "2022-01-14 09:34:25+0000",
   493  			},
   494  		},
   495  		"400 Bad request": {
   496  			params: CreatePolicySetRequest{
   497  				ContractID: "3-WNKXX1",
   498  				CreatePolicySet: CreatePolicySet{
   499  					Name:   "my_example_token",
   500  					Type:   "IMAGE",
   501  					Region: "US",
   502  				},
   503  			},
   504  			responseStatus: http.StatusBadRequest,
   505  			responseBody: `
   506  				{
   507  					"type": "https://problems.luna.akamaiapis.net/image-policy-manager/IVM_1004",
   508  					"title": "Bad Request",
   509  					"instance": "5ea0274b-2322-4a0a-92ee-fabaa5a84d41",
   510  					"status": 400,
   511  					"detail": "A contract must be specified using the ContractID header.",
   512  					"problemId": "5ea0274b-2322-4a0a-92ee-fabaa5a84d41"
   513  				}`,
   514  			expectedPath: "/imaging/v2/policysets/",
   515  			withError: &Error{
   516  				Type:      "https://problems.luna.akamaiapis.net/image-policy-manager/IVM_1004",
   517  				Title:     "Bad Request",
   518  				Instance:  "5ea0274b-2322-4a0a-92ee-fabaa5a84d41",
   519  				Status:    400,
   520  				Detail:    "A contract must be specified using the ContractID header.",
   521  				ProblemID: "5ea0274b-2322-4a0a-92ee-fabaa5a84d41",
   522  			},
   523  		},
   524  		"missing Policy set Id": {
   525  			params: CreatePolicySetRequest{
   526  				ContractID: "3-WNKXX1",
   527  			},
   528  			withError: ErrStructValidation,
   529  		},
   530  		"missing contract": {
   531  			params: CreatePolicySetRequest{
   532  				CreatePolicySet: CreatePolicySet{
   533  					Name:   "my_example_token",
   534  					Type:   "IMAGE",
   535  					Region: "US",
   536  				},
   537  			},
   538  			withError: ErrStructValidation,
   539  		},
   540  		"missing name": {
   541  			params: CreatePolicySetRequest{
   542  				ContractID: "3-WNKXX1",
   543  				CreatePolicySet: CreatePolicySet{
   544  					Type:   "IMAGE",
   545  					Region: "US",
   546  				},
   547  			},
   548  			withError: ErrStructValidation,
   549  		},
   550  		"missing type": {
   551  			params: CreatePolicySetRequest{
   552  				ContractID: "3-WNKXX1",
   553  				CreatePolicySet: CreatePolicySet{
   554  					Name:   "my_example_token",
   555  					Region: "US",
   556  				},
   557  			},
   558  			withError: ErrStructValidation,
   559  		},
   560  		"invalid type": {
   561  			params: CreatePolicySetRequest{
   562  				ContractID: "3-WNKXX1",
   563  				CreatePolicySet: CreatePolicySet{
   564  					Name:   "my_example_token",
   565  					Type:   "INVALID",
   566  					Region: "US",
   567  				},
   568  			},
   569  			withError: ErrStructValidation,
   570  		},
   571  		"missing region": {
   572  			params: CreatePolicySetRequest{
   573  				ContractID: "3-WNKXX1",
   574  				CreatePolicySet: CreatePolicySet{
   575  					Name: "my_example_token",
   576  					Type: "IMAGE",
   577  				},
   578  			},
   579  			withError: ErrStructValidation,
   580  		},
   581  		"invalid region": {
   582  			params: CreatePolicySetRequest{
   583  				ContractID: "3-WNKXX1",
   584  				CreatePolicySet: CreatePolicySet{
   585  					Name:   "my_example_token",
   586  					Type:   "IMAGE",
   587  					Region: "INVALID",
   588  				},
   589  			},
   590  			withError: ErrStructValidation,
   591  		},
   592  		"validation error": {
   593  			params:    CreatePolicySetRequest{},
   594  			withError: ErrStructValidation,
   595  		},
   596  	}
   597  
   598  	for name, test := range tests {
   599  		t.Run(name, func(t *testing.T) {
   600  			mockServer := httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
   601  				assert.Equal(t, test.expectedPath, r.URL.String())
   602  				assert.Equal(t, http.MethodPost, r.Method)
   603  				w.WriteHeader(test.responseStatus)
   604  				_, err := w.Write([]byte(test.responseBody))
   605  				assert.NoError(t, err)
   606  
   607  				if len(test.expectedRequestBody) > 0 {
   608  					body, err := ioutil.ReadAll(r.Body)
   609  					require.NoError(t, err)
   610  					assert.Equal(t, test.expectedRequestBody, string(body))
   611  				}
   612  			}))
   613  			client := mockAPIClient(t, mockServer)
   614  			result, err := client.CreatePolicySet(context.Background(), test.params)
   615  			if test.withError != nil {
   616  				assert.True(t, errors.Is(err, test.withError), "want: %s; got: %s", test.withError, err)
   617  				return
   618  			}
   619  			require.NoError(t, err)
   620  			assert.Equal(t, test.expectedResponse, result)
   621  		})
   622  	}
   623  }
   624  
   625  func TestUpdatePolicySet(t *testing.T) {
   626  	tests := map[string]struct {
   627  		params              UpdatePolicySetRequest
   628  		expectedRequestBody string
   629  		responseStatus      int
   630  		responseBody        string
   631  		expectedPath        string
   632  		expectedResponse    *PolicySet
   633  		withError           error
   634  	}{
   635  		"200 updated": {
   636  			params: UpdatePolicySetRequest{
   637  				PolicySetID: "570f9090-5dbe-11ec-8a0a-71665789c1d8",
   638  				ContractID:  "3-WNKXX1",
   639  				UpdatePolicySet: UpdatePolicySet{
   640  					Name:   "my_renamed_token_2",
   641  					Region: "US",
   642  				},
   643  			},
   644  			expectedRequestBody: `{"name":"my_renamed_token_2","region":"US"}`,
   645  			responseStatus:      http.StatusOK,
   646  			responseBody: `{
   647  				"name": "my_renamed_token",
   648  				"id": "1385f880-7477-11ec-8a0a-71665789c1d8",
   649  				"type": "IMAGE",
   650  				"region": "US",
   651  				"properties": [],
   652  				"user": "ftzgvvigljhoq5ib",
   653  				"lastModified": "2022-01-14 10:35:11+0000"
   654  			}`,
   655  			expectedPath: "/imaging/v2/policysets/570f9090-5dbe-11ec-8a0a-71665789c1d8",
   656  			expectedResponse: &PolicySet{
   657  				Name:         "my_renamed_token",
   658  				ID:           "1385f880-7477-11ec-8a0a-71665789c1d8",
   659  				Type:         "IMAGE",
   660  				Region:       "US",
   661  				Properties:   []string{},
   662  				User:         "ftzgvvigljhoq5ib",
   663  				LastModified: "2022-01-14 10:35:11+0000",
   664  			},
   665  		},
   666  		"400 Bad request": {
   667  			params: UpdatePolicySetRequest{
   668  				PolicySetID: "second",
   669  				ContractID:  "3-WNKXX1",
   670  				UpdatePolicySet: UpdatePolicySet{
   671  					Name:   "my_renamed_token",
   672  					Region: "US",
   673  				},
   674  			},
   675  			responseStatus: http.StatusBadRequest,
   676  			responseBody: `
   677  				{
   678   				    "type": "https://problems.luna.akamaiapis.net/image-policy-manager/IVM_9000",
   679  					"title": "Bad Request",
   680  					"instance": "b10b7635-b1ab-4742-bd88-82a6fcdd791a",
   681  					"status": 400,
   682  					"detail": "Policy Set does not exist.",
   683  					"extensionFields": {
   684  						"requestId": "5f9605d49c62d759"
   685  					},
   686  					"problemId": "b10b7635-b1ab-4742-bd88-82a6fcdd791a",
   687  					"requestId": "5f9605d49c62d759"
   688  				}`,
   689  			expectedPath: "/imaging/v2/policysets/second",
   690  			withError: &Error{
   691  				Type:     "https://problems.luna.akamaiapis.net/image-policy-manager/IVM_9000",
   692  				Title:    "Bad Request",
   693  				Instance: "b10b7635-b1ab-4742-bd88-82a6fcdd791a",
   694  				Status:   400,
   695  				Detail:   "Policy Set does not exist.",
   696  				ExtensionFields: map[string]string{
   697  					"requestId": "5f9605d49c62d759",
   698  				},
   699  				ProblemID: "b10b7635-b1ab-4742-bd88-82a6fcdd791a",
   700  				RequestID: "5f9605d49c62d759",
   701  			},
   702  		},
   703  		"500 Internal server error": {
   704  			params: UpdatePolicySetRequest{
   705  				PolicySetID: "second",
   706  				ContractID:  "3-WNKXX1",
   707  				UpdatePolicySet: UpdatePolicySet{
   708  					Name:   "my_renamed_token",
   709  					Region: "EMEA",
   710  				},
   711  			},
   712  			responseStatus: http.StatusBadRequest,
   713  			responseBody: `
   714  				{
   715  					"type": "https://problems.luna.akamaiapis.net/image-policy-manager/IVM_9000",
   716  					"title": "Internal Server Error",
   717  					"instance": "3692e138-f0b7-4479-b12a-48cdee93cf4d",
   718  					"status": 500,
   719  					"detail": "An unexpected error has occurred",
   720  					"extensionFields": {
   721  						"requestId": "5f960786419d5e8d"
   722  					},
   723  					"problemId": "3692e138-f0b7-4479-b12a-48cdee93cf4d",
   724  					"requestId": "5f960786419d5e8d"
   725  				}`,
   726  			expectedPath: "/imaging/v2/policysets/second",
   727  			withError: &Error{
   728  				Type:     "https://problems.luna.akamaiapis.net/image-policy-manager/IVM_9000",
   729  				Title:    "Internal Server Error",
   730  				Instance: "3692e138-f0b7-4479-b12a-48cdee93cf4d",
   731  				Status:   500,
   732  				Detail:   "An unexpected error has occurred",
   733  				ExtensionFields: map[string]string{
   734  					"requestId": "5f960786419d5e8d",
   735  				},
   736  				ProblemID: "3692e138-f0b7-4479-b12a-48cdee93cf4d",
   737  				RequestID: "5f960786419d5e8d",
   738  			},
   739  		},
   740  		"missing Policy set Id": {
   741  			params: UpdatePolicySetRequest{
   742  				ContractID: "3-WNKXX1",
   743  			},
   744  			withError: ErrStructValidation,
   745  		},
   746  		"missing contract": {
   747  			params: UpdatePolicySetRequest{
   748  				UpdatePolicySet: UpdatePolicySet{
   749  					Name:   "my_example_token",
   750  					Region: "US",
   751  				},
   752  			},
   753  			withError: ErrStructValidation,
   754  		},
   755  		"missing name": {
   756  			params: UpdatePolicySetRequest{
   757  				ContractID: "3-WNKXX1",
   758  				UpdatePolicySet: UpdatePolicySet{
   759  					Region: "US",
   760  				},
   761  			},
   762  			withError: ErrStructValidation,
   763  		},
   764  		"missing region": {
   765  			params: UpdatePolicySetRequest{
   766  				ContractID: "3-WNKXX1",
   767  				UpdatePolicySet: UpdatePolicySet{
   768  					Name: "my_example_token",
   769  				},
   770  			},
   771  			withError: ErrStructValidation,
   772  		},
   773  		"invalid region": {
   774  			params: UpdatePolicySetRequest{
   775  				ContractID: "3-WNKXX1",
   776  				UpdatePolicySet: UpdatePolicySet{
   777  					Name:   "my_example_token",
   778  					Region: "INVALID",
   779  				},
   780  			},
   781  			withError: ErrStructValidation,
   782  		},
   783  		"validation error": {
   784  			params:    UpdatePolicySetRequest{},
   785  			withError: ErrStructValidation,
   786  		},
   787  	}
   788  	for name, test := range tests {
   789  		t.Run(name, func(t *testing.T) {
   790  			mockServer := httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
   791  				assert.Equal(t, test.expectedPath, r.URL.String())
   792  				assert.Equal(t, http.MethodPut, r.Method)
   793  				w.WriteHeader(test.responseStatus)
   794  				_, err := w.Write([]byte(test.responseBody))
   795  				assert.NoError(t, err)
   796  
   797  				if len(test.expectedRequestBody) > 0 {
   798  					body, err := ioutil.ReadAll(r.Body)
   799  					require.NoError(t, err)
   800  					assert.Equal(t, test.expectedRequestBody, string(body))
   801  				}
   802  			}))
   803  			client := mockAPIClient(t, mockServer)
   804  			result, err := client.UpdatePolicySet(context.Background(), test.params)
   805  			if test.withError != nil {
   806  				assert.True(t, errors.Is(err, test.withError), "want: %s; got: %s", test.withError, err)
   807  				return
   808  			}
   809  			require.NoError(t, err)
   810  			assert.Equal(t, test.expectedResponse, result)
   811  		})
   812  	}
   813  }
   814  
   815  func TestDeletePolicySet(t *testing.T) {
   816  	tests := map[string]struct {
   817  		params              DeletePolicySetRequest
   818  		expectedRequestBody string
   819  		responseStatus      int
   820  		expectedPath        string
   821  		responseBody        string
   822  		withError           error
   823  	}{
   824  		"204 no content (deleted)": {
   825  			params: DeletePolicySetRequest{
   826  				PolicySetID: "570f9090-5dbe-11ec-8a0a-71665789c1d8",
   827  				ContractID:  "3-WNKXX1",
   828  			},
   829  			responseStatus: http.StatusNoContent,
   830  			expectedPath:   "/imaging/v2/policysets/570f9090-5dbe-11ec-8a0a-71665789c1d8",
   831  		},
   832  		"400 Bad request": {
   833  			params: DeletePolicySetRequest{
   834  				PolicySetID: "second",
   835  				ContractID:  "3-WNKXX1",
   836  			},
   837  			responseStatus: http.StatusBadRequest,
   838  			responseBody: `
   839  				{
   840  					"type": "https://problems.luna.akamaiapis.net/image-policy-manager/IVM_1004",
   841  					"title": "Bad Request",
   842  					"instance": "1a7387db-7056-4ae9-8cdf-5f23e0645487",
   843  					"status": 400,
   844  					"detail": "A contract must be specified using the ContractID header.",
   845  					"problemId": "1a7387db-7056-4ae9-8cdf-5f23e0645487"
   846  				}`,
   847  			expectedPath: "/imaging/v2/policysets/second",
   848  			withError: &Error{
   849  				Type:      "https://problems.luna.akamaiapis.net/image-policy-manager/IVM_1004",
   850  				Title:     "Bad Request",
   851  				Instance:  "1a7387db-7056-4ae9-8cdf-5f23e0645487",
   852  				Status:    400,
   853  				Detail:    "A contract must be specified using the ContractID header.",
   854  				ProblemID: "1a7387db-7056-4ae9-8cdf-5f23e0645487",
   855  			},
   856  		},
   857  		"401 Not authorized": {
   858  			params: DeletePolicySetRequest{
   859  				PolicySetID: "second",
   860  				ContractID:  "3-WNKAA1",
   861  			},
   862  			responseStatus: http.StatusBadRequest,
   863  			responseBody: `
   864  				{
   865  					"type": "https://problems.luna-dev.akamaiapis.net/-/pep-authn/deny",
   866  					"title": "Not authorized",
   867  					"status": 401,
   868  					"detail": "Inactive client token",
   869  					"instance": "https://akaa-p3wvjp6bqtotgpjh-fbk2vczjtq7b5l6a.luna-dev.akamaiapis.net/imaging/v2/policysets/058d9da0-7477-11ec-8a0a-71665789c1d8",
   870  					"method": "DELETE",
   871  					"serverIp": "104.81.220.242",
   872  					"clientIp": "22.22.22.22",
   873  					"requestId": "981a7be",
   874  					"requestTime": "2022-01-14T09:22:54Z"
   875  				}`,
   876  			expectedPath: "/imaging/v2/policysets/second",
   877  			withError: &Error{
   878  				Type:        "https://problems.luna-dev.akamaiapis.net/-/pep-authn/deny",
   879  				Title:       "Not authorized",
   880  				Status:      401,
   881  				Detail:      "Inactive client token",
   882  				Instance:    "https://akaa-p3wvjp6bqtotgpjh-fbk2vczjtq7b5l6a.luna-dev.akamaiapis.net/imaging/v2/policysets/058d9da0-7477-11ec-8a0a-71665789c1d8",
   883  				Method:      "DELETE",
   884  				ServerIP:    "104.81.220.242",
   885  				ClientIP:    "22.22.22.22",
   886  				RequestID:   "981a7be",
   887  				RequestTime: "2022-01-14T09:22:54Z",
   888  			},
   889  		},
   890  		"404 Not found": {
   891  			params: DeletePolicySetRequest{
   892  				PolicySetID: "second",
   893  				ContractID:  "3-WNKXX1",
   894  			},
   895  			responseStatus: http.StatusNotFound,
   896  			responseBody: `
   897  				{
   898  					"type": "https://problems.luna.akamaiapis.net/image-policy-manager/IVM_3001",
   899  					"title": "Not Found",
   900  					"instance": "3da0fcf0-4fb9-4b80-986c-4f9993436189",
   901  					"status": 404,
   902  					"detail": "That policy set does not exist.",
   903  					"problemId": "3da0fcf0-4fb9-4b80-986c-4f9993436189"
   904  				}`,
   905  			expectedPath: "/imaging/v2/policysets/second",
   906  			withError: &Error{
   907  				Type:      "https://problems.luna.akamaiapis.net/image-policy-manager/IVM_3001",
   908  				Title:     "Not Found",
   909  				Instance:  "3da0fcf0-4fb9-4b80-986c-4f9993436189",
   910  				Status:    404,
   911  				Detail:    "That policy set does not exist.",
   912  				ProblemID: "3da0fcf0-4fb9-4b80-986c-4f9993436189",
   913  			},
   914  		},
   915  		// 500
   916  		"missing Policy set Id": {
   917  			params: DeletePolicySetRequest{
   918  				ContractID: "3-WNKXX1",
   919  			},
   920  			withError: ErrStructValidation,
   921  		},
   922  		"missing contract": {
   923  			params: DeletePolicySetRequest{
   924  				PolicySetID: "570f9090-5dbe-11ec-8a0a-71665789c1d8",
   925  			},
   926  			withError: ErrStructValidation,
   927  		},
   928  		"validation error": {
   929  			params:    DeletePolicySetRequest{},
   930  			withError: ErrStructValidation,
   931  		},
   932  	}
   933  	for name, test := range tests {
   934  		t.Run(name, func(t *testing.T) {
   935  			mockServer := httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
   936  				assert.Equal(t, test.expectedPath, r.URL.String())
   937  				assert.Equal(t, http.MethodDelete, r.Method)
   938  				w.WriteHeader(test.responseStatus)
   939  				_, err := w.Write([]byte(test.responseBody))
   940  				assert.NoError(t, err)
   941  
   942  				if len(test.expectedRequestBody) > 0 {
   943  					body, err := ioutil.ReadAll(r.Body)
   944  					require.NoError(t, err)
   945  					assert.Equal(t, test.expectedRequestBody, string(body))
   946  				}
   947  			}))
   948  			client := mockAPIClient(t, mockServer)
   949  			err := client.DeletePolicySet(context.Background(), test.params)
   950  			if test.withError != nil {
   951  				assert.True(t, errors.Is(err, test.withError), "want: %s; got: %s", test.withError, err)
   952  				return
   953  			}
   954  			require.NoError(t, err)
   955  		})
   956  	}
   957  }