github.com/akamai/AkamaiOPEN-edgegrid-golang/v8@v8.1.0/pkg/iam/blocked_properties_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/assert"
    11  )
    12  
    13  func TestIam_ListBlockedProperties(t *testing.T) {
    14  	tests := map[string]struct {
    15  		params           ListBlockedPropertiesRequest
    16  		responseStatus   int
    17  		expectedPath     string
    18  		responseBody     string
    19  		expectedResponse []int64
    20  		withError        func(*testing.T, error)
    21  	}{
    22  		"200 OK": {
    23  			params: ListBlockedPropertiesRequest{
    24  				GroupID:    12345,
    25  				IdentityID: "1-ABCDE",
    26  			},
    27  			responseStatus: http.StatusOK,
    28  			expectedPath:   "/identity-management/v2/user-admin/ui-identities/1-ABCDE/groups/12345/blocked-properties",
    29  			responseBody: `[
    30  								10977166
    31  							]`,
    32  			expectedResponse: []int64{
    33  				10977166,
    34  			},
    35  		},
    36  		"200 OK, no blocked property": {
    37  			params: ListBlockedPropertiesRequest{
    38  				GroupID:    12345,
    39  				IdentityID: "1-ABCDE",
    40  			},
    41  			responseStatus: http.StatusOK,
    42  			expectedPath:   "/identity-management/v2/user-admin/ui-identities/1-ABCDE/groups/12345/blocked-properties",
    43  			responseBody: `[
    44  							
    45  							]`,
    46  			expectedResponse: []int64{},
    47  		},
    48  
    49  		"404 not found error": {
    50  			params: ListBlockedPropertiesRequest{
    51  				GroupID:    123450000,
    52  				IdentityID: "1-ABCDE",
    53  			},
    54  			responseStatus: http.StatusNotFound,
    55  			expectedPath:   "/identity-management/v2/user-admin/ui-identities/1-ABCDE/groups/123450000/blocked-properties",
    56  			responseBody: `
    57  			{
    58      "instance": "",
    59      "httpStatus": 404,
    60      "detail": "group not found",
    61      "title": "Not found",
    62      "type": "/useradmin-api/error-types/1700"
    63  }`,
    64  			withError: func(t *testing.T, err error) {
    65  				want := &Error{
    66  					Instance:   "",
    67  					HTTPStatus: http.StatusNotFound,
    68  					Detail:     "group not found",
    69  					Title:      "Not found",
    70  					Type:       "/useradmin-api/error-types/1700",
    71  					StatusCode: http.StatusNotFound,
    72  				}
    73  				assert.True(t, errors.Is(err, want), "want: %s; got: %s", want, err)
    74  			},
    75  		},
    76  
    77  		"500 internal server error": {
    78  			params: ListBlockedPropertiesRequest{
    79  				GroupID:    12345,
    80  				IdentityID: "1-ABCDE",
    81  			},
    82  			responseStatus: http.StatusInternalServerError,
    83  			expectedPath:   "/identity-management/v2/user-admin/ui-identities/1-ABCDE/groups/12345/blocked-properties",
    84  			responseBody: `
    85  			{
    86  				"type": "internal_error",
    87  				"title": "Internal Server Error",
    88  				"detail": "Error processing request",
    89  				"status": 500
    90  			}`,
    91  			withError: func(t *testing.T, err error) {
    92  				want := &Error{
    93  					Type:       "internal_error",
    94  					Title:      "Internal Server Error",
    95  					Detail:     "Error processing request",
    96  					StatusCode: http.StatusInternalServerError,
    97  				}
    98  				assert.True(t, errors.Is(err, want), "want: %s; got: %s", want, err)
    99  			},
   100  		},
   101  	}
   102  	for name, test := range tests {
   103  		t.Run(name, func(t *testing.T) {
   104  			mockServer := httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
   105  				assert.Equal(t, test.expectedPath, r.URL.String())
   106  				assert.Equal(t, http.MethodGet, r.Method)
   107  				w.WriteHeader(test.responseStatus)
   108  				_, err := w.Write([]byte(test.responseBody))
   109  				assert.NoError(t, err)
   110  			}))
   111  			client := mockAPIClient(t, mockServer)
   112  			users, err := client.ListBlockedProperties(context.Background(), test.params)
   113  			if test.withError != nil {
   114  				test.withError(t, err)
   115  				return
   116  			}
   117  			assert.NoError(t, err)
   118  			assert.Equal(t, test.expectedResponse, users)
   119  		})
   120  	}
   121  }
   122  
   123  func TestIam_UpdateBlockedProperties(t *testing.T) {
   124  	tests := map[string]struct {
   125  		params           UpdateBlockedPropertiesRequest
   126  		responseStatus   int
   127  		expectedPath     string
   128  		responseBody     string
   129  		expectedResponse []int64
   130  		withError        func(*testing.T, error)
   131  	}{
   132  		"200 OK": {
   133  			params: UpdateBlockedPropertiesRequest{
   134  				GroupID:    12345,
   135  				IdentityID: "1-ABCDE",
   136  				Properties: []int64{10977166, 10977167},
   137  			},
   138  			responseStatus: http.StatusOK,
   139  			expectedPath:   "/identity-management/v2/user-admin/ui-identities/1-ABCDE/groups/12345/blocked-properties",
   140  			responseBody: `[
   141  								10977166,10977167
   142  							]`,
   143  			expectedResponse: []int64{
   144  				10977166, 10977167,
   145  			},
   146  		},
   147  		"400 bad request": {
   148  			params: UpdateBlockedPropertiesRequest{
   149  				GroupID:    12345,
   150  				IdentityID: "1-ABCDE",
   151  				Properties: []int64{0, 1},
   152  			},
   153  			responseStatus: http.StatusNotFound,
   154  			expectedPath:   "/identity-management/v2/user-admin/ui-identities/1-ABCDE/groups/12345/blocked-properties",
   155  			responseBody: `
   156  			{
   157      "instance": "",
   158      "httpStatus": 400,
   159      "detail": "",
   160      "title": "Validation Exception",
   161      "type": "/useradmin-api/error-types/1003"
   162  }`,
   163  			withError: func(t *testing.T, err error) {
   164  				want := &Error{
   165  					Instance:   "",
   166  					HTTPStatus: http.StatusBadRequest,
   167  					Detail:     "",
   168  					Title:      "Validation Exception",
   169  					Type:       "/useradmin-api/error-types/1003",
   170  					StatusCode: http.StatusNotFound,
   171  				}
   172  				assert.True(t, errors.Is(err, want), "want: %s; got: %s", want, err)
   173  			},
   174  		},
   175  
   176  		"404 not found error": {
   177  			params: UpdateBlockedPropertiesRequest{
   178  				GroupID:    123450000,
   179  				IdentityID: "1-ABCDE",
   180  				Properties: []int64{10977166, 10977167},
   181  			},
   182  			responseStatus: http.StatusNotFound,
   183  			expectedPath:   "/identity-management/v2/user-admin/ui-identities/1-ABCDE/groups/123450000/blocked-properties",
   184  			responseBody: `
   185  			{
   186      "instance": "",
   187      "httpStatus": 404,
   188      "detail": "group not found",
   189      "title": "Not found",
   190      "type": "/useradmin-api/error-types/1700"
   191  }`,
   192  			withError: func(t *testing.T, err error) {
   193  				want := &Error{
   194  					Instance:   "",
   195  					HTTPStatus: http.StatusNotFound,
   196  					Detail:     "group not found",
   197  					Title:      "Not found",
   198  					Type:       "/useradmin-api/error-types/1700",
   199  					StatusCode: http.StatusNotFound,
   200  				}
   201  				assert.True(t, errors.Is(err, want), "want: %s; got: %s", want, err)
   202  			},
   203  		},
   204  
   205  		"500 internal server error": {
   206  			params: UpdateBlockedPropertiesRequest{
   207  				GroupID:    12345,
   208  				IdentityID: "1-ABCDE",
   209  			},
   210  			responseStatus: http.StatusInternalServerError,
   211  			expectedPath:   "/identity-management/v2/user-admin/ui-identities/1-ABCDE/groups/12345/blocked-properties",
   212  			responseBody: `
   213  			{
   214  				"type": "internal_error",
   215  				"title": "Internal Server Error",
   216  				"detail": "Error processing request",
   217  				"status": 500
   218  			}`,
   219  			withError: func(t *testing.T, err error) {
   220  				want := &Error{
   221  					Type:       "internal_error",
   222  					Title:      "Internal Server Error",
   223  					Detail:     "Error processing request",
   224  					StatusCode: http.StatusInternalServerError,
   225  				}
   226  				assert.True(t, errors.Is(err, want), "want: %s; got: %s", want, err)
   227  			},
   228  		},
   229  	}
   230  	for name, test := range tests {
   231  		t.Run(name, func(t *testing.T) {
   232  			mockServer := httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
   233  				assert.Equal(t, test.expectedPath, r.URL.String())
   234  				assert.Equal(t, http.MethodPut, r.Method)
   235  				w.WriteHeader(test.responseStatus)
   236  				_, err := w.Write([]byte(test.responseBody))
   237  				assert.NoError(t, err)
   238  			}))
   239  			client := mockAPIClient(t, mockServer)
   240  			users, err := client.UpdateBlockedProperties(context.Background(), test.params)
   241  			if test.withError != nil {
   242  				test.withError(t, err)
   243  				return
   244  			}
   245  			assert.NoError(t, err)
   246  			assert.Equal(t, test.expectedResponse, users)
   247  		})
   248  	}
   249  }