Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bug/ fix points redemption validation to prevent overspending #245

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@ lib
.Python
tests/
.envrc
__pycache__
__pycache__
*pyvenv.cfg
26 changes: 21 additions & 5 deletions server.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,17 +43,33 @@ def book(competition,club):

@app.route('/purchasePlaces',methods=['POST'])
def purchasePlaces():
competition = [c for c in competitions if c['name'] == request.form['competition']][0]
competition = [
c for c in competitions
if c['name'] == request.form['competition']
][0]
club = [c for c in clubs if c['name'] == request.form['club']][0]
placesRequired = int(request.form['places'])
competition['numberOfPlaces'] = int(competition['numberOfPlaces'])-placesRequired
flash('Great-booking complete!')
return render_template('welcome.html', club=club, competitions=competitions)

if int(club['points']) < placesRequired:
flash("You do not have enough points to book that many places.")
return render_template(
'welcome.html', club=club, competitions=competitions
)

club['points'] = int(club['points']) - placesRequired

competition['numberOfPlaces'] = (
int(competition['numberOfPlaces']) - placesRequired
)
flash('Great - booking complete !')
return render_template(
'welcome.html', club=club, competitions=competitions
)


# TODO: Add route for points display


@app.route('/logout')
def logout():
return redirect(url_for('index'))
return redirect(url_for('index'))
35 changes: 35 additions & 0 deletions tests/unit/test_purchase_places.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import sys
import os
import pytest
sys.path.insert(
0, os.path.abspath(
os.path.join(os.path.dirname(__file__), '../../')
)
)

from server import app, clubs, competitions # noqa


@pytest.fixture
def client():
app.config['TESTING'] = True
with app.test_client() as client:
yield client


def test_purchase_places_not_enough_points(client):
response = client.post('/purchasePlaces', data={
'competition': 'Spring Festival',
'club': 'Iron Temple',
'places': 5
})

assert b"You do not have enough points to book that many places." \
in response.data

club = next(c for c in clubs if c['name'] == 'Iron Temple')
competition = next(
c for c in competitions if c['name'] == 'Spring Festival'
)
assert str(club['points']) == "4"
assert str(competition['numberOfPlaces']) == "25"