Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion rowan/project.py
Original file line number Diff line number Diff line change
Expand Up @@ -197,8 +197,15 @@ def default_project() -> Project:

Raises:
httpx.HTTPStatusError: API request fails
ValueError: the user has no default project in the API key's organization
"""
with api_client() as client:
response = client.get("/user/me/default_project")
response.raise_for_status()
return Project(**response.json())
project = response.json()
if project is None:
raise ValueError(
"You have no default project in this organization. "
"Set a default project, or pass a folder or project."
)
return Project(**project)
35 changes: 35 additions & 0 deletions tests/test_project.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
"""Tests for project retrieval."""

from collections.abc import Iterator
from contextlib import contextmanager
from unittest.mock import MagicMock

import pytest

from rowan import project as project_module


def _mock_client(monkeypatch: pytest.MonkeyPatch, payload: dict | None) -> None:
client = MagicMock()
client.get.return_value.json.return_value = payload

@contextmanager
def mock_api_client() -> Iterator[MagicMock]:
yield client

monkeypatch.setattr(project_module, "api_client", mock_api_client)


def test_default_project_returns_the_project(monkeypatch: pytest.MonkeyPatch) -> None:
"""Return the default project the API reports."""
_mock_client(monkeypatch, {"uuid": "project-uuid", "name": "Default"})

assert project_module.default_project().uuid == "project-uuid"


def test_default_project_without_one_raises_a_clear_error(monkeypatch: pytest.MonkeyPatch) -> None:
"""Explain a missing default project instead of failing to build one from null."""
_mock_client(monkeypatch, None)

with pytest.raises(ValueError, match="no default project"):
project_module.default_project()
Loading