0
0
Expressframework~10 mins

Testing authentication flows in Express - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Testing authentication flows
Start Test
Send Login Request
Check Credentials
Generate Token
Send Response
Verify Response
End Test
This flow shows how a test sends a login request, checks credentials, returns a token or error, and verifies the response.
Execution Sample
Express
test('login success', async () => {
  const res = await request(app)
    .post('/login')
    .send({ username: 'user', password: 'pass' });
  expect(res.statusCode).toBe(200);
  expect(res.body.token).toBeDefined();
});
This test sends a login request and checks if the response status is 200 and a token is returned.
Execution Table
StepActionInputInternal CheckOutputTest Assertion
1Send POST /login{username:'user', password:'pass'}N/ARequest sentN/A
2Check credentialsuser/passCredentials valid?YesN/A
3Generate tokenuser infoToken createdToken stringN/A
4Send responseToken stringResponse status 200{token: '...'}N/A
5Verify response{token: '...'}Status 200 and token presentPassexpect(res.statusCode).toBe(200) and expect(res.body.token).toBeDefined()
6Test endsN/AN/ATest passedTest completes successfully
💡 Test ends after verifying response status and token presence
Variable Tracker
VariableStartAfter Step 2After Step 3After Step 4Final
res.statusCodeundefinedundefinedundefined200200
res.body.tokenundefinedundefinedtoken stringtoken stringtoken string
Key Moments - 2 Insights
Why do we check both status code and token in the response?
Because status code 200 means success, but token presence confirms authentication succeeded. See execution_table rows 4 and 5.
What happens if credentials are invalid?
The internal check fails at step 2, and the server returns an error response instead of a token. This stops the test from passing.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the status code after step 4?
A200
B401
C500
Dundefined
💡 Hint
Check the 'Output' column at step 4 in the execution_table.
At which step is the token generated?
AStep 4
BStep 2
CStep 3
DStep 5
💡 Hint
Look at the 'Action' column in execution_table where token creation happens.
If the credentials were invalid, what would change in the execution table?
AResponse status would be 200
BStep 2 would show 'No' and no token generated
CStep 3 would generate a token anyway
DTest would pass
💡 Hint
Refer to key_moments about invalid credentials and execution_table step 2.
Concept Snapshot
Testing authentication flows in Express:
- Send POST request with credentials
- Server checks credentials
- If valid, generate token and respond 200
- If invalid, respond with error
- Test asserts status and token presence
- Use supertest for request simulation
Full Transcript
This visual execution shows how to test authentication flows in Express. The test sends a login request with username and password. The server checks if credentials are valid. If yes, it generates a token and sends it back with status 200. The test then verifies the response status and token presence to confirm success. If credentials are invalid, the server returns an error and no token. The test fails in that case. This step-by-step trace helps beginners see how requests, checks, and responses happen in authentication testing.