Skip to content

Commit

Permalink
test cases for R5-update (#44)
Browse files Browse the repository at this point in the history
* added tests for R5

* Made update-message naming consistent

* fixing code related to test errors

* Update test_update.py

Co-authored-by: c4thy <[email protected]>
Co-authored-by: Alex Webster <17aw43@queensu,ca>
  • Loading branch information
3 people authored Dec 14, 2020
1 parent 38970d6 commit 0804d03
Show file tree
Hide file tree
Showing 6 changed files with 250 additions and 43 deletions.
15 changes: 7 additions & 8 deletions qa327/backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ def register_user(email, name, password, password2):
db.session.commit()
except:
return False

return True


Expand Down Expand Up @@ -78,11 +78,10 @@ def update_ticket(name, quantity, price, date):
ticket.quantity = quantity
ticket.price = price
ticket.date = date
db.session.update(ticket)
db.session.commit()
return 1


# Backend functionality for ticket buying
def buy_ticket(name, user, quantity):
# Make sure ticket exists
Expand All @@ -92,12 +91,12 @@ def buy_ticket(name, user, quantity):
# Make sure enough quantity of tickets exist and user has enough balance
if ticket.quantity < quantity:
return 0
elif user.balance < (ticket.price*quantity + (ticket.price*quantity*0.4)):
elif user.balance < (ticket.price * quantity + (ticket.price * quantity * 0.4)):
return 0
else:

# Subtracts the ticket amount plus services and tax from the buyers account
user.balance -= quantity*ticket.price + (quantity*ticket.price*0.4)
user.balance -= quantity * ticket.price + (quantity * ticket.price * 0.4)

# Gets the seller's user data
seller = get_user(ticket.email)
Expand All @@ -106,7 +105,7 @@ def buy_ticket(name, user, quantity):
if not seller.balance:
seller.balance = 0
# Add the ticket sale revenue to the sellers balance
seller.balance += quantity*ticket.price
seller.balance += quantity * ticket.price

# Check if there are still tickets left after the order is complete
if ticket.quantity > quantity:
Expand All @@ -118,5 +117,5 @@ def buy_ticket(name, user, quantity):
# Commit all changes to the database
db.session.commit()
else:
return 0
return 1
return 0
return 1
52 changes: 27 additions & 25 deletions qa327/frontend.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
The html templates are stored in the 'templates' folder.
"""


@app.route('/register', methods=['GET'])
def register_get():
if 'logged_in' in session:
Expand All @@ -21,7 +22,6 @@ def register_get():

@app.route('/register', methods=['POST'])
def register_post():

patternEmail = re.compile(r"(^[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]+$)")
patternPass = re.compile("^(?=.*?[A-Z])(?=.*?[a-z])(?=.*?[^A-Za-z0-9]).{6,}$")

Expand All @@ -38,17 +38,17 @@ def register_post():
session['error'] = error_message
return redirect('/login')
elif not patternEmail.fullmatch(email):
error_message = '{} format is incorrect.'.format("Email")
error_message = '{} format is incorrect.'.format("Email")
session['error'] = error_message
return redirect('/login')

elif not patternPass.fullmatch(password):
error_message = '{} format is incorrect.'.format("Password")
error_message = '{} format is incorrect.'.format("Password")
session['error'] = error_message
return redirect('/login')

elif not patternName.fullmatch(name) or len(name) < 2 or len(name) > 20:
error_message = '{} format is incorrect.'.format("Name")
error_message = '{} format is incorrect.'.format("Name")
session['error'] = error_message
return redirect('/login')

Expand Down Expand Up @@ -82,10 +82,10 @@ def login_get():
def login_post():
email = request.form.get('email')
password = request.form.get('password')
#regex for email obtained from https://emailregex.com/
# regex for email obtained from https://emailregex.com/
EMAIL_REGEX = re.compile(r"(^[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]+$)")
PASSWORD_REGEX = re.compile(r"^(?=.*?[A-Z])(?=.*?[a-z])(?=.*?[^A-Za-z0-9]).{6,}$")
if not EMAIL_REGEX.match(email) or not PASSWORD_REGEX.match(password):
if not EMAIL_REGEX.match(email) or not PASSWORD_REGEX.match(password):
return render_template('login.html', message='email/password format invalid')
user = bn.login_user(email, password)
if user:
Expand Down Expand Up @@ -159,6 +159,7 @@ def profile(user):
tickets = bn.get_all_tickets()
return render_template('index.html', user=user, tickets=tickets)


@app.errorhandler(404)
def page_not_found(e):
return render_template('404.html'), 404
Expand Down Expand Up @@ -198,9 +199,9 @@ def check_ticket_form(name=None, quantity=None, price=None, date=None):
@app.route('/sell', methods=['POST'])
def sell():
"""
Route to sell a new ticket.
Route to sell a new ticket.
This route will validate the ticket form, if valid it will use a backend function
to commit to the database
to commit to the database
"""
if 'logged_in' not in session:
return redirect('/login')
Expand Down Expand Up @@ -230,32 +231,30 @@ def buy():
if 'logged_in' not in session:
return redirect('/login')
email = session['logged_in']
#Get user information
# Get user information
user = bn.get_user(email)
#Sets the error message to blank initially
error_message=""
#Get information from the form
# Sets the error message to blank initially
error_message = ""
# Get information from the form
name = request.form.get('name')
quantity = request.form.get('quantity')
#Get all tickets to pass to backend function
# Get all tickets to pass to backend function
tickets = bn.get_all_tickets()

error_message = check_ticket_form(name, quantity)
if not error_message:
if bn.buy_ticket(name,user,int(quantity)):
if bn.buy_ticket(name, user, int(quantity)):
message = "Tickets bought succesfully"
else:
error_message = "Ticket could not be bought"
#Checks if there is an error, and if there is set the error message
# Checks if there is an error, and if there is set the error message
if len(error_message) > 0:
session['error'] = error_message
message = session["error"]
del session["error"]
return render_template('index.html', buy_message=message, user=user, tickets=tickets)

def displayUpdateMessage(message):
return render_template('index.html', update_message=message)


@app.route('/update', methods=['POST'])
def update():
"""
Expand All @@ -266,23 +265,26 @@ def update():
if 'logged_in' not in session:
return redirect('/login')

#Grab necessary information from update form
# Grab necessary information from update form
user = bn.get_user(session['logged_in'])
tickets = bn.get_all_tickets()

name = request.form.get('name')
quantity = request.form.get('quantity')
price = request.form.get('price')
date = request.form.get('date')

error_message = check_ticket_form(name, quantity, price, date)
if error_message:
return displayUpdateMessage(error_message)
return render_template('index.html', update_message=error_message, user=user, tickets=tickets)

#Check if ticket exists in database
# Check if ticket exists in database
ticket = bn.get_ticket(name)
if (ticket is None):
return displayUpdateMessage('Ticket does not exist')
if ticket is None:
return render_template('index.html', update_message='Ticket does not exist', user=user, tickets=tickets)

#Update tickets to database
# Update tickets to database
bn.update_ticket(name, quantity, price, date)
return displayUpdateMessage('Successfully updated tickets')
return render_template('index.html', update_message='Successfully updated tickets', user=user, tickets=tickets)


12 changes: 6 additions & 6 deletions qa327/templates/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -53,18 +53,18 @@ <h4 id='buy-message'>{{buy_message}}</h4>

<br>
<h3>Update a Ticket</h3>
<h4 id='message'>{{update_message}}</h4>
<h4 id='update-message'>{{update_message}}</h4>
<form action="/update" method="post">
<div class="form-group">
<label for="name">Name</label>
<input class="form-control" name="name" id="name" required>
<input class="form-control" name="name" id="update-name" required>
<label for="quantity">Quantity</label>
<input class="form-control" name="quantity" id="quantity" required>
<input class="form-control" name="quantity" id="update-quantity" required>
<label for="price">price</label>
<input class="form-control" name="price" id="price" required>
<input class="form-control" name="price" id="update-price" required>
<label for="date">expiration date</label>
<input class="form-control" type="date" name="date" id="date" required>
<input class="btn btn-lg btn-primary" type="submit" id="btn-submit" value="Update Ticket">
<input class="form-control" type="date" name="date" id="update-date" required>
<input class="btn btn-lg btn-primary" type="submit" id="update-btn-submit" value="Update Ticket">
</div>
</form>

Expand Down
8 changes: 4 additions & 4 deletions qa327_test/frontend/test_home.py
Original file line number Diff line number Diff line change
Expand Up @@ -171,10 +171,10 @@ def test_home_update_form_exists(self, *_):
self.open(base_url)

# make sure each field exists under a form with the action to update
self.assert_element_present('form[action*="/update"] #name')
self.assert_element_present('form[action*="/update"] #quantity')
self.assert_element_present('form[action*="/update"] #price')
self.assert_element_present('form[action*="/update"] #date')
self.assert_element_present('form[action*="/update"] #update-name')
self.assert_element_present('form[action*="/update"] #update-quantity')
self.assert_element_present('form[action*="/update"] #update-price')
self.assert_element_present('form[action*="/update"] #update-date')

@patch('qa327.backend.get_user', return_value=test_user)
@patch('qa327.backend.get_all_tickets', return_value=test_tickets)
Expand Down
Loading

0 comments on commit 0804d03

Please sign in to comment.