|
2 | 2 | import subprocess
|
3 | 3 |
|
4 | 4 | import pytest
|
| 5 | +from django.contrib.auth import get_user_model |
5 | 6 | from django.contrib.staticfiles.testing import StaticLiveServerTestCase
|
6 | 7 | from selenium import webdriver
|
7 | 8 | from selenium.webdriver.chrome.options import Options
|
8 | 9 | from selenium.webdriver.chrome.service import Service
|
| 10 | +from selenium.webdriver.common.by import By |
| 11 | +from selenium.webdriver.support import expected_conditions as EC |
9 | 12 | from selenium.webdriver.support.ui import WebDriverWait
|
10 | 13 |
|
11 | 14 |
|
@@ -57,3 +60,76 @@ def setUp(self):
|
57 | 60 | """Set up test case."""
|
58 | 61 | super().setUp()
|
59 | 62 | # Add any additional setup here
|
| 63 | + |
| 64 | + def create_test_user(self, username="test_user", password="test_password123", **kwargs): |
| 65 | + """Create a test user for login testing.""" |
| 66 | + User = get_user_model() |
| 67 | + |
| 68 | + # Delete user if it already exists |
| 69 | + User.objects.filter(username=username).delete() |
| 70 | + |
| 71 | + user_data = { |
| 72 | + "username": username, |
| 73 | + "is_active": True, |
| 74 | + "is_staff": True, # Ensure user is staff |
| 75 | + "is_superuser": False, # Ensure user is superuser |
| 76 | + } |
| 77 | + user_data.update(kwargs) |
| 78 | + |
| 79 | + user = User.objects.create_user(**user_data) |
| 80 | + user.set_password(password) |
| 81 | + user.save() |
| 82 | + |
| 83 | + # Verify user was created correctly |
| 84 | + print(f"\nCreated user: {username}") |
| 85 | + print(f"Is active: {user.is_active}") |
| 86 | + print(f"Is staff: {user.is_staff}") |
| 87 | + print(f"Is superuser: {user.is_superuser}") |
| 88 | + |
| 89 | + return user, password |
| 90 | + |
| 91 | + def login(self, username="test_user", password="test_password123"): |
| 92 | + """ |
| 93 | + Login helper method. |
| 94 | + Returns True if login successful, False otherwise. |
| 95 | + """ |
| 96 | + # Navigate to login page |
| 97 | + self.driver.get(f"{self.live_server_url}/accounts/login/") |
| 98 | + |
| 99 | + try: |
| 100 | + # Wait for and fill username |
| 101 | + username_input = self.wait.until(EC.presence_of_element_located((By.NAME, "login"))) |
| 102 | + username_input.send_keys(username) |
| 103 | + |
| 104 | + # Fill password |
| 105 | + password_input = self.driver.find_element(By.NAME, "password") |
| 106 | + password_input.send_keys(password) |
| 107 | + |
| 108 | + # Find and click the login button |
| 109 | + login_button = self.driver.find_element(By.CSS_SELECTOR, "button[type='submit']") |
| 110 | + login_button.click() |
| 111 | + |
| 112 | + # Wait for successful login by checking for redirect |
| 113 | + self.wait.until(EC.url_changes("/accounts/login/")) |
| 114 | + |
| 115 | + # Print debug information |
| 116 | + print(f"Current URL after login: {self.driver.current_url}") |
| 117 | + return True |
| 118 | + |
| 119 | + except Exception as e: |
| 120 | + print(f"Login failed: {str(e)}") |
| 121 | + return False |
| 122 | + |
| 123 | + def logout(self): |
| 124 | + """Logout helper method.""" |
| 125 | + try: |
| 126 | + # Click logout link/button (adjust selector based on your UI) |
| 127 | + logout_link = self.driver.find_element(By.CSS_SELECTOR, "a[href*='logout']") |
| 128 | + logout_link.click() |
| 129 | + |
| 130 | + # Wait for redirect to login page |
| 131 | + self.wait.until(EC.presence_of_element_located((By.NAME, "username"))) |
| 132 | + return True |
| 133 | + except Exception as e: |
| 134 | + print(f"Logout failed: {str(e)}") |
| 135 | + return False |
0 commit comments