Skip to content

Commit

Permalink
Merge pull request #442 from betagouv/feat/check-folders-sync
Browse files Browse the repository at this point in the history
feat: check folder sync endpoint
  • Loading branch information
wiwski authored Sep 26, 2024
2 parents 93e31d4 + 9b754c2 commit 52286a0
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 1 deletion.
31 changes: 30 additions & 1 deletion api/data.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,15 @@
import pathlib
from typing import Annotated

from fastapi import APIRouter, Depends, HTTPException, Path, BackgroundTasks
from fastapi import (
APIRouter,
Depends,
HTTPException,
Path,
BackgroundTasks,
)
from fastapi.responses import JSONResponse, StreamingResponse
import pydantic
from auth import (
ExtraPayloadTokenGetter,
generate_token_for_path,
Expand Down Expand Up @@ -296,6 +303,28 @@ def rename_run_folder(
return JSONResponse({"detail": error.message}, status_code=400)


class CheckFoldersSyncBody(pydantic.BaseModel):
project_slugs: list[str]


@router.post(
"/check-folders-sync",
status_code=200,
dependencies=[Depends(verify_is_euphrosyne_backend)],
)
def check_folders_sync(
body: CheckFoldersSyncBody,
azure_client: DataAzureClient = Depends(get_storage_azure_client),
):
unsynced_dirs = []
project_dirs = azure_client.list_project_dirs()
for slug in body.project_slugs:
if slug not in project_dirs:
unsynced_dirs.append(slug)
orphans_dirs = [dir for dir in project_dirs if dir not in body.project_slugs]
return {"unsynced_dirs": unsynced_dirs, "orphan_dirs": orphans_dirs}


def _verify_can_set_token_expiration(user: User):
if not user.is_admin:
raise HTTPException(
Expand Down
11 changes: 11 additions & 0 deletions clients/azure/data.py
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,17 @@ def __init__(self):
)
self.share_name = os.environ["AZURE_STORAGE_FILESHARE"]

def list_project_dirs(self) -> list[str]:
"""Returns all directory names in project folder"""
dir_path = _get_projects_path()
files_and_folders = self._list_files(dir_path)

def filter_folder(f: ProjectFileOrDirectory):
return f.type == "directory"

folders = filter(filter_folder, files_and_folders)
return [f.name for f in folders]

def get_project_documents(
self,
project_name: str,
Expand Down
16 changes: 16 additions & 0 deletions tests/api/test_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,22 @@ def test_generate_signed_url_for_path_with_expiration(
del app.dependency_overrides[get_current_user]


def test_check_folders_sync(app: FastAPI, client: TestClient):
app.dependency_overrides[verify_is_euphrosyne_backend] = lambda: MagicMock()
app.dependency_overrides[get_storage_azure_client] = lambda: MagicMock(
list_project_dirs=MagicMock(return_value=["project1", "project2"])
)
response = client.post(
"/data/check-folders-sync",
json={"project_slugs": ["project1", "unsynced project"]},
)
assert response.status_code == 200
assert response.json() == {
"unsynced_dirs": ["unsynced project"],
"orphan_dirs": ["project2"],
}


def test_verify_can_set_token_expiration():
with pytest.raises(HTTPException):
_verify_can_set_token_expiration(user=User(id="1", projects=[], is_admin=False))
Expand Down

0 comments on commit 52286a0

Please sign in to comment.