Lesson 4
Testing Authenticated Endpoints
Testing Authenticated Endpoints

Welcome to the last stop of our journey through automated API testing with Python. In this lesson, we'll focus on testing authenticated API endpoints. While you've already learned to organize tests and handle CRUD operations using pytest, today we'll delve into how APIs manage secure access through different authentication methods — API Keys, Sessions, and JWT (JSON Web Tokens).

Authentication is crucial for securing API endpoints against unauthorized access. By understanding how to test these mechanisms, you'll ensure that your API maintains its integrity and protects sensitive data. We'll explore practical examples for each authentication method, giving you the tools to verify that only authorized users can interact with protected resources.

API Key Authentication

API Key authentication is one of the simplest ways to secure an API. It involves sending a unique key as part of the request headers, allowing access to protected endpoints. Let's look at an example of how you can set up and test API Key authentication using the following code:

Python
1import pytest 2import requests 3 4BASE_URL = "http://localhost:8000" 5API_KEY = "123e4567-e89b-12d3-a456-426614174000" 6 7class TestAPIKeyAuthentication: 8 9 def test_API_KEY_authentication(self): 10 # Arrange 11 headers = {"X-API-Key": API_KEY} 12 13 # Act 14 response = requests.get(f"{BASE_URL}/todos", headers=headers) 15 16 # Assert 17 assert response.status_code == 200

Here, we define a test class TestAPIKeyAuthentication. Within the test method, test_API_KEY_authentication, we specify the headers, including the X-API-Key. We use the requests.get method to make a call to the /todos endpoint and pass the headers, ensuring the API key is included in the request. The test then asserts that the response status code is 200, indicating successful authentication and access to the endpoint.

Session Authentication: Setup

We begin our session-based authentication by establishing a fixture for user credentials and a helper method for login. This enables each test to authenticate independently, providing a consistent environment to test both login functionality and access to protected endpoints:

Python
1import pytest 2import requests 3 4BASE_URL = "http://localhost:8000" 5 6class TestSessionAuthentication: 7 8 @pytest.fixture 9 def auth_details(self): 10 # Arrange - Providing user credentials 11 return { 12 "username": "testuser", 13 "password": "testpass123" 14 } 15 16 # Helper method for sending a login POST request 17 def login(self, session, auth_details): 18 # Sends a login request and maintains session state 19 return session.post(f"{BASE_URL}/auth/login", json=auth_details)

The auth_details fixture supplies the required credentials, and the login helper method facilitates independent authentication for each test.

Session Authentication: Testing Login

This test leverages the login helper method to authenticate independently, validating that user credentials create a session successfully:

Python
1def test_login(self, auth_details): 2 # Arrange - Initialize session 3 session = requests.Session() 4 5 # Act - Execute the login process using the helper method 6 login_response = self.login(session, auth_details) 7 8 # Assert - Verify that the login was successful 9 assert login_response.status_code == 200

The use of the helper ensures that this test does not rely on any previous operations, providing isolated verification of login success via status code.

Session Authentication: Testing Protected Endpoint

In this test, we independently authenticate using the helper method before confirming that an established session grants access to a protected endpoint:

Python
1def test_access_with_session(self, auth_details): 2 # Arrange - Use the login helper method to authenticate 3 session = requests.Session() 4 self.login(session, auth_details) 5 6 # Act - Request the protected resource using the session 7 response = session.get(f"{BASE_URL}/todos") 8 9 # Assert - Confirm access is granted with a successful status code 10 assert response.status_code == 200

The helper method's strategic use ensures that each test individually verifies session-based access, unaffected by results of prior tests.

Session Authentication: Testing Logout

By using the login helper method, this test independently authenticates and then verifies the ability to terminate a session, ensuring logout functionality is effective:

Python
1def test_logout_with_session(self, auth_details): 2 # Arrange - Use the login helper method to authenticate 3 session = requests.Session() 4 self.login(session, auth_details) 5 6 # Act - Perform a logout operation to terminate the session 7 logout_response = session.post(f"{BASE_URL}/auth/logout") 8 9 # Assert - Verify logout success via status code 10 assert logout_response.status_code == 200

This approach confirms isolation, as each session is independently managed, ensuring logout integrity without relying on previous tests.

JWT Authentication: Setup

For JWT-based authentication, we replicate this strategy with a fixture for credentials and a login helper method, enabling each test to independently obtain and manage JWTs:

Python
1import pytest 2import requests 3 4BASE_URL = "http://localhost:8000" 5 6class TestJWTAuthentication: 7 8 @pytest.fixture 9 def auth_details(self): 10 # Arrange - User credentials for JWT 11 return { 12 "username": "testuser", 13 "password": "testpass123" 14 } 15 16 # Helper method for sending a login POST request 17 def login(self, auth_details): 18 # Obtain JWT tokens with login request 19 return requests.post(f"{BASE_URL}/auth/login", json=auth_details)

This setup empowers each test to authenticate independently, retrieving tokens without relying on the order or success of other tests.

JWT Authentication: Testing Login

Using the login helper method, this test independently verifies that successful authentication results in token issuance, forming the basis for accessing protected resources:

Python
1def test_login(self, auth_details): 2 # Act - Authenticate and retrieve JWT tokens 3 login_response = self.login(auth_details) 4 5 # Assert - Confirm receipt of tokens and check their existence 6 assert login_response.status_code == 200 7 tokens = login_response.json() 8 assert "access_token" in tokens 9 assert "refresh_token" in tokens

By independently logging in, the test ensures token validity regardless of prior tests, establishing the foundation for secure access.

JWT Authentication: Testing Protected Endpoint

This test independently authenticates using the helper method and verifies that a JWT access token provides entry to a protected endpoint:

Python
1def test_access_with_jwt(self, auth_details): 2 # Arrange - Authenticate and obtain an access token using the helper method 3 login_response = self.login(auth_details) 4 access_token = login_response.json()['access_token'] 5 6 # Act - Use the access token to request a protected endpoint 7 headers = {"Authorization": f"Bearer {access_token}"} 8 response = requests.get(f"{BASE_URL}/todos", headers=headers) 9 10 # Assert - Confirm the request was successful by checking the status code 11 assert response.status_code == 200

By ensuring the test is independent, this approach validates the token's effectiveness in protecting resources, confirming stateless authentication.

JWT Authentication: Testing Logout

Finally, this test independently authenticates to verify that access and refresh tokens can be invalidated, securing the system against unauthorized reuse:

Python
1def test_jwt_logout(self, auth_details): 2 # Arrange - Authenticate and prepare tokens using the helper method 3 login_response = self.login(auth_details) 4 access_token = login_response.json()['access_token'] 5 refresh_token = login_response.json()['refresh_token'] 6 7 headers = {"Authorization": f"Bearer {access_token}"} 8 9 # Act - Use access and refresh tokens to process logout 10 logout_response = requests.post(f"{BASE_URL}/auth/logout", headers=headers, json={"refresh_token": refresh_token}) 11 12 # Assert - Verify logout success through confirmation of status code 13 assert logout_response.status_code == 200

This test confirms the secure handling and invalidation of tokens, ensuring each test independently checks the integrity of the logout process.

Summary and Practices

Throughout this lesson, we've explored different methods of API authentication: API Keys, Sessions, and JWTs. Each method has distinct ways to test and secure access, and you've seen how to implement tests for them effectively.

When testing any form of authentication, it’s crucial to handle API credentials with care. Avoid hardcoding sensitive information in your codebase, and consider using environment variables or secure vaults in production environments.

As we conclude this lesson and the course, take pride in the journey you've completed. You've gained a comprehensive understanding of how to automate API tests using Python, mastering basic requests through to secure, authenticated endpoints. Continue to practice and explore, applying these skills to ensure robust, reliable APIs in your projects. Congratulations on reaching the end of this course!

Enjoy this lesson? Now it's time to practice with Cosmo!
Practice is how you turn knowledge into actual skills.