From 433d00d7fc88711fcb65ca8e1f711c861ab265b2 Mon Sep 17 00:00:00 2001 From: Emil Ivanichkov Date: Wed, 13 Mar 2024 18:15:22 +0200 Subject: [PATCH] test(waku api): Add tests for waku api --- libs/waku_utils/waku_pair.nim | 3 +- src/status_node_manager.nim | 4 +- tests/test1.nim | 8 ---- tests/test_waku_api.nim | 72 +++++++++++++++++++++++++++++++++++ 4 files changed, 75 insertions(+), 12 deletions(-) delete mode 100644 tests/test1.nim create mode 100644 tests/test_waku_api.nim diff --git a/libs/waku_utils/waku_pair.nim b/libs/waku_utils/waku_pair.nim index e74382b..d1c037c 100644 --- a/libs/waku_utils/waku_pair.nim +++ b/libs/waku_utils/waku_pair.nim @@ -13,8 +13,7 @@ import waku/common/logging, # Local modules - ./waku_handshake_utils, - ./waku_node + ./waku_handshake_utils type WakuPairResult* = object wakuNode*: WakuNode diff --git a/src/status_node_manager.nim b/src/status_node_manager.nim index 2c292e5..7de9ee0 100644 --- a/src/status_node_manager.nim +++ b/src/status_node_manager.nim @@ -63,8 +63,8 @@ proc run(snm: SNM) {.async.} = proc setupLogLevel*(level: LogLevel) = topics_registry.setLogLevel(level) -proc doRunStatusNodeManager(config: StatusNodeManagerConfig, - rng: ref HmacDrbgContext) = +proc doRunStatusNodeManager*(config: StatusNodeManagerConfig, + rng: ref HmacDrbgContext) = notice "Starting Status Node Manager" let snm = waitFor SNM.init(rng, config) diff --git a/tests/test1.nim b/tests/test1.nim deleted file mode 100644 index 7707c5f..0000000 --- a/tests/test1.nim +++ /dev/null @@ -1,8 +0,0 @@ -# This is just an example to get you started. You may wish to put all of your -# tests into a single file, or separate them into multiple `test1`, `test2` -# etc. files (better names are recommended, just make sure the name starts with -# the letter 't'). -# -# To run these tests, simply execute `nimble test`. - -import unittest diff --git a/tests/test_waku_api.nim b/tests/test_waku_api.nim new file mode 100644 index 0000000..5adeeda --- /dev/null +++ b/tests/test_waku_api.nim @@ -0,0 +1,72 @@ +import + std/json, + + confutils, chronos, + libp2p/crypto/crypto, + presto/[route, segpath, server, client], + serialization, json_serialization, + sequtils, + stew/byteutils, + testutils/unittests, + # unittest, + + ../src/status_node_manager, + ../src/status_node_manager/[config, utils, waku], + ../src/status_node_manager/status_node_manager_status, + ../src/status_node_manager/rest/common, + ../src/status_node_manager/rest/apis/waku/[ + rest_waku_api, + rest_waku_calls, + types + ] + +const wakuApiPort = 13100 + +proc startSNM(rng: ref HmacDrbgContext) = + let conf = try: StatusNodeManagerConfig.load(cmdLine = mapIt([ + "--rest=true", + "--rest-address=127.0.0.1", + "--rest-port=" & $wakuApiPort, + ], it)) + except: + quit 1 + + doRunStatusNodeManager(conf, rng) + +proc runTest(wakuApiPort: Port) {.async.} = + let client = RestClientRef.new(initTAddress("127.0.0.1", wakuApiPort)) + + suite "Waku API tests": + const testRequestData = WakuPairRequestData( + qr: "testQr", + qrMessageNameTag: "testQrMessageNameTag", + pubSubTopic: "testPubSubTopic", + ) + + asyncTest "Server side is not complete, so request with correct data, it should return 500 error.": + let + response = await client.wakuPairPlain(testRequestData) + responseJson = Json.decode(response.data, JsonNode) + check: + response.status == 500 + responseJson["message"].getStr() == "Internal Server Error" + +proc delayedTests() {.async.} = + while snmStatus != SNMStatus.Running: + await sleepAsync(1.seconds) + + await sleepAsync(2.seconds) + + let deadline = sleepAsync(10.minutes) + await runTest(Port(wakuApiPort)) or deadline + + snmStatus = SNMStatus.Stopping + +proc main() {.async.} = + let rng = HmacDrbgContext.new() + + asyncSpawn delayedTests() + + startSNM(rng) + +waitFor main()