Skip to content

Commit

Permalink
Responding with error when filter is not mounted
Browse files Browse the repository at this point in the history
  • Loading branch information
gabrielmer committed Jan 8, 2024
1 parent 45fabb6 commit 0012d60
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 45 deletions.
47 changes: 4 additions & 43 deletions tests/wakunode_rest/test_rest_admin.nim
Original file line number Diff line number Diff line change
Expand Up @@ -136,44 +136,6 @@ suite "Waku v2 Rest API - Admin":
($getRes.data).contains(expectedFilterData2)
($getRes.data).contains(expectedFilterData3)

asyncTest "Get filter data":
await allFutures(node1.mountFilter(), node2.mountFilterClient(), node3.mountFilterClient())

let
contentFiltersNode2 = @[DefaultContentTopic, ContentTopic("2"), ContentTopic("3")]
contentFiltersNode3 = @[ContentTopic("3"), ContentTopic("4")]
pubsubTopicNode2 = DefaultPubsubTopic
pubsubTopicNode3 = PubsubTopic("/waku/2/custom-waku/proto")

expectedFilterData2 = fmt"(peerId: ""{$peerInfo2}"", filterCriteria:" &
fmt" @[(pubsubTopic: ""{pubsubTopicNode2}"", contentTopic: ""{contentFiltersNode2[0]}""), " &
fmt"(pubsubTopic: ""{pubsubTopicNode2}"", contentTopic: ""{contentFiltersNode2[1]}""), " &
fmt"(pubsubTopic: ""{pubsubTopicNode2}"", contentTopic: ""{contentFiltersNode2[2]}"")]"

expectedFilterData3 = fmt"(peerId: ""{$peerInfo3}"", filterCriteria:" &
fmt" @[(pubsubTopic: ""{pubsubTopicNode3}"", contentTopic: ""{contentFiltersNode3[0]}""), " &
fmt"(pubsubTopic: ""{pubsubTopicNode3}"", contentTopic: ""{contentFiltersNode3[1]}"")]"

let
subscribeResponse2 = await node2.wakuFilterClient.subscribe(
peerInfo1, pubsubTopicNode2, contentFiltersNode2
)
subscribeResponse3 = await node3.wakuFilterClient.subscribe(
peerInfo1, pubsubTopicNode3, contentFiltersNode3
)

assert subscribeResponse2.isOk(), $subscribeResponse2.error
assert subscribeResponse3.isOk(), $subscribeResponse3.error

let getRes = await client.getFilterSubscriptions()

check:
getRes.status == 200
$getRes.contentType == $MIMETYPE_JSON
getRes.data.len() == 2
($getRes.data).contains(expectedFilterData2)
($getRes.data).contains(expectedFilterData3)

asyncTest "Get filter data - no filter subscribers":
await node1.mountFilter()

Expand All @@ -185,9 +147,8 @@ suite "Waku v2 Rest API - Admin":
getRes.data.len() == 0

asyncTest "Get filter data - filter not mounted":
let getRes = await client.getFilterSubscriptions()
let getRes = await client.getFilterSubscriptionsFilterNotMounted()

check:
getRes.status == 200
$getRes.contentType == $MIMETYPE_JSON
getRes.data.len() == 0
getRes.status == 400
getRes.data == "Error: Filter Protocol is not mounted to the node"
4 changes: 4 additions & 0 deletions waku/waku_api/rest/admin/client.nim
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,7 @@ proc postPeers*(body: seq[string]):
proc getFilterSubscriptions*():
RestResponse[seq[FilterSubscription]]
{.rest, endpoint: "/admin/v1/filter/subscriptions", meth: HttpMethod.MethodGet.}

proc getFilterSubscriptionsFilterNotMounted*():
RestResponse[string]
{.rest, endpoint: "/admin/v1/filter/subscriptions", meth: HttpMethod.MethodGet.}
6 changes: 4 additions & 2 deletions waku/waku_api/rest/admin/handlers.nim
Original file line number Diff line number Diff line change
Expand Up @@ -113,15 +113,17 @@ proc installAdminV1PostPeersHandler(router: var RestRouter, node: WakuNode) =

proc installAdminV1GetFilterSubsHandler(router: var RestRouter, node: WakuNode) =
router.api(MethodGet, ROUTE_ADMIN_V1_FILTER_SUBS) do () -> RestApiResponse:


if node.wakuFilter.isNil():
return RestApiResponse.badRequest("Error: Filter Protocol is not mounted to the node")

var subscriptions: seq[FilterSubscription] = @[]

if not node.wakuFilter.isNil():

var filterCriteria: seq[FilterTopic]

for (peerId, criteria) in node.wakuFilter.subscriptions.pairs():

filterCriteria = criteria.toSeq().mapIt(FilterTopic(pubsubTopic: it[0],
contentTopic: it[1]))

Expand Down

0 comments on commit 0012d60

Please sign in to comment.