|
| 1 | +import json |
| 2 | +import unittest |
| 3 | + |
| 4 | +import boto3 |
| 5 | +from flask import Flask |
| 6 | + |
| 7 | +from smoosense.handlers.fs import fs_bp |
| 8 | +from smoosense.my_logging import getLogger |
| 9 | + |
| 10 | +logger = getLogger(__name__) |
| 11 | + |
| 12 | + |
| 13 | +class TestS3LSEndpoint(unittest.TestCase): |
| 14 | + """Test cases for the /ls endpoint with S3 paths.""" |
| 15 | + |
| 16 | + def setUp(self): |
| 17 | + """Set up test fixtures before each test method.""" |
| 18 | + self.app = Flask(__name__) |
| 19 | + self.app.register_blueprint(fs_bp) |
| 20 | + self.app.config["TESTING"] = True |
| 21 | + self.app.config["S3_CLIENT"] = boto3.client("s3") |
| 22 | + self.client = self.app.test_client() |
| 23 | + |
| 24 | + # Set up application context for all tests |
| 25 | + self.app_context = self.app.app_context() |
| 26 | + self.app_context.push() |
| 27 | + |
| 28 | + def tearDown(self): |
| 29 | + """Clean up after each test method.""" |
| 30 | + self.app_context.pop() |
| 31 | + |
| 32 | + def test_ls_s3_bucket_root(self): |
| 33 | + """Test listing S3 bucket root.""" |
| 34 | + response = self.client.get("/ls?path=s3://smoosense-demo/") |
| 35 | + self.assertEqual(response.status_code, 200) |
| 36 | + |
| 37 | + data = json.loads(response.get_data(as_text=True)) |
| 38 | + self.assertIsInstance(data, list) |
| 39 | + self.assertGreater(len(data), 0) |
| 40 | + |
| 41 | + # Each item should have required fields |
| 42 | + for item in data: |
| 43 | + self.assertIn("name", item) |
| 44 | + self.assertIn("size", item) |
| 45 | + self.assertIn("lastModified", item) |
| 46 | + self.assertIn("isDir", item) |
| 47 | + |
| 48 | + def test_ls_s3_nested_path(self): |
| 49 | + """Test listing S3 nested path.""" |
| 50 | + response = self.client.get("/ls?path=s3://smoosense-demo/datasets/") |
| 51 | + self.assertEqual(response.status_code, 200) |
| 52 | + |
| 53 | + data = json.loads(response.get_data(as_text=True)) |
| 54 | + self.assertIsInstance(data, list) |
| 55 | + |
| 56 | + def test_ls_s3_with_limit(self): |
| 57 | + """Test listing S3 with limit parameter.""" |
| 58 | + response = self.client.get("/ls?path=s3://smoosense-demo/&limit=2") |
| 59 | + self.assertEqual(response.status_code, 200) |
| 60 | + |
| 61 | + data = json.loads(response.get_data(as_text=True)) |
| 62 | + self.assertIsInstance(data, list) |
| 63 | + self.assertLessEqual(len(data), 2) |
| 64 | + |
| 65 | + def test_ls_s3_nonexistent_bucket(self): |
| 66 | + """Test listing non-existent S3 bucket returns 404.""" |
| 67 | + response = self.client.get("/ls?path=s3://this-bucket-definitely-does-not-exist-12345/") |
| 68 | + self.assertEqual(response.status_code, 404) |
| 69 | + |
| 70 | + data = json.loads(response.get_data(as_text=True)) |
| 71 | + self.assertIn("error", data) |
| 72 | + self.assertIn("NoSuchBucket", data["error"]) |
| 73 | + |
| 74 | + def test_ls_s3_access_denied(self): |
| 75 | + """Test listing S3 bucket without access returns 403.""" |
| 76 | + # Use a known bucket that exists but we don't have access to |
| 77 | + response = self.client.get("/ls?path=s3://amazon-reviews-pds/") |
| 78 | + |
| 79 | + # This should return 403 if access is denied |
| 80 | + # Note: This test depends on not having access to this public bucket |
| 81 | + # If you do have access, this test may pass with 200 |
| 82 | + if response.status_code == 403: |
| 83 | + data = json.loads(response.get_data(as_text=True)) |
| 84 | + self.assertIn("error", data) |
| 85 | + self.assertIn("AccessDenied", data["error"]) |
| 86 | + else: |
| 87 | + # If we happen to have access, just verify it returns valid data |
| 88 | + self.assertEqual(response.status_code, 200) |
| 89 | + |
| 90 | + def test_ls_s3_missing_path(self): |
| 91 | + """Test listing without path parameter returns 400.""" |
| 92 | + response = self.client.get("/ls") |
| 93 | + self.assertEqual(response.status_code, 400) |
| 94 | + |
| 95 | + data = json.loads(response.get_data(as_text=True)) |
| 96 | + self.assertIn("error", data) |
| 97 | + |
| 98 | + |
| 99 | +if __name__ == "__main__": |
| 100 | + unittest.main() |
0 commit comments