# Guide on how to add BSL API test ## Getting started: - Make sure the function you want to add in `bsl_mock_server.go` exists in the schema/graphql. Can't test something that doesn't exist right :) - If we add/edit a service method the mocks needs to be updated. You can do this by running `just update-mocks` ## Writing tests: - In `GetMockBspServer`, there are some standard HTTP methods `POST`, `GET`, `PUT` and `ANY` - `POST`: create new resources - `GET`: retrieve a source - `PUT`: update or create a source - `ANY`: you can use any of the single HTTP method - If you need to add any of bsp server call, make sure you add them in their according location in this function. You will see a comment telling you where to place them. - Let's use this example in `// Post` to create call back for `Login` : - Make sure the endpoint is correct because a service will make call to that endpoint. For ex: `/security/authentication/login` - Create a function underneath called `loginCallback` to mock data when our service method is making that call. ``` const ( basicAuthPrefix = "Basic " ... } ... func loginCallback(w http.ResponseWriter, r *http.Request) { basicAuth := r.Header.Get("Authorization") if !strings.HasPrefix(basicAuth, basicAuthPrefix) { testutils.WriteHTTPBadResponse(w) return } username, password, err := extractAuth(basicAuth) if err != nil || password == "" { testutils.WriteHTTPBadResponse(w) return } authResponse := &types.SecurityTokenData{Token: "anything", Username: username, Authorities: []string{"NEP_IDENTITY_VIEWER"}, MaxSessionTime: 900} writeResponse(w, authResponse) } ``` - Once finished the function, now add the server method under `// POST` ``` // Post server.Post("Login", "/security/authentication/login", loginCallback, nil) ``` - Now you will write your `TestLogin`. I will use the example in `user_management_service.go` - Write your test function: ``` func TestLogin(t *testing.T) { } ``` - Set up your mock bsp server: ``` bspMockServer := GetMockBspServer(testOrganization, testOrganization, testUsername, model.RoleEdgeBannerViewer.String(), "testEmail", "testResetURL") appConfig := edgetypes.Config{ BSP: types.BSPConfig{ Endpoint: bspMockServer.URL, }, Bff: edgetypes.BffConfig{TopLevelProjectID: testOrganization}, } ``` - Set up bsl client and service: ``` bslClient := bsl.NewBSLClient(appConfig.BSP, nil) service := NewUserManagementService(appConfig, bslClient, nil, nil, nil) ``` - Make call to login. You pass in required params for `service.Login`, which are `context`, `testUsername`, `testPassword`, and `testOrg`. These test vars can be found in `pkg/edge/api/services/bsp_site_service_test.go`: ``` const ( testStoreName = "test-store" testEnterpriseUnitName = "test-enterprise-unit-name" testEuid = "test-euid" testEuidNotExist = "test-euid-not-exist" testUsername = "test-user" testBSLToken = "test-token" testOrganization = "test-org" testBanner = "test-banner" testReferenceID = "test-reference-id" testBannerID = "test-banner" ) ``` - Then we verify if the returned user is empty or not ``` ctx := context.Background() user, err := service.Login(ctx, testUsername, "testPassword", testOrganization) assert.Nil(t, err) assert.NotEmpty(t, user) assert.Equal(t, 1, len(user.Roles)) ``` ### If you have any question, please reach out to the Platform team :)