Authentication Feature Testing in GraphQL
Explore how to effectively test authentication features in GraphQL backend applications using Go. Understand generating JWT tokens, preparing test data, creating test functions, and running tests for sign-up and login scenarios. This lesson helps you build reliable GraphQL authentication tests using practical examples.
We'll cover the following...
Test the application
The testing database is already prepared for testing. To create a test, we add a file called server_test.go to the project’s root directory. After the file is created, we create a function called graphQLHandler() to return the handler for the GraphQL application.
Below is an explanation of the code above:
-
In line 3, the application router is created with the
NewGraphQLHandler()function. -
In line 6, the testing database is connected with the
Connect()function. -
In line 16, the handler is returned.
Next, we create a function called getJWTToken() to generate a JWT token for testing features that require authentication, like creating a new blog.
As seen in the code above, the JWT token is generated with GenerateNewAccessToken() with the ID of the user as a parameter. If the token generation is successful, the JWT token is returned ...