Skip to content

Commit eaac160

Browse files
committed
updated README.md and requirements.txt related to tests
created conftest.py and test_auth.py rename db file
1 parent 055b878 commit eaac160

File tree

7 files changed

+91
-1
lines changed

7 files changed

+91
-1
lines changed

README.md

+4
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,10 @@ Note: You can register a new account but admin user is the admin as the id of Us
5555
│ ├── js # js file related to bootstrap
5656
│ └── translations # language json files
5757
├── templates # route html files
58+
├── tests # tests
59+
│ ├── conftest.py # set up for testing
60+
│ ├── test_auth.py # test for auth routes
61+
│ └── other routes # will be committed later
5862
├── flask_app
5963
│ ├── __init__.py # Initialize Flask app and extensions
6064
│ ├── forms.py # Forms

flask_app/config.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ class DevelopmentConfig(Config):
1313

1414
class TestingConfig(Config):
1515
TESTING = True
16-
SQLALCHEMY_DATABASE_URI = 'sqlite:///test.db' # 'sqlite:///:memory:' # In-memory database for testing
16+
SQLALCHEMY_DATABASE_URI = 'sqlite:///:memory:' # In-memory database for testing
1717
WTF_CSRF_ENABLED = False # Disable CSRF for testing
1818

1919

File renamed without changes.

instance/prod.db

32 KB
Binary file not shown.

requirements.txt

110 Bytes
Binary file not shown.

tests/conftest.py

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import pytest
2+
from flask_app import create_app
3+
from flask_app.models import db
4+
from dotenv import load_dotenv
5+
6+
7+
@pytest.fixture
8+
def app():
9+
# Load environment variables from .env file
10+
load_dotenv()
11+
12+
app = create_app(config_name='testing')
13+
14+
with app.app_context():
15+
db.create_all() # Create tables for tests
16+
yield app
17+
db.drop_all() # Clean up after tests
18+
19+
# Print the SECRET_KEY to verify it's being loaded
20+
print(f"\nSECRET_KEY in tests: {app.config['SECRET_KEY']}\n")
21+
22+
23+
@pytest.fixture
24+
def client(app):
25+
return app.test_client()
26+
27+
28+
@pytest.fixture
29+
def runner(app):
30+
return app.test_cli_runner()

tests/test_auth.py

+56
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
def test_register_page(client):
2+
response = client.get('/register')
3+
assert response.status_code == 200
4+
assert b"Register" in response.data # Check if "Register" is in the page
5+
6+
7+
def test_register_form(client):
8+
response = client.post('/register', data={
9+
'email': 'test@email.com',
10+
'password': 'test',
11+
'name': 'Test User'
12+
})
13+
assert response.status_code == 302 # Expecting a redirect after a successful registration
14+
15+
16+
def test_login_page(client):
17+
response = client.get('/login')
18+
assert response.status_code == 200
19+
assert b"Login" in response.data
20+
21+
22+
def test_login_form(client):
23+
# Register first
24+
client.post('/register', data={
25+
'email': 'test@email.com',
26+
'password': 'test',
27+
'name': 'Test User'
28+
})
29+
30+
# Test for login
31+
response = client.post('/login', data={
32+
'email': 'test@email.com',
33+
'password': 'test'
34+
})
35+
assert response.status_code == 302 # Expecting a redirect after a successful registration
36+
37+
38+
def test_logout(client):
39+
# Register
40+
client.post('/register', data={
41+
'email': 'test@email.com',
42+
'password': 'test',
43+
'name': 'Test User'
44+
})
45+
46+
# Log in
47+
client.post('/register', data={
48+
'email': 'test@email.com',
49+
'password': 'test',
50+
'name': 'Test User'
51+
})
52+
53+
# Test for logout
54+
response = client.get('/logout')
55+
assert response.status_code == 302
56+
assert b"Login" in response.data

0 commit comments

Comments
 (0)