From 4b9c8efc4b42940b9451a6107f528f837697323f Mon Sep 17 00:00:00 2001 From: def Date: Thu, 1 Sep 2022 20:46:48 +0200 Subject: [PATCH] add job tests --- tests/test_graphql/test_job.py | 103 +++++++++++++++++++++++++++++++++ 1 file changed, 103 insertions(+) create mode 100644 tests/test_graphql/test_job.py diff --git a/tests/test_graphql/test_job.py b/tests/test_graphql/test_job.py new file mode 100644 index 0000000..f5fd583 --- /dev/null +++ b/tests/test_graphql/test_job.py @@ -0,0 +1,103 @@ +import pytest + + +class ProcessMock: + """Mock subprocess.Popen""" + + def __init__(self, args, **kwargs): + self.args = args + self.kwargs = kwargs + + def communicate(): # pylint: disable=no-method-argument + return (b"NEW_HASHED", None) + + returncode = 0 + + +class JobsMock: + def remove_by_uuid(self, job_uuid: str): + return True + +class JobsMockReturnFalse: + def remove_by_uuid(self, job_uuid: str): + return False + + +@pytest.fixture +def mock_subprocess_popen(mocker): + mock = mocker.patch("subprocess.Popen", autospec=True, return_value=ProcessMock) + return mock + + +@pytest.fixture +def mock_jobs(mocker): + mock = mocker.patch("selfprivacy_api.jobs.__init__.Jobs", autospec=True, return_value=JobsMock) + return mock + +@pytest.fixture +def mock_jobs_return_false(mocker): + mock = mocker.patch("selfprivacy_api.jobs.__init__.Jobs", autospec=True, return_value=JobsMock) + return mock + + +API_REMOVE_JOB_MUTATION = """ +mutation removeJob($sshInput: SshMutationInput!) { + removeJob(sshInput: $sshInput) { + success + message + code + } +} +""" + + +def test_graphql_remove_job_unauthorized(client, some_users, mock_subprocess_popen, mock_jobs): + response = client.post( + "/graphql", + json={ + "query": API_REMOVE_JOB_MUTATION, + "variables": { + "sshInput": { + "username": "user1", + "sshKey": "ssh-rsa KEY test_key@pc", + }, + }, + }, + ) + assert response.status_code == 200 + assert response.json().get("data") is None + + +def test_graphql_remove_job(authorized_client, some_users, mock_subprocess_popen, mock_jobs): + response = authorized_client.post( + "/graphql", + json={ + "query": API_REMOVE_JOB_MUTATION, + "variables": { + "job_id": "3301", + }, + }, + ) + assert response.status_code == 200 + assert response.json().get("data") is not None + + assert response.json()["data"]["removeJob"]["code"] == 200 + assert response.json()["data"]["removeJob"]["message"] is not None + assert response.json()["data"]["removeJob"]["success"] is True + +def test_graphql_remove_job_not_found(authorized_client, some_users, mock_subprocess_popen, mock_jobs_return_false): + response = authorized_client.post( + "/graphql", + json={ + "query": API_REMOVE_JOB_MUTATION, + "variables": { + "job_id": "3301", + }, + }, + ) + assert response.status_code == 200 + assert response.json().get("data") is not None + + assert response.json()["data"]["removeJob"]["code"] == 404 + assert response.json()["data"]["removeJob"]["message"] is not None + assert response.json()["data"]["removeJob"]["success"] is False \ No newline at end of file