selfprivacy-rest-api/tests/test_graphql/test_api_storage.py

300 lines
7.6 KiB
Python
Raw Normal View History

2022-08-26 03:07:55 +00:00
import pytest
2022-08-25 15:37:32 +00:00
2022-08-26 03:07:55 +00:00
class BlockDevicesMockReturnNone:
2022-08-25 15:37:32 +00:00
"""Mock BlockDevices"""
def __init__(self, args, **kwargs):
self.args = args
self.kwargs = kwargs
2022-08-26 03:07:55 +00:00
def get_block_device(self, name: str):
2022-08-25 15:37:32 +00:00
return None
2022-08-26 03:07:55 +00:00
def resize(self):
pass
returncode = 0
class BlockDevicesMock:
"""Mock BlockDevices"""
def __init__(self, args, **kwargs):
self.args = args
self.kwargs = kwargs
def get_block_device(self, name: str):
return 0
2022-08-27 15:10:36 +00:00
def mount(self):
return True
def unmount(self):
return True
def resize(self):
pass
returncode = 0
class BlockDevicesMockReturnFalse:
"""Mock BlockDevices"""
def __init__(self, args, **kwargs):
self.args = args
self.kwargs = kwargs
def get_block_device(self, name: str):
return 0
def mount(self):
return False
def unmount(self):
return False
2022-08-26 03:07:55 +00:00
def resize(self):
pass
2022-08-25 15:37:32 +00:00
returncode = 0
2022-08-26 03:07:55 +00:00
@pytest.fixture
def mock_block_device_return_none(mocker):
mock = mocker.patch(
"selfprivacy_api.graphql.mutations.storage_mutation.BlockDevices", autospec=True
)
return mock
@pytest.fixture
2022-08-27 15:10:36 +00:00
def mock_block_device_return_true(mocker):
mock = mocker.patch(
"selfprivacy_api.graphql.mutations.storage_mutation.BlockDevices", autospec=True
)
return mock
@pytest.fixture
def mock_block_device_return_false(mocker):
2022-08-26 03:07:55 +00:00
mock = mocker.patch(
"selfprivacy_api.graphql.mutations.storage_mutation.BlockDevices", autospec=True
)
2022-08-25 15:37:32 +00:00
return mock
API_RESIZE_VOLUME_MUTATION = """
2022-08-26 03:07:55 +00:00
mutation resizeVolume($name: String!) {
2022-08-25 15:37:32 +00:00
resizeVolume(name: $name) {
success
message
code
}
}
"""
2022-08-26 03:07:55 +00:00
2022-08-27 15:10:36 +00:00
def test_graphql_resize_volumea_unathorized_client(client, mock_block_device_return_true):
2022-08-26 03:07:55 +00:00
response = client.post(
"/graphql",
json={
"query": API_RESIZE_VOLUME_MUTATION,
"variables": {"name": "sdx"},
},
)
assert response.status_code == 200
assert response.json().get("data") is None
def test_graphql_resize_volume_nonexistent_block_device(
2022-08-27 15:10:36 +00:00
authorized_client, mock_block_device_return_none
2022-08-26 03:07:55 +00:00
):
2022-08-25 15:37:32 +00:00
response = authorized_client.post(
"/graphql",
json={
"query": API_RESIZE_VOLUME_MUTATION,
2022-08-26 03:07:55 +00:00
"variables": {"name": "sdx"},
2022-08-25 15:37:32 +00:00
},
)
assert response.status_code == 200
assert response.json().get("data") is not None
2022-08-25 15:41:31 +00:00
assert response.json()["data"]["resizeVolume"]["code"] == 404
assert response.json()["data"]["resizeVolume"]["message"] is not None
assert response.json()["data"]["resizeVolume"]["success"] is False
2022-08-26 03:07:55 +00:00
2022-08-27 15:10:36 +00:00
def test_graphql_resize_volume(authorized_client, mock_block_device_return_true):
2022-08-26 03:07:55 +00:00
response = authorized_client.post(
"/graphql",
json={
"query": API_RESIZE_VOLUME_MUTATION,
"variables": {"name": "sdx"},
},
)
assert response.status_code == 200
assert response.json().get("data") is not None
assert response.json()["data"]["resizeVolume"]["code"] == 200
assert response.json()["data"]["resizeVolume"]["message"] is not None
assert response.json()["data"]["resizeVolume"]["success"] is True
2022-08-27 15:10:36 +00:00
API_MOUNT_VOLUME_MUTATION = """
mutation mountVolume($name: String!) {
mountVolume(name: $name) {
success
message
code
}
}
"""
def test_graphql_mount_volume_unathorized_client(client, mock_block_device_return_true):
response = client.post(
"/graphql",
json={
"query": API_MOUNT_VOLUME_MUTATION,
"variables": {"name": "sdx"},
},
)
assert response.status_code == 200
assert response.json().get("data") is None
def test_graphql_mount_already_mounted_volume(
authorized_client, mock_block_device_return_false
):
response = authorized_client.post(
"/graphql",
json={
"query": API_MOUNT_VOLUME_MUTATION,
"variables": {"name": "sdx"},
},
)
assert response.status_code == 200
assert response.json().get("data") is not None
assert response.json()["data"]["mountVolume"]["code"] == 409
assert response.json()["data"]["mountVolume"]["message"] is not None
assert response.json()["data"]["mountVolume"]["success"] is False
def test_graphql_mount_not_found_volume(
authorized_client, mock_block_device_return_none
):
response = authorized_client.post(
"/graphql",
json={
"query": API_MOUNT_VOLUME_MUTATION,
"variables": {"name": "sdx"},
},
)
assert response.status_code == 200
assert response.json().get("data") is not None
assert response.json()["data"]["mountVolume"]["code"] == 409
assert response.json()["data"]["mountVolume"]["message"] is not None
assert response.json()["data"]["mountVolume"]["success"] is False
def test_graphql_mount_volume(
authorized_client, mock_block_device_return_true
):
response = authorized_client.post(
"/graphql",
json={
"query": API_MOUNT_VOLUME_MUTATION,
"variables": {"name": "sdx"},
},
)
assert response.status_code == 200
assert response.json().get("data") is not None
assert response.json()["data"]["mountVolume"]["code"] == 200
assert response.json()["data"]["mountVolume"]["message"] is not None
assert response.json()["data"]["mountVolume"]["success"] is True
API_UNMOUNT_VOLUME_MUTATION = """
mutation unmountVolume($name: String!) {
unmountVolume(name: $name) {
success
message
code
}
}
"""
def test_graphql_unmount_volume_unathorized_client(client, mock_block_device_return_true):
response = client.post(
"/graphql",
json={
"query": API_UNMOUNT_VOLUME_MUTATION,
"variables": {"name": "sdx"},
},
)
assert response.status_code == 200
assert response.json().get("data") is None
def test_graphql_unmount_not_fount_volume(
authorized_client, mock_block_device_return_none
):
response = authorized_client.post(
"/graphql",
json={
"query": API_UNMOUNT_VOLUME_MUTATION,
"variables": {"name": "sdx"},
},
)
assert response.status_code == 200
assert response.json().get("data") is not None
assert response.json()["data"]["unmountVolume"]["code"] == 404
assert response.json()["data"]["unmountVolume"]["message"] is not None
assert response.json()["data"]["unmountVolume"]["success"] is False
def test_graphql_unmount_volume_false(
authorized_client, mock_block_device_return_false
):
response = authorized_client.post(
"/graphql",
json={
"query": API_UNMOUNT_VOLUME_MUTATION,
"variables": {"name": "sdx"},
},
)
assert response.status_code == 200
assert response.json().get("data") is not None
assert response.json()["data"]["unmountVolume"]["code"] == 409
assert response.json()["data"]["unmountVolume"]["message"] is not None
assert response.json()["data"]["unmountVolume"]["success"] is False
def test_graphql_unmount_volume(
authorized_client, mock_block_device_return_true
):
response = authorized_client.post(
"/graphql",
json={
"query": API_UNMOUNT_VOLUME_MUTATION,
"variables": {"name": "sdx"},
},
)
assert response.status_code == 200
assert response.json().get("data") is not None
assert response.json()["data"]["unmountVolume"]["code"] == 200
assert response.json()["data"]["unmountVolume"]["message"] is not None
assert response.json()["data"]["unmountVolume"]["success"] is True