Skip to main content

Overview

Integrating Firebase Authentication with Locust can enhance your load testing by simulating real-world user authentication scenarios.

Setup

1. Set Up Firebase Authentication:
  • Create a Firebase Project: If you haven’t already, create a project in the Firebase Console.
  • Enable Authentication Methods: Navigate to the “Authentication” section and enable the desired sign-in methods, such as Email/Password, Google, or others.
2. Obtain Firebase Authentication Tokens: To authenticate users during load testing, you’ll need to generate Firebase Authentication tokens. This can be achieved using Firebase’s REST API:
  • Email/Password Authentication: Send a POST request to Firebase’s identity toolkit endpoint with the user’s email and password to receive an ID token. Detailed instructions are available in the Firebase Authentication REST API guide.
  • Custom Authentication Systems: If you’re using a custom authentication system, you can integrate it with Firebase Authentication to generate tokens. This process is outlined in the Firebase Authentication documentation.
3. Integrate Firebase Authentication with Locust: Once you have the ID tokens, you can incorporate them into your Locust tests:
  • Include the ID Token in Requests: In your Locust tasks, add the Authorization header with the ID token for each request:
    from locust import HttpUser, task
    
    class FirebaseUser(HttpUser):
        def on_start(self):
            # Obtain the Firebase ID token
            self.id_token = "YOUR_FIREBASE_ID_TOKEN"
    
        @task
        def authenticated_request(self):
            headers = {
                "Authorization": f"Bearer {self.id_token}"
            }
            self.client.get("/your/protected/endpoint", headers=headers)
    
  • Automate Token Retrieval (Optional): For dynamic token generation, implement a method to obtain the ID token programmatically within the on_start method. This ensures each simulated user has a unique token.
I