Coverage for apiObjects / api_objects.py: 97%

34 statements  

« prev     ^ index     » next       coverage.py v7.13.5, created at 2026-04-03 04:37 +0000

1import time 

2from typing import Any 

3 

4from playwright.sync_api import APIRequestContext, APIResponse 

5 

6# --- Configuration --- 

7SIGNUP_ENDPOINT = "/api/authentication/signup/" 

8SIGNUP_CONFIRM_ENDPOINT = "/api/authentication/signup/confirm/" 

9SIGNUP_RESEND_CODE_ENDPOINT = "/api/authentication/signup/resend-code/" 

10 

11 

12# --- API Object Model --- 

13class SignupClient: 

14 """Helper class to interact with the Signup API.""" 

15 

16 def __init__(self, request_context: APIRequestContext): 

17 self.request = request_context 

18 

19 def create_user(self, payload: dict[str, Any]) -> APIResponse: 

20 """Sends a POST request to create a user.""" 

21 return self.request.post(SIGNUP_ENDPOINT, data=payload) 

22 

23 def confirm_signup(self, payload: dict[str, Any]) -> APIResponse: 

24 """Sends a POST request to confirm/verify user signup.""" 

25 return self.request.post(SIGNUP_CONFIRM_ENDPOINT, data=payload) 

26 

27 def resend_confirmation_code(self, payload: dict[str, Any]) -> APIResponse: 

28 """Sends a POST request to resend confirmation code.""" 

29 return self.request.post(SIGNUP_RESEND_CODE_ENDPOINT, data=payload) 

30 

31 def test_resend_rate_limit(self, email: str, max_attempts: int = 5) -> dict[str, Any]: 

32 """Tests resend OTP rate limiting by making multiple attempts. 

33 

34 Args: 

35 email: Email address to resend OTP for 

36 max_attempts: Number of successful attempts before rate limit (default: 5) 

37 

38 Returns: 

39 Dict with success count and final blocked response 

40 """ 

41 resend_payload = {"email": email} 

42 successful_attempts = 0 

43 

44 # Make max_attempts successful resends 

45 for _attempt in range(1, max_attempts + 1): 

46 response = self.resend_confirmation_code(resend_payload) 

47 if response.status == 200: 

48 successful_attempts += 1 

49 else: 

50 break 

51 

52 # Try one more time to trigger rate limit 

53 blocked_response = self.resend_confirmation_code(resend_payload) 

54 

55 return {"successful_attempts": successful_attempts, "blocked_response": blocked_response} 

56 

57 @staticmethod 

58 def generate_unique_email(prefix: str = "test") -> str: 

59 """Generates a unique email address based on timestamp.""" 

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

61 return f"{prefix}_{timestamp}@example.com" 

62 

63 @staticmethod 

64 def default_payload(email_prefix: str = "user") -> dict[str, str]: 

65 """Generates a valid default payload with a unique email.""" 

66 timestamp = int(time.time()) 

67 email = SignupClient.generate_unique_email(email_prefix) 

68 return {"name": f"Test User {timestamp}", "email": email, "password": "Password123!"}