brighter-trading/tests/test_app.py

74 lines
2.5 KiB
Python

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(self):
"""
Test the login route with valid and invalid credentials.
"""
# Valid credentials
valid_data = {'user_name': 'test_user', 'password': 'test_password'}
response = self.app.post('/login', data=valid_data)
self.assertEqual(response.status_code, 302) # Redirects on success
# Invalid credentials
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
self.assertIn(b'Invalid user_name or password', response.data)
def test_signup(self):
"""
Test the signup route.
"""
data = {'email': 'test@example.com', 'user_name': 'new_user', '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_history(self):
"""
Test the history route.
"""
data = {"user_name": "test_user"}
response = self.app.post('/api/history', data=json.dumps(data), content_type='application/json')
self.assertEqual(response.status_code, 200)
self.assertIn(b'price_history', response.data)
def test_indicator_init(self):
"""
Test the indicator initialization route.
"""
data = {"user_name": "test_user"}
response = self.app.post('/api/indicator_init', data=json.dumps(data), content_type='application/json')
self.assertEqual(response.status_code, 200)
self.assertIn(b'indicator_data', response.data)
if __name__ == '__main__':
unittest.main()