rifalto/test/test_app.py

65 lines
2.6 KiB
Python
Raw Normal View History

2023-06-14 09:20:36 -03:00
def test_new_purchase(test_app, test_db):
from rifaserver.models import Seller, Purchase
seller = Seller(username='test', password_hash='test')
test_db.session.add(seller)
test_db.session.commit()
with test_app.test_client() as client:
resp = client.post('/seller', data={'amount': 5})
assert resp.status_code == 200
assert Purchase.query.count() == 1
purchase = Purchase.query.first()
assert purchase.total_numbers == 5
assert purchase.numbers_left == 5
def test_ticket_purchase(test_app, test_db):
from rifaserver.models import Seller, Purchase, Raffle, Ticket
seller = Seller(username='test', password_hash='test')
test_db.session.add(seller)
raffle = Raffle(description='test raffle')
test_db.session.add(raffle)
test_db.session.commit()
purchase = Purchase(id='test123', seller_id=seller.id, total_numbers=5, numbers_left=5)
test_db.session.add(purchase)
test_db.session.commit()
with test_app.test_client() as client:
resp = client.post(f'/purchase/{purchase.id}', data={'raffle_id': raffle.id, 'name': 'test buyer', 'contact': 'test contact', 'number': 1})
assert resp.status_code == 200
assert purchase.numbers_left == 4
assert Ticket.query.count() == 1
ticket = Ticket.query.first()
assert ticket.buyer_name == 'test buyer'
assert ticket.buyer_contact == 'test contact'
assert ticket.chosen_number == 1
def test_valid_login(test_app, test_db):
from your_flask_app.models import Seller
seller = Seller(username='test', password_hash='test')
test_db.session.add(seller)
test_db.session.commit()
with test_app.test_client() as client:
resp = client.post('/login', data={'username': 'test', 'password': 'test'}, follow_redirects=True)
assert resp.status_code == 200
assert b'Logged in successfully' in resp.data
def test_invalid_login(test_app):
with test_app.test_client() as client:
resp = client.post('/login', data={'username': 'test', 'password': 'wrong_password'}, follow_redirects=True)
assert resp.status_code == 200
assert b'Invalid username or password' in resp.data
def test_protected_route_while_logged_in(test_app, test_db):
from your_flask_app.models import Seller
seller = Seller(username='test', password_hash='test')
test_db.session.add(seller)
test_db.session.commit()
with test_app.test_client() as client:
client.post('/login', data={'username': 'test', 'password': 'test'}, follow_redirects=True)
resp = client.get('/seller', follow_redirects=True)
assert resp.status_code == 200