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

     1  package iam
     2  
     3  import (
     4  	"context"
     5  	"errors"
     6  	"net/http"
     7  	"net/http/httptest"
     8  	"testing"
     9  
    10  	"github.com/stretchr/testify/require"
    11  	"github.com/tj/assert"
    12  )
    13  
    14  func TestIAM_LockUser(t *testing.T) {
    15  	tests := map[string]struct {
    16  		params         LockUserRequest
    17  		responseStatus int
    18  		responseBody   string
    19  		expectedPath   string
    20  		withError      func(*testing.T, error)
    21  	}{
    22  		"200 OK": {
    23  			params: LockUserRequest{
    24  				IdentityID: "1-ABCDE",
    25  			},
    26  			responseStatus: http.StatusOK,
    27  			responseBody:   "",
    28  			expectedPath:   "/identity-management/v2/user-admin/ui-identities/1-ABCDE/lock",
    29  		},
    30  		"204 No Content": {
    31  			params: LockUserRequest{
    32  				IdentityID: "1-ABCDE",
    33  			},
    34  			responseStatus: http.StatusNoContent,
    35  			responseBody:   "",
    36  			expectedPath:   "/identity-management/v2/user-admin/ui-identities/1-ABCDE/lock",
    37  		},
    38  		"404 Not Found": {
    39  			params: LockUserRequest{
    40  				IdentityID: "X1-ABCDE",
    41  			},
    42  			responseStatus: http.StatusNotFound,
    43  			responseBody: `
    44  			{
    45  				"instance": "",
    46  				"httpStatus": 404,
    47  				"detail": "",
    48  				"title": "User not found",
    49  				"type": "/useradmin-api/error-types/1100"
    50  			}`,
    51  			expectedPath: "/identity-management/v2/user-admin/ui-identities/X1-ABCDE/lock",
    52  			withError: func(t *testing.T, err error) {
    53  				want := &Error{
    54  					Instance:   "",
    55  					HTTPStatus: http.StatusNotFound,
    56  					Detail:     "",
    57  					Title:      "User not found",
    58  					Type:       "/useradmin-api/error-types/1100",
    59  					StatusCode: http.StatusNotFound,
    60  				}
    61  				assert.True(t, errors.Is(err, want), "want: %s; got: %s", want, err)
    62  			},
    63  		},
    64  		"500 internal server error": {
    65  			params: LockUserRequest{
    66  				IdentityID: "1-ABCDE",
    67  			},
    68  			responseStatus: http.StatusInternalServerError,
    69  			responseBody: `
    70  			{
    71  				"type": "internal_error",
    72  				"title": "Internal Server Error",
    73  				"detail": "Error making request",
    74  				"status": 500
    75  			}`,
    76  			expectedPath: "/identity-management/v2/user-admin/ui-identities/1-ABCDE/lock",
    77  			withError: func(t *testing.T, err error) {
    78  				want := &Error{
    79  					Type:       "internal_error",
    80  					Title:      "Internal Server Error",
    81  					Detail:     "Error making request",
    82  					StatusCode: http.StatusInternalServerError,
    83  				}
    84  				assert.True(t, errors.Is(err, want), "want: %s; got: %s", want, err)
    85  			},
    86  		},
    87  	}
    88  
    89  	for name, test := range tests {
    90  		t.Run(name, func(t *testing.T) {
    91  			mockServer := httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
    92  				assert.Equal(t, test.expectedPath, r.URL.String())
    93  				assert.Equal(t, http.MethodPost, r.Method)
    94  				w.WriteHeader(test.responseStatus)
    95  				_, err := w.Write([]byte(test.responseBody))
    96  				assert.NoError(t, err)
    97  			}))
    98  			client := mockAPIClient(t, mockServer)
    99  			err := client.LockUser(context.Background(), test.params)
   100  			if test.withError != nil {
   101  				test.withError(t, err)
   102  				return
   103  			}
   104  			require.NoError(t, err)
   105  		})
   106  	}
   107  }
   108  
   109  func TestIAM_UnlockUser(t *testing.T) {
   110  	tests := map[string]struct {
   111  		params         UnlockUserRequest
   112  		responseStatus int
   113  		responseBody   string
   114  		expectedPath   string
   115  		withError      func(*testing.T, error)
   116  	}{
   117  		"200 OK": {
   118  			params: UnlockUserRequest{
   119  				IdentityID: "1-ABCDE",
   120  			},
   121  			responseStatus: http.StatusOK,
   122  			responseBody:   "",
   123  			expectedPath:   "/identity-management/v2/user-admin/ui-identities/1-ABCDE/unlock",
   124  		},
   125  		"204 No Content": {
   126  			params: UnlockUserRequest{
   127  				IdentityID: "1-ABCDE",
   128  			},
   129  			responseStatus: http.StatusNoContent,
   130  			responseBody:   "",
   131  			expectedPath:   "/identity-management/v2/user-admin/ui-identities/1-ABCDE/unlock",
   132  		},
   133  		"404 Not Found": {
   134  			params: UnlockUserRequest{
   135  				IdentityID: "X1-ABCDE",
   136  			},
   137  			responseStatus: http.StatusNotFound,
   138  			responseBody: `
   139  			{
   140  				"instance": "",
   141  				"httpStatus": 404,
   142  				"detail": "",
   143  				"title": "User not found",
   144  				"type": "/useradmin-api/error-types/1100"
   145  			}`,
   146  			expectedPath: "/identity-management/v2/user-admin/ui-identities/X1-ABCDE/unlock",
   147  			withError: func(t *testing.T, err error) {
   148  				want := &Error{
   149  					Instance:   "",
   150  					HTTPStatus: http.StatusNotFound,
   151  					Detail:     "",
   152  					Title:      "User not found",
   153  					Type:       "/useradmin-api/error-types/1100",
   154  					StatusCode: http.StatusNotFound,
   155  				}
   156  				assert.True(t, errors.Is(err, want), "want: %s; got: %s", want, err)
   157  			},
   158  		},
   159  		"500 internal server error": {
   160  			params: UnlockUserRequest{
   161  				IdentityID: "1-ABCDE",
   162  			},
   163  			responseStatus: http.StatusInternalServerError,
   164  			responseBody: `
   165  			{
   166  				"type": "internal_error",
   167  				"title": "Internal Server Error",
   168  				"detail": "Error making request",
   169  				"status": 500
   170  			}`,
   171  			expectedPath: "/identity-management/v2/user-admin/ui-identities/1-ABCDE/unlock",
   172  			withError: func(t *testing.T, err error) {
   173  				want := &Error{
   174  					Type:       "internal_error",
   175  					Title:      "Internal Server Error",
   176  					Detail:     "Error making request",
   177  					StatusCode: http.StatusInternalServerError,
   178  				}
   179  				assert.True(t, errors.Is(err, want), "want: %s; got: %s", want, err)
   180  			},
   181  		},
   182  	}
   183  
   184  	for name, test := range tests {
   185  		t.Run(name, func(t *testing.T) {
   186  			mockServer := httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
   187  				assert.Equal(t, test.expectedPath, r.URL.String())
   188  				assert.Equal(t, http.MethodPost, r.Method)
   189  				w.WriteHeader(test.responseStatus)
   190  				_, err := w.Write([]byte(test.responseBody))
   191  				assert.NoError(t, err)
   192  			}))
   193  			client := mockAPIClient(t, mockServer)
   194  			err := client.UnlockUser(context.Background(), test.params)
   195  			if test.withError != nil {
   196  				test.withError(t, err)
   197  				return
   198  			}
   199  			require.NoError(t, err)
   200  		})
   201  	}
   202  }