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

     1  package clientlists
     2  
     3  import (
     4  	"context"
     5  	"errors"
     6  	"io/ioutil"
     7  	"net/http"
     8  	"net/http/httptest"
     9  	"testing"
    10  
    11  	"github.com/akamai/AkamaiOPEN-edgegrid-golang/v8/pkg/session"
    12  	"github.com/stretchr/testify/require"
    13  	"github.com/tj/assert"
    14  )
    15  
    16  func TestCreateActivation(t *testing.T) {
    17  	uri := "/client-list/v1/lists/1234_NORTHAMERICAGEOALLOWLIST/activations"
    18  
    19  	tests := map[string]struct {
    20  		params              CreateActivationRequest
    21  		responseStatus      int
    22  		expectedRequestBody string
    23  		responseBody        string
    24  		expectedPath        string
    25  		expectedResponse    *CreateActivationResponse
    26  		withError           error
    27  	}{
    28  		"200 OK": {
    29  			params: CreateActivationRequest{
    30  				ListID: "1234_NORTHAMERICAGEOALLOWLIST",
    31  				ActivationParams: ActivationParams{
    32  					Action:                 Activate,
    33  					Network:                Production,
    34  					Comments:               "Activation of GEO allowlist list",
    35  					SiebelTicketID:         "12_B",
    36  					NotificationRecipients: []string{"a@a.com", "c@c.com"},
    37  				},
    38  			},
    39  			expectedRequestBody: `{"action":"ACTIVATE","comments":"Activation of GEO allowlist list","network":"PRODUCTION","notificationRecipients":["a@a.com","c@c.com"],"siebelTicketId":"12_B"}`,
    40  			responseStatus:      http.StatusOK,
    41  			responseBody: `{
    42  				"action": "ACTIVATE",
    43  				"activationStatus": "PENDING_ACTIVATION",
    44  				"listId": "1234_NORTHAMERICAGEOALLOWLIST",
    45  				"network": "PRODUCTION",
    46  				"notificationRecipients": ["aa@dd.com"],
    47  				"version": 1,
    48  				"activationId": 12,
    49  				"createDate": "2023-04-05T18:46:56.365Z",
    50  				"createdBy": "jdoe",
    51  				"network": "PRODUCTION",
    52  				"comments": "Activation of GEO allowlist list",
    53  				"siebelTicketId": "12_AB"
    54  			}`,
    55  			expectedPath: uri,
    56  			expectedResponse: &CreateActivationResponse{
    57  				Action:                 "ACTIVATE",
    58  				ActivationID:           12,
    59  				ActivationStatus:       PendingActivation,
    60  				CreateDate:             "2023-04-05T18:46:56.365Z",
    61  				CreatedBy:              "jdoe",
    62  				Comments:               "Activation of GEO allowlist list",
    63  				ListID:                 "1234_NORTHAMERICAGEOALLOWLIST",
    64  				Network:                Production,
    65  				NotificationRecipients: []string{"aa@dd.com"},
    66  				SiebelTicketID:         "12_AB",
    67  				Version:                1,
    68  			},
    69  		},
    70  		"500 internal server error": {
    71  			params: CreateActivationRequest{
    72  				ListID: "1234_NORTHAMERICAGEOALLOWLIST",
    73  				ActivationParams: ActivationParams{
    74  					Network: Production,
    75  				},
    76  			},
    77  			responseStatus: http.StatusInternalServerError,
    78  			responseBody: `
    79  				{
    80  					"type": "internal_error",
    81  					"title": "Internal Server Error",
    82  					"detail": "Error creating client lists activation",
    83  					"status": 500
    84  				}`,
    85  			expectedPath: uri,
    86  			withError: &Error{
    87  				Type:       "internal_error",
    88  				Title:      "Internal Server Error",
    89  				Detail:     "Error creating client lists activation",
    90  				StatusCode: http.StatusInternalServerError,
    91  			},
    92  		},
    93  		"validation error": {
    94  			params:    CreateActivationRequest{},
    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.MethodPost, r.Method)
   104  				w.WriteHeader(test.responseStatus)
   105  				_, err := w.Write([]byte(test.responseBody))
   106  				assert.NoError(t, err)
   107  
   108  				if len(test.expectedRequestBody) > 0 {
   109  					body, err := ioutil.ReadAll(r.Body)
   110  					require.NoError(t, err)
   111  					assert.Equal(t, test.expectedRequestBody, string(body))
   112  				}
   113  			}))
   114  			client := mockAPIClient(t, mockServer)
   115  			result, err := client.CreateActivation(
   116  				session.ContextWithOptions(
   117  					context.Background(),
   118  				),
   119  				test.params)
   120  			if test.withError != nil {
   121  				assert.True(t, errors.Is(err, test.withError), "want: %s; got: %s", test.withError, err)
   122  				return
   123  			}
   124  			require.NoError(t, err)
   125  			assert.Equal(t, test.expectedResponse, result)
   126  		})
   127  	}
   128  }
   129  
   130  func TestGetActivation(t *testing.T) {
   131  	uri := "/client-list/v1/activations/12"
   132  
   133  	tests := map[string]struct {
   134  		params           GetActivationRequest
   135  		responseStatus   int
   136  		responseBody     string
   137  		expectedPath     string
   138  		expectedResponse *GetActivationResponse
   139  		withError        error
   140  	}{
   141  		"200 OK": {
   142  			params: GetActivationRequest{
   143  				ActivationID: 12,
   144  			},
   145  			responseStatus: http.StatusOK,
   146  			responseBody: `{
   147  				"action": "ACTIVATE",
   148  				"activationId": 12,
   149  				"activationStatus": "PENDING_ACTIVATION",
   150  				"comments": "latest activation",
   151  				"createDate": "2023-04-05T18:46:56.365Z",
   152  				"createdBy": "jdoe",
   153  				"fast": true,
   154  				"listId": "1234_NORTHAMERICAGEOALLOWLIST",
   155  				"network": "PRODUCTION",
   156  				"notificationRecipients": [
   157  						"qw@ff.com"
   158  				],
   159  				"siebelTicketId": "q",
   160  				"version": 1
   161  		}`,
   162  			expectedPath: uri,
   163  			expectedResponse: &GetActivationResponse{
   164  				ActivationID:      12,
   165  				ListID:            "1234_NORTHAMERICAGEOALLOWLIST",
   166  				Version:           1,
   167  				CreateDate:        "2023-04-05T18:46:56.365Z",
   168  				CreatedBy:         "jdoe",
   169  				Fast:              true,
   170  				InitialActivation: false,
   171  				ActivationStatus:  "PENDING_ACTIVATION",
   172  				ActivationParams: ActivationParams{
   173  					Action:                 Activate,
   174  					NotificationRecipients: []string{"qw@ff.com"},
   175  					Comments:               "latest activation",
   176  					Network:                Production,
   177  					SiebelTicketID:         "q",
   178  				},
   179  			},
   180  		},
   181  		"500 internal server error": {
   182  			params: GetActivationRequest{
   183  				ActivationID: 12,
   184  			},
   185  			responseStatus: http.StatusInternalServerError,
   186  			responseBody: `
   187  				{
   188  					"type": "internal_error",
   189  					"title": "Internal Server Error",
   190  					"detail": "Error fetching client lists activation",
   191  					"status": 500
   192  				}`,
   193  			expectedPath: uri,
   194  			withError: &Error{
   195  				Type:       "internal_error",
   196  				Title:      "Internal Server Error",
   197  				Detail:     "Error fetching client lists activation",
   198  				StatusCode: http.StatusInternalServerError,
   199  			},
   200  		},
   201  		"validation error": {
   202  			params:    GetActivationRequest{},
   203  			withError: ErrStructValidation,
   204  		},
   205  	}
   206  
   207  	for name, test := range tests {
   208  		t.Run(name, func(t *testing.T) {
   209  			mockServer := httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
   210  				assert.Equal(t, test.expectedPath, r.URL.String())
   211  				assert.Equal(t, http.MethodGet, r.Method)
   212  				w.WriteHeader(test.responseStatus)
   213  				_, err := w.Write([]byte(test.responseBody))
   214  				assert.NoError(t, err)
   215  			}))
   216  			client := mockAPIClient(t, mockServer)
   217  			result, err := client.GetActivation(
   218  				session.ContextWithOptions(
   219  					context.Background(),
   220  				),
   221  				test.params)
   222  			if test.withError != nil {
   223  				assert.True(t, errors.Is(err, test.withError), "want: %s; got: %s", test.withError, err)
   224  				return
   225  			}
   226  			require.NoError(t, err)
   227  			assert.Equal(t, test.expectedResponse, result)
   228  		})
   229  	}
   230  }
   231  
   232  func TestGetActivationStatus(t *testing.T) {
   233  	uri := "/client-list/v1/lists/1234_NORTHAMERICAGEOALLOWLIST/environments/PRODUCTION/status"
   234  
   235  	tests := map[string]struct {
   236  		params           GetActivationStatusRequest
   237  		responseStatus   int
   238  		responseBody     string
   239  		expectedPath     string
   240  		expectedResponse *GetActivationStatusResponse
   241  		withError        error
   242  	}{
   243  		"200 OK": {
   244  			params: GetActivationStatusRequest{
   245  				ListID:  "1234_NORTHAMERICAGEOALLOWLIST",
   246  				Network: Production,
   247  			},
   248  			responseStatus: http.StatusOK,
   249  			responseBody: `{
   250  				"action": "ACTIVATE",
   251  				"activationStatus": "PENDING_ACTIVATION",
   252  				"listId": "1234_NORTHAMERICAGEOALLOWLIST",
   253  				"network": "PRODUCTION",
   254  				"notificationRecipients": [],
   255  				"version": 1,
   256  				"activationId": 12,
   257  				"createDate": "2023-04-05T18:46:56.365Z",
   258  				"createdBy": "jdoe",
   259  				"network": "PRODUCTION",
   260  				"comments": "Activation of GEO allowlist list",
   261  				"siebelTicketId": "12_AB"
   262  			}`,
   263  			expectedPath: uri,
   264  			expectedResponse: &GetActivationStatusResponse{
   265  				Action:                 "ACTIVATE",
   266  				ActivationID:           12,
   267  				ActivationStatus:       PendingActivation,
   268  				CreateDate:             "2023-04-05T18:46:56.365Z",
   269  				CreatedBy:              "jdoe",
   270  				Comments:               "Activation of GEO allowlist list",
   271  				ListID:                 "1234_NORTHAMERICAGEOALLOWLIST",
   272  				Network:                Production,
   273  				NotificationRecipients: []string{},
   274  				SiebelTicketID:         "12_AB",
   275  				Version:                1,
   276  			},
   277  		},
   278  		"500 internal server error": {
   279  			params: GetActivationStatusRequest{
   280  				ListID:  "1234_NORTHAMERICAGEOALLOWLIST",
   281  				Network: Production,
   282  			},
   283  			responseStatus: http.StatusInternalServerError,
   284  			responseBody: `
   285  				{
   286  					"type": "internal_error",
   287  					"title": "Internal Server Error",
   288  					"detail": "Error fetching client lists activation",
   289  					"status": 500
   290  				}`,
   291  			expectedPath: uri,
   292  			withError: &Error{
   293  				Type:       "internal_error",
   294  				Title:      "Internal Server Error",
   295  				Detail:     "Error fetching client lists activation",
   296  				StatusCode: http.StatusInternalServerError,
   297  			},
   298  		},
   299  		"validation error": {
   300  			params:    GetActivationStatusRequest{},
   301  			withError: ErrStructValidation,
   302  		},
   303  	}
   304  
   305  	for name, test := range tests {
   306  		t.Run(name, func(t *testing.T) {
   307  			mockServer := httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
   308  				assert.Equal(t, test.expectedPath, r.URL.String())
   309  				assert.Equal(t, http.MethodGet, r.Method)
   310  				w.WriteHeader(test.responseStatus)
   311  				_, err := w.Write([]byte(test.responseBody))
   312  				assert.NoError(t, err)
   313  			}))
   314  			client := mockAPIClient(t, mockServer)
   315  			result, err := client.GetActivationStatus(
   316  				session.ContextWithOptions(
   317  					context.Background(),
   318  				),
   319  				test.params)
   320  			if test.withError != nil {
   321  				assert.True(t, errors.Is(err, test.withError), "want: %s; got: %s", test.withError, err)
   322  				return
   323  			}
   324  			require.NoError(t, err)
   325  			assert.Equal(t, test.expectedResponse, result)
   326  		})
   327  	}
   328  }