DEV Community

Mohammad Waseem
Mohammad Waseem

Posted on

Streamlining Test Account Management with Node.js and Open Source Tools

Streamlining Test Account Management with Node.js and Open Source Tools

Efficiently managing test accounts is a common challenge faced by QA engineers, especially when testing environments require multiple user accounts with varying roles and permissions. Manual creation and teardown of these accounts can be error-prone and time-consuming, hampering the overall testing cycle. In this article, we'll explore how a Lead QA Engineer can leverage Node.js coupled with open source tools to automate and streamline the management of test accounts.

The Challenge

Test environments often demand dynamic provisioning of accounts, resetting their states, and ensuring data isolation across tests. Traditional approaches—manual setup or static scripts—fail to scale or adapt to rapid CI/CD pipelines. To address this, automation becomes essential.

Our Approach

We'll build a solution that programmatically manages test accounts using Node.js, utilizing open-source packages such as axios for HTTP requests, dotenv for environment management, and node-schedule for scheduling maintenance tasks. The core idea is to create or reset accounts as needed, authenticate them, and keep the environment ready for continuous testing.

Implementation Details

1. Setup

First, initialize a Node.js project and install necessary packages:

npm init -y
npm install axios dotenv node-schedule
Enter fullscreen mode Exit fullscreen mode

Create a .env file to store your API endpoint and credentials:

API_URL=https://api.example.com
ADMIN_TOKEN=your_admin_token
Enter fullscreen mode Exit fullscreen mode

2. Managing Accounts

Create a script manageAccounts.js that handles account creation, deletion, and reset operations:

require('dotenv').config();
const axios = require('axios');
const schedule = require('node-schedule');

const apiUrl = process.env.API_URL;
const adminToken = process.env.ADMIN_TOKEN;

// Function to create a test account
async function createTestAccount() {
  const response = await axios.post(`${apiUrl}/accounts`, {
    username: `test_user_${Date.now()}`,
    role: 'tester',
    status: 'active'
  }, {
    headers: { Authorization: `Bearer ${adminToken}` }
  });
  console.log(`Created account: ${response.data.username}`);
  return response.data;
}

// Function to reset account data
async function resetAccount(accountId) {
  await axios.post(`${apiUrl}/accounts/${accountId}/reset`, {}, {
    headers: { Authorization: `Bearer ${adminToken}` }
  });
  console.log(`Reset account ID: ${accountId}`);
}

// Schedule daily cleanup at midnight
schedule.scheduleJob('0 0 * * *', async () => {
  console.log('Starting scheduled cleanup...');
  const accounts = await axios.get(`${apiUrl}/accounts?role=tester`, {
    headers: { Authorization: `Bearer ${adminToken}` }
  });
  for (const account of accounts.data) {
    await resetAccount(account.id);
  }
});

// Example usage: create and reset an account
(async () => {
  const account = await createTestAccount();
  // Perform tests...
  // Optionally reset after tests
  await resetAccount(account.id);
})();
Enter fullscreen mode Exit fullscreen mode

This script allows you to automate account creation, reset their states before or after tests, and schedule routine cleanup. You can extend it further by integrating with your CI pipeline or adding more sophisticated account lifecycle management.

Leveraging Open Source Tools

  • Axios: Facilitates HTTP requests to your application's API endpoints, enabling programmatic account management.
  • Dotenv: Manages environment variables securely, avoiding hardcoded sensitive data.
  • Node-schedule: Schedules recurring tasks for routine cleanup, ensuring environment readiness without manual intervention.

Conclusion

Automating test account management significantly enhances testing efficiency and reliability. By leveraging Node.js and open source tools, QA teams can build robust, scalable solutions that integrate seamlessly into their workflows. This approach reduces manual effort, minimizes errors, and accelerates release cycles, enabling rapid, confident deployments.

For further scalability, consider integrating cloud-based identity and access management systems or containerized environments to dynamically provision test accounts in isolated sessions.

References:


🛠️ QA Tip

To test this safely without using real user data, I use TempoMail USA.

Top comments (0)