Skip to content

Commit

Permalink
Fixed failing test cases and added new test case to test insertion of…
Browse files Browse the repository at this point in the history
… data into database

Co-authored-by: Harshini <s10204001@connect.np.edu.sg>
  • Loading branch information
LWJ-Nicholas and harshini53 committed Feb 4, 2024
1 parent 367b203 commit 8d514bf
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 12 deletions.
31 changes: 21 additions & 10 deletions test_pytest/test_database.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
### testing database via Pytests

from website.models import User
from website import db
from main import app
Expand Down Expand Up @@ -33,37 +32,37 @@ def test_database_duplicate_usernames():
def test_non_existing_username_in_db():
with app.app_context():
non_existing_username = 'thisisanonexistentuser'
users_with_username = User.query.filter_by(username=non_existing_username)
assert len(users_with_username) == 0
users_with_username = User.query.filter_by(username=non_existing_username).first()
assert users_with_username == None

def test_incorrect_password_in_db():
with app.app_context():
username='Testing'
incorrect_password = 'thisisanincorrectpassword'
users_with_password = User.query.filter_by(username=username)
assert check_password_hash(users_with_password.password, incorrect_password) == False
user = User.query.filter_by(username=username).first()
assert check_password_hash(user.password, incorrect_password) == False


# Check for values existing in database
def test_existing_username():
with app.app_context():
existing_username = 'Testing'
existing_user = User.query.filter_by(username=existing_username)
existing_user = User.query.filter_by(username=existing_username).first()
assert existing_user

def test_correct_password():
with app.app_context():
username='Testing'
correct_password = 'testing'
user = User.query.filter_by(username=username)
user = User.query.filter_by(username=username).first()
password_matched = check_password_hash(user.password, correct_password)
assert password_matched

# Retrieve values based on column value in database
def test_retrieve_password_by_username():
with app.app_context():
existing_username = 'Testing'
user = User.query.filter_by(username=existing_username)
user = User.query.filter_by(username=existing_username).first()
retrieved_password = user.password
assert retrieved_password

Expand All @@ -72,6 +71,18 @@ def test_retrieve_username_and_password_by_id():
existing_user_id = 3
user = User.query.get(existing_user_id)
retrieved_username = user.username
retrieved_password = user.password
assert retrieved_username and retrieved_password
assert retrieved_username



# Inserting new values into database
def test_insert_new_user():
with app.app_context():
new_username = 'testing5'
new_password = 'testing5'
new_user_role = 'user'
new_user = User(username=new_username, password=new_password, role=new_user_role)
db.session.add(new_user)
db.session.commit()
inserted_user = User.query.filter_by(username=new_username).first()
assert (inserted_user.username == new_username) and (inserted_user.password == new_password)
4 changes: 2 additions & 2 deletions test_robotFramework/login_account.robot
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ Library SeleniumLibrary
${website_url} http://127.0.0.1:5000
${login_admin_username} Admin
${login_admin_password} password
${login_user_username} Testing9
${login_user_password} Testing9
${login_user_username} test8
${login_user_password} test8
${login_btn} xpath=//button[contains(text(),'Login')]
${login_anchor} xpath=//a[contains(text(),'Login')]
${logout_btn} xpath=//a[contains(text(),'Logout')]
Expand Down

0 comments on commit 8d514bf

Please sign in to comment.