import unittest from flask import Flask from src.app import app import json class FlaskAppTests(unittest.TestCase): def setUp(self): """ Set up the test client and any other test configuration. """ self.app = app.test_client() self.app.testing = True def test_index(self): """ Test the index route. """ response = self.app.get('/') self.assertEqual(response.status_code, 200) self.assertIn(b'Welcome', response.data) # Adjust this based on your actual landing page content def test_login_redirect_on_invalid(self): """ Test that login redirects on invalid credentials. """ # Invalid credentials should redirect to login page invalid_data = {'user_name': 'wrong_user', 'password': 'wrong_password'} response = self.app.post('/login', data=invalid_data) self.assertEqual(response.status_code, 302) # Redirects on failure # Follow redirect to check error flash message response = self.app.post('/login', data=invalid_data, follow_redirects=True) # The page should contain login form or error message self.assertIn(b'login', response.data.lower()) def test_login_with_valid_credentials(self): """ Test the login route with credentials that may or may not exist. """ # Valid credentials (test user may not exist) valid_data = {'user_name': 'test_user', 'password': 'test_password'} response = self.app.post('/login', data=valid_data) # Should redirect regardless (to index if success, to login if failure) self.assertEqual(response.status_code, 302) def test_signup(self): """ Test the signup route. """ import random username = f'test_user_{random.randint(10000, 99999)}' data = {'email': f'{username}@example.com', 'user_name': username, 'password': 'new_password'} response = self.app.post('/signup_submit', data=data) self.assertEqual(response.status_code, 302) # Redirects on success def test_signout(self): """ Test the signout route. """ response = self.app.get('/signout') self.assertEqual(response.status_code, 302) # Redirects on signout def test_indicator_init_requires_auth(self): """ Test that indicator_init requires authentication. """ data = {"user_name": "test_user"} response = self.app.post('/api/indicator_init', data=json.dumps(data), content_type='application/json') # Should return 401 without proper session self.assertIn(response.status_code, [200, 401]) # Either authenticated or not def test_login_page_loads(self): """ Test that login page loads. """ response = self.app.get('/login') self.assertEqual(response.status_code, 200) def test_signup_page_loads(self): """ Test that signup page loads. """ response = self.app.get('/signup') self.assertEqual(response.status_code, 200) if __name__ == '__main__': unittest.main()