Example #1
0
def test_failed_unlock_raises_exception(client: StructurizrClient,
                                        mocker: MockerFixture):
    """Check failing to unlock raises an exception.

    Not quite sure how this could occur, but check the handling anyway.
    """
    def fake_send(request: Request):
        if request.method == "PUT":
            return Response(
                200,
                content='{"success": true, "message": "OK"}'.encode("ascii"),
                request=request,
            )
        else:
            return Response(
                200,
                content='{"success": false, "message": "Not OK"}'.encode(
                    "ascii"),
                request=request,
            )

    mocker.patch.object(client._client, "send", new=fake_send)
    with pytest.raises(StructurizrClientException, match="Failed to unlock"):
        with client.lock():
            pass
Example #2
0
def test_failed_lock_bad_http_code(client: StructurizrClient,
                                   mocker: MockerFixture):
    """Check getting a non-200 HTTP response raises an HTTPX exception.

    Trying to lock a workspace which is already locked by someone else actually
    returns a 200 status, but with success as false in the message.
    """
    def fake_send(request: Request):
        msg = "Server failure"
        return Response(
            500,
            content=msg.encode("ascii"),
            request=request,
        )

    mocker.patch.object(client._client, "send", new=fake_send)
    with pytest.raises(httpx.HTTPStatusError):
        with client.lock():
            pass
Example #3
0
def test_failed_lock_raises_exception(client: StructurizrClient,
                                      mocker: MockerFixture):
    """Check failing to lock raises an exception.

    Trying to lock a workspace which is already locked by someone else actually
    returns a 200 status, but with success as false in the message.
    """
    def fake_send(request: Request):
        msg = '{"success": false, "message": "The workspace is already locked"}'
        return Response(
            200,
            content=msg.encode("ascii"),
            request=request,
        )

    mocker.patch.object(client._client, "send", new=fake_send)
    with pytest.raises(StructurizrClientException, match="Failed to lock"):
        with client.lock():
            pass
Example #4
0
def test_failed_lock_on_free_plan_doesnt_attempt_unlock(
        client: StructurizrClient, mocker: MockerFixture):
    """Check that if lock failed because on free plan then unlock isn't called."""
    requests: List[Request] = []

    def fake_send(request: Request):
        nonlocal requests
        requests.append(request)
        return Response(
            200,
            content='{"success": false, "message": "Cannot lock on free plan"}'
            .encode("ascii"),
            request=request,
        )

    mocker.patch.object(client._client, "send", new=fake_send)
    with client.lock():
        pass

    assert len(requests) == 1
Example #5
0
def test_locking_and_unlocking_with_context_manager(client: StructurizrClient,
                                                    mocker: MockerFixture):
    """Check new-style locking using .lock()."""
    requests: List[Request] = []

    def fake_send(request: Request):
        nonlocal requests
        requests.append(request)
        return Response(
            200,
            content='{"success": true, "message": "OK"}'.encode("ascii"),
            request=request,
        )

    mocker.patch.object(client._client, "send", new=fake_send)
    with client.lock():
        pass

    assert len(requests) == 2
    assert requests[0].method == "PUT"
    assert requests[0].url.path == "/workspace/19/lock"
    assert requests[1].method == "DELETE"
    assert requests[1].url.path == "/workspace/19/lock"