def add_ticket(ticket_name, quantity, price, expiration, owners_email):
    """
    this function takes a defintion of a ticket, ensures that the seller has not already posted the ticket, then either:
    posts the new ticket to the database, or
    returns an error message explaining why the ticket could not be posted.
    """
    #get any existing tickets of the same name posted by the user.
    existing = get_existing_tickets(ticket_name, owners_email)
    if existing == []:  #if no existing tickets were found, add a new ticket to the database
        try:
            ticket = Ticket(ticket_name=ticket_name,
                            quantity=quantity,
                            price=price,
                            expiration=expiration,
                            owners_email=owners_email)
            db.session.add(ticket)
            db.session.commit()
            return None
        except:
            return 'error'
    else:  #otherwise,
        if existing[
                0].ticket_name == ticket_name:  #return an error if the name was in use
            return "You have already posted a ticket with that name; if you are updating a batch, please use the update form"
        if existing[0].price != int(
                price
        ):  #return an error if the user is attempting to list the same batch at a new price.
            return "Please update the quantity/price of your existing tickets instead of posting a new batch."
    return "Failed to list tickets."
def post_tickets(ticket_name, num_tickets, ticket_price, ticket_date, email):
    """
    Add new tickets to the db

    :param ticket_name: The name of the ticket
    :param num_tickets: Number of tickets being created
    :param ticket_price: Price of each ticket
    :param email: email of the ticket owner
    :return: True when ticket created successfully
    """

    # Check ticket of same name does not already exist
    if get_ticket(ticket_name):
        return False

    # store the encrypted password rather than the plain password
    new_ticket = Ticket(ticket_name=ticket_name,
                        num_tickets=num_tickets,
                        ticket_price=ticket_price,
                        ticket_date=ticket_date,
                        ticket_owner=email)

    db.session.add(new_ticket)

    try:
        db.session.commit()
        return True
    except Exception as e:
        db.session.rollback()  # Rollback the current transaction in progress
        db.session.flush()  # Reset non-commited .add()
        return False
Example #3
0
def get_update():
    name = request.form['name_update_changes']
    quantity = request.form['quantity_update_changes']
    price = ['price_update_changes']
    exp_date = ['exp_date_update_changes']
    updated_ticket = Ticket(name, quantity, price, exp_date)
    return updated_ticket
def create_ticket(name, quantity, price, date, email):
    new_ticket = Ticket(name=name,
                        quantity=quantity,
                        price=price,
                        date=date,
                        email=email)
    db.session.add(new_ticket)
    db.session.commit()
Example #5
0
def create_ticket(title, quantity, price, expDate):
    new_ticket = Ticket(title=title,
                        quantity=quantity,
                        price=price,
                        expDate=expDate)
    db.session.add(new_ticket)
    db.session.commit()
    return None
 def add_ticket(self):
     """Add a test ticket to the database"""
     testTicket = Ticket(name='test buy int ticket',
                         email='testbuyint@email',
                         quantity=99,
                         price=50,
                         expiration_date=20221225)
     db.session.add(testTicket)
     db.session.commit()
def sell_ticket(user, name, quantity, price, expiration):
    '''sell a ticket, returns a message'''
    ticket = Ticket(name=name,
                    quantity=quantity,
                    price=price,
                    expires=parse_date(expiration),
                    seller=user)
    db.session.add(ticket)
    db.session.commit()
    return 'ticket sold successfully'
Example #8
0
def update_ticekt(name, quantity, price, date, user):
    ticket = Ticket()
    ticket.name = name
    ticket.quantity = quantity
    ticket.price = price
    ticket.date = date
    ticket.user = user
    db.session.add(ticket)
    db.session.sell(ticket)
    db.session.commit()
Example #9
0
def sell_ticket(name, quantity, price, date, user):
#Used to test the sell ticket function
    ticket = Ticket()
    ticket.name = name
    ticket.quantity = quantity
    ticket.price = price
    ticket.date = date
    ticket.user = user
    db.session.add(ticket)
    db.session.commit()
Example #10
0
def new_ticket_for_sell(name, email, quantity, price, date):
    """
    Create new ticekt for selling by user
    :param name: ticket name for sell
    :param quantity: ticket quantity for sell
    :param price: ticket price for sell
    :param date: ticket expiratation date for sell
    """
    new_ticket = Ticket(name=name,
                        owner_email=email,
                        quantity=quantity,
                        price=price,
                        date=date)
    db.session.add(new_ticket)
    db.session.commit()
    return new_ticket
Example #11
0
def sell_ticket(quantity, name, email, price, expiration_date):
    """
    :Param quantity: Quantity of tickets that are being sold.
    :Param name: The name of the ticket.
    :Param email: The owners email that is selling the ticket.
    :Param price: The price of the ticket being sold.
    :Param expiration date: The date the ticket expires.

    Takes the information inputted in the form on the website assigns and adds it to the list
    of available tickets in the data base.
    """
    for i in range(int(quantity)):
        new_ticket = Ticket(name=name, email=email,
                            price=price, date=expiration_date)
        db.session.add(new_ticket)
        db.session.commit()
    return True
Example #12
0
def sell_tickets(name, email, quantity, price, expiration_date):
    """
    Add ticket to listing
    :param name: the name of the ticket
    :param quantity: the number of tickets to sell
    :param price: the price of each ticket in dollars
    :param expiration_date: the day the tickets expire
    :return: an error message if there is any, or None if ticket listing succeeds
    """
    new_ticket = Ticket(name=name,
                        email=email,
                        quantity=quantity,
                        price=price,
                        expiration_date=expiration_date)
    db.session.add(new_ticket)
    db.session.commit()
    return None
Example #13
0
def add_new_ticket(name,quantity,price,expiration_date):
    """
    Create new ticket to database
    :param name: the name of the ticket
    :param quantity: the quantity of tickets
    :param price: the price of the ticket
    :param expiration_date: the expiration date of the ticket
    :return: returns None
    """
    new_ticket = Ticket(name=name,quantity=quantity,price=price,expiration_date=expiration_date)
    if new_ticket.expiration_date < 20200831:
        return None
    elif not Ticket.query.filter_by(name=new_ticket.name).count():
        db.session.add(new_ticket)
        db.session.commit()

    return None
Example #14
0
def register_ticket(owner, name, quantity, price, date):
    """Register the ticket in the database
    :param owner: The user selling the ticket
    :param name: the name of the ticket
    :param quantity: quantity available of the ticket
    :param price: the price of each ticket
    :param date: the date for ticket use
    :return: an error message if there is any, or None if action succeeds."""

    new_ticket = Ticket(owner=owner,
                        name=name,
                        quantity=quantity,
                        price=price,
                        date=date)

    db.session.add(new_ticket)
    db.session.commit()

    return new_ticket
Example #15
0
def update_ticket(name, email, quantity, price, date):
    """
    Change ticket information
    :param name: ticket name for sell
    :param quantity: ticket quantity for sell
    :param price: ticket price for sell
    :param date: ticket expiratation date for sell
    """
    # Find target ticket in database, and delete it.
    Ticket.query.filter(Ticket.name == name).delete()
    # Then add a new ticket to the database, with same name but modified information.
    new_ticket = Ticket(name=name,
                        owner_email=email,
                        quantity=quantity,
                        price=price,
                        date=date)
    db.session.add(new_ticket)
    db.session.commit()
    return new_ticket
Example #16
0
def set_ticket(owner, name, quantity, price, date):
    """
    Register a ticket to the database
    :param owner: the email of the ticket seller
    :param name: the name of the ticket
    :param quantity: the quantity of tickets being sold
    :param price: the price of each ticket being sold
    :param date: the date the tickets expire
    :return: an error message if there is any, or None if register succeeds
    """
    new_ticket = Ticket(owner=owner,
                        name=name,
                        quantity=quantity,
                        price=price,
                        date=date)

    db.session.add(new_ticket)
    db.session.commit()
    return None
    def setup(self):
        db.drop_all()
        db.create_all()

        # Create the state of the application to make it able to buy a ticket for a new user.
        # This allows the test to be independent from the ability to sell a ticket.
        # Remember this test is to test the full ability to create a user and buy a ticket not to sell one.
        owner = User(name="test",
                     email="*****@*****.**",
                     balance=5000,
                     password=generate_password_hash('Test1234!',
                                                     method='sha256'))
        ticket = Ticket(name="Test",
                        quantity=2,
                        price=20,
                        email="*****@*****.**")
        db.session.add(ticket)
        db.session.add(owner)
        db.session.commit()
Example #18
0
def sell_ticket(name, quantity, price, date, user):
    """
    Create new ticket in the database
    :param ticket_id: the id of the ticket to be updated
    :param name: the name of the ticket
    :param quantity: the amount of tickets for sale
    :param price: the price of the ticket
    :param date: the expiry date of the ticket
    :param user: seller of the ticket
    :return: an error message if there is any, or None if creation succeeds
    """
    ticket = Ticket()
    ticket.name = name
    ticket.quantity = quantity
    ticket.price = price
    ticket.date = date
    ticket.user = user
    db.session.add(ticket)
    db.session.commit()
Example #19
0
def sell_ticket(name: str, price: float, day: str, amount: int,
                user: User) -> Union[str, None]:

    errors = validate_ticket_inputs(name, price, day, amount, user)

    if (errors != None): return errors

    if not (price in range(10, 101)):
        return "Please enter an amount between 10 and 100"

    if (Ticket.query.filter_by(name=name).filter_by(date=day).first() != None):
        return "There is a ticket already specified"

    ticket = Ticket(name=name,
                    price=price,
                    date=day,
                    creator=user.id,
                    quantity=amount)

    db.session.add(ticket)
    db.session.commit()
Example #20
0
def create_ticket(name, quantity, price, date):
    """
    Creates ticket quantity, price, and expiration date
    :param name: The ticket name to update
    :param quantity: The new quantity
    :param price: The new price
    :param date: The new expiration date
    """
    try:
        quantity = int(quantity)
        price = float(price)
        date = datetime.strptime(date, '%Y%m%d')
        new_ticket = Ticket(name=name,
                            quantity=quantity,
                            price=price,
                            expiration_date=date)

        db.session.add(new_ticket)
        db.session.commit()
        return None
    except:
        return "Unable to parse query"
Example #21
0
def sell_ticket(user, name, quantity, price, expiryDate):
    """
    Update the quantity, price and expiry date of a ticket of a given name
    :param user: the user who is selling the ticket
    :param name: the name of the ticket to be updated
    :param quantity: the new quantity for the ticket
    :param price: the new price for the ticket
    :param expiryDate: the new expiry date of the ticket
    :return: a string describing the error that occurred, or False for no error
    """
    if not isinstance(user, User):
        return "Internal Error: 'user' must be of type 'User'"
    if not isinstance(name, str):
        return "Internal Error: 'name' must be of type 'str'"
    if not isinstance(quantity, int):
        return "Internal Error: 'quantity' must be of type 'int'"
    if not isinstance(price, float):
        return "Internal Error: 'price' must be of type 'float'"
    if not isinstance(expiryDate, date):
        return "Internal Error: 'expiryDate' must be of type 'date'"
    if User.query.filter_by(id=user.id).first() is None:
        return "Internal Error: user does not exist in database"
    new_ticket = Ticket()
    new_ticket.owner = user
    new_ticket.name = name
    new_ticket.quantity = quantity
    new_ticket.price = int(price * 100)
    new_ticket.expiry = expiryDate
    db.session.add(new_ticket)
    try:
        db.session.commit()
    except IntegrityError as e:
        db.session.rollback()
        # We got an integrity error. Check if it was due to
        # UNIQUE constraint being violated.
        # I can't find a better way to do this unfortunately.
        args_str = ' '.join(e.args)
        if 'UNIQUE constraint failed' in args_str:
            return 'A ticket with that name already exists.'
        raise e
    return False
Example #22
0
def sell_ticket(name, qty, price, date):
    errors = []
    try:
        errors += (validate_ticket_name(name))
        errors += (validate_ticket_qty(qty))
        errors += (validate_ticket_price(price))
        if not validate_ticket_date(date):
            errors.append("invalid date format")
    except ValueError:
        ""
    if len(errors) > 0:
        return errors

    #Sell Ticket using database
    year = date[0:4]
    month = date[4:6]
    #Changed day to accept end of string in format: "00"
    day = date[6:8]
    converteddate = datetime(int(year), int(month), int(day))
    #update db with new ticket info
    ticket = Ticket(name=name, qty=qty, price=price, exp=converteddate)
    db.session.add(ticket)
    db.session.commit()
    return []
from unittest.mock import patch
from werkzeug.security import generate_password_hash, check_password_hash

test_user = User(email='*****@*****.**',
                 name='test_user',
                 password=generate_password_hash('test_frontendA1$'),
                 balance=100)

test_user2 = User(email='*****@*****.**',
                  name='test_user',
                  password=generate_password_hash('test_frontendA1$'),
                  balance=5000)

test_ticket = Ticket(name='test ticket',
                     owner=1,
                     qty=100,
                     price=1,
                     exp='20201119')

test_ticket2 = Ticket(name='test ticket',
                      owner=1,
                      qty=10,
                      price=15,
                      exp='20201119')

test_ticket3 = Ticket(name='test ticket 3',
                      owner=1,
                      qty=100,
                      price=10,
                      exp='20201207')
Example #24
0
from seleniumbase import BaseCase

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

# Moch a sample user
test_user = User(email='*****@*****.**',
                 name='test_user',
                 password=generate_password_hash('Test!!'),
                 balance=5000)

# Moch some sample tickets
test_tickets = Ticket(title='TestTest',
                      quantity=50,
                      price=50,
                      expDate=20201212)


class R3Test(BaseCase):

    # R3.1 If the user is not logged in, redirect to login page
    def test_R3_1(self, *_):
        self.open(base_url + '/logout')
        self.open(base_url + '/login')
        self.assert_element("#login-header")
        self.assert_text("Log In", "#login-header")

# R3.2 This page shows a header 'Hi {}'.format(user.name)

    @patch('qa327.backend.get_user', return_value=test_user)
Example #25
0
from qa327.models import db, User, Ticket
from werkzeug.security import generate_password_hash, check_password_hash

# Mock a sample user
test_user = User(
    email='*****@*****.**',
    name='test_frontend',
    password=generate_password_hash('Password!'),
    balance=5000
)

# Mock a ticket
test_ticket = Ticket(
    owner = "*****@*****.**",
    name = "test_ticket",
    quantity = 10,
    price = 10,
    date = 20201230
)

class IndexTest(BaseCase):

    @patch('qa327.backend.get_user', return_value=test_user)
    @patch('qa327.backend.login_user', return_value=test_user)
    def _login(self, *_):
        # Navigate to /logout to invalidate any existing sessions
        self.open(base_url + '/logout')
        # Navigate to /login
        self.open(base_url + '/login')
        # Submit correct credentials
        self.type("#email", test_user.email)
# Mock a sample user
TEST_USER = User(email='*****@*****.**',
                 name='test_frontend',
                 password=generate_password_hash('test_frontend'),
                 balance=500)

TEST_USER_SELLER = User(email='*****@*****.**',
                        name='test_seller',
                        password=generate_password_hash('Password99!'),
                        balance=500)

# Mock a sample ticket
TEST_TICKET = Ticket(name='helloworld',
                     seller=TEST_USER_SELLER,
                     price=20,
                     quantity=20,
                     expires="20220101")


class GeekBaseCase(BaseCase):
    '''
    Selenium base case with some
    GeekSeek utilities
    '''
    def assert_flash(self, text):
        '''asserts that message exists in flashes'''
        for flash_dom in self.find_elements('.flash'):
            if flash_dom.text == text:
                return
            print(flash_dom.text)
Example #27
0
import pytest
from seleniumbase import BaseCase

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

test_user = User(email='*****@*****.**',
                 name='Test',
                 password=generate_password_hash('Testing!'),
                 balance=5000)
test_ticket = Ticket(name='test ticket yo',
                     quantity='10',
                     price='10',
                     expiration_date=20201201)
test_ticket_old = Ticket(name='test ticket old',
                         quantity='10',
                         price='10',
                         expiration_date=20101101)
test_update = Ticket(name='update ticket',
                     quantity='10',
                     price='10',
                     expiration_date=20201201)
test_sell = Ticket(name='sell ticket',
                   quantity='10',
                   price='10',
                   expiration_date=20201201)
test_buy = Ticket(name='test ticket',
                  quantity='10',
                  price='10',
Example #28
0
import pytest
from seleniumbase import BaseCase
from qa327.models import db, User, Ticket
from qa327_test.conftest import base_url
from unittest.mock import patch
from werkzeug.security import generate_password_hash, check_password_hash

test_user = User(email='*****@*****.**',
                 name='test_user',
                 password=generate_password_hash('test_frontendA1$'))
test_ticket = Ticket(name='test ticket',
                     owner=1,
                     qty=10,
                     price=15,
                     exp='20201119')


class R4Test1(BaseCase):
    @patch('qa327.backend.get_user', return_value=test_user)
    def test_r4_5_2_1(self, *_):
        #logout if logged in
        self.open(base_url + '/logout')
        #login
        self.open(base_url + '/login')
        #enter user credentials
        self.type("#email", "*****@*****.**")
        self.type("#password", "test_frontendA1$")

        self.click("input[type='submit']")
        self.open(base_url)
        #make ticket
Example #29
0
def store_ticket(name, price, quantity, date):
    new_ticket = Ticket(name=name, price=price, quantity=quantity, date=date)
    
    db.session.add(new_ticket)
    db.session.commit()
    return None
Example #30
0
Annotate @patch before unit tests can mock backend methods (for that testing function)
"""
# Mock a sample user
test_user = User(email='*****@*****.**',
                 name='test_frontend',
                 password=generate_password_hash('Test_frontend@'),
                 balance=500)
# Mock a user that will not have enough money to buy a ticket
test_poor_user = User(email='*****@*****.**',
                      name='namepoor',
                      password=generate_password_hash('Name_register@1'),
                      balance=10)
# Mock some sample tickets
test_ticket = Ticket(name='t1',
                     price=100,
                     quantity=2,
                     email='*****@*****.**',
                     date='20200223')

test_tickets = [
    Ticket(name='t1',
           price=100,
           quantity=2,
           email='*****@*****.**',
           date='20200223'),
    Ticket(name='t2',
           price=110,
           quantity=10,
           email='*****@*****.**',
           date='20200314')
]