Skip to content

Bug/ prevent booking for past competitions and limit booking page access to future events #254

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

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
19 changes: 11 additions & 8 deletions server.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import json
from flask import Flask,render_template,request,redirect,flash,url_for
from datetime import datetime


def loadClubs():
Expand Down Expand Up @@ -31,15 +32,17 @@ def showSummary():


@app.route('/book/<competition>/<club>')
def book(competition,club):
foundClub = [c for c in clubs if c['name'] == club][0]
foundCompetition = [c for c in competitions if c['name'] == competition][0]
if foundClub and foundCompetition:
return render_template('booking.html',club=foundClub,competition=foundCompetition)
else:
flash("Something went wrong-please try again")
def book(competition, club):
competition = next((c for c in competitions if c['name'] == competition), None)
club = next((c for c in clubs if c['name'] == club), None)

competition_date = datetime.strptime(competition['date'], "%Y-%m-%d %H:%M:%S")
if competition_date < datetime.now():
flash("You cannot book places for a past competition.")
return render_template('welcome.html', club=club, competitions=competitions)

return render_template('booking.html', club=club, competition=competition)


@app.route('/purchasePlaces',methods=['POST'])
def purchasePlaces():
Expand All @@ -56,4 +59,4 @@ def purchasePlaces():

@app.route('/logout')
def logout():
return redirect(url_for('index'))
return redirect(url_for('index'))
34 changes: 34 additions & 0 deletions tests/unit/test_purchase_places.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
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_book_past_competition(client):
competition = next(
c for c in competitions if c['name'] == 'Spring Festival'
)
competition['date'] = "2020-03-27 10:00:00"

response = client.get(
'/book/Spring Festival/Simply Lift', follow_redirects=True
)

assert b"You cannot book places for a past competition." in response.data

club = next(c for c in clubs if c['name'] == 'Simply Lift')
assert club['points'] == "13"
assert competition['numberOfPlaces'] == "25"