# Advanced Scenarios

# Scenario 1: Dynamic Environment Selection

In this scenario, we'll implement a dynamic environment selection process based on a configuration variable.

async function selectEnvironment() {
    // Retrieve stored configuration
    const { targetEnvironment } = await getVariables();
    
    // Define environment configurations
    const environments = {
        'dev': {
            baseUrl: 'https://dev-api.kusho.com',
            apiKey: 'dev-key-123'
        },
        'staging': {
            baseUrl: 'https://staging-api.kusho.com',
            apiKey: 'staging-key-456'
        },
        'prod': {
            baseUrl: 'https://api.kusho.com',
            apiKey: 'prod-key-789'
        }
    };
    
    // Select the appropriate environment
    const selectedEnv = environments[targetEnvironment] || environments['dev'];
    
    // Set the environment variables
    setVariables(selectedEnv);
    
    debug(`Environment set to: ${targetEnvironment}`);
}

// Execute the environment selection process
await selectEnvironment();

# Scenario 2: Test Data Generation

This scenario demonstrates how to generate test data for use across multiple tests.

function generateTestData() {
    const testUsers = [];
    const numUsers = 5;
    
    for (let i = 0; i < numUsers; i++) {
        testUsers.push({
            id: i + 1,
            name: `User${i + 1}`,
            email: `user${i + 1}@kusho.com`,
            role: i % 2 === 0 ? 'admin' : 'user'
        });
    }
    
    setVariables({ testUsers: testUsers });
    
    debug(`Generated ${numUsers} test users`);
}

// Execute the test data generation
generateTestData();

# Example: Encrypting Sensitive Data

In this example, we'll demonstrate how to encrypt sensitive data like API keys or passwords before storing them as variables. We'll use the built-in crypto module in Node.js for encryption.

const crypto = require('crypto');

// Define the encryption key and algorithm
const encryptionKey = 'your-secret-encryption-key';
const algorithm = 'aes-256-cbc';

// Example sensitive data to encrypt
const apiKey = 'your-api-key';
const password = 'your-password';

// Encrypt the sensitive data
function encrypt(text) {
  const iv = crypto.randomBytes(16);
  const cipher = crypto.createCipheriv(algorithm, Buffer.from(encryptionKey), iv);
  let encrypted = cipher.update(text);
  encrypted = Buffer.concat([encrypted, cipher.final()]);
  return { 
    iv: iv.toString('hex'), 
    encryptedData: encrypted.toString('hex') 
  };
}

// Encrypt the API key and password
const encryptedApiKey = encrypt(apiKey);
const encryptedPassword = encrypt(password);

// Store the encrypted data as variables
setVariables({
  encryptedApiKey: encryptedApiKey.encryptedData,
  encryptedPassword: encryptedPassword.encryptedData,
  encryptionIv: encryptedApiKey.iv
});

debug('Sensitive data encrypted and stored as variables');

In this example:

  1. We import the crypto module to access the built-in encryption functions.
  2. We define the encryption key and algorithm to be used for encryption. Make sure to replace 'your-secret-encryption-key' with your actual encryption key.
  3. We define the sensitive data to be encrypted, in this case, an API key and a password.
  4. We create an encrypt function that takes the plaintext data, generates a random initialization vector (IV), creates a cipher using the encryption key and IV, and encrypts the data.
  5. We call the encrypt function for the API key and password, storing the encrypted data and IV.
  6. We store the encrypted data and IV as variables using setVariables.
  7. Finally, we log a debug message indicating that the sensitive data has been encrypted and stored.

When you need to use the encrypted data in your tests, you can retrieve the variables and decrypt the data using the same encryption key and IV.Remember to keep your encryption key secure and never share it in your code or commit it to version control. It's recommended to store the encryption key as an environment variable or use a secure key management service.By implementing encryption in your pre-run scripts, you can securely store sensitive data and protect it from unauthorized access.

Share

Rewrite