📖 Scenario: You are building a simple Flask web app that requires users to log in. You want to write tests to check if the login and logout processes work correctly.
🎯 Goal: Create a test suite using Flask's test client to verify the authentication flow: logging in with correct credentials, failing with wrong credentials, and logging out.
📋 What You'll Learn
Create a Flask app with a login route and a logout route
Set up a test client for the Flask app
Write a test to check successful login with username 'user' and password 'pass'
Write a test to check login failure with wrong password
Write a test to check logout clears the session
💡 Why This Matters
🌍 Real World
Testing authentication flows is essential to ensure users can securely log in and out of web applications without errors.
💼 Career
Many web developer and QA roles require writing tests for user authentication to maintain app security and reliability.
Progress0 / 4 steps
1
Set up Flask app with login and logout routes
Create a Flask app instance called app. Define a route /login that accepts POST requests and checks if the form data has username equal to 'user' and password equal to 'pass'. If correct, set session['logged_in'] = True and return 'Logged in'. Otherwise, return 'Failed'. Also define a route /logout that clears the session and returns 'Logged out'.
Flask
Hint
Use Flask(__name__) to create the app. Use @app.route decorators for routes. Use session to store login state.
2
Set up Flask test client
Create a variable called client by calling app.test_client(). This will be used to send test requests to your Flask app.
Flask
Hint
Use app.test_client() to create the test client.
3
Write test for successful login
Use the client to send a POST request to /login with form data {'username': 'user', 'password': 'pass'}. Store the response in a variable called response. Then check if b'Logged in' is in response.data.
Flask
Hint
Use client.post with data= for form data. Check response.data for the expected bytes.
4
Write tests for failed login and logout
Write a test that sends a POST request to /login with wrong password {'username': 'user', 'password': 'wrong'} and checks that b'Failed' is in the response data. Then send a GET request to /logout and check that b'Logged out' is in the response data.
Flask
Hint
Use client.post with wrong password and check for failure message. Use client.get for logout and check for logout message.
Practice
(1/5)
1. What is the main purpose of testing authentication flows in a Flask application?
easy
A. To test if the server starts without errors
B. To check if the database connection is working
C. To ensure the UI layout is responsive
D. To verify that only authorized users can access protected routes
Solution
Step 1: Understand authentication flow purpose
Authentication flows control user access to secure parts of the app.
Step 2: Identify the goal of testing these flows
Testing ensures only users with correct credentials can access protected routes.
Final Answer:
To verify that only authorized users can access protected routes -> Option D
Quick Check:
Authentication testing = Access control verification [OK]
Hint: Authentication tests check user access rights only [OK]
Common Mistakes:
Confusing authentication with UI or server startup tests
Thinking database connection tests are part of authentication
Assuming authentication tests check layout or styling
2. Which Flask test client method is used to simulate a POST request for login in authentication tests?
easy
A. client.get()
B. client.put()
C. client.post()
D. client.delete()
Solution
Step 1: Identify HTTP method for login submission
Login forms usually send data via POST requests to submit credentials.
Step 2: Match Flask test client method
client.post() simulates POST requests in Flask tests.
Final Answer:
client.post() -> Option C
Quick Check:
POST request for login = client.post() [OK]
Hint: Use client.post() to simulate form submissions [OK]
Common Mistakes:
Using client.get() for login POST requests
Confusing HTTP methods like PUT or DELETE for login
Not passing data with the POST request
3. Given this Flask test code snippet, what will response.status_code be after a successful login?