Spaces:
Sleeping
Sleeping
File size: 11,673 Bytes
f6278c5 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 | """
Simplified integration tests for authentication flows
Focus on core functionality without complex session management
"""
import unittest
from unittest.mock import Mock, patch, MagicMock
from datetime import datetime
from bson import ObjectId
import json
# Import the Flask app and models
from app import app
from models import User, ChatSession
class TestSimpleAuthenticationFlows(unittest.TestCase):
"""Simplified integration tests for authentication flows"""
def setUp(self):
"""Set up test client"""
self.app = app
self.app.config['TESTING'] = True
self.app.config['WTF_CSRF_ENABLED'] = False
self.app.config['SECRET_KEY'] = 'test-secret-key'
self.client = self.app.test_client()
self.test_user_data = {
'_id': ObjectId(),
'email': 'test@example.com',
'password_hash': 'hashed_password',
'created_at': datetime.utcnow(),
'is_active': True
}
self.test_user = User(self.test_user_data)
def test_registration_form_display(self):
"""Test that registration form is displayed correctly"""
response = self.client.get('/register')
self.assertEqual(response.status_code, 200)
self.assertIn(b'Create Account', response.data)
self.assertIn(b'email', response.data)
self.assertIn(b'password', response.data)
def test_login_form_display(self):
"""Test that login form is displayed correctly"""
response = self.client.get('/login')
self.assertEqual(response.status_code, 200)
self.assertIn(b'Login to Chatty', response.data)
self.assertIn(b'email', response.data)
self.assertIn(b'password', response.data)
def test_registration_with_valid_data(self):
"""Test registration with valid data"""
with patch('models.get_users_collection') as mock_users_collection, \
patch('models.User.find_by_email') as mock_find_by_email, \
patch('models.User.validate_email_format', return_value=True), \
patch('models.User.validate_password_strength', return_value=True):
# Setup mocks
mock_collection = Mock()
mock_users_collection.return_value = mock_collection
mock_find_by_email.return_value = None # User doesn't exist
mock_collection.insert_one.return_value = Mock(inserted_id=self.test_user_data['_id'])
mock_collection.find_one.return_value = self.test_user_data
registration_data = {
'email': 'test@example.com',
'password': 'password123',
'confirm_password': 'password123'
}
response = self.client.post('/register', data=registration_data, follow_redirects=False)
# Should redirect to login page
self.assertEqual(response.status_code, 302)
self.assertIn('/login', response.location)
def test_registration_with_mismatched_passwords(self):
"""Test registration with mismatched passwords"""
registration_data = {
'email': 'test@example.com',
'password': 'password123',
'confirm_password': 'different_password'
}
response = self.client.post('/register', data=registration_data)
# Should stay on registration page with error
self.assertEqual(response.status_code, 200)
self.assertIn(b'Passwords do not match', response.data)
def test_login_with_valid_credentials(self):
"""Test login with valid credentials"""
with patch('models.User.find_by_email') as mock_find_by_email, \
patch.object(User, 'check_password', return_value=True):
mock_find_by_email.return_value = self.test_user
login_data = {
'email': 'test@example.com',
'password': 'password123'
}
response = self.client.post('/login', data=login_data, follow_redirects=False)
# Should redirect to chat interface
self.assertEqual(response.status_code, 302)
self.assertIn('/chat', response.location)
def test_login_with_invalid_credentials(self):
"""Test login with invalid credentials"""
with patch('models.User.find_by_email') as mock_find_by_email:
mock_find_by_email.return_value = None # User doesn't exist
login_data = {
'email': 'nonexistent@example.com',
'password': 'password123'
}
response = self.client.post('/login', data=login_data)
# Should stay on login page with error
self.assertEqual(response.status_code, 200)
self.assertIn(b'Invalid email or password', response.data)
def test_unauthenticated_access_to_chat_redirects(self):
"""Test that unauthenticated users are redirected from chat page"""
response = self.client.get('/chat', follow_redirects=False)
# Should redirect to login
self.assertEqual(response.status_code, 302)
self.assertIn('/login', response.location)
def test_unauthenticated_api_chat_request(self):
"""Test that unauthenticated API requests are rejected"""
chat_data = {
'message': 'Hello, this should fail'
}
response = self.client.post(
'/api/chat',
data=json.dumps(chat_data),
content_type='application/json'
)
# Should return 401 or redirect
self.assertIn(response.status_code, [401, 302])
def test_unauthenticated_history_request(self):
"""Test that unauthenticated history requests are rejected"""
response = self.client.get('/api/user-history')
# Should return 401 or redirect
self.assertIn(response.status_code, [401, 302])
def test_root_route_redirects_to_login_when_unauthenticated(self):
"""Test that root route redirects to login for unauthenticated users"""
response = self.client.get('/', follow_redirects=False)
self.assertEqual(response.status_code, 302)
self.assertIn('/login', response.location)
def test_database_error_handling_in_login(self):
"""Test graceful handling of database errors during login"""
with patch('models.User.find_by_email') as mock_find_by_email:
mock_find_by_email.side_effect = Exception("Database connection failed")
login_data = {
'email': 'test@example.com',
'password': 'password123'
}
response = self.client.post('/login', data=login_data)
# Should handle error gracefully
self.assertEqual(response.status_code, 200)
self.assertIn(b'We're experiencing technical difficulties', response.data)
def test_database_error_handling_in_registration(self):
"""Test graceful handling of database errors during registration"""
with patch('models.User.create_user') as mock_create_user:
mock_create_user.side_effect = Exception("Database connection failed")
registration_data = {
'email': 'test@example.com',
'password': 'password123',
'confirm_password': 'password123'
}
response = self.client.post('/register', data=registration_data)
# Should handle error gracefully
self.assertEqual(response.status_code, 200)
self.assertIn(b'We're experiencing technical difficulties', response.data)
def test_weak_password_validation(self):
"""Test that weak passwords are rejected"""
registration_data = {
'email': 'test@example.com',
'password': 'weak', # Too short
'confirm_password': 'weak'
}
response = self.client.post('/register', data=registration_data)
# Should stay on registration page with error
self.assertEqual(response.status_code, 200)
self.assertIn(b'Password must be at least 8 characters long', response.data)
def test_invalid_email_format_validation(self):
"""Test that invalid email formats are rejected"""
registration_data = {
'email': 'invalid_email', # Invalid format
'password': 'password123',
'confirm_password': 'password123'
}
response = self.client.post('/register', data=registration_data)
# Should stay on registration page with error
self.assertEqual(response.status_code, 200)
self.assertIn(b'Please enter a valid email address', response.data)
def test_existing_email_registration_attempt(self):
"""Test registration attempt with existing email"""
with patch('models.User.find_by_email') as mock_find_by_email, \
patch('models.User.validate_email_format', return_value=True), \
patch('models.User.validate_password_strength', return_value=True):
# User already exists
mock_find_by_email.return_value = self.test_user
registration_data = {
'email': 'test@example.com',
'password': 'password123',
'confirm_password': 'password123'
}
response = self.client.post('/register', data=registration_data)
# Should stay on registration page with error
self.assertEqual(response.status_code, 200)
self.assertIn(b'Email already registered', response.data)
def test_login_with_wrong_password(self):
"""Test login with correct email but wrong password"""
with patch('models.User.find_by_email') as mock_find_by_email, \
patch.object(User, 'check_password', return_value=False):
mock_find_by_email.return_value = self.test_user
login_data = {
'email': 'test@example.com',
'password': 'wrongpassword'
}
response = self.client.post('/login', data=login_data)
# Should stay on login page with error
self.assertEqual(response.status_code, 200)
self.assertIn(b'Invalid email or password', response.data)
def test_login_with_inactive_user(self):
"""Test login attempt with inactive user account"""
# Create inactive user
inactive_user_data = self.test_user_data.copy()
inactive_user_data['is_active'] = False
inactive_user = User(inactive_user_data)
with patch('models.User.find_by_email') as mock_find_by_email:
mock_find_by_email.return_value = inactive_user
login_data = {
'email': 'test@example.com',
'password': 'password123'
}
response = self.client.post('/login', data=login_data)
# Should stay on login page with error
self.assertEqual(response.status_code, 200)
self.assertIn(b'disabled', response.data)
if __name__ == '__main__':
unittest.main(verbosity=2) |