2022-06-10 11:30:51 +00:00
|
|
|
{.used.}
|
|
|
|
|
|
|
|
import
|
|
|
|
std/sequtils,
|
|
|
|
stew/byteutils,
|
|
|
|
stew/shims/net,
|
|
|
|
testutils/unittests,
|
2022-10-28 09:51:46 +00:00
|
|
|
presto, presto/client as presto_client,
|
2023-02-10 14:17:50 +00:00
|
|
|
libp2p/crypto/crypto
|
2022-06-10 11:30:51 +00:00
|
|
|
import
|
2023-02-23 08:31:06 +00:00
|
|
|
../../waku/common/base64,
|
2023-04-05 09:58:59 +00:00
|
|
|
../../waku/v2/waku_node,
|
2023-02-13 14:22:24 +00:00
|
|
|
../../waku/v2/node/rest/server,
|
|
|
|
../../waku/v2/node/rest/client,
|
|
|
|
../../waku/v2/node/rest/responses,
|
|
|
|
../../waku/v2/node/rest/relay/types,
|
|
|
|
../../waku/v2/node/rest/relay/handlers as relay_api,
|
|
|
|
../../waku/v2/node/rest/relay/client as relay_api_client,
|
|
|
|
../../waku/v2/node/rest/relay/topic_cache,
|
2023-04-19 11:29:23 +00:00
|
|
|
../../waku/v2/waku_core,
|
2023-04-18 13:22:10 +00:00
|
|
|
../../waku/v2/waku_relay,
|
2022-10-21 13:01:39 +00:00
|
|
|
../../waku/v2/utils/time,
|
2023-04-05 14:01:51 +00:00
|
|
|
../testlib/wakucore,
|
|
|
|
../testlib/wakunode
|
2022-06-10 11:30:51 +00:00
|
|
|
|
|
|
|
|
2023-02-10 14:17:50 +00:00
|
|
|
proc testWakuNode(): WakuNode =
|
|
|
|
let
|
2023-02-13 10:43:49 +00:00
|
|
|
privkey = generateSecp256k1Key()
|
2022-06-10 11:30:51 +00:00
|
|
|
bindIp = ValidIpAddress.init("0.0.0.0")
|
|
|
|
extIp = ValidIpAddress.init("127.0.0.1")
|
2023-02-13 14:22:24 +00:00
|
|
|
port = Port(0)
|
2022-06-10 11:30:51 +00:00
|
|
|
|
2023-04-05 14:01:51 +00:00
|
|
|
newTestWakuNode(privkey, bindIp, port, some(extIp), some(port))
|
2022-06-10 11:30:51 +00:00
|
|
|
|
|
|
|
|
2023-02-13 14:22:24 +00:00
|
|
|
suite "Waku v2 Rest API - Relay":
|
2023-02-10 14:17:50 +00:00
|
|
|
asyncTest "Subscribe a node to an array of topics - POST /relay/v1/subscriptions":
|
2022-06-10 11:30:51 +00:00
|
|
|
# Given
|
|
|
|
let node = testWakuNode()
|
|
|
|
await node.start()
|
2022-09-07 15:31:27 +00:00
|
|
|
await node.mountRelay()
|
2022-06-10 11:30:51 +00:00
|
|
|
|
2023-02-13 14:22:24 +00:00
|
|
|
let restPort = Port(58011)
|
2022-06-10 11:30:51 +00:00
|
|
|
let restAddress = ValidIpAddress.init("0.0.0.0")
|
2022-06-28 10:22:59 +00:00
|
|
|
let restServer = RestServerRef.init(restAddress, restPort).tryGet()
|
2022-06-10 11:30:51 +00:00
|
|
|
|
|
|
|
let topicCache = TopicCache.init()
|
|
|
|
|
|
|
|
installRelayPostSubscriptionsV1Handler(restServer.router, node, topicCache)
|
|
|
|
restServer.start()
|
|
|
|
|
|
|
|
let pubSubTopics = @[
|
2022-11-09 14:00:11 +00:00
|
|
|
PubSubTopic("pubsub-topic-1"),
|
|
|
|
PubSubTopic("pubsub-topic-2"),
|
|
|
|
PubSubTopic("pubsub-topic-3")
|
2022-06-10 11:30:51 +00:00
|
|
|
]
|
|
|
|
|
|
|
|
# When
|
|
|
|
let client = newRestHttpClient(initTAddress(restAddress, restPort))
|
|
|
|
let requestBody = RelayPostSubscriptionsRequest(pubSubTopics)
|
|
|
|
let response = await client.relayPostSubscriptionsV1(requestBody)
|
|
|
|
|
|
|
|
# Then
|
|
|
|
check:
|
|
|
|
response.status == 200
|
2022-10-28 09:51:46 +00:00
|
|
|
$response.contentType == $MIMETYPE_TEXT
|
2022-06-10 11:30:51 +00:00
|
|
|
response.data == "OK"
|
|
|
|
|
|
|
|
check:
|
|
|
|
topicCache.isSubscribed("pubsub-topic-1")
|
|
|
|
topicCache.isSubscribed("pubsub-topic-2")
|
|
|
|
topicCache.isSubscribed("pubsub-topic-3")
|
|
|
|
|
|
|
|
check:
|
|
|
|
# Node should be subscribed to default + new topics
|
2023-02-10 16:55:47 +00:00
|
|
|
toSeq(node.wakuRelay.subscribedTopics).len == pubSubTopics.len
|
2023-02-10 14:17:50 +00:00
|
|
|
|
2022-06-10 11:30:51 +00:00
|
|
|
await restServer.stop()
|
|
|
|
await restServer.closeWait()
|
|
|
|
await node.stop()
|
|
|
|
|
2023-02-10 14:17:50 +00:00
|
|
|
asyncTest "Unsubscribe a node from an array of topics - DELETE /relay/v1/subscriptions":
|
2022-06-10 11:30:51 +00:00
|
|
|
# Given
|
|
|
|
let node = testWakuNode()
|
|
|
|
await node.start()
|
2022-09-07 15:31:27 +00:00
|
|
|
await node.mountRelay()
|
2022-06-10 11:30:51 +00:00
|
|
|
|
2023-02-13 14:22:24 +00:00
|
|
|
let restPort = Port(58012)
|
2022-06-10 11:30:51 +00:00
|
|
|
let restAddress = ValidIpAddress.init("0.0.0.0")
|
2022-06-28 10:22:59 +00:00
|
|
|
let restServer = RestServerRef.init(restAddress, restPort).tryGet()
|
2022-06-10 11:30:51 +00:00
|
|
|
|
|
|
|
let topicCache = TopicCache.init()
|
|
|
|
topicCache.subscribe("pubsub-topic-1")
|
|
|
|
topicCache.subscribe("pubsub-topic-2")
|
|
|
|
topicCache.subscribe("pubsub-topic-3")
|
|
|
|
topicCache.subscribe("pubsub-topic-x")
|
|
|
|
|
|
|
|
installRelayDeleteSubscriptionsV1Handler(restServer.router, node, topicCache)
|
|
|
|
restServer.start()
|
|
|
|
|
|
|
|
let pubSubTopics = @[
|
2023-02-10 14:17:50 +00:00
|
|
|
PubSubTopic("pubsub-topic-1"),
|
2022-11-09 14:00:11 +00:00
|
|
|
PubSubTopic("pubsub-topic-2"),
|
|
|
|
PubSubTopic("pubsub-topic-3"),
|
|
|
|
PubSubTopic("pubsub-topic-y")
|
2022-06-10 11:30:51 +00:00
|
|
|
]
|
|
|
|
|
|
|
|
# When
|
|
|
|
let client = newRestHttpClient(initTAddress(restAddress, restPort))
|
|
|
|
let requestBody = RelayDeleteSubscriptionsRequest(pubSubTopics)
|
|
|
|
let response = await client.relayDeleteSubscriptionsV1(requestBody)
|
|
|
|
|
|
|
|
# Then
|
|
|
|
check:
|
|
|
|
response.status == 200
|
2022-10-28 09:51:46 +00:00
|
|
|
$response.contentType == $MIMETYPE_TEXT
|
2022-06-10 11:30:51 +00:00
|
|
|
response.data == "OK"
|
|
|
|
|
|
|
|
check:
|
|
|
|
not topicCache.isSubscribed("pubsub-topic-1")
|
|
|
|
not topicCache.isSubscribed("pubsub-topic-2")
|
|
|
|
not topicCache.isSubscribed("pubsub-topic-3")
|
|
|
|
topicCache.isSubscribed("pubsub-topic-x")
|
|
|
|
|
|
|
|
await restServer.stop()
|
|
|
|
await restServer.closeWait()
|
|
|
|
await node.stop()
|
|
|
|
|
|
|
|
|
2023-02-10 14:17:50 +00:00
|
|
|
asyncTest "Get the latest messages for topic - GET /relay/v1/messages/{topic}":
|
2022-06-10 11:30:51 +00:00
|
|
|
# Given
|
|
|
|
let node = testWakuNode()
|
|
|
|
await node.start()
|
2022-09-07 15:31:27 +00:00
|
|
|
await node.mountRelay()
|
2022-06-10 11:30:51 +00:00
|
|
|
|
2023-02-13 14:22:24 +00:00
|
|
|
let restPort = Port(58013)
|
2022-06-10 11:30:51 +00:00
|
|
|
let restAddress = ValidIpAddress.init("0.0.0.0")
|
2022-06-28 10:22:59 +00:00
|
|
|
let restServer = RestServerRef.init(restAddress, restPort).tryGet()
|
2022-06-10 11:30:51 +00:00
|
|
|
|
|
|
|
let pubSubTopic = "/waku/2/default-waku/proto"
|
|
|
|
let messages = @[
|
|
|
|
fakeWakuMessage(contentTopic = "content-topic-x", payload = toBytes("TEST-1")),
|
|
|
|
fakeWakuMessage(contentTopic = "content-topic-x", payload = toBytes("TEST-1")),
|
|
|
|
fakeWakuMessage(contentTopic = "content-topic-x", payload = toBytes("TEST-1")),
|
|
|
|
]
|
|
|
|
|
|
|
|
let topicCache = TopicCache.init()
|
|
|
|
|
|
|
|
topicCache.subscribe(pubSubTopic)
|
|
|
|
for msg in messages:
|
|
|
|
topicCache.addMessage(pubSubTopic, msg)
|
|
|
|
|
|
|
|
installRelayGetMessagesV1Handler(restServer.router, node, topicCache)
|
|
|
|
restServer.start()
|
|
|
|
|
|
|
|
# When
|
|
|
|
let client = newRestHttpClient(initTAddress(restAddress, restPort))
|
|
|
|
let response = await client.relayGetMessagesV1(pubSubTopic)
|
|
|
|
|
|
|
|
# Then
|
|
|
|
check:
|
|
|
|
response.status == 200
|
2022-10-28 09:51:46 +00:00
|
|
|
$response.contentType == $MIMETYPE_JSON
|
2022-06-10 11:30:51 +00:00
|
|
|
response.data.len == 3
|
2023-02-10 14:17:50 +00:00
|
|
|
response.data.all do (msg: RelayWakuMessage) -> bool:
|
2023-02-23 08:31:06 +00:00
|
|
|
msg.payload == base64.encode("TEST-1") and
|
2022-08-29 14:54:11 +00:00
|
|
|
msg.contentTopic.get().string == "content-topic-x" and
|
2022-10-21 13:01:39 +00:00
|
|
|
msg.version.get() == 2 and
|
|
|
|
msg.timestamp.get() != Timestamp(0)
|
2022-06-10 11:30:51 +00:00
|
|
|
|
|
|
|
|
|
|
|
check:
|
|
|
|
topicCache.isSubscribed(pubSubTopic)
|
|
|
|
topicCache.getMessages(pubSubTopic).tryGet().len == 0
|
|
|
|
|
|
|
|
await restServer.stop()
|
|
|
|
await restServer.closeWait()
|
|
|
|
await node.stop()
|
|
|
|
|
2023-02-10 14:17:50 +00:00
|
|
|
asyncTest "Post a message to topic - POST /relay/v1/messages/{topic}":
|
|
|
|
## "Relay API: publish and subscribe/unsubscribe":
|
2022-06-10 11:30:51 +00:00
|
|
|
# Given
|
|
|
|
let node = testWakuNode()
|
|
|
|
await node.start()
|
2022-09-07 15:31:27 +00:00
|
|
|
await node.mountRelay()
|
2022-06-10 11:30:51 +00:00
|
|
|
|
|
|
|
# RPC server setup
|
2023-02-13 14:22:24 +00:00
|
|
|
let restPort = Port(58014)
|
2022-06-10 11:30:51 +00:00
|
|
|
let restAddress = ValidIpAddress.init("0.0.0.0")
|
2022-06-28 10:22:59 +00:00
|
|
|
let restServer = RestServerRef.init(restAddress, restPort).tryGet()
|
2022-06-10 11:30:51 +00:00
|
|
|
|
|
|
|
let topicCache = TopicCache.init()
|
|
|
|
|
|
|
|
installRelayApiHandlers(restServer.router, node, topicCache)
|
|
|
|
restServer.start()
|
|
|
|
|
|
|
|
let client = newRestHttpClient(initTAddress(restAddress, restPort))
|
2023-02-10 14:17:50 +00:00
|
|
|
|
2023-02-10 16:55:47 +00:00
|
|
|
node.subscribe(DefaultPubsubTopic)
|
2023-02-10 14:17:50 +00:00
|
|
|
require:
|
|
|
|
toSeq(node.wakuRelay.subscribedTopics).len == 1
|
|
|
|
|
2022-06-10 11:30:51 +00:00
|
|
|
|
|
|
|
# When
|
|
|
|
let newTopics = @[
|
2022-11-09 14:00:11 +00:00
|
|
|
PubSubTopic("pubsub-topic-1"),
|
|
|
|
PubSubTopic("pubsub-topic-2"),
|
|
|
|
PubSubTopic("pubsub-topic-3")
|
2022-06-10 11:30:51 +00:00
|
|
|
]
|
|
|
|
discard await client.relayPostSubscriptionsV1(newTopics)
|
2023-02-10 14:17:50 +00:00
|
|
|
|
2022-11-09 08:55:47 +00:00
|
|
|
let response = await client.relayPostMessagesV1(DefaultPubsubTopic, RelayWakuMessage(
|
2023-02-23 08:31:06 +00:00
|
|
|
payload: base64.encode("TEST-PAYLOAD"),
|
2023-02-10 14:17:50 +00:00
|
|
|
contentTopic: some(DefaultContentTopic),
|
2022-06-10 11:30:51 +00:00
|
|
|
timestamp: some(int64(2022))
|
|
|
|
))
|
|
|
|
|
|
|
|
# Then
|
|
|
|
check:
|
|
|
|
response.status == 200
|
2022-10-28 09:51:46 +00:00
|
|
|
$response.contentType == $MIMETYPE_TEXT
|
2022-06-10 11:30:51 +00:00
|
|
|
response.data == "OK"
|
|
|
|
|
|
|
|
# TODO: Check for the message to be published to the topic
|
|
|
|
|
|
|
|
await restServer.stop()
|
|
|
|
await restServer.closeWait()
|
|
|
|
await node.stop()
|