Skip to content

Commit

Permalink
minimum necessary to see moto server working
Browse files Browse the repository at this point in the history
  • Loading branch information
metazool committed Aug 21, 2024
1 parent 110721f commit 79a7d9f
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 19 deletions.
12 changes: 8 additions & 4 deletions src/os_api/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,10 @@ def boto_session() -> aioboto3.Session:
return aioboto3.Session()


def s3_endpoint() -> str:
return os.environ.get("AWS_URL_ENDPOINT", "")


session = boto_session()


Expand All @@ -83,7 +87,7 @@ async def create_bucket(bucket_name: str = Query("", description="")) -> JSONRes
"s3",
aws_access_key_id=AWS_ACCESS_KEY_ID,
aws_secret_access_key=AWS_SECRET_ACCESS_KEY,
endpoint_url=AWS_URL_ENDPOINT,
endpoint_url=s3_endpoint(),
) as s3_client:
try:
await s3_client.create_bucket(Bucket=bucket_name)
Expand Down Expand Up @@ -115,7 +119,7 @@ async def generate_presigned_url(
"s3",
aws_access_key_id=AWS_ACCESS_KEY_ID,
aws_secret_access_key=AWS_SECRET_ACCESS_KEY,
endpoint_url=AWS_URL_ENDPOINT,
endpoint_url=s3_endpoint(),
)
try:
# Generate a presigned URL for the S3
Expand Down Expand Up @@ -167,7 +171,7 @@ async def upload_file(s3_bucket_name: str, key: str, file: UploadFile) -> None:
"s3",
aws_access_key_id=AWS_ACCESS_KEY_ID,
aws_secret_access_key=AWS_SECRET_ACCESS_KEY,
endpoint_url=AWS_URL_ENDPOINT,
endpoint_url=s3_endpoint(),
) as s3_client:
try:
# Upload updated file to S3
Expand All @@ -191,7 +195,7 @@ async def check_file_exist(filename: str = Form(...)) -> JSONResponse:
"s3",
aws_access_key_id=AWS_ACCESS_KEY_ID,
aws_secret_access_key=AWS_SECRET_ACCESS_KEY,
endpoint_url=AWS_URL_ENDPOINT,
endpoint_url=s3_endpoint(),
)

try:
Expand Down
36 changes: 21 additions & 15 deletions tests/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,17 @@
At level of "do endpoints exist, and resolve"
"""
import os
from os_api.api import app
from os_api.api import app, boto_session, s3_endpoint
import pytest
import aioboto3
from fastapi import FastAPI
from fastapi.testclient import TestClient
from moto.server import ThreadedMotoServer
from moto import mock_aws
import logging

logging.basicConfig(level=logging.INFO)
# TODO resolve how moto should work with aioboto3 Session + fastapi dependency overrides

from moto.server import ThreadedMotoServer

@pytest.fixture(scope="module")
def moto_server():
Expand All @@ -23,39 +26,42 @@ def moto_server():
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


@pytest.mark.asyncio
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
print(response.content)


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

response = client.post('/generate-presigned-url/')
assert response

def test_upload():

response = client.post("/upload/")
response = client.post('/upload/')
assert response


def test_check_file_exist():

response = client.post("/check-file-exist/")
response = client.post('/check-file-exist/')
assert response

def test_import_api_module():
import os_api.api

0 comments on commit 79a7d9f

Please sign in to comment.