| 1 | # Copyright 2026 Google LLC |
| 2 | # |
| 3 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | # you may not use this file except in compliance with the License. |
| 5 | # You may obtain a copy of the License at |
| 6 | # |
| 7 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | # |
| 9 | # Unless required by applicable law or agreed to in writing, software |
| 10 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | # See the License for the specific language governing permissions and |
| 13 | # limitations under the License. |
| 14 | |
| 15 | import base64 |
| 16 | from unittest.mock import MagicMock, patch |
| 17 | |
| 18 | import pytest |
| 19 | from colab_cli.contents import ContentsClient |
| 20 | from requests import Response |
| 21 | |
| 22 | from colab_cli.state import SessionState |
| 23 | |
| 24 | |
| 25 | @pytest.fixture |
| 26 | def session(): |
| 27 | return SessionState( |
| 28 | name="test-session", |
| 29 | token="test-token", |
| 30 | url="https://fake-endpoint.colab.dev", |
| 31 | endpoint="endpoint", |
| 32 | ) |
| 33 | |
| 34 | |
| 35 | @pytest.fixture |
| 36 | def client(session): |
| 37 | return ContentsClient(session) |
| 38 | |
| 39 | |
| 40 | @patch("colab_cli.contents.requests.request") |
| 41 | def test_list_dir(mock_request, client): |
| 42 | mock_resp = MagicMock(spec=Response) |
| 43 | mock_resp.status_code = 200 |
| 44 | mock_resp.json.return_value = { |
| 45 | "name": "content", |
| 46 | "type": "directory", |
| 47 | "content": [ |
| 48 | {"name": "file.txt", "type": "file"}, |
| 49 | {"name": "dir", "type": "directory"}, |
| 50 | ], |
| 51 | } |
| 52 | mock_request.return_value = mock_resp |
| 53 | |
| 54 | res = client.list_dir("content") |
| 55 | |
| 56 | mock_request.assert_called_once_with( |
| 57 | "GET", |
| 58 | "https://fake-endpoint.colab.dev/api/contents/content", |
| 59 | params={"authuser": "0", "colab-runtime-proxy-token": "test-token"}, |
| 60 | json=None, |
| 61 | ) |
| 62 | assert res["type"] == "directory" |
| 63 | assert len(res["content"]) == 2 |
| 64 | |
| 65 | |
| 66 | @patch("colab_cli.contents.requests.request") |
| 67 | def test_rm_file(mock_request, client): |
| 68 | mock_resp = MagicMock(spec=Response) |
| 69 | mock_resp.status_code = 204 |
| 70 | mock_request.return_value = mock_resp |
| 71 | |
| 72 | client.rm("content/file.txt") |
| 73 | |
| 74 | mock_request.assert_called_once_with( |
| 75 | "DELETE", |
| 76 | "https://fake-endpoint.colab.dev/api/contents/content/file.txt", |
| 77 | params={"authuser": "0", "colab-runtime-proxy-token": "test-token"}, |
| 78 | json=None, |
| 79 | ) |
| 80 | |
| 81 | |
| 82 | @patch("colab_cli.contents.requests.request") |
| 83 | def test_404_error(mock_request, client): |
| 84 | mock_resp = MagicMock(spec=Response) |
| 85 | mock_resp.status_code = 404 |
| 86 | mock_request.return_value = mock_resp |
| 87 | |
| 88 | with pytest.raises(FileNotFoundError): |
| 89 | client.list_dir("nonexistent") |
| 90 | |
| 91 | |
| 92 | @patch("colab_cli.contents.requests.request") |
| 93 | def test_download_file(mock_request, client, tmp_path): |
| 94 | mock_resp = MagicMock(spec=Response) |
| 95 | mock_resp.status_code = 200 |
| 96 | |
| 97 | # Mocking a base64 encoded response |
| 98 | content_bytes = b"Hello world!" |
| 99 | b64_content = base64.b64encode(content_bytes).decode("ascii") |
| 100 | |
| 101 | mock_resp.json.return_value = { |
| 102 | "name": "test.txt", |
| 103 | "type": "file", |
| 104 | "format": "base64", |
| 105 | "content": b64_content, |
| 106 | } |
| 107 | mock_request.return_value = mock_resp |
| 108 | |
| 109 | local_file = tmp_path / "test.txt" |
| 110 | client.download("content/test.txt", str(local_file)) |
| 111 | |
| 112 | mock_request.assert_called_once_with( |
| 113 | "GET", |
| 114 | "https://fake-endpoint.colab.dev/api/contents/content/test.txt", |
| 115 | params={ |
| 116 | "authuser": "0", |
| 117 | "colab-runtime-proxy-token": "test-token", |
| 118 | "content": "1", |
| 119 | }, |
| 120 | json=None, |
| 121 | ) |
| 122 | |
| 123 | assert local_file.read_bytes() == content_bytes |
| 124 | |
| 125 | |
| 126 | @patch("colab_cli.contents.requests.request") |
| 127 | def test_upload_file(mock_request, client, tmp_path): |
| 128 | mock_resp = MagicMock(spec=Response) |
| 129 | mock_resp.status_code = 200 |
| 130 | mock_request.return_value = mock_resp |
| 131 | |
| 132 | local_file = tmp_path / "test.txt" |
| 133 | content_bytes = b"Hello upload!" |
| 134 | local_file.write_bytes(content_bytes) |
| 135 | |
| 136 | client.upload(str(local_file), "content/test.txt") |
| 137 | |
| 138 | expected_b64 = base64.b64encode(content_bytes).decode("ascii") |
| 139 | |
| 140 | mock_request.assert_called_once_with( |
| 141 | "PUT", |
| 142 | "https://fake-endpoint.colab.dev/api/contents/content/test.txt", |
| 143 | params={"authuser": "0", "colab-runtime-proxy-token": "test-token"}, |
| 144 | json={ |
| 145 | "name": "test.txt", |
| 146 | "path": "content/test.txt", |
| 147 | "type": "file", |
| 148 | "format": "base64", |
| 149 | "content": expected_b64, |
| 150 | "chunk": 1, |
| 151 | }, |
| 152 | ) |