Skip to content

Commit

Permalink
Refactor tests to use global endpoint constants
Browse files Browse the repository at this point in the history
  • Loading branch information
rdmark committed Dec 1, 2023
1 parent 7969394 commit 6f65f24
Show file tree
Hide file tree
Showing 8 changed files with 139 additions and 112 deletions.
6 changes: 3 additions & 3 deletions python/web/src/templates/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@
<td class="name" align="center">{{ device.device_name }}</td>
<td class="parameters">
{% if "No Media" in device.status %}
<form action="/scsi/attach_device" method="post">
<form action="/scsi/attach" method="post">
<input name="scsi_id" type="hidden" value="{{ device.id }}">
<input name="unit" type="hidden" value="{{ device.unit }}">
<input name="type" type="hidden" value="{{ device.device_type }}">
Expand Down Expand Up @@ -227,7 +227,7 @@
<div>{{ type }}</div>
</td>
<td>
<form action="/scsi/attach_device" method="post" class="device-attach">
<form action="/scsi/attach" method="post" class="device-attach">
<input name="type" type="hidden" value="{{ type }}">
{% for key, value in device_types[type]["params"] | dictsort %}
<label for="param_{{ type }}_{{ key }}">{{ key }}:</label>
Expand Down Expand Up @@ -444,7 +444,7 @@
<input type="submit" value="{{ _("Extract") }}" title="{{ _("Extract") }}" onclick="processNotify('{{ _("Extracting all files...") }}')">
</form>
{% else %}
<form action="/scsi/attach_device" method="post" class="file-attach">
<form action="/scsi/attach" method="post" class="file-attach">
<input name="file_name" type="hidden" value="{{ file['name'] }}">
<label for="image_list_scsi_id_{{ file["name"] }}">{{ _("ID") }}</label>
<select name="scsi_id" id="image_list_scsi_id_{{ file["name"] }}">
Expand Down
2 changes: 1 addition & 1 deletion python/web/src/web.py
Original file line number Diff line number Diff line change
Expand Up @@ -643,7 +643,7 @@ def log_level():
return response(error=True, message=process["msg"])


@APP.route("/scsi/attach_device", methods=["POST"])
@APP.route("/scsi/attach", methods=["POST"])
@login_required
def attach_device():
"""
Expand Down
43 changes: 39 additions & 4 deletions python/web/tests/api/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,41 @@
STATUS_SUCCESS = "success"
STATUS_ERROR = "error"

ENV_ENDPOINT = "/env"
HEALTHCHECK_ENDPOINT = "/healthcheck"
PWA_FAVICON_ENDPOINT = "/pwa/favicon.ico"
LOGIN_ENDPOINT = "/login"
LOGOUT_ENDPOINT = "/logout"
ATTACH_ENDPOINT = "/scsi/attach"
DETACH_ENDPOINT = "/scsi/detach"
DETACH_ALL_ENDPOINT = "/scsi/detach_all"
EJECT_ENDPOINT = "/scsi/eject"
RESERVE_ENDPOINT = "/scsi/reserve"
RELEASE_ENDPOINT = "/scsi/release"
INFO_ENDPOINT = "/scsi/info"
CREATE_ENDPOINT = "/files/create"
RENAME_ENDPOINT = "/files/rename"
COPY_ENDPOINT = "/files/copy"
DELETE_ENDPOINT = "/files/delete"
DOWNLOAD_URL_ENDPOINT = "/files/download_url"
DOWNLOAD_IMAGE_ENDPOINT = "/files/download_image"
DOWNLOAD_CONFIG_ENDPOINT = "/files/download_config"
EXTRACT_IMAGE_ENDPOINT = "/files/extract_image"
UPLOAD_ENDPOINT = "/files/upload"
CREATE_ISO_ENDPOINT = "/files/create_iso"
DISKINFO_ENDPOINT = "/files/diskinfo"
DRIVE_LIST_ENDPOINT = "/drive/list"
DRIVE_CREATE_ENDPOINT = "/drive/create"
DRIVE_CDROM_ENDPOINT = "/drive/cdrom"
MANPAGE_ENDPOINT = "/sys/manpage?app=piscsi"
LANGUAGE_ENDPOINT = "/language"
LOG_LEVEL_ENDPOINT = "/logs/level"
LOG_SHOW_ENDPOINT = "/logs/show"
CONFIG_SAVE_ENDPOINT = "/config/save"
CONFIG_ACTION_ENDPOINT = "/config/action"
THEME_ENDPOINT = "/theme"
SYS_RENAME_ENDPOINT = "/sys/rename"


@pytest.fixture(scope="function")
def create_test_image(request, http_client):
Expand All @@ -18,7 +53,7 @@ def create(image_type="hds", size=1, auto_delete=True):
file_name = f"{file_prefix}.{image_type}"

response = http_client.post(
"/files/create",
CREATE_ENDPOINT,
data={
"file_name": file_prefix,
"type": image_type,
Expand All @@ -42,7 +77,7 @@ def create(image_type="hds", size=1, auto_delete=True):

def delete():
for image in images:
response = http_client.post("/files/delete", data={"file_name": image["file_name"]})
response = http_client.post(DELETE_ENDPOINT, data={"file_name": image["file_name"]})
if response.status_code != 200 or response.json()["status"] != STATUS_SUCCESS:
warnings.warn(
f"Failed to auto-delete file created with create_test_image fixture: {image}"
Expand Down Expand Up @@ -71,7 +106,7 @@ def files():
@pytest.fixture(scope="function")
def delete_file(http_client):
def delete(file_name):
response = http_client.post("/files/delete", data={"file_name": file_name})
response = http_client.post(DELETE_ENDPOINT, data={"file_name": file_name})
if response.status_code != 200 or response.json()["status"] != STATUS_SUCCESS:
warnings.warn(f"Failed to delete file via delete_file fixture: {file_name}")

Expand All @@ -81,7 +116,7 @@ def delete(file_name):
@pytest.fixture(scope="function")
def detach_devices(http_client):
def detach():
response = http_client.post("/scsi/detach_all")
response = http_client.post(DETACH_ALL_ENDPOINT)
if response.json()["status"] == STATUS_SUCCESS:
return True
raise Exception("Failed to detach SCSI devices")
Expand Down
11 changes: 4 additions & 7 deletions python/web/tests/api/test_auth.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
from conftest import STATUS_SUCCESS, STATUS_ERROR
from conftest import STATUS_SUCCESS, STATUS_ERROR, LOGIN_ENDPOINT, LOGOUT_ENDPOINT


# route("/login", methods=["POST"])
def test_login_with_valid_credentials(pytestconfig, http_client_unauthenticated):
# Note: This test depends on the piscsi group existing and 'username' a member the group
response = http_client_unauthenticated.post(
"/login",
LOGIN_ENDPOINT,
data={
"username": pytestconfig.getoption("piscsi_username"),
"password": pytestconfig.getoption("piscsi_password"),
Expand All @@ -19,10 +18,9 @@ def test_login_with_valid_credentials(pytestconfig, http_client_unauthenticated)
assert "env" in response_data["data"]


# route("/login", methods=["POST"])
def test_login_with_invalid_credentials(http_client_unauthenticated):
response = http_client_unauthenticated.post(
"/login",
LOGIN_ENDPOINT,
data={
"username": "__INVALID_USER__",
"password": "__INVALID_PASS__",
Expand All @@ -38,7 +36,6 @@ def test_login_with_invalid_credentials(http_client_unauthenticated):
)


# route("/logout")
def test_logout(http_client):
response = http_client.get("/logout")
response = http_client.get(LOGOUT_ENDPOINT)
assert response.status_code == 200
39 changes: 19 additions & 20 deletions python/web/tests/api/test_devices.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,21 @@
from conftest import (
SCSI_ID,
STATUS_SUCCESS,
ATTACH_ENDPOINT,
DETACH_ENDPOINT,
DETACH_ALL_ENDPOINT,
EJECT_ENDPOINT,
RESERVE_ENDPOINT,
RELEASE_ENDPOINT,
INFO_ENDPOINT,
)


# route("/scsi/attach_device", methods=["POST"])
def test_attach_device_with_image(http_client, create_test_image, detach_devices):
test_image = create_test_image()

response = http_client.post(
"/scsi/attach_device",
ATTACH_ENDPOINT,
data={
"file_name": test_image,
"scsi_id": SCSI_ID,
Expand All @@ -31,7 +37,6 @@ def test_attach_device_with_image(http_client, create_test_image, detach_devices
detach_devices()


# route("/scsi/attach_device", methods=["POST"])
@pytest.mark.parametrize(
"device_name,device_config",
[
Expand Down Expand Up @@ -87,7 +92,7 @@ def test_attach_device(env, http_client, detach_devices, device_name, device_con
device_config["unit"] = 0

response = http_client.post(
"/scsi/attach_device",
ATTACH_ENDPOINT,
data=device_config,
)

Expand All @@ -103,12 +108,11 @@ def test_attach_device(env, http_client, detach_devices, device_name, device_con
detach_devices()


# route("/scsi/detach", methods=["POST"])
def test_detach_device(http_client, create_test_image):
test_image = create_test_image()

http_client.post(
"/scsi/attach_device",
ATTACH_ENDPOINT,
data={
"file_name": test_image,
"scsi_id": SCSI_ID,
Expand All @@ -118,7 +122,7 @@ def test_detach_device(http_client, create_test_image):
)

response = http_client.post(
"/scsi/detach",
DETACH_ENDPOINT,
data={
"scsi_id": SCSI_ID,
"unit": 0,
Expand All @@ -132,7 +136,6 @@ def test_detach_device(http_client, create_test_image):
assert response_data["messages"][0]["message"] == f"Detached SCSI ID {SCSI_ID} LUN 0"


# route("/scsi/detach_all", methods=["POST"])
def test_detach_all_devices(http_client, create_test_image, list_attached_images):
test_images = []
scsi_ids = [4, 5, 6]
Expand All @@ -142,7 +145,7 @@ def test_detach_all_devices(http_client, create_test_image, list_attached_images
test_images.append(test_image)

http_client.post(
"/scsi/attach_device",
ATTACH_ENDPOINT,
data={
"file_name": test_image,
"scsi_id": scsi_id,
Expand All @@ -153,20 +156,19 @@ def test_detach_all_devices(http_client, create_test_image, list_attached_images

assert list_attached_images() == test_images

response = http_client.post("/scsi/detach_all")
response = http_client.post(DETACH_ALL_ENDPOINT)
response_data = response.json()

assert response.status_code == 200
assert response_data["status"] == STATUS_SUCCESS
assert list_attached_images() == []


# route("/scsi/eject", methods=["POST"])
def test_eject_device(http_client, create_test_image, detach_devices):
test_image = create_test_image()

http_client.post(
"/scsi/attach_device",
ATTACH_ENDPOINT,
data={
"file_name": test_image,
"scsi_id": SCSI_ID,
Expand All @@ -176,7 +178,7 @@ def test_eject_device(http_client, create_test_image, detach_devices):
)

response = http_client.post(
"/scsi/eject",
EJECT_ENDPOINT,
data={
"scsi_id": SCSI_ID,
"unit": 0,
Expand All @@ -193,12 +195,11 @@ def test_eject_device(http_client, create_test_image, detach_devices):
detach_devices()


# route("/scsi/info", methods=["POST"])
def test_show_device_info(http_client, create_test_image, detach_devices):
test_image = create_test_image()

http_client.post(
"/scsi/attach_device",
ATTACH_ENDPOINT,
data={
"file_name": test_image,
"scsi_id": SCSI_ID,
Expand All @@ -208,7 +209,7 @@ def test_show_device_info(http_client, create_test_image, detach_devices):
)

response = http_client.post(
"/scsi/info",
INFO_ENDPOINT,
)

response_data = response.json()
Expand All @@ -222,13 +223,11 @@ def test_show_device_info(http_client, create_test_image, detach_devices):
detach_devices()


# route("/scsi/reserve", methods=["POST"])
# route("/scsi/release", methods=["POST"])
def test_reserve_and_release_device(http_client):
scsi_id = 0

response = http_client.post(
"/scsi/reserve",
RESERVE_ENDPOINT,
data={
"scsi_id": scsi_id,
"memo": "TEST",
Expand All @@ -242,7 +241,7 @@ def test_reserve_and_release_device(http_client):
assert response_data["messages"][0]["message"] == f"Reserved SCSI ID {scsi_id}"

response = http_client.post(
"/scsi/release",
RELEASE_ENDPOINT,
data={
"scsi_id": scsi_id,
},
Expand Down
Loading

0 comments on commit 6f65f24

Please sign in to comment.