github.com/akamai/AkamaiOPEN-edgegrid-golang/v2@v2.17.0/pkg/papi/activation_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/stretchr/testify/assert"
    11  	"github.com/stretchr/testify/require"
    12  )
    13  
    14  func TestPapi_CreateActivation(t *testing.T) {
    15  	tests := map[string]struct {
    16  		request          CreateActivationRequest
    17  		responseStatus   int
    18  		responseBody     string
    19  		expectedPath     string
    20  		expectedResponse *CreateActivationResponse
    21  		withError        error
    22  	}{
    23  		"200 OK": {
    24  			request: CreateActivationRequest{
    25  				PropertyID: "prp_175780",
    26  				ContractID: "ctr_1-1TJZFW",
    27  				GroupID:    "grp_15166",
    28  				Activation: Activation{
    29  					PropertyVersion: 1,
    30  					Network:         ActivationNetworkStaging,
    31  					UseFastFallback: false,
    32  					NotifyEmails: []string{
    33  						"you@example.com",
    34  						"them@example.com",
    35  					},
    36  					AcknowledgeWarnings: []string{"msg_baa4560881774a45b5fd25f5b1eab021d7c40b4f"},
    37  				},
    38  			},
    39  			responseStatus: http.StatusCreated,
    40  			responseBody: `
    41  {
    42  	"activationLink": "/papi/v1/properties/prp_173136/activations/atv_67037?contractId=ctr_1-1TJZFB&groupId=grp_15225"
    43  }`,
    44  			expectedPath: "/papi/v1/properties/prp_175780/activations?contractId=ctr_1-1TJZFW&groupId=grp_15166",
    45  			expectedResponse: &CreateActivationResponse{
    46  				ActivationID:   "atv_67037",
    47  				ActivationLink: "/papi/v1/properties/prp_173136/activations/atv_67037?contractId=ctr_1-1TJZFB&groupId=grp_15225",
    48  			},
    49  		},
    50  		"500 internal server error": {
    51  			request: CreateActivationRequest{
    52  				PropertyID: "prp_175780",
    53  				ContractID: "ctr_1-1TJZFW",
    54  				GroupID:    "grp_15166",
    55  				Activation: Activation{
    56  					PropertyVersion: 1,
    57  					Network:         ActivationNetworkStaging,
    58  					UseFastFallback: false,
    59  					NotifyEmails: []string{
    60  						"you@example.com",
    61  						"them@example.com",
    62  					},
    63  					AcknowledgeWarnings: []string{"msg_baa4560881774a45b5fd25f5b1eab021d7c40b4f"},
    64  				},
    65  			},
    66  			responseStatus: http.StatusInternalServerError,
    67  			responseBody: `
    68  {
    69  	"type": "internal_error",
    70      "title": "Internal Server Error",
    71      "detail": "Error creating activation",
    72      "status": 500
    73  }`,
    74  			expectedPath: "/papi/v1/properties/prp_175780/activations?contractId=ctr_1-1TJZFW&groupId=grp_15166",
    75  			withError: &Error{
    76  				Type:       "internal_error",
    77  				Title:      "Internal Server Error",
    78  				Detail:     "Error creating activation",
    79  				StatusCode: http.StatusInternalServerError,
    80  			},
    81  		},
    82  		"validation error - missing property ID": {
    83  			request: CreateActivationRequest{
    84  				ContractID: "ctr_1-1TJZFW",
    85  				GroupID:    "grp_15166",
    86  				Activation: Activation{
    87  					PropertyVersion: 1,
    88  					Network:         ActivationNetworkStaging,
    89  					UseFastFallback: false,
    90  					NotifyEmails: []string{
    91  						"you@example.com",
    92  						"them@example.com",
    93  					},
    94  					AcknowledgeWarnings: []string{"msg_baa4560881774a45b5fd25f5b1eab021d7c40b4f"},
    95  				},
    96  			},
    97  			withError: ErrStructValidation,
    98  		},
    99  	}
   100  
   101  	for name, test := range tests {
   102  		t.Run(name, func(t *testing.T) {
   103  			mockServer := httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
   104  				assert.Equal(t, test.expectedPath, r.URL.String())
   105  				assert.Equal(t, http.MethodPost, r.Method)
   106  				w.WriteHeader(test.responseStatus)
   107  				_, err := w.Write([]byte(test.responseBody))
   108  				assert.NoError(t, err)
   109  			}))
   110  			client := mockAPIClient(t, mockServer)
   111  			result, err := client.CreateActivation(context.Background(), test.request)
   112  			if test.withError != nil {
   113  				assert.True(t, errors.Is(err, test.withError), "want: %s; got: %s", test.withError, err)
   114  				return
   115  			}
   116  			require.NoError(t, err)
   117  			assert.Equal(t, test.expectedResponse, result)
   118  		})
   119  	}
   120  }
   121  
   122  func TestPapi_GetActivations(t *testing.T) {
   123  	tests := map[string]struct {
   124  		request          GetActivationsRequest
   125  		responseStatus   int
   126  		responseBody     string
   127  		expectedPath     string
   128  		expectedResponse *GetActivationsResponse
   129  		withError        error
   130  	}{
   131  		"200 OK": {
   132  			request: GetActivationsRequest{
   133  				PropertyID: "prp_175780",
   134  				ContractID: "ctr_1-1TJZFW",
   135  				GroupID:    "grp_15166",
   136  			},
   137  			responseStatus: http.StatusOK,
   138  			responseBody: `
   139  {
   140  	"accountId": "act_1-1TJZFB",
   141  	"contractId": "ctr_1-1TJZFW",
   142  	"groupId": "grp_15166",
   143  	"activations": {
   144  		"items": [
   145  			{
   146  				"activationId": "atv_1696985",
   147  				"propertyName": "example.com",
   148  				"propertyId": "prp_173136",
   149  				"propertyVersion": 1,
   150  				"network": "STAGING",
   151  				"activationType": "ACTIVATE",
   152  				"status": "PENDING",
   153  				"submitDate": "2014-03-02T02:22:12Z",
   154  				"updateDate": "2014-03-01T21:12:57Z",
   155  				"note": "Sample activation",
   156  				"fmaActivationState": "steady",
   157  				"notifyEmails": [
   158  					"you@example.com",
   159  					"them@example.com"
   160  				],
   161  				"fallbackInfo": {
   162  					"fastFallbackAttempted": false,
   163  					"fallbackVersion": 10,
   164  					"canFastFallback": true,
   165  					"steadyStateTime": 1506448172,
   166  					"fastFallbackExpirationTime": 1506451772,
   167  					"fastFallbackRecoveryState": null
   168  				}
   169  			}
   170  		]
   171  	}
   172  }`,
   173  			expectedPath: "/papi/v1/properties/prp_175780/activations?contractId=ctr_1-1TJZFW&groupId=grp_15166",
   174  			expectedResponse: &GetActivationsResponse{
   175  				Response: Response{
   176  					AccountID:  "act_1-1TJZFB",
   177  					ContractID: "ctr_1-1TJZFW",
   178  					GroupID:    "grp_15166",
   179  				},
   180  				Activations: ActivationsItems{Items: []*Activation{{
   181  					ActivationID:       "atv_1696985",
   182  					PropertyName:       "example.com",
   183  					PropertyID:         "prp_173136",
   184  					PropertyVersion:    1,
   185  					Network:            ActivationNetworkStaging,
   186  					ActivationType:     ActivationTypeActivate,
   187  					Status:             ActivationStatusPending,
   188  					SubmitDate:         "2014-03-02T02:22:12Z",
   189  					UpdateDate:         "2014-03-01T21:12:57Z",
   190  					Note:               "Sample activation",
   191  					FMAActivationState: "steady",
   192  					NotifyEmails: []string{
   193  						"you@example.com",
   194  						"them@example.com",
   195  					},
   196  					FallbackInfo: &ActivationFallbackInfo{
   197  						FastFallbackAttempted:      false,
   198  						FallbackVersion:            10,
   199  						CanFastFallback:            true,
   200  						SteadyStateTime:            1506448172,
   201  						FastFallbackExpirationTime: 1506451772,
   202  						FastFallbackRecoveryState:  nil,
   203  					},
   204  				}},
   205  				},
   206  			},
   207  		},
   208  		"500 internal server error": {
   209  			request: GetActivationsRequest{
   210  				PropertyID: "prp_175780",
   211  				ContractID: "ctr_1-1TJZFW",
   212  				GroupID:    "grp_15166",
   213  			},
   214  			responseStatus: http.StatusInternalServerError,
   215  			responseBody: `
   216  {
   217  	"type": "internal_error",
   218      "title": "Internal Server Error",
   219      "detail": "Error fetching activation",
   220      "status": 500
   221  }`,
   222  			expectedPath: "/papi/v1/properties/prp_175780/activations?contractId=ctr_1-1TJZFW&groupId=grp_15166",
   223  			withError: &Error{
   224  				Type:       "internal_error",
   225  				Title:      "Internal Server Error",
   226  				Detail:     "Error fetching activation",
   227  				StatusCode: http.StatusInternalServerError,
   228  			},
   229  		},
   230  		"validation error": {
   231  			request: GetActivationsRequest{
   232  				ContractID: "ctr_1-1TJZFW",
   233  				GroupID:    "grp_15166",
   234  			},
   235  			withError: ErrStructValidation,
   236  		},
   237  	}
   238  
   239  	for name, test := range tests {
   240  		t.Run(name, func(t *testing.T) {
   241  			mockServer := httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
   242  				assert.Equal(t, test.expectedPath, r.URL.String())
   243  				assert.Equal(t, http.MethodGet, r.Method)
   244  				w.WriteHeader(test.responseStatus)
   245  				_, err := w.Write([]byte(test.responseBody))
   246  				assert.NoError(t, err)
   247  			}))
   248  			client := mockAPIClient(t, mockServer)
   249  			result, err := client.GetActivations(context.Background(), test.request)
   250  			if test.withError != nil {
   251  				assert.True(t, errors.Is(err, test.withError), "want: %s; got: %s", test.withError, err)
   252  				return
   253  			}
   254  			require.NoError(t, err)
   255  			assert.Equal(t, test.expectedResponse, result)
   256  		})
   257  	}
   258  }
   259  
   260  func TestPapi_GetActivation(t *testing.T) {
   261  	tests := map[string]struct {
   262  		request          GetActivationRequest
   263  		responseStatus   int
   264  		responseBody     string
   265  		expectedPath     string
   266  		expectedResponse *GetActivationResponse
   267  		withError        error
   268  	}{
   269  		"200 OK": {
   270  			request: GetActivationRequest{
   271  				PropertyID:   "prp_175780",
   272  				ActivationID: "atv_1696855",
   273  				ContractID:   "ctr_1-1TJZFW",
   274  				GroupID:      "grp_15166",
   275  			},
   276  			responseStatus: http.StatusOK,
   277  			responseBody: `
   278  {
   279  	"accountId": "act_1-1TJZFB",
   280  	"contractId": "ctr_1-1TJZFW",
   281  	"groupId": "grp_15166",
   282  	"activations": {
   283  		"items": [
   284  			{
   285  				"activationId": "atv_1696985",
   286  				"propertyName": "example.com",
   287  				"propertyId": "prp_173136",
   288  				"propertyVersion": 1,
   289  				"network": "STAGING",
   290  				"activationType": "ACTIVATE",
   291  				"status": "PENDING",
   292  				"submitDate": "2014-03-02T02:22:12Z",
   293  				"updateDate": "2014-03-01T21:12:57Z",
   294  				"note": "Sample activation",
   295  				"fmaActivationState": "steady",
   296  				"notifyEmails": [
   297  					"you@example.com",
   298  					"them@example.com"
   299  				],
   300  				"fallbackInfo": {
   301  					"fastFallbackAttempted": false,
   302  					"fallbackVersion": 10,
   303  					"canFastFallback": true,
   304  					"steadyStateTime": 1506448172,
   305  					"fastFallbackExpirationTime": 1506451772,
   306  					"fastFallbackRecoveryState": null
   307  				}
   308  			}
   309  		]
   310  	}
   311  }`,
   312  			expectedPath: "/papi/v1/properties/prp_175780/activations/atv_1696855?contractId=ctr_1-1TJZFW&groupId=grp_15166",
   313  			expectedResponse: &GetActivationResponse{
   314  				GetActivationsResponse: GetActivationsResponse{
   315  					Response: Response{
   316  						AccountID:  "act_1-1TJZFB",
   317  						ContractID: "ctr_1-1TJZFW",
   318  						GroupID:    "grp_15166",
   319  					},
   320  					Activations: ActivationsItems{Items: []*Activation{{
   321  						ActivationID:       "atv_1696985",
   322  						PropertyName:       "example.com",
   323  						PropertyID:         "prp_173136",
   324  						PropertyVersion:    1,
   325  						Network:            ActivationNetworkStaging,
   326  						ActivationType:     ActivationTypeActivate,
   327  						Status:             ActivationStatusPending,
   328  						SubmitDate:         "2014-03-02T02:22:12Z",
   329  						UpdateDate:         "2014-03-01T21:12:57Z",
   330  						Note:               "Sample activation",
   331  						FMAActivationState: "steady",
   332  						NotifyEmails: []string{
   333  							"you@example.com",
   334  							"them@example.com",
   335  						},
   336  						FallbackInfo: &ActivationFallbackInfo{
   337  							FastFallbackAttempted:      false,
   338  							FallbackVersion:            10,
   339  							CanFastFallback:            true,
   340  							SteadyStateTime:            1506448172,
   341  							FastFallbackExpirationTime: 1506451772,
   342  							FastFallbackRecoveryState:  nil,
   343  						},
   344  					}},
   345  					},
   346  				},
   347  				Activation: &Activation{
   348  					ActivationID:       "atv_1696985",
   349  					PropertyName:       "example.com",
   350  					PropertyID:         "prp_173136",
   351  					PropertyVersion:    1,
   352  					Network:            ActivationNetworkStaging,
   353  					ActivationType:     ActivationTypeActivate,
   354  					Status:             ActivationStatusPending,
   355  					SubmitDate:         "2014-03-02T02:22:12Z",
   356  					UpdateDate:         "2014-03-01T21:12:57Z",
   357  					Note:               "Sample activation",
   358  					FMAActivationState: "steady",
   359  					NotifyEmails: []string{
   360  						"you@example.com",
   361  						"them@example.com",
   362  					},
   363  					FallbackInfo: &ActivationFallbackInfo{
   364  						FastFallbackAttempted:      false,
   365  						FallbackVersion:            10,
   366  						CanFastFallback:            true,
   367  						SteadyStateTime:            1506448172,
   368  						FastFallbackExpirationTime: 1506451772,
   369  						FastFallbackRecoveryState:  nil,
   370  					},
   371  				},
   372  			},
   373  		},
   374  		"activation not found": {
   375  			request: GetActivationRequest{
   376  				PropertyID:   "prp_175780",
   377  				ActivationID: "atv_1696855",
   378  				ContractID:   "ctr_1-1TJZFW",
   379  				GroupID:      "grp_15166",
   380  			},
   381  			responseStatus: http.StatusOK,
   382  			responseBody: `
   383  {
   384  	"accountId": "act_1-1TJZFB",
   385  	"contractId": "ctr_1-1TJZFW",
   386  	"groupId": "grp_15166",
   387  	"activations": {
   388  		"items": [
   389  		]
   390  	}
   391  }`,
   392  			expectedPath: "/papi/v1/properties/prp_175780/activations/atv_1696855?contractId=ctr_1-1TJZFW&groupId=grp_15166",
   393  			withError:    ErrNotFound,
   394  		},
   395  		"500 internal server error": {
   396  			request: GetActivationRequest{
   397  				PropertyID:   "prp_175780",
   398  				ActivationID: "atv_1696855",
   399  				ContractID:   "ctr_1-1TJZFW",
   400  				GroupID:      "grp_15166",
   401  			},
   402  			responseStatus: http.StatusInternalServerError,
   403  			responseBody: `
   404  {
   405  	"type": "internal_error",
   406      "title": "Internal Server Error",
   407      "detail": "Error fetching activation",
   408      "status": 500
   409  }`,
   410  			expectedPath: "/papi/v1/properties/prp_175780/activations/atv_1696855?contractId=ctr_1-1TJZFW&groupId=grp_15166",
   411  			withError: &Error{
   412  				Type:       "internal_error",
   413  				Title:      "Internal Server Error",
   414  				Detail:     "Error fetching activation",
   415  				StatusCode: http.StatusInternalServerError,
   416  			},
   417  		},
   418  		"validation error": {
   419  			request: GetActivationRequest{
   420  				ActivationID: "atv_1696855",
   421  				ContractID:   "ctr_1-1TJZFW",
   422  				GroupID:      "grp_15166",
   423  			},
   424  			withError: ErrStructValidation,
   425  		},
   426  	}
   427  
   428  	for name, test := range tests {
   429  		t.Run(name, func(t *testing.T) {
   430  			mockServer := httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
   431  				assert.Equal(t, test.expectedPath, r.URL.String())
   432  				assert.Equal(t, http.MethodGet, r.Method)
   433  				w.WriteHeader(test.responseStatus)
   434  				_, err := w.Write([]byte(test.responseBody))
   435  				assert.NoError(t, err)
   436  			}))
   437  			client := mockAPIClient(t, mockServer)
   438  			result, err := client.GetActivation(context.Background(), test.request)
   439  			if test.withError != nil {
   440  				assert.True(t, errors.Is(err, test.withError), "want: %s; got: %s", test.withError, err)
   441  				return
   442  			}
   443  			require.NoError(t, err)
   444  			assert.Equal(t, test.expectedResponse, result)
   445  		})
   446  	}
   447  }
   448  
   449  func TestPapi_CancelActivation(t *testing.T) {
   450  	tests := map[string]struct {
   451  		request          CancelActivationRequest
   452  		responseStatus   int
   453  		responseBody     string
   454  		expectedPath     string
   455  		expectedResponse *CancelActivationResponse
   456  		withError        error
   457  	}{
   458  		"200 OK": {
   459  			request: CancelActivationRequest{
   460  				PropertyID:   "prp_175780",
   461  				ActivationID: "atv_1696855",
   462  				ContractID:   "ctr_1-1TJZFW",
   463  				GroupID:      "grp_15166",
   464  			},
   465  			responseStatus: http.StatusOK,
   466  			responseBody: `
   467  {
   468  	"activations": {
   469  		"items": [
   470  			{
   471  				"activationId": "atv_1696985",
   472  				"propertyName": "example.com",
   473  				"propertyId": "prp_173136",
   474  				"propertyVersion": 1,
   475  				"network": "STAGING",
   476  				"activationType": "ACTIVATE",
   477  				"status": "ABORTED",
   478  				"submitDate": "2014-03-02T02:22:12Z",
   479  				"updateDate": "2014-03-01T21:12:57Z",
   480  				"note": "Sample activation",
   481  				"fmaActivationState": "steady",
   482  				"notifyEmails": [
   483  					"you@example.com",
   484  					"them@example.com"
   485  				],
   486  				"fallbackInfo": {
   487  					"fastFallbackAttempted": false,
   488  					"fallbackVersion": 10,
   489  					"canFastFallback": true,
   490  					"steadyStateTime": 1506448172,
   491  					"fastFallbackExpirationTime": 1506451772,
   492  					"fastFallbackRecoveryState": null
   493  				}
   494  			}
   495  		]
   496  	}
   497  }`,
   498  			expectedPath: "/papi/v1/properties/prp_175780/activations/atv_1696855?contractId=ctr_1-1TJZFW&groupId=grp_15166",
   499  			expectedResponse: &CancelActivationResponse{
   500  				Activations: ActivationsItems{Items: []*Activation{{
   501  					ActivationID:       "atv_1696985",
   502  					PropertyName:       "example.com",
   503  					PropertyID:         "prp_173136",
   504  					PropertyVersion:    1,
   505  					Network:            ActivationNetworkStaging,
   506  					ActivationType:     ActivationTypeActivate,
   507  					Status:             ActivationStatusAborted,
   508  					SubmitDate:         "2014-03-02T02:22:12Z",
   509  					UpdateDate:         "2014-03-01T21:12:57Z",
   510  					Note:               "Sample activation",
   511  					FMAActivationState: "steady",
   512  					NotifyEmails: []string{
   513  						"you@example.com",
   514  						"them@example.com",
   515  					},
   516  					FallbackInfo: &ActivationFallbackInfo{
   517  						FastFallbackAttempted:      false,
   518  						FallbackVersion:            10,
   519  						CanFastFallback:            true,
   520  						SteadyStateTime:            1506448172,
   521  						FastFallbackExpirationTime: 1506451772,
   522  						FastFallbackRecoveryState:  nil,
   523  					},
   524  				}},
   525  				},
   526  			},
   527  		},
   528  		"500 internal server error": {
   529  			request: CancelActivationRequest{
   530  				PropertyID:   "prp_175780",
   531  				ActivationID: "atv_1696855",
   532  				ContractID:   "ctr_1-1TJZFW",
   533  				GroupID:      "grp_15166",
   534  			},
   535  			responseStatus: http.StatusInternalServerError,
   536  			responseBody: `
   537  {
   538  	"type": "internal_error",
   539      "title": "Internal Server Error",
   540      "detail": "Error deleting activation",
   541      "status": 500
   542  }`,
   543  			expectedPath: "/papi/v1/properties/prp_175780/activations/atv_1696855?contractId=ctr_1-1TJZFW&groupId=grp_15166",
   544  			withError: &Error{
   545  				Type:       "internal_error",
   546  				Title:      "Internal Server Error",
   547  				Detail:     "Error deleting activation",
   548  				StatusCode: http.StatusInternalServerError,
   549  			},
   550  		},
   551  		"validation error": {
   552  			request: CancelActivationRequest{
   553  				ActivationID: "atv_1696855",
   554  				ContractID:   "ctr_1-1TJZFW",
   555  				GroupID:      "grp_15166",
   556  			},
   557  			withError: ErrStructValidation,
   558  		},
   559  	}
   560  
   561  	for name, test := range tests {
   562  		t.Run(name, func(t *testing.T) {
   563  			mockServer := httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
   564  				assert.Equal(t, test.expectedPath, r.URL.String())
   565  				assert.Equal(t, http.MethodDelete, r.Method)
   566  				w.WriteHeader(test.responseStatus)
   567  				_, err := w.Write([]byte(test.responseBody))
   568  				assert.NoError(t, err)
   569  			}))
   570  			client := mockAPIClient(t, mockServer)
   571  			result, err := client.CancelActivation(context.Background(), test.request)
   572  			if test.withError != nil {
   573  				assert.True(t, errors.Is(err, test.withError), "want: %s; got: %s", test.withError, err)
   574  				return
   575  			}
   576  			require.NoError(t, err)
   577  			assert.Equal(t, test.expectedResponse, result)
   578  		})
   579  	}
   580  }