Coverage for data_factory.py: 100%
37 statements
« prev ^ index » next coverage.py v7.13.5, created at 2026-04-03 04:37 +0000
« prev ^ index » next coverage.py v7.13.5, created at 2026-04-03 04:37 +0000
1"""
2Test Data Factory using Faker library for generating realistic test data.
3This module provides utilities for creating test data for API automation.
4"""
6import time
7from typing import Any
9from faker import Faker
11fake = Faker()
14class UserDataFactory:
15 """Factory class for generating user-related test data."""
17 @staticmethod
18 def generate_unique_email(prefix: str = "test", domain: str = "example.com") -> str:
19 """
20 Generates a unique email address based on timestamp.
22 Args:
23 prefix: Email prefix/username part
24 domain: Email domain (default: example.com)
26 Returns:
27 Unique email address string
28 """
29 timestamp = int(time.time() * 1000)
30 return f"{prefix}_{timestamp}@{domain}"
32 @staticmethod
33 def random_name(locale: str = "en_US") -> str:
34 """
35 Generates a random realistic name.
37 Args:
38 locale: Locale for name generation (default: en_US)
40 Returns:
41 Random full name
42 """
43 fake_locale = Faker(locale)
44 return fake_locale.name()
46 @staticmethod
47 def random_password(length: int = 12, include_special: bool = True) -> str:
48 """
49 Generates a random password meeting complexity requirements.
51 Args:
52 length: Password length (default: 12)
53 include_special: Include special characters (default: True)
55 Returns:
56 Random secure password
57 """
58 import secrets
59 import string
61 # Ensure password meets complexity requirements
62 password = [
63 secrets.choice(string.ascii_uppercase), # At least one uppercase
64 secrets.choice(string.ascii_lowercase), # At least one lowercase
65 secrets.choice(string.digits), # At least one digit
66 ]
68 if include_special:
69 password.append(secrets.choice("!@#$%^&*"))
71 # Fill the rest with random characters
72 remaining_length = length - len(password)
73 all_chars = string.ascii_letters + string.digits
74 if include_special:
75 all_chars += "!@#$%^&*"
77 password.extend(secrets.choice(all_chars) for _ in range(remaining_length))
79 # Shuffle using secrets for security
80 # Convert to list, shuffle indices, and reconstruct
81 indices = list(range(len(password)))
82 shuffled_password = []
83 while indices:
84 idx = secrets.choice(indices)
85 shuffled_password.append(password[idx])
86 indices.remove(idx)
87 return "".join(shuffled_password)
89 @staticmethod
90 def create_signup_payload(
91 name: str | None = None, email: str | None = None, password: str | None = None, **kwargs
92 ) -> dict[str, Any]:
93 """
94 Creates a complete signup payload with optional custom values.
96 Args:
97 name: User name (generated if None)
98 email: Email address (generated if None)
99 password: Password (generated if None)
100 **kwargs: Additional fields to include in payload
102 Returns:
103 Complete signup payload dictionary
104 """
105 payload = {
106 "name": name or UserDataFactory.random_name(),
107 "email": email or UserDataFactory.generate_unique_email(),
108 "password": password or UserDataFactory.random_password(),
109 }
111 # Add any additional fields
112 payload.update(kwargs)
114 return payload