Skip to content

Commit 82cbf3e

Browse files
committed
Add few initial frontend tests
1 parent 8e8100b commit 82cbf3e

File tree

3 files changed

+189
-0
lines changed

3 files changed

+189
-0
lines changed

sde_collections/tests/frontend/base.py

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,13 @@
22
import subprocess
33

44
import pytest
5+
from django.contrib.auth import get_user_model
56
from django.contrib.staticfiles.testing import StaticLiveServerTestCase
67
from selenium import webdriver
78
from selenium.webdriver.chrome.options import Options
89
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
912
from selenium.webdriver.support.ui import WebDriverWait
1013

1114

@@ -57,3 +60,76 @@ def setUp(self):
5760
"""Set up test case."""
5861
super().setUp()
5962
# 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
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
from .base import BaseTestCase
2+
3+
4+
class TestAuthentication(BaseTestCase):
5+
"""Test authentication functionality."""
6+
7+
def setUp(self):
8+
super().setUp()
9+
self.test_username = "test_user"
10+
self.test_password = "test_password123"
11+
self.user, _ = self.create_test_user(username=self.test_username, password=self.test_password)
12+
13+
def test_successful_login(self):
14+
"""Test successful login process."""
15+
# Attempt login
16+
login_success = self.login(self.test_username, self.test_password)
17+
assert login_success, "Login should be successful"
18+
19+
# Verify we're on the dashboard or home page
20+
assert "Welcome back!" in self.driver.page_source
21+
22+
# print(self.driver.page_source)
23+
24+
# # Verify user menu is present
25+
# user_menu = self.wait.until(
26+
# EC.presence_of_element_located((By.CLASS_NAME, "user-menu"))
27+
# )
28+
# assert self.test_username in user_menu.text
29+
30+
# def test_failed_login(self):
31+
# """Test login failure with incorrect credentials."""
32+
# login_success = self.login(self.test_username, "wrong_password")
33+
# assert not login_success, "Login should fail with incorrect password"
34+
35+
# # Verify error message
36+
# error_message = self.wait.until(
37+
# EC.presence_of_element_located((By.CLASS_NAME, "alert-error"))
38+
# )
39+
# assert "Please enter a correct username and password" in error_message.text
40+
41+
# def test_logout(self):
42+
# """Test logout functionality."""
43+
# # First login
44+
# login_success = self.login(self.test_username, self.test_password)
45+
# assert login_success, "Login should be successful"
46+
47+
# # Then logout
48+
# logout_success = self.logout()
49+
# assert logout_success, "Logout should be successful"
50+
51+
# # Verify we're back at login page
52+
# assert "login" in self.driver.current_url.lower()
53+
54+
def tearDown(self):
55+
"""Clean up after each test."""
56+
self.user.delete()
57+
super().tearDown()
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
from selenium.webdriver.common.by import By
2+
from selenium.webdriver.support import expected_conditions as EC
3+
4+
from ..factories import CollectionFactory, UserFactory
5+
from .base import BaseTestCase
6+
7+
8+
class TestCollections(BaseTestCase):
9+
"""Test collection-related functionality."""
10+
11+
def setUp(self):
12+
"""Set up test data."""
13+
super().setUp()
14+
# Create test user and collections
15+
self.user = UserFactory(is_staff=True)
16+
self.user.set_password("test_password123")
17+
self.user.save()
18+
19+
# Create 3 test collections
20+
self.collections = [CollectionFactory(curated_by=self.user) for _ in range(3)]
21+
# Store collection names for verification
22+
self.collection_names = [collection.name for collection in self.collections]
23+
24+
def test_collections_display(self):
25+
"""Test that collections are displayed after login."""
26+
# Login
27+
self.login(self.user.username, "test_password123")
28+
29+
# Navigate to collections page
30+
self.driver.get(f"{self.live_server_url}/")
31+
32+
# Print page source for debugging
33+
# print(f"\nCurrent URL: {self.driver.current_url}")
34+
print(f"Page Source: {self.driver.page_source}")
35+
36+
# Wait for specific table to load using ID
37+
table = self.wait.until(EC.presence_of_element_located((By.ID, "collection_table")))
38+
39+
# Additional verification that it's the right table
40+
assert "table-striped dataTable" in table.get_attribute("class")
41+
42+
# Print debug info
43+
print(f"\nCurrent URL: {self.driver.current_url}")
44+
print(f"Table HTML: {table.get_attribute('outerHTML')}")
45+
46+
# Get all table text
47+
table_text = table.text
48+
49+
# Verify each collection name is present
50+
for collection_name in self.collection_names:
51+
assert collection_name in table_text, f"Collection '{collection_name}' not found in table"
52+
print(f"Found collection: {collection_name}")
53+
54+
def tearDown(self):
55+
"""Clean up test data."""
56+
super().tearDown()

0 commit comments

Comments
 (0)