...

Source file src/edge-infra.dev/pkg/edge/api/services/bsl_mock_server.go

Documentation: edge-infra.dev/pkg/edge/api/services

     1  package services
     2  
     3  import (
     4  	"bytes"
     5  	"encoding/base64"
     6  	"encoding/json"
     7  	"fmt"
     8  	"io"
     9  	"net/http"
    10  	"net/http/httptest"
    11  	"strings"
    12  
    13  	"edge-infra.dev/pkg/edge/api/bsl/types"
    14  	"edge-infra.dev/pkg/edge/api/graph/model"
    15  	"edge-infra.dev/pkg/edge/api/testutils"
    16  	"edge-infra.dev/pkg/edge/api/utils"
    17  )
    18  
    19  const (
    20  	basicAuthPrefix                   = "Basic "
    21  	bslUsername                       = "testing"
    22  	enterpriseUnitFormat              = "/provisioning/enterprise-units?typeNamePattern=organization&pageNumber=%d&pageSize=200"
    23  	enterpriseUnitGrantFormat         = "/provisioning/enterprise-unit-grants/unit-grants?username=acct:emerald-edge-dev@%s&pageNumber=%d&pageSize=200"
    24  	enterpriseUnitGrantFormatFullname = "/provisioning/enterprise-unit-grants/unit-grants?username=acct:emerald-edge-dev@%s&pageNumber=%d&pageSize=200"
    25  )
    26  
    27  var (
    28  	emptyBody = []byte(nil)
    29  )
    30  
    31  func GetMockBspServer(testOrg, testOrgID, testUser, testUserRole, testEmail, testResetURL string) *httptest.Server { //nolint
    32  	server := utils.NewMockHTTPTestServer().AddAllowedContentType("application/json").AddNotFound(notFound)
    33  
    34  	// Post
    35  	server.Post("Login", "/security/authentication/login", loginCallback, nil)
    36  	server.Post("Recover Password", "/security/security-user-passwords/testing/recover", func(w http.ResponseWriter, r *http.Request) { recoverCallback(w, r, testResetURL) }, nil)
    37  	server.Post("Create Enterprise Unit", "/provisioning/enterprise-units", createEnterpriseUnitCallback, nil)
    38  	server.Post("Create Organization", "/provisioning/organizations", createOrganizationCallback, nil)
    39  	server.Post("BSL Exchange Token", "/security/security-tokens/exchange", tokenExchangeCallback, nil)
    40  	server.Post("Okta Exchange Token", "/security/authentication/okta-exchange", tokenExchangeCallback, nil)
    41  	server.Post("Users Details for EU", "/provisioning/users/user-details", func(w http.ResponseWriter, _ *http.Request) { getEUUsersCallback(w) }, nil)
    42  
    43  	//Get
    44  	server.Get("Get User Profile", "/provisioning/user-profiles", func(w http.ResponseWriter, _ *http.Request) { getUserProfileCallback(w, testUser, testEmail) }, func(_ http.ResponseWriter, r *http.Request) bool {
    45  		return r.URL.Path == "/provisioning/user-profiles"
    46  	})
    47  	server.Get("Get Users", "/provisioning/users?pageNumber=0&pageSize=200&userType=PRIMARY", func(w http.ResponseWriter, _ *http.Request) { getUsersCallback(w) }, nil)
    48  	server.Get("Get External Users", "/provisioning/users?pageNumber=0&pageSize=200&userType=EXTERNAL", func(w http.ResponseWriter, _ *http.Request) { getExternalUsersCallback(w) }, nil)
    49  	server.Get("Get EU Users", "/provisioning/enterprise-unit-grants/unit-grants?enterpriseUnitId=eu-banner-bsl-id&pageNumber=0&pageSize=200", func(w http.ResponseWriter, _ *http.Request) { findUnitGrantsResponseCallback(w) }, nil)
    50  	server.Get("Get Group Memberships", "", func(w http.ResponseWriter, r *http.Request) { handleGetRoleResponse(w, r, testUser, testUserRole) }, func(_ http.ResponseWriter, r *http.Request) bool {
    51  		return strings.Contains(r.URL.String(), "/security/group-memberships/groups?username=")
    52  	})
    53  	server.Get("Get Enterprise Unit", "", func(w http.ResponseWriter, _ *http.Request) { getEnterpriseUnitCallback(w) }, func(_ http.ResponseWriter, r *http.Request) bool {
    54  		return r.Method == http.MethodGet && strings.Contains(r.URL.String(), "/provisioning/enterprise-units/") && !strings.Contains(r.URL.String(), "test-bsl-id-for-deleted-eu")
    55  	})
    56  	server.Get("Get Deleted Enterprise Unit", "", func(w http.ResponseWriter, _ *http.Request) { getDeletedEnterpriseUnitCallback(w) }, func(_ http.ResponseWriter, r *http.Request) bool {
    57  		return r.Method == http.MethodGet && r.URL.String() == "/provisioning/enterprise-units/test-bsl-id-for-deleted-eu"
    58  	})
    59  	server.Get("Get Organization", "", func(w http.ResponseWriter, _ *http.Request) { getOrganizationCallback(w, testOrg, testOrgID) }, func(_ http.ResponseWriter, r *http.Request) bool {
    60  		return r.Method == http.MethodGet && (r.URL.String() == "/provisioning/organizations/"+testOrg || strings.Contains(r.URL.String(), "/provisioning/organizations/find-by-parent?parentOrganizationName="))
    61  	})
    62  	server.Get("Get Deleted Organization", "", func(w http.ResponseWriter, _ *http.Request) { getDeletedOrganizationCallback(w) }, func(_ http.ResponseWriter, r *http.Request) bool {
    63  		return r.Method == http.MethodGet && (r.URL.String() == "/provisioning/organizations/test-bsl-id-for-deleted-org")
    64  	})
    65  	server.Get("Get Organization by BSL ID", "/provisioning/organizations/3ea81a9de9564c3584015b98dd8bcfe9", func(w http.ResponseWriter, _ *http.Request) { getOrganizationCallback(w, testOrg, testOrgID) }, nil)
    66  	server.Get("Get External User Account - No Data", "", func(w http.ResponseWriter, _ *http.Request) { getExternalUserNoDataCallback(w) }, func(_ http.ResponseWriter, r *http.Request) bool {
    67  		return r.Method == http.MethodGet && strings.HasPrefix(r.URL.String(), "/provisioning/users/external/acct:emerald-edge-dev@nodata/accounts")
    68  	})
    69  	server.Get("Get External User Accounts", "", func(w http.ResponseWriter, _ *http.Request) { getExternalUserCallback(w, testOrg, testOrgID) }, func(_ http.ResponseWriter, r *http.Request) bool {
    70  		return r.Method == http.MethodGet && (r.URL.String() == "/provisioning/users/external/acct:emerald-edge-dev@testing/accounts?pageNumber=0&pageSize=200" || r.URL.String() == "/provisioning/users/external/acct:test-org@multiple/accounts?pageNumber=0&pageSize=200" || r.URL.String() == "/provisioning/users/external/acct:emerald-edge-dev@multiple/accounts?pageNumber=0&pageSize=200" || r.URL.String() == "/provisioning/users/external/acct:emerald-edge-dev@multiple/accounts?pageNumber=0&pageSize=200" || r.URL.String() == "/provisioning/users/external/acct:test-org@/accounts?pageNumber=0&pageSize=200" || r.URL.String() == "/provisioning/users/external/test/accounts?pageNumber=0&pageSize=200")
    71  	})
    72  	server.Get("Get User", "/provisioning/users/acct:edge-dev0-edge-b413cb@test", func(w http.ResponseWriter, _ *http.Request) { getUserCallback(w) }, func(_ http.ResponseWriter, r *http.Request) bool {
    73  		return r.Method == http.MethodGet && strings.HasPrefix(r.URL.String(), "/provisioning/users/acct:")
    74  	})
    75  	server.Get("Effective Organizations", "/provisioning/user-profiles/effective-organizations", func(w http.ResponseWriter, _ *http.Request) {
    76  		getEffectiveOrganizationsCallback(w, 0, 1, 5, true, getEffectiveOrganizations())
    77  	}, func(_ http.ResponseWriter, r *http.Request) bool {
    78  		return r.Method == http.MethodGet && r.URL.Path == "/provisioning/user-profiles/effective-organizations"
    79  	})
    80  	server.Get("Get Effective Role Grants", "/provisioning/role-grants/user-grants/self/effective-roles", func(w http.ResponseWriter, _ *http.Request) {
    81  		getEffectiveRolesCallback(w)
    82  	}, func(_ http.ResponseWriter, r *http.Request) bool {
    83  		return r.Method == http.MethodGet && r.URL.Path == "/provisioning/role-grants/user-grants/self/effective-roles"
    84  	})
    85  	//Any
    86  	server.Any("Get or Patch BSL Site", getBSLSiteCallback, func(_ http.ResponseWriter, r *http.Request) bool {
    87  		return (r.Method == http.MethodGet || r.Method == http.MethodPatch) && strings.HasPrefix(r.URL.String(), BslSitePath)
    88  	})
    89  	server.Any("Post or Put BSL Site", createBSLSiteCallback, func(_ http.ResponseWriter, r *http.Request) bool {
    90  		return (r.Method == http.MethodPost || r.Method == http.MethodPut) && strings.HasPrefix(r.URL.String(), BslSitePath)
    91  	})
    92  	server.Any("Get Enterprise Unit & Grant Format - LastPage False", func(w http.ResponseWriter, _ *http.Request) {
    93  		pageContent := []*types.EnterpriseUnitData{
    94  			getEUData(),
    95  		}
    96  		getEnterpriseUnitAndGrantCallback(w, false, pageContent, 0, 1, 1)
    97  	}, func(_ http.ResponseWriter, r *http.Request) bool {
    98  		return r.Method == http.MethodGet && (strings.HasSuffix(r.URL.String(), fmt.Sprintf(enterpriseUnitFormat, 0)) || strings.HasSuffix(r.URL.String(), fmt.Sprintf(enterpriseUnitGrantFormat, bslUsername, 0)) || strings.HasSuffix(r.URL.String(), fmt.Sprintf(enterpriseUnitGrantFormatFullname, bslUsername, 0)))
    99  	})
   100  	server.Any("Get Enterprise Unit & Grant Format - No Page Content & No Pages", func(w http.ResponseWriter, _ *http.Request) {
   101  		getEnterpriseUnitAndGrantCallback(w, true, nil, 0, 0, 0)
   102  	}, func(_ http.ResponseWriter, r *http.Request) bool {
   103  		return r.Method == http.MethodGet && (strings.HasSuffix(r.URL.String(), fmt.Sprintf(enterpriseUnitGrantFormat, "nodata", 0)) || strings.HasSuffix(r.URL.String(), fmt.Sprintf(enterpriseUnitGrantFormatFullname, "nodata", 0)))
   104  	})
   105  	server.Any("Get Enterprise Unit & Grant Format - No Page Content", func(w http.ResponseWriter, _ *http.Request) {
   106  		getEnterpriseUnitAndGrantCallback(w, true, nil, 1, 1, 1)
   107  	}, func(_ http.ResponseWriter, r *http.Request) bool {
   108  		return r.Method == http.MethodGet && (strings.HasSuffix(r.URL.String(), fmt.Sprintf(enterpriseUnitFormat, 1)) || strings.HasSuffix(r.URL.String(), fmt.Sprintf(enterpriseUnitGrantFormat, bslUsername, 1)) || strings.HasSuffix(r.URL.String(), fmt.Sprintf(enterpriseUnitGrantFormatFullname, bslUsername, 1)) || r.URL.String() == "/provisioning/enterprise-unit-grants/unit-grants?username=acct:test-org@&pageNumber=0&pageSize=200" || r.URL.String() == "/provisioning/enterprise-unit-grants/unit-grants?username=test&pageNumber=0&pageSize=200")
   109  	})
   110  	server.Any("Get Enterprise Unit & Grant Format - Multiple", func(w http.ResponseWriter, _ *http.Request) {
   111  		pageContent := []*types.EnterpriseUnitData{
   112  			getEUData(),
   113  			getEUData(),
   114  		}
   115  		getEnterpriseUnitAndGrantCallback(w, true, pageContent, 0, 1, 2)
   116  	}, func(_ http.ResponseWriter, r *http.Request) bool {
   117  		return r.Method == http.MethodGet && (strings.HasSuffix(r.URL.String(), fmt.Sprintf(enterpriseUnitGrantFormat, "multiple", 0)) || strings.HasSuffix(r.URL.String(), fmt.Sprintf(enterpriseUnitGrantFormatFullname, "multiple", 0)))
   118  	})
   119  
   120  	//Put
   121  	server.Put("Reset User Password", ResetPasswordWithTokenURLPath, resetPasswordCallback, nil)
   122  	server.Put("Update User", "/provisioning/users", func(w http.ResponseWriter, _ *http.Request) { updateUserCallback(w) }, nil)
   123  
   124  	return server.Server
   125  }
   126  
   127  // getMockBSLStoreSite returns a mock response of a BSL Site
   128  func getMockBSLStoreSite(id string) *model.StoreSiteInfo {
   129  	storeName := "test-store"
   130  	day := "Monday"
   131  	description := "test day part"
   132  	name := "store dayparts"
   133  	startTime := "9am"
   134  	endTime := "5pm"
   135  	return &model.StoreSiteInfo{
   136  		ID:                 &id,
   137  		EnterpriseUnitName: "test-enterprise-unit-name",
   138  		SiteName:           storeName,
   139  		Status:             ActiveStatus,
   140  		Coordinates: &model.Coordinates{
   141  			Latitude:  DefaultLatitude,
   142  			Longitude: DefaultLongitude,
   143  		},
   144  		EnterpriseSettings: []*model.EnterpriseSettings{
   145  			{
   146  				EnterpriseUnitID: &storeName,
   147  			},
   148  		},
   149  		Dayparts: []*model.Dayparts{
   150  			{
   151  				Day:         &day,
   152  				Description: &description,
   153  				Name:        &name,
   154  				StartTime:   &startTime,
   155  				EndTime:     &endTime,
   156  			},
   157  		},
   158  	}
   159  }
   160  
   161  // getBSLSiteID trims the /v1/sites/ path to obtain the BSL Site ID
   162  func getBSLSiteID(path string) string {
   163  	return strings.TrimPrefix(path, BslSitePath)
   164  }
   165  
   166  func extractAuth(auth string) (string, string, error) {
   167  	encodedCredentials := strings.Split(auth, " ")[1]
   168  	userAndPassword, err := base64.StdEncoding.DecodeString(encodedCredentials)
   169  	if err != nil {
   170  		return "", "", err
   171  	}
   172  	token := strings.Split(string(userAndPassword), ":")
   173  
   174  	// username may be, e.g. acct:<org>@<name>, with a `:` char.
   175  	// assume password does not include `:` char, and is the last item in token
   176  	// username should be all remaining items joined with a `:` char
   177  	nuParts := len(token)
   178  	password := token[nuParts-1]
   179  	username := strings.Join(token[:nuParts-1], ":")
   180  
   181  	return username, password, nil
   182  }
   183  
   184  func writeResponse(w http.ResponseWriter, v interface{}) {
   185  	res, err := json.Marshal(v)
   186  	if err != nil {
   187  		testutils.WriteHTTPBadResponse(w)
   188  		return
   189  	}
   190  	w.WriteHeader(http.StatusOK)
   191  	_, _ = w.Write(res)
   192  }
   193  
   194  func loginCallback(w http.ResponseWriter, r *http.Request) {
   195  	basicAuth := r.Header.Get("Authorization")
   196  	if !strings.HasPrefix(basicAuth, basicAuthPrefix) {
   197  		testutils.WriteHTTPBadResponse(w)
   198  		return
   199  	}
   200  	username, password, err := extractAuth(basicAuth)
   201  	if err != nil || password == "" {
   202  		testutils.WriteHTTPBadResponse(w)
   203  		return
   204  	}
   205  	authResponse := &types.SecurityTokenData{Token: "anything", Username: username, Authorities: []string{"NEP_IDENTITY_VIEWER"}, MaxSessionTime: 900}
   206  	writeResponse(w, authResponse)
   207  }
   208  
   209  func getUsersCallback(w http.ResponseWriter) {
   210  	emailOne := "johndoe@ncr.com"
   211  	response := &types.FindUsersResponse{
   212  		LastPage: true,
   213  		PageContent: []*model.User{
   214  			{
   215  				Username:   "acct:edge-banner-1@user1",
   216  				FamilyName: "John",
   217  				GivenName:  "Doe",
   218  				FullName:   "John Doe",
   219  				Email:      &emailOne,
   220  				Status:     "ACTIVE",
   221  			},
   222  			{
   223  				Username:   "test-bffuser",
   224  				FamilyName: "BFF",
   225  				GivenName:  "User",
   226  				FullName:   "BFF User",
   227  				Email:      &emailOne,
   228  				Status:     "ACTIVE",
   229  			},
   230  		},
   231  		PageNumber:   1,
   232  		TotalPages:   1,
   233  		TotalResults: 1,
   234  	}
   235  	res, err := json.Marshal(response)
   236  	if err != nil {
   237  		testutils.WriteHTTPBadResponse(w)
   238  		return
   239  	}
   240  	w.WriteHeader(http.StatusOK)
   241  	_, _ = w.Write(res)
   242  }
   243  
   244  func getExternalUsersCallback(w http.ResponseWriter) {
   245  	emailOne := "orguser@ncr.com"
   246  	response := &types.FindUsersResponse{
   247  		LastPage: true,
   248  		PageContent: []*model.User{
   249  			{
   250  				Username:   "acct:test-org-banner@orguser",
   251  				FamilyName: "John",
   252  				GivenName:  "Doe",
   253  				FullName:   "John Doe",
   254  				Email:      &emailOne,
   255  				Status:     "ACTIVE",
   256  			},
   257  		},
   258  		PageNumber:   1,
   259  		TotalPages:   1,
   260  		TotalResults: 1,
   261  	}
   262  	res, err := json.Marshal(response)
   263  	if err != nil {
   264  		testutils.WriteHTTPBadResponse(w)
   265  		return
   266  	}
   267  	w.WriteHeader(http.StatusOK)
   268  	_, _ = w.Write(res)
   269  }
   270  
   271  func findUnitGrantsResponseCallback(w http.ResponseWriter) {
   272  	response := &types.FindUnitGrantsResponse{
   273  		LastPage: true,
   274  		PageContent: []*types.UnitGrantViewData{
   275  			{
   276  				Username:         "acct:test_eu_banner@euuser",
   277  				EnterpriseUnitID: "1",
   278  			},
   279  		},
   280  		PageNumber:   1,
   281  		TotalPages:   1,
   282  		TotalResults: 1,
   283  	}
   284  	res, err := json.Marshal(response)
   285  	if err != nil {
   286  		testutils.WriteHTTPBadResponse(w)
   287  		return
   288  	}
   289  	w.WriteHeader(http.StatusOK)
   290  	_, _ = w.Write(res)
   291  }
   292  
   293  func getEUUsersCallback(w http.ResponseWriter) {
   294  	emailOne := "euuser@ncr.com"
   295  	response := &types.GetUserDetailsResponse{
   296  		Users: []*model.User{
   297  			{
   298  				Username:   "acct:test_eu_banner@euuser",
   299  				FamilyName: "John",
   300  				GivenName:  "Doe",
   301  				FullName:   "John Doe",
   302  				Email:      &emailOne,
   303  				Status:     "ACTIVE",
   304  			},
   305  		},
   306  	}
   307  	res, err := json.Marshal(response)
   308  	if err != nil {
   309  		testutils.WriteHTTPBadResponse(w)
   310  		return
   311  	}
   312  	w.WriteHeader(http.StatusOK)
   313  	_, _ = w.Write(res)
   314  }
   315  
   316  func getUserCallback(w http.ResponseWriter) {
   317  	email := "test@ncr.com"
   318  	user := &model.User{
   319  		Username:   "acct:edge-dev0-edge-b413cb@test",
   320  		FamilyName: "Doe",
   321  		GivenName:  "Peter",
   322  		FullName:   "Peter Doe",
   323  		Email:      &email,
   324  		Status:     "INACTIVE",
   325  	}
   326  	writeResponse(w, user)
   327  }
   328  
   329  func recoverCallback(w http.ResponseWriter, r *http.Request, testResetURL string) {
   330  	body, err := io.ReadAll(r.Body)
   331  	if err != nil {
   332  		writeHTTPBadResponse(w)
   333  		return
   334  	}
   335  	rpReq := &types.RecoverPasswordRequest{}
   336  	err = json.Unmarshal(body, rpReq)
   337  	if err != nil {
   338  		writeHTTPBadResponse(w)
   339  		return
   340  	}
   341  	if rpReq.ResetURL != testResetURL || rpReq.TokenParam != "token" {
   342  		writeHTTPBadResponse(w)
   343  		return
   344  	}
   345  	w.WriteHeader(http.StatusOK)
   346  	_, _ = w.Write(emptyBody)
   347  }
   348  
   349  func getUserProfileCallback(w http.ResponseWriter, testUser, testEmail string) {
   350  	user := &model.User{
   351  		Username:   testUser,
   352  		FamilyName: "doe",
   353  		GivenName:  "john",
   354  		FullName:   "John Doe",
   355  		Email:      &testEmail,
   356  		Status:     "",
   357  	}
   358  	writeResponse(w, user)
   359  }
   360  
   361  func createEnterpriseUnitCallback(w http.ResponseWriter, r *http.Request) {
   362  	body, err := io.ReadAll(r.Body)
   363  	if err != nil {
   364  		testutils.WriteHTTPBadResponse(w)
   365  		return
   366  	}
   367  	eu := &types.EnterpriseUnitData{}
   368  	err = json.Unmarshal(body, eu)
   369  	if err != nil {
   370  		testutils.WriteHTTPBadResponse(w)
   371  		return
   372  	}
   373  	eu.EnterpriseUnitID = "euid"
   374  	res, err := json.Marshal(eu)
   375  	if err != nil {
   376  		testutils.WriteHTTPBadResponse(w)
   377  		return
   378  	}
   379  	w.WriteHeader(http.StatusOK)
   380  	_, _ = w.Write(res)
   381  }
   382  
   383  func getEnterpriseUnitCallback(w http.ResponseWriter) {
   384  	data := getEUData()
   385  	res, err := json.Marshal(data)
   386  	if err != nil {
   387  		testutils.WriteHTTPBadResponse(w)
   388  		return
   389  	}
   390  	w.WriteHeader(http.StatusOK)
   391  	_, _ = w.Write(res)
   392  }
   393  
   394  func getDeletedEnterpriseUnitCallback(w http.ResponseWriter) {
   395  	data := getDeletedEUData()
   396  	res, err := json.Marshal(data)
   397  	if err != nil {
   398  		testutils.WriteHTTPBadResponse(w)
   399  		return
   400  	}
   401  	w.WriteHeader(http.StatusOK)
   402  	_, _ = w.Write(res)
   403  }
   404  
   405  func createOrganizationCallback(w http.ResponseWriter, r *http.Request) {
   406  	bd, err := io.ReadAll(r.Body)
   407  	if err != nil {
   408  		testutils.WriteHTTPBadResponse(w)
   409  		return
   410  	}
   411  	rq := &types.CreateOrganizationRequest{}
   412  	err = json.Unmarshal(bd, rq)
   413  	if err != nil {
   414  		testutils.WriteHTTPBadResponse(w)
   415  		return
   416  	}
   417  	rp := &types.CreateOrganizationResponse{
   418  		ID:               "euid",
   419  		OrganizationName: rq.Name,
   420  		Description:      rq.Description,
   421  	}
   422  	res, err := json.Marshal(rp)
   423  	if err != nil {
   424  		testutils.WriteHTTPBadResponse(w)
   425  		return
   426  	}
   427  	w.WriteHeader(http.StatusOK)
   428  	_, _ = w.Write(res)
   429  }
   430  
   431  func tokenExchangeCallback(w http.ResponseWriter, _ *http.Request) {
   432  	data := &model.AuthPayload{
   433  		Token: "test-token-exchange",
   434  	}
   435  	res, err := json.Marshal(data)
   436  	if err != nil {
   437  		testutils.WriteHTTPBadResponse(w)
   438  		return
   439  	}
   440  	w.WriteHeader(http.StatusOK)
   441  	_, _ = w.Write(res)
   442  }
   443  
   444  func getOrganizationCallback(w http.ResponseWriter, testOrg, testOrgID string) {
   445  	data := getOrgsData(testOrg, testOrgID)
   446  	res, err := json.Marshal(data)
   447  	if err != nil {
   448  		testutils.WriteHTTPBadResponse(w)
   449  		return
   450  	}
   451  	w.WriteHeader(http.StatusOK)
   452  	_, _ = w.Write(res)
   453  }
   454  
   455  func getDeletedOrganizationCallback(w http.ResponseWriter) {
   456  	data := getDeletedOrgsData()
   457  	res, err := json.Marshal(data)
   458  	if err != nil {
   459  		testutils.WriteHTTPBadResponse(w)
   460  		return
   461  	}
   462  	w.WriteHeader(http.StatusOK)
   463  	_, _ = w.Write(res)
   464  }
   465  
   466  func getExternalUserNoDataCallback(w http.ResponseWriter) {
   467  	orgs := &types.GetExternalUserResponse{
   468  		LastPage:     true,
   469  		PageNumber:   0,
   470  		TotalPages:   1,
   471  		TotalResults: 1,
   472  		PageContent:  nil,
   473  	}
   474  	res, err := json.Marshal(orgs)
   475  	if err != nil {
   476  		testutils.WriteHTTPBadResponse(w)
   477  		return
   478  	}
   479  	w.WriteHeader(http.StatusOK)
   480  	_, _ = w.Write(res)
   481  }
   482  
   483  func getExternalUserCallback(w http.ResponseWriter, testOrg, testOrgID string) {
   484  	orgs := getExternalUserDate(testOrg, testOrgID)
   485  	res, err := json.Marshal(orgs)
   486  	if err != nil {
   487  		testutils.WriteHTTPBadResponse(w)
   488  		return
   489  	}
   490  	w.WriteHeader(http.StatusOK)
   491  	_, _ = w.Write(res)
   492  }
   493  
   494  func getBSLSiteCallback(w http.ResponseWriter, r *http.Request) {
   495  	bslSiteID := getBSLSiteID(r.URL.Path)
   496  	bslSite := getMockBSLStoreSite(strings.Trim(bslSiteID, "/"))
   497  	response, err := json.Marshal(bslSite)
   498  	if err != nil {
   499  		testutils.WriteHTTPBadResponse(w)
   500  		return
   501  	}
   502  	w.WriteHeader(200)
   503  	_, _ = w.Write(response)
   504  }
   505  
   506  func createBSLSiteCallback(w http.ResponseWriter, r *http.Request) {
   507  	bd, err := io.ReadAll(r.Body)
   508  	if err != nil {
   509  		writeHTTPBadResponse(w)
   510  		return
   511  	}
   512  	rq := &types.BSLSite{}
   513  	err = json.Unmarshal(bd, rq)
   514  	if err != nil {
   515  		writeHTTPBadResponse(w)
   516  		return
   517  	}
   518  	rq.ID = "test-site-id"
   519  	res, err := json.Marshal(rq)
   520  	if err != nil {
   521  		writeHTTPBadResponse(w)
   522  		return
   523  	}
   524  	w.WriteHeader(http.StatusOK)
   525  	_, _ = w.Write(res)
   526  }
   527  
   528  func resetPasswordCallback(w http.ResponseWriter, r *http.Request) {
   529  	body, err := io.ReadAll(r.Body)
   530  	if err != nil || bytes.Contains(body, []byte("bad-password")) {
   531  		w.WriteHeader(http.StatusBadRequest)
   532  		return
   533  	} else if bytes.Contains(body, []byte("bad-token")) {
   534  		w.WriteHeader(http.StatusForbidden)
   535  		return
   536  	}
   537  	w.WriteHeader(http.StatusNoContent)
   538  }
   539  
   540  func updateUserCallback(w http.ResponseWriter) {
   541  	email := "test@ncr.com"
   542  	user := &model.User{
   543  		Username:   "acct:edge-dev0-edge-b413cb@test",
   544  		FamilyName: "Doe",
   545  		GivenName:  "John",
   546  		FullName:   "John Doe",
   547  		Email:      &email,
   548  		Status:     "ACTIVE",
   549  	}
   550  	writeResponse(w, user)
   551  }
   552  
   553  func getEnterpriseUnitAndGrantCallback(w http.ResponseWriter, lastpage bool, pageContent []*types.EnterpriseUnitData, pageNumber, totalPages, totalResults int) {
   554  	data := &types.FindEnterpriseUnitsResponse{
   555  		LastPage:     lastpage,
   556  		PageContent:  pageContent,
   557  		PageNumber:   pageNumber,
   558  		TotalPages:   totalPages,
   559  		TotalResults: totalResults,
   560  	}
   561  	res, err := json.Marshal(data)
   562  	if err != nil {
   563  		testutils.WriteHTTPBadResponse(w)
   564  	} else {
   565  		w.WriteHeader(http.StatusOK)
   566  		_, _ = w.Write(res)
   567  	}
   568  }
   569  
   570  func getEffectiveOrganizationsCallback(w http.ResponseWriter, pageNumber, totalPages, totalResults int, lastpage bool, pageContent []*types.EffectiveOrganization) {
   571  	data := &types.EffectiveOrganizationsResponse{
   572  		LastPage:     lastpage,
   573  		PageNumber:   pageNumber,
   574  		TotalPages:   totalPages,
   575  		TotalResults: totalResults,
   576  		PageContent:  pageContent,
   577  	}
   578  	res, err := json.Marshal(data)
   579  	if err != nil {
   580  		testutils.WriteHTTPBadResponse(w)
   581  	} else {
   582  		w.WriteHeader(http.StatusOK)
   583  		_, _ = w.Write(res)
   584  	}
   585  }
   586  
   587  func getEffectiveRolesCallback(w http.ResponseWriter) {
   588  	data := &types.EffectiveRolesResponse{
   589  		Content: []types.EffectiveRole{
   590  			{
   591  				RoleName: "NEP_IDENTITY_VIEWER",
   592  			},
   593  			{
   594  				RoleName: "EDGE_ORG_ADMIN",
   595  			},
   596  			{
   597  				RoleName: "CORE_INVITE_DELETE",
   598  			},
   599  			{
   600  				RoleName: "NEP_ENTERPRISE_GRANT_ENTERPRISE_UNIT",
   601  			},
   602  		},
   603  	}
   604  	res, err := json.Marshal(data)
   605  	if err != nil {
   606  		testutils.WriteHTTPBadResponse(w)
   607  	} else {
   608  		w.WriteHeader(http.StatusOK)
   609  		_, _ = w.Write(res)
   610  	}
   611  }
   612  
   613  func notFound(w http.ResponseWriter, r *http.Request) {
   614  	body, err := io.ReadAll(r.Body)
   615  	if err != nil {
   616  		testutils.WriteHTTPBadResponse(w)
   617  		return
   618  	}
   619  	if bytes.Contains(body, []byte("org-not-exist")) {
   620  		w.WriteHeader(http.StatusBadRequest)
   621  		_, _ = w.Write(emptyBody)
   622  	} else {
   623  		w.WriteHeader(http.StatusOK)
   624  		_, _ = w.Write(emptyBody)
   625  	}
   626  }
   627  
   628  func handleGetRoleResponse(w http.ResponseWriter, r *http.Request, testUser, testUserRole string) {
   629  	if strings.Contains(r.URL.String(), "nodata") {
   630  		resp := &types.GroupDataResponse{
   631  			Username: testUser,
   632  		}
   633  		writeResponse(w, resp)
   634  		return
   635  	}
   636  	resp := &types.GroupDataResponse{
   637  		Username: testUser,
   638  		Memberships: []map[string]string{
   639  			{
   640  				"groupName": testUserRole,
   641  			},
   642  		},
   643  	}
   644  	writeResponse(w, resp)
   645  }
   646  
   647  func writeHTTPBadResponse(w http.ResponseWriter) {
   648  	w.WriteHeader(http.StatusBadRequest)
   649  	_, _ = w.Write(emptyBody)
   650  }
   651  
   652  func getEUData() *types.EnterpriseUnitData {
   653  	return &types.EnterpriseUnitData{
   654  		Active:             true,
   655  		Description:        "test_description",
   656  		EnterpriseTypeName: types.IsOrganization,
   657  		EnterpriseUnitID:   "eu-banner-bsl-id",
   658  		Name:               "test-banner-eu",
   659  	}
   660  }
   661  
   662  func getDeletedEUData() *types.EnterpriseUnitData {
   663  	return &types.EnterpriseUnitData{
   664  		Active:             false,
   665  		Description:        "test_description",
   666  		EnterpriseTypeName: types.IsOrganization,
   667  		EnterpriseUnitID:   "eu-banner-bsl-id",
   668  		Name:               "test-banner-eu-deleted-test-time",
   669  	}
   670  }
   671  
   672  func getExternalUserDate(testOrg, testOrgID string) *types.GetExternalUserResponse {
   673  	return &types.GetExternalUserResponse{
   674  		LastPage:     true,
   675  		PageNumber:   0,
   676  		TotalPages:   1,
   677  		TotalResults: 1,
   678  		PageContent: []*types.ExternalUserData{
   679  			{
   680  				ID:               testOrgID,
   681  				OrganizationName: testOrg,
   682  				DisplayName:      testOrg,
   683  				Description:      "test_description",
   684  				Primary:          false,
   685  			},
   686  			{
   687  				ID:               "test-org-banner",
   688  				OrganizationName: "test-org-banner",
   689  				DisplayName:      "test-org-banner",
   690  				Description:      "test_description",
   691  				Primary:          false,
   692  			},
   693  		},
   694  	}
   695  }
   696  
   697  func getOrgsData(testOrg, testOrgID string) *types.GetOrganizationSubOrgsResponse {
   698  	return &types.GetOrganizationSubOrgsResponse{
   699  		LastPage:     true,
   700  		PageNumber:   0,
   701  		TotalPages:   1,
   702  		TotalResults: 1,
   703  		PageContent: []*types.OrganizationViewData{
   704  			{
   705  				ID:               testOrgID,
   706  				OrganizationName: testOrg,
   707  				DisplayName:      testOrg,
   708  				Parent:           true,
   709  				Description:      "test_description",
   710  			},
   711  			{
   712  				ID:               "test-org-banner",
   713  				OrganizationName: "test-org-banner",
   714  				DisplayName:      "test-org-banner",
   715  				Parent:           true,
   716  				Description:      "test_description",
   717  			},
   718  			{
   719  				ID:               "store-status",
   720  				OrganizationName: "store-status-banner",
   721  				DisplayName:      "store-status-banner",
   722  				Parent:           true,
   723  				Description:      "test_description",
   724  			},
   725  			{
   726  				ID:               "store-status02",
   727  				OrganizationName: "store-status-banner02",
   728  				DisplayName:      "store-status-banner02",
   729  				Parent:           true,
   730  				Description:      "test_description",
   731  			},
   732  		},
   733  	}
   734  }
   735  
   736  func getDeletedOrgsData() *types.OrganizationViewData {
   737  	return &types.OrganizationViewData{
   738  		ID:               "test-bsl-id-for-deleted-org",
   739  		OrganizationName: "test-org-deleted",
   740  		DisplayName:      "test-org-deleted",
   741  		Parent:           true,
   742  		Description:      "test_description",
   743  	}
   744  }
   745  
   746  func getEffectiveOrganizations() []*types.EffectiveOrganization {
   747  	return []*types.EffectiveOrganization{
   748  		{
   749  			ID:               "test-id-1",
   750  			DisplayName:      "test-bsl-org-1",
   751  			OrganizationName: "/customers/test-bsl-org-1",
   752  		},
   753  		{
   754  			ID:               "test-id-2",
   755  			DisplayName:      "test-banner",
   756  			OrganizationName: "/customers/test-bsl-org-1/test-banner",
   757  		},
   758  		{
   759  			ID:               "test-id-3",
   760  			DisplayName:      "test-bsl-org-2",
   761  			OrganizationName: "/customers/test-bsl-org-2",
   762  		},
   763  		{
   764  			ID:               "test-id-4",
   765  			DisplayName:      "test-bsl-org-3",
   766  			OrganizationName: "/customers/test-bsl-org-3",
   767  		},
   768  		{
   769  			ID:               "test-id-5",
   770  			DisplayName:      "test-api-banner",
   771  			OrganizationName: "/customers/test-bsl-org-3/test-api-banner",
   772  		},
   773  	}
   774  }
   775  

View as plain text