Skip to content

Commit 5db9418

Browse files
committed
fix unit test using pytest
1 parent 953008c commit 5db9418

File tree

6 files changed

+209
-89
lines changed

6 files changed

+209
-89
lines changed

__pycache__/app.cpython-313.pyc

375 Bytes
Binary file not shown.

app.py

Lines changed: 10 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
1919
app.config['SECRET_KEY'] = 'supersecretkey'
2020

21-
2221
# Initialize the database object only once
2322
migrate = Migrate(app, db)
2423
db.init_app(app)
@@ -31,13 +30,11 @@
3130
def load_user(user_id):
3231
return User.query.get(int(user_id))
3332

34-
3533
# Home Page (Landing Page)
3634
@app.route('/')
3735
def home():
3836
return render_template('index.html')
3937

40-
4138
# Register Page
4239
@app.route('/register', methods=['GET', 'POST'])
4340
def register():
@@ -57,12 +54,11 @@ def register():
5754
db.session.add(new_user)
5855
db.session.commit()
5956

60-
flash("Registration successful! Please log in.", "success")
57+
flash("Registration Successful!", "success")
6158
return redirect(url_for('login'))
6259

6360
return render_template('register.html')
6461

65-
6662
# Login Page
6763
@app.route('/login', methods=['GET', 'POST'])
6864
def login():
@@ -75,42 +71,40 @@ def login():
7571
if user and user.check_password(password):
7672
# Log the user in using Flask-Login
7773
login_user(user) # This will manage the session automatically
78-
79-
flash("Login successful!", "success")
74+
flash(f"Welcome back, {user.username}!", "success")
8075
return redirect(url_for('dashboard')) # Redirect to the dashboard
8176
else:
8277
flash("Invalid credentials! Try again.", "danger")
8378

8479
return render_template('login.html')
8580

86-
87-
8881
@app.route('/config-check')
8982
def config_check():
9083
return f"Testing Mode: {app.config['TESTING']}"
9184

92-
9385
@app.route('/user-check')
9486
def user_check():
9587
if current_user.is_authenticated:
9688
return f"Logged in as: {current_user.username}"
9789
else:
9890
return "No user is currently logged in."
9991

100-
10192
# Dashboard Page
10293
@app.route('/dashboard')
10394
@login_required
10495
def dashboard():
10596
print(f"[DEBUG] Logged in user: {current_user.username}")
10697
return render_template('dashboard.html', username=current_user.username)
10798

108-
109-
11099
@app.route('/guess', methods=['POST'])
111100
@login_required
112101
def guess():
113-
guess = int(request.form['guess'])
102+
try:
103+
guess = int(request.form['guess'])
104+
except ValueError:
105+
flash("Invalid guess! Please enter a valid number.", "danger")
106+
return redirect(url_for('play_game', level=session.get('level', 'easy')))
107+
114108
number_to_guess = session.get('number_to_guess')
115109

116110
if guess == number_to_guess:
@@ -122,7 +116,6 @@ def guess():
122116
else:
123117
return jsonify({'result': 'Too high!'})
124118

125-
126119
# Game Page (Handles Easy, Medium, Hard)
127120
@app.route('/play/<level>', methods=['GET', 'POST'])
128121
@login_required # This ensures the user is logged in
@@ -141,6 +134,7 @@ def play_game(level):
141134
session['random_number'] = random.randint(1, levels[level])
142135
session['attempts'] = 5 # Total number of attempts
143136
session['game_status'] = None # Track the game status (win/loss)
137+
session['level'] = level # Store the current level
144138

145139
# Game variables
146140
correct_number = session['random_number']
@@ -194,19 +188,16 @@ def play_game(level):
194188
correct_number=correct_number,
195189
game_status=game_status)
196190

197-
198191
# Leaderboard
199192
@app.route('/leaderboard')
200193
def leaderboard():
201-
202194
leaderboard_data = User.query.order_by(User.score.desc()).all()
203195
leaderboard = [{"username": user.username, "score": user.score} for user in leaderboard_data]
204196
return render_template('leaderboard.html', leaderboard=leaderboard)
205-
#return render_template('leaderboard.html', leaderboard=leaderboard)
206197

207198
@app.route('/reset')
208199
def reset_game():
209-
session.pop('number', None)
200+
session.pop('random_number', None)
210201
session.pop('attempts', None)
211202
level = session.get('level', 'easy') # fallback level
212203
return redirect(url_for('play_game', level=level))
@@ -219,7 +210,6 @@ def logout():
219210
flash("You have been logged out successfully.", "info")
220211
return redirect(url_for('home'))
221212

222-
223213
# Run the application
224214
if __name__ == "__main__":
225215
with app.app_context():

login.html

Lines changed: 24 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,24 +4,37 @@
44
<title>Login</title>
55
<meta charset="UTF-8">
66
<meta name="viewport" content="width=device-width, initial-scale=1.0">
7-
<link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-QWTKZyjpPEjISv5WaRU9OFeRpok6YctnYmDr5pNlyT2bRjXh0JMhjY6hW+ALEwIH" crossorigin="anonymous">
8-
<link rel="stylesheet" type="text/css" href="{{ url_for('static', filename='styles.css') }}">
7+
<link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet">
8+
<link rel="stylesheet" type="text/css" href="{{ url_for('static', filename='styles.css') }}">
99
</head>
1010
<body>
11-
<div class="login shadow-sm rounded p-4">
12-
<h2 class="p-3" >Login</h2>
11+
<div class="container">
12+
<h2 class="text-center">Login</h2>
1313

14-
<form method="POST">
15-
<input type="text" class="form-control" name="username" placeholder="Username" required>
16-
<input type="password" class="form-control" name="password" placeholder="Password" required>
17-
<button type="submit" class="">Login</button>
18-
</form>
19-
<p>New here? <a href="{{ url_for('register') }}">Register</a></p>
14+
<!-- Display Flash Messages -->
15+
{% with messages = get_flashed_messages(with_categories=true) %}
16+
{% if messages %}
17+
<div class="alert alert-dismissible fade show" role="alert">
18+
{% for category, message in messages %}
19+
<div class="alert alert-{{ category }}">
20+
{{ message }}
21+
</div>
22+
{% endfor %}
23+
</div>
24+
{% endif %}
25+
{% endwith %}
2026

27+
<form method="POST">
28+
<label class="form-label">Username</label>
29+
<input type="text" class="form-control" name="username" required>
30+
<label class="form-label">Password</label>
31+
<input type="password" class="form-control" name="password" required>
32+
<button type="submit" class="btn btn-outline-primary">Login</button>
33+
</form>
2134

35+
<p>New here? <a href="{{ url_for('register') }}">Register</a></p>
2236
</div>
2337

2438
<script src="{{ url_for('static', filename='script.js') }}"></script>
25-
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js" integrity="sha384-YvpcrYf0tY3lHB60NNkmXc5s9fDVZLESaAA55NDzOxhy9GkcIdslK1eN7N6jIeHz" crossorigin="anonymous"></script>
2639
</body>
2740
</html>

register.html

Lines changed: 26 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -4,32 +4,39 @@
44
<title>Register</title>
55
<meta charset="UTF-8">
66
<meta name="viewport" content="width=device-width, initial-scale=1.0">
7-
<link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-QWTKZyjpPEjISv5WaRU9OFeRpok6YctnYmDr5pNlyT2bRjXh0JMhjY6hW+ALEwIH" crossorigin="anonymous">
8-
<link rel="stylesheet" type="text/css" href="{{ url_for('static', filename='styles.css') }}">
7+
<link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet">
8+
<link rel="stylesheet" type="text/css" href="{{ url_for('static', filename='styles.css') }}">
99
</head>
1010
<body>
11-
<div class="register mt-4 p-4 bg-white shadow-sm rounded text-center">
12-
13-
<h2 class="text-secondary p-2">Register</h2>
14-
<form method="POST">
15-
<label class="form-label text-muted">Username</label>
16-
<input type="text" class ="form-control border-light bg-light" name="username" required>
11+
<div class="container">
12+
<h2 class="text-center">Register</h2>
1713

18-
<label class="form-label text-muted">Password</label>
19-
<input type="password" class ="form-control border-light bg-light" name="password" required>
14+
<!-- Display Flash Messages -->
15+
{% with messages = get_flashed_messages(with_categories=true) %}
16+
{% if messages %}
17+
<div class="alert alert-dismissible fade show" role="alert">
18+
{% for category, message in messages %}
19+
<div class="alert alert-{{ category }}">
20+
{{ message }}
21+
</div>
22+
{% endfor %}
23+
</div>
24+
{% endif %}
25+
{% endwith %}
2026

21-
<label class="form-label text-muted">Comfirm Password</label>
22-
<input type="password" class ="form-control border-light bg-light" name="confirm_password" required>
23-
24-
<button type="submit" class="btn btn-outline-secondary">Register</button>
25-
</form>
26-
27-
<p class="text-muted">Already have an account? <a href="{{ url_for('login') }}">Login</a></p>
27+
<form method="POST">
28+
<label class="form-label">Username</label>
29+
<input type="text" class="form-control" name="username" required>
30+
<label class="form-label">Password</label>
31+
<input type="password" class="form-control" name="password" required>
32+
<label class="form-label">Confirm Password</label>
33+
<input type="password" class="form-control" name="confirm_password" required>
34+
<button type="submit" class="btn btn-outline-primary">Register</button>
35+
</form>
2836

37+
<p>Already have an account? <a href="{{ url_for('login') }}">Login</a></p>
2938
</div>
3039

31-
3240
<script src="{{ url_for('static', filename='script.js') }}"></script>
33-
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js" integrity="sha384-YvpcrYf0tY3lHB60NNkmXc5s9fDVZLESaAA55NDzOxhy9GkcIdslK1eN7N6jIeHz" crossorigin="anonymous"></script>
3441
</body>
3542
</html>
1.12 KB
Binary file not shown.

0 commit comments

Comments
 (0)