|
| 1 | +from unittest.mock import MagicMock |
| 2 | + |
| 3 | +import pytest |
| 4 | +from tornado.escape import json_decode |
| 5 | +from tornado.httpclient import HTTPClientError |
| 6 | + |
| 7 | +from jupyter_server_fileid.manager import BaseFileIdManager |
| 8 | + |
| 9 | + |
| 10 | +class MockFileIdManager(BaseFileIdManager): |
| 11 | + _normalize_path = MagicMock() |
| 12 | + _from_normalized_path = MagicMock() |
| 13 | + index = MagicMock() |
| 14 | + move = MagicMock() |
| 15 | + copy = MagicMock() |
| 16 | + delete = MagicMock() |
| 17 | + save = MagicMock() |
| 18 | + get_handlers_by_action = MagicMock() |
| 19 | + get_path = MagicMock(return_value="mock_path") |
| 20 | + get_id = MagicMock(return_value="mock_id") |
| 21 | + |
| 22 | + |
| 23 | +@pytest.fixture |
| 24 | +def jp_server_config(): |
| 25 | + yield { |
| 26 | + "ServerApp": {"jpserver_extensions": {"jupyter_server_fileid": True}}, |
| 27 | + "FileIdExtension": {"file_id_manager_class": MockFileIdManager}, |
| 28 | + } |
| 29 | + |
| 30 | + |
| 31 | +@pytest.fixture |
| 32 | +def file_id_extension(jp_serverapp): |
| 33 | + ext_pkg = jp_serverapp.extension_manager.extensions["jupyter_server_fileid"] |
| 34 | + ext_point = ext_pkg.extension_points["jupyter_server_fileid"] |
| 35 | + return ext_point.app |
| 36 | + |
| 37 | + |
| 38 | +async def test_file_id_handler(jp_fetch, file_id_extension): |
| 39 | + response = await jp_fetch("api/fileid/id", params={"path": "test"}) |
| 40 | + file_id_extension.file_id_manager.get_id.assert_called_once() |
| 41 | + body = json_decode(response.body) |
| 42 | + assert "id" in body |
| 43 | + assert body["id"] == "mock_id" |
| 44 | + |
| 45 | + |
| 46 | +async def test_file_path_handler(jp_fetch, file_id_extension): |
| 47 | + response = await jp_fetch("api/fileid/path", params={"id": "test"}) |
| 48 | + file_id_extension.file_id_manager.get_path.assert_called_once() |
| 49 | + body = json_decode(response.body) |
| 50 | + assert "path" in body |
| 51 | + assert body["path"] == "mock_path" |
| 52 | + |
| 53 | + |
| 54 | +async def test_missing_query_param_in_id_handler(jp_fetch): |
| 55 | + with pytest.raises(HTTPClientError) as err: |
| 56 | + response = await jp_fetch("api/fileid/id") |
| 57 | + |
| 58 | + assert err.value.code == 400 |
| 59 | + |
| 60 | + |
| 61 | +async def test_missing_query_param_in_path_handler(jp_fetch): |
| 62 | + with pytest.raises(HTTPClientError) as err: |
| 63 | + response = await jp_fetch("api/fileid/path") |
| 64 | + |
| 65 | + assert err.value.code == 400 |
| 66 | + |
| 67 | + |
| 68 | +async def test_resource_not_found_in_id_handler(jp_fetch, monkeypatch): |
| 69 | + def mock_get_id_with_no_entry(self, path): |
| 70 | + return None |
| 71 | + |
| 72 | + monkeypatch.setattr(MockFileIdManager, "get_id", mock_get_id_with_no_entry) |
| 73 | + |
| 74 | + with pytest.raises(HTTPClientError) as err: |
| 75 | + await jp_fetch("api/fileid/id", params={"path": "test"}) |
| 76 | + |
| 77 | + assert err.value.code == 404 |
| 78 | + assert err.value.message.startswith("The ID for file") |
| 79 | + |
| 80 | + |
| 81 | +async def test_resource_not_found_in_path_handler(jp_fetch, monkeypatch): |
| 82 | + def mock_get_path_with_no_entry(self, id): |
| 83 | + return None |
| 84 | + |
| 85 | + monkeypatch.setattr(MockFileIdManager, "get_path", mock_get_path_with_no_entry) |
| 86 | + |
| 87 | + with pytest.raises(HTTPClientError) as err: |
| 88 | + await jp_fetch("api/fileid/path", params={"id": "test"}) |
| 89 | + |
| 90 | + assert err.value.code == 404 |
| 91 | + assert err.value.message.startswith("The path for file") |
0 commit comments