📖 Scenario: You are building a simple Express server that handles user login authentication. You want to test the authentication flow to ensure users can log in with correct credentials and are rejected otherwise.
🎯 Goal: Build a basic Express app with a user data setup, a configuration for a test user, a login route that checks credentials, and a final middleware to handle authentication results.
📋 What You'll Learn
Create a user data object with a username and password
Add a configuration variable for the test username
Implement a login route that checks the username and password
Add a middleware that sends a success or failure response
💡 Why This Matters
🌍 Real World
Testing authentication flows is essential for any web app that requires user login. This ensures only valid users can access protected resources.
💼 Career
Understanding how to build and test authentication routes is a key skill for backend developers working with Express or similar frameworks.
Progress0 / 4 steps
1
DATA SETUP: Create user data
Create a constant object called users with one entry: username 'testuser' and password 'mypassword'.
Express
Hint
Use a JavaScript object to store username and password pairs.
2
CONFIGURATION: Add test username
Add a constant called testUsername and set it to the string 'testuser'.
Express
Hint
This variable will help us check the login for this specific user.
3
CORE LOGIC: Implement login route
Create an Express POST route at /login that reads username and password from req.body. Check if users[username] equals password. If yes, set req.authenticated = true, else req.authenticated = false.
Express
Hint
Use req.body to get login data and set a flag req.authenticated accordingly.
4
COMPLETION: Add response middleware
Add a middleware after the login route that sends a JSON response with { success: true } if req.authenticated is true, else { success: false }. Export the app object.
Express
Hint
This middleware sends the final response based on authentication result.
Practice
(1/5)
1. What is the main purpose of testing authentication flows in an Express app?
easy
A. To check if the server can handle large amounts of traffic
B. To verify users can log in and access protected routes correctly
C. To ensure the database schema is optimized
D. To test the styling of the login page
Solution
Step 1: Understand authentication flow testing
Testing authentication flows means checking if users can log in and access parts of the app that require login.
Step 2: Identify the main goal
The main goal is to verify correct login and access control, not performance or styling.
Final Answer:
To verify users can log in and access protected routes correctly -> Option B
Quick Check:
Authentication testing = verify login and access [OK]
Hint: Authentication tests check login and access only [OK]
Common Mistakes:
Confusing authentication testing with performance testing
Thinking styling or database optimization is tested here
Assuming authentication tests check UI design
2. Which of the following is the correct way to import and use supertest for testing an Express app's authentication route?
easy
A. const request = require('supertest'); const app = require('./app');
B. const supertest = import('supertest'); const app = require('./app');
C. import request from 'supertest'; import app from './app';
D. import * as request from 'supertest'; const app = require('./app');
Solution
Step 1: Identify CommonJS import syntax
Most Express apps use CommonJS syntax with require, so const request = require('supertest'); is correct.
Step 2: Match import style for app
Similarly, const app = require('./app'); is correct for CommonJS modules.
But the test fails even when the server returns 200. What is the likely mistake?
medium
A. The test should use GET instead of POST
B. The test is missing async/await keywords
C. The server does not properly check password and returns 200 anyway
D. The test should expect 404 instead of 401
Solution
Step 1: Analyze test expectation vs server behavior
The test expects 401 for wrong password, but server returns 200, so server likely accepts wrong password.
Step 2: Identify root cause
The server probably lacks proper password validation, causing incorrect success response.
Final Answer:
The server does not properly check password and returns 200 anyway -> Option C
Quick Check:
Wrong password but 200 means server validation missing [OK]
Hint: Check server password validation if test expects 401 but gets 200 [OK]
Common Mistakes:
Assuming test syntax error without checking server logic
Using wrong HTTP method for login
Expecting wrong status code for authentication failure
5. You want to test that after a successful login, the user can access a protected route using the received token. Which sequence correctly tests this flow using supertest?
hard
A. Post login with wrong password, then get protected route with token
B. Get protected route first, then post login with token in body
C. Post login without saving token, then get protected route without headers
D. Post login, save token from response, then get protected route with Authorization header set to token
Solution
Step 1: Understand authentication token flow
After login, server returns a token. This token must be sent in Authorization header to access protected routes.
Step 2: Identify correct test sequence
First post login, save token from response, then use token in Authorization header for protected route request.
Final Answer:
Post login, save token from response, then get protected route with Authorization header set to token -> Option D
Quick Check:
Login -> save token -> use token in header for protected route [OK]
Hint: Save token after login, then use it in Authorization header [OK]
Common Mistakes:
Trying to access protected route before login
Not sending token in Authorization header
Using wrong password in login step
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.