Skip to content

Commit

Permalink
Merge pull request #5 from NERC-CEH/bucket_list
Browse files Browse the repository at this point in the history
Add a bucket listing GET method and a test
  • Loading branch information
metazool authored Oct 8, 2024
2 parents 6bcafb2 + d15a406 commit 7ef8af3
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 17 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ pip install -e .[docs]
To work on tests:

```
pip install -e .[tests]
pip install -e .[test]
```

To run the linter and githook:
Expand Down
12 changes: 12 additions & 0 deletions src/os_api/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,18 @@ async def main() -> RedirectResponse:
return RedirectResponse(url="/docs")


@app.get("/list-buckets/")
async def list_buckets() -> JSONResponse:
"Endpoint to create a new bucket in the server."
s3 = boto3_client()
bucket_names = [n["Name"] for n in s3.list_buckets()["Buckets"]]
try:
return JSONResponse(status_code=200, content=bucket_names)
except Exception as err:
logging.info(err)
return JSONResponse(status_code=500, content=f"Error while listing buckets: {str(err)}")


@app.post("/create-bucket/", tags=["Data"])
async def create_bucket(bucket_name: str = Query("", description="")) -> JSONResponse:
"Endpoint to create a new bucket in the server."
Expand Down
3 changes: 2 additions & 1 deletion tests/conftest.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import os
import pytest


@pytest.fixture
def fixture_dir():
"""
Expand All @@ -14,4 +15,4 @@ def text_file(fixture_dir):
"""
Sample text file
"""
return open(os.path.join(fixture_dir, "1_test.txt"), 'rb')
return open(os.path.join(fixture_dir, "1_test.txt"), "rb")
47 changes: 32 additions & 15 deletions tests/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
At level of "do endpoints exist, and resolve"
"""

import os
from os_api.api import app, s3_endpoint
import pytest
Expand All @@ -14,61 +15,77 @@
from fastapi.testclient import TestClient
from moto import mock_aws
import logging

logging.basicConfig(level=logging.INFO)

from moto.server import ThreadedMotoServer


@pytest.fixture(scope="module")
def moto_server():
"""Fixture to run a mocked AWS server for testing."""
# Note: pass `port=0` to get a random free port.
server = ThreadedMotoServer(port=0)
server.start()
host, port = server.get_host_and_port()
os.environ['AWS_URL_ENDPOINT'] = f"http://{host}:{port}"
os.environ["AWS_URL_ENDPOINT"] = f"http://{host}:{port}"
yield f"http://{host}:{port}"
server.stop()


# Without both the dependency override _and_ the environment variable set in the fixture,
# the endpoint URL doesn't get set for the API properly - wish i fully understood why! - JW
app.dependency_overrides[s3_endpoint] = moto_server

client = TestClient(app)


def test_read_main():
response = client.get("/")
assert response.status_code == 200


def test_create_bucket(moto_server):
params = {'bucket_name': 'test_bucket'}
response = client.post('/create-bucket/', params=params)
params = {"bucket_name": "test_bucket"}
response = client.post("/create-bucket/", params=params)
assert response.status_code == 200


def test_generate_presigned_url(moto_server):
params = {'filename': 'demo.txt',
'file_type': 'text/plain',
'bucket_name': 'test_bucket'}
response = client.post('/generate-presigned-url/', data=params)
params = {
"filename": "demo.txt",
"file_type": "text/plain",
"bucket_name": "test_bucket",
}
response = client.post("/generate-presigned-url/", data=params)
assert response.status_code == 200


def test_upload(text_file):
data = {'bucket_name': 'test_bucket'}
response = client.post('/create-bucket/', params=data)
response = client.post('/upload/', data=data, files=[('files', text_file)])
data = {"bucket_name": "test_bucket"}
response = client.post("/create-bucket/", params=data)
response = client.post("/upload/", data=data, files=[("files", text_file)])
assert response.status_code == 200


def test_check_file_exist(text_file):
data = {'bucket_name': 'test_bucket'}
response = client.post('/create-bucket/', params=data)
response = client.post('/upload/', data=data, files=[('files', text_file)])
data['filename'] = "1_test.txt"
response = client.post('/check-file-exist/', data=data)
data = {"bucket_name": "test_bucket"}
response = client.post("/create-bucket/", params=data)
response = client.post("/upload/", data=data, files=[("files", text_file)])
data["filename"] = "1_test.txt"
response = client.post("/check-file-exist/", data=data)
assert response.status_code == 200


def test_list_buckets():
buckets = ["hello", "world"]
for b in buckets:
client.post("/create-bucket/", params={"bucket_name": b})
res = client.get("/list-buckets/")
bucket_list = res.json()
for b in buckets:
assert b in bucket_list


def test_import_api_module():
import os_api.api

0 comments on commit 7ef8af3

Please sign in to comment.