...
1# Guide on how to add BSL API test
2
3## Getting started:
4- 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 :)
5- If we add/edit a service method the mocks needs to be updated. You can do this by running `just update-mocks`
6
7## Writing tests:
8- In `GetMockBspServer`, there are some standard HTTP methods `POST`, `GET`, `PUT` and `ANY`
9- `POST`: create new resources
10- `GET`: retrieve a source
11- `PUT`: update or create a source
12- `ANY`: you can use any of the single HTTP method
13- 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.
14- Let's use this example in `// Post` to create call back for `Login` :
15
16- Make sure the endpoint is correct because a service will make call to that endpoint. For ex: `/security/authentication/login`
17- Create a function underneath called `loginCallback` to mock data when our service method is making that call.
18
19```
20const (
21 basicAuthPrefix = "Basic "
22 ...
23}
24...
25
26func loginCallback(w http.ResponseWriter, r *http.Request) {
27 basicAuth := r.Header.Get("Authorization")
28 if !strings.HasPrefix(basicAuth, basicAuthPrefix) {
29 testutils.WriteHTTPBadResponse(w)
30 return
31 }
32 username, password, err := extractAuth(basicAuth)
33 if err != nil || password == "" {
34 testutils.WriteHTTPBadResponse(w)
35 return
36 }
37 authResponse := &types.SecurityTokenData{Token: "anything", Username: username, Authorities: []string{"NEP_IDENTITY_VIEWER"}, MaxSessionTime: 900}
38 writeResponse(w, authResponse)
39}
40```
41
42- Once finished the function, now add the server method under `// POST`
43
44```
45// Post
46server.Post("Login", "/security/authentication/login", loginCallback, nil)
47```
48
49- Now you will write your `TestLogin`. I will use the example in `user_management_service.go`
50
51- Write your test function:
52
53```
54func TestLogin(t *testing.T) {
55
56}
57```
58
59- Set up your mock bsp server:
60
61```
62 bspMockServer := GetMockBspServer(testOrganization, testOrganization, testUsername, model.RoleEdgeBannerViewer.String(), "testEmail", "testResetURL")
63 appConfig := edgetypes.Config{
64 BSP: types.BSPConfig{
65 Endpoint: bspMockServer.URL,
66 },
67 Bff: edgetypes.BffConfig{TopLevelProjectID: testOrganization},
68 }
69
70```
71
72- Set up bsl client and service:
73
74```
75 bslClient := bsl.NewBSLClient(appConfig.BSP, nil)
76 service := NewUserManagementService(appConfig, bslClient, nil, nil, nil)
77
78```
79
80- 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`:
81
82```
83const (
84 testStoreName = "test-store"
85 testEnterpriseUnitName = "test-enterprise-unit-name"
86 testEuid = "test-euid"
87 testEuidNotExist = "test-euid-not-exist"
88 testUsername = "test-user"
89 testBSLToken = "test-token"
90 testOrganization = "test-org"
91 testBanner = "test-banner"
92 testReferenceID = "test-reference-id"
93 testBannerID = "test-banner"
94)
95```
96
97- Then we verify if the returned user is empty or not
98
99```
100 ctx := context.Background()
101 user, err := service.Login(ctx, testUsername, "testPassword", testOrganization)
102 assert.Nil(t, err)
103 assert.NotEmpty(t, user)
104 assert.Equal(t, 1, len(user.Roles))
105```
106
107
108### If you have any question, please reach out to the Platform team :)
View as plain text