Skip to content

Commit

Permalink
Merge pull request #39 from ted537/refractor
Browse files Browse the repository at this point in the history
Refractor
  • Loading branch information
nicoleooi authored Nov 19, 2020
2 parents 0805dc1 + ed3eb72 commit 624408e
Show file tree
Hide file tree
Showing 9 changed files with 107 additions and 127 deletions.
12 changes: 12 additions & 0 deletions qa327/backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,3 +49,15 @@ def register_user(email, name, password, password2):
def get_all_tickets():
"""Going to be implemented when /sell and /buy is implemented"""
return []

def buy_ticket(form):
'''buy a ticket, returns a message'''
raise "TODO"

def sell_ticket(form):
'''sell a ticket, returns a message'''
raise 'TODO'

def update_ticket(form):
'''update a ticket, returns a message'''
raise 'TODO'
15 changes: 9 additions & 6 deletions qa327/frontend.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,18 +110,21 @@ def login_post():

@app.route('/buy', methods=['POST'])
def buy_post():
flash('ticket bought successfully')
return redirect('/',303)
'''buy a ticket using the HTML form'''
flash(bn.buy_ticket(request.form))
return redirect('/', 303)

@app.route('/sell', methods=['POST'])
def sell_post():
flash('ticket sold successfully')
return redirect('/',303)
'''sell a ticket using the HTML form'''
flash(bn.sell_ticket(request.form))
return redirect('/', 303)

@app.route('/update', methods=['POST'])
def update_post():
flash('ticket updated successfully')
return redirect('/',303)
'''update a ticket using the HTML form'''
flash(bn.update_ticket(request.form))
return redirect('/', 303)


@app.route('/logout')
Expand Down
7 changes: 5 additions & 2 deletions qa327_test/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,17 @@
import threading
from werkzeug.serving import make_server

# separate port allows tests to be run while hosting
# actual app
FLASK_TEST_PORT = 8082

base_url = 'http://localhost:{}'.format(FLASK_PORT)
base_url = 'http://localhost:{}'.format(FLASK_TEST_PORT)

class ServerThread(threading.Thread):

def __init__(self):
threading.Thread.__init__(self)
self.srv = make_server('127.0.0.1', FLASK_PORT, app)
self.srv = make_server('127.0.0.1', FLASK_TEST_PORT, app)
self.ctx = app.app_context()
self.ctx.push()

Expand Down
32 changes: 32 additions & 0 deletions qa327_test/frontend/geek_base.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
from seleniumbase import BaseCase
from werkzeug.security import generate_password_hash
from qa327_test.conftest import base_url
from qa327.models import User

# Mock a sample user
TEST_USER = User(
email='[email protected]',
name='test_frontend',
password=generate_password_hash('test_frontend'),
balance=140
)

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
raise AssertionError(f'Flash not found for text "{text}"')

def login_test_user(self, email=TEST_USER.email, password='test_frontend'):
'''login our test user'''
self.open(base_url+'/login')
self.input('#email', email)
self.input('#password', password)
self.click('#btn-submit')
27 changes: 6 additions & 21 deletions qa327_test/frontend/test_r1.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,31 +8,16 @@

from qa327_test.conftest import base_url
from qa327.models import User

# Mock a sample user
TEST_USER = User(
email='[email protected]',
name='test_frontend',
password=generate_password_hash('test_frontend'),
balance=140
)
from qa327_test.frontend.geek_base import GeekBaseCase, TEST_USER

INVALID_EMAILS = ['test_frontendtest.com', '[email protected]', 'test\f[email protected]']

INVALID_PASSWORDS = ['Pass!', 'password123!', 'PASSWORD123!', 'Password123']

class R1Test(BaseCase):
class R1Test(GeekBaseCase):
'''
Contains test cases specific to R1
'''

def login_test_user(self):
'''login our test user'''
self.open(base_url+'/login')
self.input('#email', TEST_USER.email)
self.input('#password', 'test_frontend')
self.click('#btn-submit')

def test_login_redirects(self, *_):
'''see r1.1'''
self.open(base_url)
Expand Down Expand Up @@ -71,7 +56,7 @@ def test_form_password_missing(self, *_):
self.open(base_url+'/login')
self.input('#email', TEST_USER.email)
self.click('#btn-submit')
#leave password empty
# leave password empty
message = self.driver.find_element_by_id('password')
assert message.get_attribute('validationMessage') == 'Please fill out this field.'

Expand All @@ -81,7 +66,7 @@ def test_form_email_missing(self, *_):
self.open(base_url+'/login')
self.input('#password', 'test_frontend')
self.click('#btn-submit')
#leave password empty
# leave password empty
message = self.driver.find_element_by_id('email')
assert message.get_attribute('validationMessage') == 'Please fill out this field.'

Expand All @@ -93,7 +78,7 @@ def test_email_rfc_specs(self, *_):

def test_invalid_email_rfc_specs(self, *_):
'''see r1.7 (negative)'''
#invalid email format
# invalid email format
for invalid_email in INVALID_EMAILS:
self.open(base_url+'/login')
self.input('#email', invalid_email)
Expand All @@ -111,7 +96,7 @@ def test_password_complexity(self, *_):

def test_invalid_password_complexity(self, *_):
'''see r1.8 (negative)'''
#invalid password complexity
# invalid password complexity
for invalid_pass in INVALID_PASSWORDS:
self.open(base_url+'/login')
self.input('#email', TEST_USER.email)
Expand Down
76 changes: 26 additions & 50 deletions qa327_test/frontend/test_R2.py → qa327_test/frontend/test_r2.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,54 +4,45 @@

from unittest.mock import patch
from werkzeug.security import generate_password_hash
from seleniumbase import BaseCase
from qa327_test.conftest import base_url

import qa327.models

# Defines user class to make testing more streamlined
class User:
def __init__(self, email=None, name=None, password=None):
self.email = email
self.name = name
self.password = password

from qa327.models import User
from qa327_test.conftest import base_url
from qa327_test.frontend.geek_base import GeekBaseCase

# Defines test information
TEST_USER_A = qa327.models.User(
TEST_USER_A = User(
email='[email protected]',
name='test_frontend',
password=generate_password_hash('Password123!', method='sha256'),
balance=5000
)

TEST_USER_B = qa327.models.User(
email='[email protected]',
name='test_frontend',
password='Password123!'
)


VALID_USER = User(
email='[email protected]',
name='test_frontend',
password='Password123!'
)

INVALID_USER_NAME_FORMATS = ['', 'test_frontend123!', ' test_frontend123',
'test_frontend123 ']
INVALID_USER_NAME_FORMATS = [
'', 'test_frontend123!',
' test_frontend123', 'test_frontend123 '
]

INVALID_USER_NAME_LENGTHS = ['te', 'test_frontend1234567890']

INVALID_USER_EMAILS = ['', 'test_frontendtest.com', 'test_frontend@testcom',
'[email protected]']
INVALID_USER_EMAILS = [
'', 'test_frontendtest.com',
'test_frontend@testcom', '[email protected]'
]

INVALID_USER_PASSWORDS = ['', 'Pass!', 'password123!', 'PASSWORD123!',
'Password123']
INVALID_USER_PASSWORDS = [
'', 'Pass!', 'password123!',
'PASSWORD123!','Password123'
]

MISMATCHED_PASSWORD2 = 'Password123! '

class R2Test(BaseCase):
class R2Test(GeekBaseCase):
'''
Contains test cases specific to R2
'''
Expand All @@ -71,27 +62,6 @@ def register_test_user(self, *_):
# Submits the inputted information
self.click('#register-submit')


def login_test_user(self):
'''login our test user'''
# Opens login page
self.open(base_url+'/login')

# Logins in with user information
self.input('#email', TEST_USER_A.email)
self.input('#password', VALID_USER.password)

# Submits the inputted information
self.click('#btn-submit')

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
raise AssertionError(f'Flash not found for text "{text}"')


@patch('qa327.backend.get_user', return_value=TEST_USER_A)
def test_r2_1(self, *_):
'''
Expand All @@ -100,7 +70,10 @@ def test_r2_1(self, *_):
'''

# Logs in user to
self.login_test_user()
self.login_test_user(
email=TEST_USER_A.email,
password='Password123!'
)

# Opens the user profile page /
self.open(base_url)
Expand Down Expand Up @@ -354,7 +327,7 @@ def test_r2_9(self, *_):
assert self.get_current_url() == base_url+'/login'


@patch('qa327.backend.get_user', return_value=TEST_USER_B)
@patch('qa327.backend.get_user', return_value=VALID_USER)
def test_r2_10(self, *_):
'''
10) Test Case R2.10 - If the email already exists, show message
Expand Down Expand Up @@ -398,7 +371,10 @@ def test_r2_11(self, get_user_function, *_):
get_user_function.return_value = TEST_USER_A

# Logs in user with newly registered user
self.login_test_user()
self.login_test_user(
email=TEST_USER_A.email,
password='Password123!'
)

# Opens the user profile page /
self.open(base_url)
Expand Down
34 changes: 6 additions & 28 deletions qa327_test/frontend/test_r3.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,46 +3,21 @@
'''

from unittest.mock import patch
from seleniumbase import BaseCase
from werkzeug.security import generate_password_hash

from qa327_test.conftest import base_url
from qa327.models import User

# Moch a sample user
TEST_USER = User(
email='[email protected]',
name='test_frontend',
password=generate_password_hash('test_frontend'),
balance=140
)
from qa327_test.frontend.geek_base import GeekBaseCase, TEST_USER

# Moch some sample tickets
TEST_TICKETS = [
{'name': 't1', 'price': '100', 'owner': 'god', 'count': 2},
{'name': 't2', 'price': '90', 'owner': 'geek', 'count': 3},
]


class R3Test(BaseCase):
class R3Test(GeekBaseCase):
'''
Contains test cases specific to R3
'''

def login_test_user(self):
'''login our test user'''
self.open(base_url+'/login')
self.input('#email', TEST_USER.email)
self.input('#password', 'test_frontend')
self.click('#btn-submit')

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
raise AssertionError(f'Flash not found for text "{text}"')

def test_login_redirects(self, *_):
'''see r3.1'''
self.open(base_url)
Expand Down Expand Up @@ -124,6 +99,7 @@ def test_update_form(self, *_):
self.assert_element('#update-ticket-expiration-date')

@patch('qa327.backend.get_user', return_value=TEST_USER)
@patch('qa327.backend.sell_ticket', return_value='ticket sold successfully')
def test_sell_posts(self, *_):
'''see r3.9'''
self.login_test_user()
Expand All @@ -136,6 +112,7 @@ def test_sell_posts(self, *_):
self.assert_flash('ticket sold successfully')

@patch('qa327.backend.get_user', return_value=TEST_USER)
@patch('qa327.backend.buy_ticket', return_value='ticket bought successfully')
def test_buy_posts(self, *_):
'''see r3.10'''
self.login_test_user()
Expand All @@ -146,6 +123,7 @@ def test_buy_posts(self, *_):
self.assert_flash('ticket bought successfully')

@patch('qa327.backend.get_user', return_value=TEST_USER)
@patch('qa327.backend.update_ticket', return_value='ticket updated successfully')
def test_update_posts(self, *_):
'''see r3.11'''
self.login_test_user()
Expand All @@ -155,4 +133,4 @@ def test_update_posts(self, *_):
self.input('#update-ticket-price', 'dont-care')
self.input('#update-ticket-expiration-date', 'dont-care')
self.click('#update-submit')
self.assert_flash('ticket updated successfully')
self.assert_flash('ticket updated successfully')
Loading

0 comments on commit 624408e

Please sign in to comment.