-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtests.py
More file actions
216 lines (175 loc) · 6.65 KB
/
tests.py
File metadata and controls
216 lines (175 loc) · 6.65 KB
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
import pytest
import os
import tempfile
from app import app, db
from models import User, Transaction
from datetime import datetime
@pytest.fixture
def client():
"""Create a test client for the app."""
app.config['TESTING'] = True
app.config['WTF_CSRF_ENABLED'] = False
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///:memory:'
with app.test_client() as client:
with app.app_context():
db.create_all()
# Create a test user
test_user = User(username='testuser', email='test@example.com')
test_user.set_password('TestPassword123!')
test_user.balance = 1000.00
db.session.add(test_user)
db.session.commit()
# Create some test transactions
deposit = Transaction(
user_id=test_user.id,
amount=500.00,
transaction_type='deposit',
description='Initial deposit',
timestamp=datetime.utcnow()
)
withdrawal = Transaction(
user_id=test_user.id,
amount=200.00,
transaction_type='withdrawal',
description='Test withdrawal',
timestamp=datetime.utcnow()
)
db.session.add_all([deposit, withdrawal])
db.session.commit()
yield client
with app.app_context():
db.session.remove()
db.drop_all()
def test_index_page(client):
"""Test that the index page loads correctly."""
response = client.get('/')
assert response.status_code == 200
assert b'Welcome to HackFreeBank' in response.data
def test_register_get(client):
"""Test the registration page loads."""
response = client.get('/register')
assert response.status_code == 200
assert b'Create an Account' in response.data
def test_register_post(client):
"""Test user registration works."""
response = client.post('/register', data={
'username': 'newuser',
'email': 'new@example.com',
'password': 'NewPassword123!',
'confirm_password': 'NewPassword123!'
}, follow_redirects=True)
assert response.status_code == 200
assert b'Your account has been created' in response.data
# Check that the user was created in the database
with app.app_context():
user = User.query.filter_by(username='newuser').first()
assert user is not None
assert user.email == 'new@example.com'
def test_login_get(client):
"""Test the login page loads."""
response = client.get('/login')
assert response.status_code == 200
assert b'Login to Your Account' in response.data
def test_login_success(client):
"""Test successful login."""
response = client.post('/login', data={
'username': 'testuser',
'password': 'TestPassword123!',
'remember': False
}, follow_redirects=True)
assert response.status_code == 200
assert b'Dashboard' in response.data
def test_login_failure(client):
"""Test login with incorrect credentials."""
response = client.post('/login', data={
'username': 'testuser',
'password': 'WrongPassword123!',
'remember': False
}, follow_redirects=True)
assert response.status_code == 200
assert b'Login failed' in response.data
def test_logout(client):
"""Test logout functionality."""
# First log in
client.post('/login', data={
'username': 'testuser',
'password': 'TestPassword123!',
'remember': False
})
# Then log out
response = client.get('/logout', follow_redirects=True)
assert response.status_code == 200
assert b'You have been logged out' in response.data
def test_dashboard_without_login(client):
"""Test that dashboard redirects to login when not authenticated."""
response = client.get('/dashboard', follow_redirects=True)
assert response.status_code == 200
assert b'Login to Your Account' in response.data
def test_dashboard_with_login(client):
"""Test dashboard access when authenticated."""
client.post('/login', data={
'username': 'testuser',
'password': 'TestPassword123!',
'remember': False
})
response = client.get('/dashboard')
assert response.status_code == 200
assert b'$1000.00' in response.data # Balance check
assert b'Recent Transactions' in response.data
def test_deposit(client):
"""Test deposit functionality."""
# First log in
client.post('/login', data={
'username': 'testuser',
'password': 'TestPassword123!',
'remember': False
})
# Make a deposit
response = client.post('/deposit', data={
'amount': '250.00',
'description': 'Test deposit'
}, follow_redirects=True)
assert response.status_code == 200
assert b'Deposit of $250.00 successful' in response.data
# Check that balance was updated
with app.app_context():
user = User.query.filter_by(username='testuser').first()
assert user.balance == 1250.00
def test_withdraw(client):
"""Test withdrawal functionality."""
# First log in
client.post('/login', data={
'username': 'testuser',
'password': 'TestPassword123!',
'remember': False
})
# Make a withdrawal
response = client.post('/withdraw', data={
'amount': '150.00',
'description': 'Test withdrawal'
}, follow_redirects=True)
assert response.status_code == 200
assert b'Withdrawal of $150.00 successful' in response.data
# Check that balance was updated
with app.app_context():
user = User.query.filter_by(username='testuser').first()
assert user.balance == 850.00
def test_withdraw_insufficient_funds(client):
"""Test withdrawal with insufficient funds."""
# First log in
client.post('/login', data={
'username': 'testuser',
'password': 'TestPassword123!',
'remember': False
})
# Attempt to withdraw more than available balance
response = client.post('/withdraw', data={
'amount': '1500.00',
'description': 'Test withdrawal'
}, follow_redirects=True)
assert response.status_code == 200
assert b'Insufficient funds' in response.data
# Check that balance was not changed
with app.app_context():
user = User.query.filter_by(username='testuser').first()
assert user.balance == 1000.00