294 lines
9.2 KiB
Python
294 lines
9.2 KiB
Python
|
|
import pytest
|
|
import autotest_ui
|
|
import flask
|
|
|
|
|
|
@pytest.fixture
|
|
def client():
|
|
autotest_ui.app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///:memory:'
|
|
autotest_ui.app.config['TESTING'] = True
|
|
|
|
|
|
with autotest_ui.app.test_client() as client:
|
|
with autotest_ui.app.app_context():
|
|
autotest_ui.db.create_all()
|
|
yield client
|
|
autotest_ui.db.drop_all()
|
|
|
|
|
|
|
|
# database helper functions
|
|
|
|
def setup_create_user_admin(client):
|
|
with autotest_ui.app.app_context():
|
|
admin_user = autotest_ui.User(
|
|
username="admin",
|
|
email="admin@localhost",
|
|
password="admin",
|
|
user_role_value=autotest_ui.UserRoles.ADMIN
|
|
)
|
|
autotest_ui.db.session.add(admin_user)
|
|
autotest_ui.db.session.commit()
|
|
|
|
|
|
|
|
|
|
def init_database(client):
|
|
setup_create_user_admin(client)
|
|
|
|
|
|
# login helper function
|
|
|
|
def login_as_admin(client):
|
|
return client.post("/signin", data={
|
|
"username": "admin",
|
|
"password": "admin"
|
|
}, follow_redirects=True)
|
|
|
|
|
|
|
|
|
|
|
|
# test functions
|
|
|
|
|
|
class Test_home:
|
|
|
|
def test_page_loads(self, client):
|
|
init_database(client)
|
|
|
|
response_logged_out = client.get("/")
|
|
assert response_logged_out.status_code == 200
|
|
|
|
login_as_admin(client)
|
|
response_logged_in = client.get("/")
|
|
assert response_logged_in.status_code == 200
|
|
|
|
|
|
class Test_signin:
|
|
|
|
def test_signin(self, client):
|
|
init_database(client)
|
|
|
|
# ensure the user page doesn't load when logged out
|
|
response = client.get("/account")
|
|
assert response.status_code == 302
|
|
assert "/signin" in response.headers.get("Location")
|
|
|
|
# ensure the page loads
|
|
response_logged_out = client.get("/signin")
|
|
assert response_logged_out.status_code == 200
|
|
|
|
# actually login
|
|
login_as_admin(client)
|
|
|
|
# check the account page loads
|
|
response = client.get("/account")
|
|
assert response.status_code == 200
|
|
|
|
# signing out redirects to signin page
|
|
response = client.get("/signout")
|
|
assert response.status_code == 302
|
|
assert "/signin" in response.headers.get("Location")
|
|
|
|
# ensure the uses is actually logged out
|
|
response = client.get("/account")
|
|
assert response.status_code == 302
|
|
assert "/signin" in response.headers.get("Location")
|
|
|
|
|
|
with autotest_ui.app.app_context():
|
|
with client.session_transaction() as session:
|
|
flashed_messages = flask.get_flashed_messages(with_categories=True)
|
|
error_messages = [msg for cat, msg in flashed_messages if cat == "error"]
|
|
assert len(error_messages) == 0, f"Unexpected errors found: {error_messages}"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class Test_product_setup:
|
|
|
|
def test_page_loads(self, client):
|
|
init_database(client)
|
|
login_as_admin(client)
|
|
response = client.get("/product-setup")
|
|
assert response.status_code == 200
|
|
|
|
with autotest_ui.app.app_context():
|
|
with client.session_transaction() as session:
|
|
flashed_messages = flask.get_flashed_messages(with_categories=True)
|
|
error_messages = [msg for cat, msg in flashed_messages if cat == "error"]
|
|
assert len(error_messages) == 0, f"Unexpected errors found: {error_messages}"
|
|
|
|
|
|
def test_add_family(self, client):
|
|
init_database(client)
|
|
login_as_admin(client)
|
|
|
|
response = client.post("/product-setup/add-family", data={
|
|
"family_name": "DPX-S"
|
|
})
|
|
assert response.status_code == 302
|
|
|
|
response = client.post("/product-setup/add-family", data={
|
|
"family_name": "DPX-M"
|
|
})
|
|
assert response.status_code == 302
|
|
|
|
with autotest_ui.app.app_context():
|
|
with client.session_transaction() as session:
|
|
flashed_messages = flask.get_flashed_messages(with_categories=True)
|
|
error_messages = [msg for cat, msg in flashed_messages if cat == "error"]
|
|
assert len(error_messages) == 0, f"Unexpected errors found: {error_messages}"
|
|
|
|
response = client.post("/product-setup/add-family", data={
|
|
"family_name": ""
|
|
})
|
|
assert response.status_code == 302
|
|
|
|
response = client.post("/product-setup/add-family")
|
|
assert response.status_code == 302
|
|
|
|
with autotest_ui.app.app_context():
|
|
with client.session_transaction() as session:
|
|
flashed_messages = flask.get_flashed_messages(with_categories=True)
|
|
error_messages = [msg for cat, msg in flashed_messages if cat == "error"]
|
|
assert len(error_messages) == 2, f"Expected 2 errors. Errors found: {error_messages}"
|
|
|
|
with autotest_ui.app.app_context():
|
|
inserted_family = autotest_ui.BoardFamily.query.filter_by(
|
|
board_family_prefix="DPX-S"
|
|
).first()
|
|
|
|
assert inserted_family is not None, "The record was not found in the database"
|
|
assert inserted_family.board_family_prefix == "DPX-S"
|
|
|
|
|
|
def test_add_board(self, client):
|
|
|
|
init_database(client)
|
|
login_as_admin(client)
|
|
|
|
response = client.post("/product-setup/add-family", data={
|
|
"family_name": "DPX-S"
|
|
})
|
|
assert response.status_code == 302
|
|
|
|
with autotest_ui.app.app_context():
|
|
inserted_boards = autotest_ui.BoardFamily.query.filter_by(
|
|
board_family_prefix="DPX-S"
|
|
).first()
|
|
|
|
|
|
response = client.post("/product-setup/add-board", data={
|
|
"board_name": "450",
|
|
"board_family_id": inserted_boards.board_family_id
|
|
})
|
|
assert response.status_code == 302
|
|
|
|
with autotest_ui.app.app_context():
|
|
with client.session_transaction() as session:
|
|
flashed_messages = flask.get_flashed_messages(with_categories=True)
|
|
error_messages = [msg for cat, msg in flashed_messages if cat == "error"]
|
|
assert len(error_messages) == 0, f"Unexpected errors found: {error_messages}"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class Test_operating_system:
|
|
|
|
def test_page_loads(self, client):
|
|
init_database(client)
|
|
login_as_admin(client)
|
|
|
|
response = client.get("/operating-system-setup")
|
|
assert response.status_code == 200
|
|
|
|
|
|
def test_add_os(self, client):
|
|
init_database(client)
|
|
login_as_admin(client)
|
|
|
|
client.post("/operating-system-setup/add-os", data={ "os_name": "Windows 10" })
|
|
client.post("/operating-system-setup/add-os", data={ "os_name": "Windows 11" })
|
|
client.post("/operating-system-setup/add-os", data={ "os_name": "Ubuntu 22" })
|
|
client.post("/operating-system-setup/add-os", data={ "os_name": "Ubuntu 24" })
|
|
client.post("/operating-system-setup/add-os", data={ "os_name": "Ubuntu 26" })
|
|
client.post("/operating-system-setup/add-os", data={ "os_name": "Fedora 43" })
|
|
client.post("/operating-system-setup/add-os", data={ "os_name": "Fedora 44" })
|
|
|
|
|
|
with autotest_ui.app.app_context():
|
|
with client.session_transaction() as session:
|
|
flashed_messages = flask.get_flashed_messages(with_categories=True)
|
|
error_messages = [msg for cat, msg in flashed_messages if cat == "error"]
|
|
assert len(error_messages) == 0, f"Unexpected errors found: {error_messages}"
|
|
|
|
# test invalid records
|
|
client.post("/operating-system-setup/add-os", data={ "os_name": "" })
|
|
client.post("/operating-system-setup/add-os")
|
|
client.post("/operating-system-setup/add-os", data={ "os_name": "Test-ing!£" })
|
|
client.post("/operating-system-setup/add-os", data={ "os_name": "Windows 11" })
|
|
|
|
|
|
with autotest_ui.app.app_context():
|
|
with client.session_transaction() as session:
|
|
flashed_messages = flask.get_flashed_messages(with_categories=True)
|
|
error_messages = [msg for cat, msg in flashed_messages if cat == "error"]
|
|
assert len(error_messages) == 4, f"Expected 4 errors. Errors found: {error_messages}"
|
|
|
|
|
|
with autotest_ui.app.app_context():
|
|
inserted_os = autotest_ui.OperatingSystem.query.all()
|
|
assert len(inserted_os) == 7
|
|
|
|
|
|
|
|
class Test_projects:
|
|
|
|
def test_project(self, client):
|
|
|
|
init_database(client)
|
|
login_as_admin(client)
|
|
|
|
response = client.get("/projects")
|
|
assert response.status_code == 200
|
|
|
|
|
|
client.post("/projects/add-project", data={
|
|
"project_name": "DirectPCI"
|
|
})
|
|
|
|
with autotest_ui.app.app_context():
|
|
inserted_project = autotest_ui.Project.query.all()
|
|
assert len(inserted_project) == 1
|
|
|
|
|
|
|
|
client.post("/projects/add-version", data={
|
|
"project_id": inserted_project[0].project_id,
|
|
"major": 1,
|
|
"minor": 2,
|
|
"micro": 0
|
|
})
|
|
|
|
client.post("/projects/add-version", data={
|
|
"project_id": inserted_project[0].project_id,
|
|
"major": 1,
|
|
"minor": 3,
|
|
"micro": 0
|
|
})
|
|
|
|
|
|
with autotest_ui.app.app_context():
|
|
inserted_project_version = autotest_ui.ProjectVersion.query.all()
|
|
assert len(inserted_project_version) == 2
|
|
|
|
|