Skip to content

Commit

Permalink
added test_error.py for the error route
Browse files Browse the repository at this point in the history
  • Loading branch information
taka-rl committed Oct 14, 2024
1 parent 87e40bf commit 9b5cbfc
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 0 deletions.
7 changes: 7 additions & 0 deletions flask_app/routes/errors.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,10 @@ def forbidden(error):
@with_translations
def server_error(error):
return render_template('500.html'), 500


# temporal route for testing 500 errors
@errors_bp.route('/trigger-500')
@with_translations
def trigger_500():
return render_template('500.html'), 500
57 changes: 57 additions & 0 deletions tests/test_error.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
# utility functions for testing
def register_for_test(client, data):
# Register
response = client.post('/register', data={
'email': data['email'],
'password': data['password'],
'name': data['name']
}, follow_redirects=True)
assert response.status_code == 200


def test_500_page(client):
response = client.get('/trigger-500')
assert response.status_code == 500
assert b"500 - Internal Server Error" in response.data


def test_404_page(client):
response = client.get('/nonexistent')
assert response.status_code == 404
assert b"Page Not Found" in response.data


def test_403_page(client):
# Register as an admin
register_for_test(client, data={
'email': '[email protected]',
'password': 'admin',
'name': 'Super Admin'
})

logout_response = client.get('/logout')
assert logout_response.status_code == 302

# Register as a test user
register_for_test(client, data={
'email': '[email protected]',
'password': 'test',
'name': 'Test User'
})

# Log in as test user
response = client.post('/login', data={
'email': '[email protected]',
'password': 'test'
}, follow_redirects=True)

# Ensure the login was successful
assert response.status_code == 200

# Move to /useful_info route, only allowed by admin
response = client.get('/useful_info')

# Check that the status code is 403 (forbidden) for non-admin users
assert response.status_code == 403
assert b"Forbidden" in response.data

0 comments on commit 9b5cbfc

Please sign in to comment.