Skip to content

Commit

Permalink
refactored index.py, changed function & route names for consistency
Browse files Browse the repository at this point in the history
  • Loading branch information
Justin-Kwan committed Jul 3, 2019
1 parent 9e25fc2 commit c11e93a
Show file tree
Hide file tree
Showing 32 changed files with 241 additions and 159 deletions.
30 changes: 30 additions & 0 deletions server/src/IndexReturnDecider.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
from ResultCodes import ResultCodes

resultCodes = ResultCodes()

class IndexReturnDecider():

def determineSignUpRedirectPage(self, resultCode):
if resultCode == resultCodes.SUCCESS:
return 'signUpSuccess'
elif resultCode == resultCodes.ERROR_DUPLICATE_USERNAME:
return 'signUpExistingUsername'
elif resultCode == resultCodes.ERROR_INVALID_USERNAME_CHARS:
return 'signUpInvalidUsernameCharacters'
elif resultCode == resultCodes.ERROR_USERNAME_LENGTH_INVALID:
return 'signUpUsernameOutOfRange'
elif resultCode == resultCodes.ERROR_PASSWORD_LENGTH_INVALID:
return 'signUpPasswordOutOfRange'
else:
return 'signUpEmptyFields'

def determineLoginRedirectPage(self, resultCode):
if resultCode == resultCodes.ERROR_INVALID_USERNAME_OR_PASSWORD:
return 'loginInvalidUsernameOrPassword'
else:
return 'loginEmptyFields'

def checkIfTokenReturned(self, resultCode):
if resultCode == resultCodes.SUCCESS:
return True
return False
25 changes: 13 additions & 12 deletions server/src/ResultCodes.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@

class ResultCodes():

# result codes for SignupController
ERROR_USERNAME_LENGTH_INVALID = 'INVALID_USERNAME_LENGTH'
ERROR_PASSWORD_LENGTH_INVALID = 'INVALID_PASSWORD_LENGTH'
ERROR_INVALID_USERNAME_CHARS = 'INVALID_USERNAME_CHARS'
ERROR_DUPLICATE_USERNAME = 'DUPLICATE_USERNAME'
ERROR_EMPTY_USERNAME = 'EMPTY_USERNAME'
ERROR_EMPTY_PASSWORD = 'EMPTY_PASSWORD'
ERROR_EMPTY_FIELDS = 'EMPTY_FIELDS'
SUCCESS_USERNAME_PASSWORD_LENGTH = 'GOOD_USERNAME_&_PASSWORD_LENGTH'
SUCCESS_FIELDS_FILLED = 'ALL_FIELDS_FILLED'
SUCCESS = 'SUCCESS'
# result codes for SignupHandler
ERROR_USERNAME_LENGTH_INVALID = 'INVALID_USERNAME_LENGTH'
ERROR_PASSWORD_LENGTH_INVALID = 'INVALID_PASSWORD_LENGTH'
ERROR_INVALID_USERNAME_CHARS = 'INVALID_USERNAME_CHARS'
ERROR_DUPLICATE_USERNAME = 'DUPLICATE_USERNAME'
ERROR_EMPTY_USERNAME = 'EMPTY_USERNAME'
ERROR_EMPTY_PASSWORD = 'EMPTY_PASSWORD'
ERROR_EMPTY_FIELDS = 'EMPTY_FIELDS'
SUCCESS_USERNAME_PASSWORD_LENGTH = 'GOOD_USERNAME_&_PASSWORD_LENGTH'
SUCCESS_FIELDS_FILLED = 'ALL_FIELDS_FILLED'
SUCCESS = 'SUCCESS'

# result codes for LoginController
# result codes for LoginHandler
ERROR_INVALID_USERNAME_OR_PASSWORD = 'INVALID_USERNAME_OR_PASSWORD'
NO_TOKEN = 'NO_TOKEN'
Binary file not shown.
Binary file modified server/src/__pycache__/ResultCodes.cpython-36.pyc
Binary file not shown.
Binary file modified server/src/__pycache__/index.cpython-36.pyc
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -9,38 +9,38 @@
resultCodes = ResultCodes()
DBA = DatabaseAccessor()

class LoginController():
class LoginHandler():

def handleUserLogin(self, username, password):

# check if inputs are null
fieldNullCheckResult = inputHandler.checkInputNull(username, password)
if(fieldNullCheckResult != resultCodes.SUCCESS_FIELDS_FILLED):
return fieldNullCheckResult
if fieldNullCheckResult != resultCodes.SUCCESS_FIELDS_FILLED:
return [resultCodes.NO_TOKEN, fieldNullCheckResult]

user = self.getUser(str(username.lower()), str(password))

# check if fields are empty strings
fieldEmptyCheckResult = inputHandler.handleEmptyFields(user)
if(fieldEmptyCheckResult != resultCodes.SUCCESS_FIELDS_FILLED):
return fieldEmptyCheckResult
if fieldEmptyCheckResult != resultCodes.SUCCESS_FIELDS_FILLED:
return [resultCodes.NO_TOKEN, fieldEmptyCheckResult]

# check if user exists
doesUsernameExist = inputHandler.checkForExistingUsername(user)
if(doesUsernameExist == False):
return resultCodes.ERROR_INVALID_USERNAME_OR_PASSWORD
if doesUsernameExist == False:
return [resultCodes.NO_TOKEN, resultCodes.ERROR_INVALID_USERNAME_OR_PASSWORD]

# check if input password matches user's password
isPasswordCorrect = inputHandler.verifyPassword(user)
if(isPasswordCorrect == False):
return resultCodes.ERROR_INVALID_USERNAME_OR_PASSWORD
if isPasswordCorrect == False:
return [resultCodes.NO_TOKEN, resultCodes.ERROR_INVALID_USERNAME_OR_PASSWORD]

userId = DBA.selectUserId(user)
user.updateUserId(userId)
user.generateAndUpdateSecurityToken()
securityToken = user.getSecurityToken()

return securityToken
return [securityToken, resultCodes.SUCCESS]

def getUser(self, username, password):
user = User(username, password)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,35 +10,35 @@
resultCodes = ResultCodes()
DBA = DatabaseAccessor()

class SignUpController():
class SignUpHandler():

def handleUserSignUp(self, username, password):

# check if inputs are null
fieldNullCheckResult = inputHandler.checkInputNull(username, password)
if(fieldNullCheckResult != resultCodes.SUCCESS_FIELDS_FILLED):
if fieldNullCheckResult != resultCodes.SUCCESS_FIELDS_FILLED:
return fieldNullCheckResult

user = self.getUser(str(username.lower()), str(password))

# check if inputs are empty strings
fieldEmptyCheckResult = inputHandler.handleEmptyFields(user)
if(fieldEmptyCheckResult != resultCodes.SUCCESS_FIELDS_FILLED):
if fieldEmptyCheckResult != resultCodes.SUCCESS_FIELDS_FILLED:
return fieldEmptyCheckResult

# check for proper string input lengths
inputLengthResult = inputHandler.handleInputLengthChecks(user)
if(inputLengthResult != resultCodes.SUCCESS_USERNAME_PASSWORD_LENGTH):
if inputLengthResult != resultCodes.SUCCESS_USERNAME_PASSWORD_LENGTH:
return inputLengthResult

# check for invalid characters in inputs
isUsernameCharsValid = inputHandler.checkForInvalidUsernameChars(user)
if(isUsernameCharsValid == False):
if isUsernameCharsValid == False:
return resultCodes.ERROR_INVALID_USERNAME_CHARS

# check if username already exists
doesUsernameExist = inputHandler.checkForExistingUsername(user)
if(doesUsernameExist):
if doesUsernameExist:
return resultCodes.ERROR_DUPLICATE_USERNAME

# insert user info into db
Expand Down
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
125 changes: 61 additions & 64 deletions server/src/index.py
Original file line number Diff line number Diff line change
@@ -1,50 +1,56 @@
from flask import Flask, render_template, request, redirect, url_for, jsonify
import sys
sys.path.append('/Users/justinkwan/Documents/WebApps/UserAuth/server/src/controllers')
from SignUpController import SignUpController
from LoginController import LoginController
from SignUpHandler import SignUpHandler
from LoginHandler import LoginHandler
from IndexReturnDecider import IndexReturnDecider

app = Flask(__name__)

'''
signup routes
'''

@app.route("/success")
def success():
return render_template('Success.html')
@app.route("/signUpSuccess")
def signUpSuccess():
return render_template('SignUpPages/SignUpSuccess.html')

@app.route("/error=existingUsername")
def existingUsername():
return render_template('ErrorExistingUsernameSignUp.html')
@app.route("/signUpError=existingUsername")
def signUpExistingUsername():
return render_template('SignUpPages/SignUpErrorExistingUsername.html')

@app.route("/error=invalidUsernameCharacters")
def invalidUsernameCharacters():
return render_template('ErrorInvalidUsernameCharacters.html')
@app.route("/signUpError=invalidUsernameCharacters")
def signUpInvalidUsernameCharacters():
return render_template('SignUpPages/SignUpErrorInvalidUsernameCharacters.html')

@app.route("/error=usernameOutOfRange")
def usernameOutOfRange():
return render_template('ErrorUsernameOutOfRange.html')
@app.route("/signUpError=usernameOutOfRange")
def signUpUsernameOutOfRange():
return render_template('SignUpPages/SignUpErrorUsernameOutOfRange.html')

@app.route("/error=passwordOutOfRange")
def passwordOutOfRange():
return render_template('ErrorPasswordOutOfRange.html')
@app.route("/signUpError=passwordOutOfRange")
def signUpPasswordOutOfRange():
return render_template('SignUpPages/SignUpErrorPasswordOutOfRange.html')

@app.route("/signUpError=emptyFields")
def signUpEmptyFields():
return render_template('SignUpPages/SignUpErrorEmptyFields.html')

@app.route("/signUp", methods=['GET'])
def signUp():
return render_template('SignUp.html')

@app.route("/error=emptySignUpFields")
def emptySignUpFields():
return render_template('ErrorEmptyFieldsSignUp.html')
return render_template('SignUpPages/SignUp.html')


@app.route("/signUpSubmit", methods=['POST'])
def signUpSubmit():
if(request.method == 'POST'):
signUpController = SignUpController()
processResult = signUpController.handleUserSignUp(request.form['username'], request.form['password'])
redirectPage = determineSignUpRedirectPage(processResult)
if request.method == 'POST':
signUpHandler = SignUpHandler()
IRD = IndexReturnDecider()

username = request.form['username']
password = request.form['password']

resultCode = signUpHandler.handleUserSignUp(username, password)
redirectPage = IRD.determineSignUpRedirectPage(resultCode)
return redirect(url_for(redirectPage))

'''
Expand All @@ -53,46 +59,37 @@ def signUpSubmit():

@app.route("/login", methods=['GET'])
def login():
return render_template('Login.html')
return render_template('LoginPages/Login.html')

@app.route("/error=emptyLoginFields")
def emptyLoginFields():
return render_template('ErrorEmptyFieldsLogin.html')
@app.route("/loginError=emptyFields")
def loginEmptyFields():
return render_template('LoginPages/LoginErrorEmptyFields.html')

@app.route("/error=invalidUsernameOrPassword")
def invalidUsernameOrPasswordLogin():
return render_template('ErrorInvalidUsernamePassword.html')
@app.route("/loginError=invalidUsernameOrPassword")
def loginInvalidUsernameOrPassword():
return render_template('LoginPages/LoginErrorInvalidUsernamePassword.html')

@app.route("/loginSubmit", methods=['POST'])
def loginSubmit():
if(request.method == 'POST'):

print("POST DATA!!!")

loginController = LoginController()
processResult = loginController.handleUserLogin(request.form['username'], request.form['password'])

if(processResult == 'EMPTY_USERNAME' or processResult == 'EMPTY_PASSWORD' or processResult == 'EMPTY_FIELDS'):
return redirect(url_for('emptyLoginFields'))
elif(processResult == 'INVALID_USERNAME_OR_PASSWORD'):
return redirect(url_for('invalidUsernameOrPasswordLogin'))

# return a json web security token
return jsonify({
'success' : 'true',
'token' : processResult
})

def determineSignUpRedirectPage(processResult):
if(processResult == 'SUCCESS'):
return 'success'
elif(processResult == 'DUPLICATE_USERNAME'):
return 'existingUsername'
elif(processResult == 'INVALID_USERNAME_CHARS'):
return 'invalidUsernameCharacters'
elif(processResult == 'INVALID_USERNAME_LENGTH'):
return 'usernameOutOfRange'
elif(processResult == 'INVALID_PASSWORD_LENGTH'):
return 'passwordOutOfRange'
else:
return 'emptySignUpFields'
if request.method == 'POST':
loginHandler = LoginHandler()
IRD = IndexReturnDecider()

username = request.form['username']
password = request.form['password']

# list of token and result code returned if successful
resultPackage = loginHandler.handleUserLogin(username, password)
isTokenReturned = IRD.checkIfTokenReturned(resultPackage[1])

if isTokenReturned:
securityToken = resultPackage[0]
# return a json web security token
return jsonify({
'success': 'true',
'token': securityToken
})
else:
# determine redirect page using result code
redirectPage = IRD.determineLoginRedirectPage(resultPackage[1])
return redirect(url_for(redirectPage))
File renamed without changes.
File renamed without changes.
File renamed without changes.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file modified server/test/__pycache__/test_User.cpython-36-PYTEST.pyc
Binary file not shown.
13 changes: 13 additions & 0 deletions server/test/test_DatabaseAccessor.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,19 @@ def test_insertUserInfo():
del user
DBA.clearDatabase()

user = getUser('fake-user', 'password123', '')
user.generateAndUpdateUserId()
DBA.insertUserInfo(user)
selectedUsername = DBA.selectUsername(user)
selectedHashedPassword = DBA.selectHashedPassword(user)
selectedUserId = DBA.selectUserId(user)
assert selectedUsername == 'fake-user'
assert selectedHashedPassword != None
assert selectedUserId == user.getUserId()

del user
DBA.clearDatabase()

# test username selecting function
def test_selectUsername():
DBA.clearDatabase()
Expand Down
31 changes: 31 additions & 0 deletions server/test/test_IndexReturnDecider.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import pytest
import sys
sys.path.append('/Users/justinkwan/Documents/WebApps/UserAuth/server/src')
from IndexReturnDecider import IndexReturnDecider
from ResultCodes import ResultCodes

IRD = IndexReturnDecider()
resultCodes = ResultCodes()

def test_determineSignUpRedirectPage():
assert IRD.determineSignUpRedirectPage(resultCodes.SUCCESS) == 'signUpSuccess'
assert IRD.determineSignUpRedirectPage(resultCodes.ERROR_DUPLICATE_USERNAME) == 'signUpExistingUsername'
assert IRD.determineSignUpRedirectPage(resultCodes.ERROR_INVALID_USERNAME_CHARS) == 'signUpInvalidUsernameCharacters'
assert IRD.determineSignUpRedirectPage(resultCodes.ERROR_USERNAME_LENGTH_INVALID) == 'signUpUsernameOutOfRange'
assert IRD.determineSignUpRedirectPage(resultCodes.ERROR_PASSWORD_LENGTH_INVALID) == 'signUpPasswordOutOfRange'
assert IRD.determineSignUpRedirectPage(resultCodes.ERROR_EMPTY_FIELDS) == 'signUpEmptyFields'
assert IRD.determineSignUpRedirectPage(resultCodes.ERROR_EMPTY_PASSWORD) == 'signUpEmptyFields'
assert IRD.determineSignUpRedirectPage(resultCodes.ERROR_EMPTY_USERNAME) == 'signUpEmptyFields'

def test_determineLoginRedirectPage():
assert IRD.determineLoginRedirectPage(resultCodes.ERROR_INVALID_USERNAME_OR_PASSWORD) == 'loginInvalidUsernameOrPassword'
assert IRD.determineLoginRedirectPage(resultCodes.ERROR_EMPTY_FIELDS) == 'loginEmptyFields'
assert IRD.determineLoginRedirectPage(resultCodes.ERROR_EMPTY_USERNAME) == 'loginEmptyFields'
assert IRD.determineLoginRedirectPage(resultCodes.ERROR_EMPTY_PASSWORD) == 'loginEmptyFields'

def test_checkIfTokenReturned():
assert IRD.checkIfTokenReturned(resultCodes.SUCCESS) == True
assert IRD.checkIfTokenReturned(resultCodes.ERROR_INVALID_USERNAME_OR_PASSWORD) == False
assert IRD.checkIfTokenReturned(resultCodes.ERROR_EMPTY_FIELDS) == False
assert IRD.checkIfTokenReturned(resultCodes.ERROR_EMPTY_USERNAME) == False
assert IRD.checkIfTokenReturned(resultCodes.ERROR_EMPTY_PASSWORD) == False
Loading

0 comments on commit c11e93a

Please sign in to comment.