Skip to content

Commit

Permalink
server/integrations/open_collective: handle case where host is null
Browse files Browse the repository at this point in the history
Fix #3652
  • Loading branch information
frankie567 committed Jul 15, 2024
1 parent 6277f16 commit db18149
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 6 deletions.
8 changes: 5 additions & 3 deletions server/polar/integrations/open_collective/service.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import contextlib
import dataclasses
from collections.abc import AsyncGenerator
from typing import Any

import httpx

Expand All @@ -11,11 +12,11 @@
@dataclasses.dataclass
class OpenCollectiveCollective:
slug: str
host_slug: str
isActive: bool
isApproved: bool
isArchived: bool
isFrozen: bool
host_slug: str | None = None

@property
def is_eligible(self) -> bool:
Expand Down Expand Up @@ -84,9 +85,10 @@ async def get_collective(self, slug: str) -> OpenCollectiveCollective:
if "errors" in json:
raise CollectiveNotFoundError(slug)

collective = json["data"]["collective"]
collective: dict[str, Any] = json["data"]["collective"]
host = collective.pop("host")
return OpenCollectiveCollective(**collective, host_slug=host["slug"])
host_slug = host["slug"] if host is not None else None
return OpenCollectiveCollective(**collective, host_slug=host_slug)

@contextlib.asynccontextmanager
async def _get_graphql_client(self) -> AsyncGenerator[httpx.AsyncClient, None]:
Expand Down
21 changes: 18 additions & 3 deletions server/tests/account/test_endpoints.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,8 +92,23 @@ async def test_create_open_collective_get_collective_error(
@pytest.mark.parametrize(
"collective",
[
OpenCollectiveCollective("polar", "custom", True, True, False, False),
OpenCollectiveCollective("polar", "opensource", False, True, False, False),
OpenCollectiveCollective("polar", True, True, False, False, "custom"),
OpenCollectiveCollective(
"polar",
False,
True,
False,
False,
"opensource",
),
OpenCollectiveCollective(
"polar",
False,
True,
False,
False,
None,
),
],
)
@pytest.mark.auth
Expand Down Expand Up @@ -129,7 +144,7 @@ async def test_create_open_collective(
) -> None:
open_collective_mock = mocker.patch.object(open_collective, "get_collective")
open_collective_mock.return_value = OpenCollectiveCollective(
"polar", "opensource", True, True, False, False
"polar", True, True, False, False, "opensource"
)

response = await client.post(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"data": {
"collective": {
"slug": "notready",
"host": null,
"isActive": false,
"isApproved": false,
"isArchived": false,
"isFrozen": false
}
}
}
15 changes: 15 additions & 0 deletions server/tests/integrations/open_collective/test_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,21 @@ async def test_get_collective_collective_not_found(
await open_collective.get_collective("babel")


@pytest.mark.asyncio
async def test_get_collective_collective_not_ready(
open_collective_graphql_mock: respx.Route,
) -> None:
with open(
"tests/fixtures/cassettes/open_collective/collective/not_ready.json"
) as f:
cassette = json.loads(f.read())
open_collective_graphql_mock.mock(return_value=httpx.Response(200, json=cassette))
collective = await open_collective.get_collective("notready")

assert collective.slug == "notready"
assert collective.host_slug is None


@pytest.mark.asyncio
async def test_get_collective(
open_collective_graphql_mock: respx.Route,
Expand Down

0 comments on commit db18149

Please sign in to comment.