Skip to content

Commit

Permalink
defined user data as constant in parameters.py
Browse files Browse the repository at this point in the history
  • Loading branch information
taka-rl committed Oct 15, 2024
1 parent 9b5cbfc commit c563f75
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 30 deletions.
9 changes: 3 additions & 6 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,7 @@
from flask_app import create_app
from flask_app.models import db, User
from werkzeug.security import generate_password_hash


SUPER_ADMIN_EMAIL = '[email protected]'
SUPER_ADMIN_PASSWORD = 'admin'
from parameters import SUPER_ADMIN_EMAIL, SUPER_ADMIN_PASSWORD, SUPER_ADMIN_NAME


@pytest.fixture
Expand All @@ -18,8 +15,8 @@ def app():
# Create a super admin user
super_admin = User(email=SUPER_ADMIN_EMAIL,
password=generate_password_hash(SUPER_ADMIN_PASSWORD),
name='Super Admin',
role='super_admin'
name=SUPER_ADMIN_NAME,
role='Super Admin'
)
db.session.add(super_admin)
db.session.commit()
Expand Down
7 changes: 7 additions & 0 deletions tests/parameters.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
SUPER_ADMIN_EMAIL = '[email protected]'
SUPER_ADMIN_PASSWORD = 'admin'
SUPER_ADMIN_NAME = 'Super Admin'

TEST_EMAIL = '[email protected]'
TEST_PASSWORD = 'test'
TEST_NAME = 'Test User'
50 changes: 26 additions & 24 deletions tests/test_auth.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
from flask_app.models import User, db
from werkzeug.security import generate_password_hash
from parameters import TEST_NAME, TEST_EMAIL, TEST_PASSWORD


def test_register_page(client):
response = client.get('/register')
assert response.status_code == 200
Expand All @@ -6,36 +11,33 @@ def test_register_page(client):

def test_register_form(client):
response = client.post('/register', data={
'email': '[email protected]',
'password': 'test',
'name': 'Test User'
'email': TEST_EMAIL,
'password': TEST_PASSWORD,
'name': TEST_NAME
})
assert response.status_code == 302 # Expecting a redirect after a successful registration


def test_duplicated_email_register(client):
# Create a new user manually for testing
from flask_app.models import User, db
from werkzeug.security import generate_password_hash

new_user = User(name='Test User',
email='[email protected]',
password=generate_password_hash('test')
new_user = User(name=TEST_NAME,
email=TEST_EMAIL,
password=generate_password_hash(TEST_PASSWORD)
)

with client.application.app_context():
db.session.add(new_user)
db.session.commit()

# Verify the user exists in the database
user = User.query.filter_by(email='[email protected]').first()
user = User.query.filter_by(email=TEST_EMAIL).first()
assert user is not None

# Register the same user
response = client.post('/register', data={
'email': '[email protected]',
'password': 'test',
'name': 'Test User'
'email': TEST_EMAIL,
'password': TEST_PASSWORD,
'name': TEST_NAME
}, follow_redirects=True)

# Check if the application redirects from registration page to log in page with a status code 200
Expand All @@ -52,25 +54,25 @@ def test_login_page(client):
def test_login_form(client):
# Register first
client.post('/register', data={
'email': '[email protected]',
'password': 'test',
'name': 'Test User'
'email': TEST_EMAIL,
'password': TEST_PASSWORD,
'name': TEST_NAME
})

# Test for login
response = client.post('/login', data={
'email': '[email protected]',
'password': 'test'
'email': TEST_EMAIL,
'password': TEST_PASSWORD
})
assert response.status_code == 302 # Expecting a redirect after a successful registration


def test_logout(client):
# Register
register_response = client.post('/register', data={
'email': '[email protected]',
'password': 'test',
'name': 'Test User'
'email': TEST_EMAIL,
'password': TEST_PASSWORD,
'name': TEST_NAME
})

assert register_response.status_code == 302 # Check if registration redirects after success
Expand All @@ -81,9 +83,9 @@ def test_logout(client):

# Log in
login_response = client.post('/login', data={
'email': '[email protected]',
'password': 'test',
'name': 'Test User'
'email': TEST_EMAIL,
'password': TEST_PASSWORD,
'name': TEST_NAME
})
assert login_response.status_code == 302 # Check if login redirects after success

Expand Down

0 comments on commit c563f75

Please sign in to comment.