...
1# Guide to test resolver integration test:
2
3- For this, you will create a test function and a helper function to mock graphql call in order to run your integration test
4- I will use `TestLogin` as an example:
5
6- First you will create a helper function called `loginMutation` to mock the graphql called for the `login`
7- Try to keep all these mock graphql function at the bottom of your test file
8- You can find this mutation by running graphql locally or go to live graphql `https://dev1.edge-preprod.dev/graphiql/`. From there, navigate down to `Query`, open the drop down menu and select `mutation` since we are looking for `login` mutation.
9- Use the sidebar on the left to look for `login` and click on it. Once you click on it, it will show you all the required params, which are `org`, `username` and `password`:
10
11```
12mutation MyMutation {
13 login(organization: "", password: "", username: "")
14}
15
16```
17
18- Now you will write your helper function, that take in `username`, `password` and `org`:
19
20```
21func loginMutation(username, password, organization string) string {
22 return MustParse(graphb.Query{
23 Type: graphb.TypeMutation,
24 Fields: []*graphb.Field{
25 {
26 Name: "login",
27 Arguments: []graphb.Argument{
28 graphb.ArgumentString("username", username),
29 graphb.ArgumentString("password", password),
30 graphb.ArgumentString("organization", organization),
31 },
32 Fields: graphb.Fields("token", "firstName", "fullName", "roles", "credentialsExpired", "sessionTime"),
33 },
34 },
35 })
36}
37```
38
39**Note: if you want to write test for query instead of mutation, just change your `Type` to `Type: graphb.TypeQuery`**
40- Name is the name of your mutation/query
41- Arguments set to all required params
42- Fields: what you want to get back from calling that mutation. These fields **MUST match** the return type in the GraphQL schema. If you aren't sure how the return type looks like, you can browse to its scheme at `pkg/edge/api/graph/schema/` and go to the file you looking for.
43
44
45- Now you can begin writing your test. Set up your test:
46
47```
48func (s *Suite) TestLogin() {
49
50}
51```
52
53- Set up your response:
54
55```
56 integration.SkipIf(s.Framework)
57 var resp struct{ Login *model.AuthPayload }
58```
59
60- Calling the mock graphql function:
61
62```
63 mutation := loginMutation(testUser, "password", testOrg)
64```
65
66- `testUser` and `testOrg` can be found in `pkg/edge/api/graph/integration/resolver_test.go`
67
68```
69var (
70 cfg = &types.TestConfig{}
71 chariotConfig = &chariotClientApi.Config{}
72 err error
73 ResolverClient GraphqlClient
74
75 testOrg = "test-org"
76 testRootOrg = "/bsl-root/test-org/"
77 testRegion = "region-infra"
78 testClusterNumNodes = 3
79 testClusterVersion = "1.21.4-gke.2300"
80 testClusterZone = "us-east1-b"
81 testClusterMachineType = "n1-standard-4"
82 testUser = "acct:emerald-edge-dev@testing"
83 testEmail = "jd250001@ncr.com"
84 testUserName = "testing"
85...
86
87```
88
89- Lastly, verify all returned values from that graphql mutation to see if they are empty or not
90
91```
92 s.NoError(ResolverClient.Post(mutation, &resp))
93 s.NotNil(resp.Login)
94 s.NotEmpty(resp.Login.Token)
95 s.False(resp.Login.CredentialsExpired)
96 s.NotEmpty(resp.Login.FullName)
97 s.NotEmpty(resp.Login.FirstName)
98 s.Contains(resp.Login.Roles, "EDGE_ORG_ADMIN")
99 s.Equal(resp.Login.SessionTime, 15.0)
100```
101
102- Test data for user can be found `pkg/edge/api/graph/test/test_data_seeding.go`. In here you can add new testing data you like to have.
103
104
105- Align them with all listed in `Fields` of your function: `Fields: graphb.Fields("token", "firstName", "fullName", "roles", "credentialsExpired", "sessionTime")`
106
107- Once you're done, go to your terminal and run `bazel test //pkg/edge/api/graph/integration:integration_test` to run your integration test
108
109### If there is any question or confusion on this guide, you can use some other tests in this folder or reach out to the Platform team at `#ncr-edge-api` or `#ncr-edge-backend` for more guidance. Thank you! :D
View as plain text