Skip to content

Commit

Permalink
Merge pull request #37 from atangent/18kdm2
Browse files Browse the repository at this point in the history
Test cases for R4-R6
  • Loading branch information
SethDamiani authored Dec 14, 2020
2 parents 3d86734 + 4311378 commit 8f1abe9
Show file tree
Hide file tree
Showing 6 changed files with 591 additions and 32 deletions.
28 changes: 0 additions & 28 deletions Test Requirements/tests-amy.md
Original file line number Diff line number Diff line change
Expand Up @@ -244,31 +244,3 @@ Actions:
- click element input[type="submit"]
- check that the ticket is not added, and that the user is redirected to / with the correct message

### Test case 6001 - R5.6 - Check that the ticket id exists
Assumptions:
- ticket1 = ticket id that exists in the database
- ticket2 = ticket id that doesn't exist in the database

Mocking:
- mock backend.get_user to return a test_user instance
- mock backend.create_ticket to return True

Actions:
- open /logout (to invalidate any logged-in sessions that may exist)
- open /login
- enter test_user's email into element #email
- enter test_user's password into element #password
- click element input[type="submit"]
- open /
- enter " test_ticket_name " into #name for ticket1
- enter 5 into #quantity
- enter 15 into #price
- enter (today's date + 1 day) to #date
- click element input[type="submit"]
- check that the ticket is added, and that the user is redirected to /
- enter " test_ticket_name " into #name for ticket2
- enter 5 into #quantity
- enter 15 into #price
- enter (today's date + 1 day) to #date
- click element input[type="submit"]
- check that the ticket is not added, and that the user is redirected to / with the correct message
8 changes: 8 additions & 0 deletions qa327/frontend.py
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,14 @@ def is_ticket_date_valid(ticket_date):
:param ticket_date: the date of the ticket to be tested
:returns: True if the ticket date satisfies all requirements
"""
try:
if isinstance(ticket_date, str):
datetime.datetime.strptime(ticket_date, "%Y%m%d")
except ValueError:
return False

if isinstance(ticket_date, str):
return datetime.datetime.strptime(ticket_date, "%Y%m%d") > datetime.datetime.now()
return ticket_date > datetime.datetime.now()

def does_user_have_sufficient_balance(user_balance, ticket_price):
Expand Down
2 changes: 1 addition & 1 deletion qa327/templates/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ <h2>Edit ticket</h2>
Price: <input id="update_price" type="number" name="price"/><br/>
Expiration Date (YYYMMDD): <input id="update_exp_date" type="text" name="date"/><br/>
<input type="hidden" value="{{ user.email }}" name="user"/>
<input type="submit" value="Save"/>
<input id="update_submit" type="submit" value="Save"/>
</form>

{% endblock %}
113 changes: 112 additions & 1 deletion qa327_test/frontend/buy/test_frontend_buy.py
Original file line number Diff line number Diff line change
@@ -1 +1,112 @@
# All tests for R6 - /buy go here
# All tests for R6 - /buy go here

import pytest
import decimal
from seleniumbase import BaseCase
from datetime import datetime, timedelta

from qa327_test.conftest import base_url
from unittest.mock import patch
from qa327.models import db, User, Ticket
from werkzeug.security import generate_password_hash, check_password_hash

"""
File contains all unit tests for R6 Buy
Frontend
"""

# Mock a sample ticket
test_ticket = Ticket(
id=5,
user=10,
name="Test Ticket",
price=5,
quantity=1
)

# Mock tickets
test_tickets = [
Ticket(
id=3,
user=10,
name='TestTicket1',
price=50,
quantity=2
),
Ticket(
id=4,
user=10,
name='TestTicket2',
price=10,
quantity=1
)
]

# Mock Users
test_user = User(
id=2,
email='test_frontend@test.com',
name='test_frontend',
password=generate_password_hash('test_Frontend0!'),
balance=decimal.Decimal("5000")
)

broke_user = User(
id=5,
email='test_frontend2@test.com',
name='test_frontend2',
password=generate_password_hash('test_Frontend0!'),
balance=decimal.Decimal("10")
)

nextdayobj = datetime.now() + timedelta(days=1)
nextday = nextdayobj.strftime('%Y%m%d')

yesterdayobj = datetime.now() - timedelta(days=1)
yesterday = yesterdayobj.strftime('%Y%m%d')

class FrontEndBuyTest(BaseCase):

@patch('qa327.backend.get_user', return_value=test_user)
@patch('qa327.backend.get_all_tickets', return_value=test_tickets)
@patch('qa327.backend.get_ticket', return_value=test_tickets[1])
@patch('qa327.backend.buy_ticket', return_value=None)
def test_ticket_buy_success(self, *_):
# log in user
self.open(base_url + '/logout')
self.open(base_url + '/login')
self.type("#email", "test_frontend@test.com")
self.type("#password", "test_Frontend0!")
self.click('input[type="submit"]')
self.open(base_url)

# buy a ticket the user can't afford
self.click('#ticket-TestTicket2-buy')
self.click('#buy_submit')

# assert that they failed to purchase the ticket
self.assert_element('#message')
self.assert_text("Ticket purchased successfully.", "#message")

@patch('qa327.backend.get_user', return_value=broke_user)
@patch('qa327.backend.get_all_tickets', return_value=test_tickets)
@patch('qa327.backend.get_ticket', return_value=test_tickets[0])
@patch('qa327.backend.buy_ticket', return_value=None)
def test_ticket_buy_fail(self, *_):
# log in user
self.open(base_url + '/logout')
self.open(base_url + '/login')
self.type("#email", "test_frontend@test.com")
self.type("#password", "test_Frontend0!")
self.click('input[type="submit"]')
self.open(base_url)

# buy a ticket the user can't afford
self.click('#ticket-TestTicket1-buy')
self.click('#buy_submit')

# assert that they failed to purchase the ticket
self.assert_element('#message')
self.assert_text("User balance does not have sufficient funds.", "#message")

Loading

0 comments on commit 8f1abe9

Please sign in to comment.