Coverage for data_factory.py: 100%

37 statements  

« 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""" 

5 

6import time 

7from typing import Any 

8 

9from faker import Faker 

10 

11fake = Faker() 

12 

13 

14class UserDataFactory: 

15 """Factory class for generating user-related test data.""" 

16 

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. 

21 

22 Args: 

23 prefix: Email prefix/username part 

24 domain: Email domain (default: example.com) 

25 

26 Returns: 

27 Unique email address string 

28 """ 

29 timestamp = int(time.time() * 1000) 

30 return f"{prefix}_{timestamp}@{domain}" 

31 

32 @staticmethod 

33 def random_name(locale: str = "en_US") -> str: 

34 """ 

35 Generates a random realistic name. 

36 

37 Args: 

38 locale: Locale for name generation (default: en_US) 

39 

40 Returns: 

41 Random full name 

42 """ 

43 fake_locale = Faker(locale) 

44 return fake_locale.name() 

45 

46 @staticmethod 

47 def random_password(length: int = 12, include_special: bool = True) -> str: 

48 """ 

49 Generates a random password meeting complexity requirements. 

50 

51 Args: 

52 length: Password length (default: 12) 

53 include_special: Include special characters (default: True) 

54 

55 Returns: 

56 Random secure password 

57 """ 

58 import secrets 

59 import string 

60 

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 ] 

67 

68 if include_special: 

69 password.append(secrets.choice("!@#$%^&*")) 

70 

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 += "!@#$%^&*" 

76 

77 password.extend(secrets.choice(all_chars) for _ in range(remaining_length)) 

78 

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) 

88 

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. 

95 

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 

101 

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 } 

110 

111 # Add any additional fields 

112 payload.update(kwargs) 

113 

114 return payload