Skip to content

Fix api validation test #1201

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
6 changes: 3 additions & 3 deletions tests/contracts/testMarket.nim
Original file line number Diff line number Diff line change
Expand Up @@ -541,14 +541,14 @@ ethersuite "On-Chain Market":
(await market.queryPastStorageRequestedEvents(blocksAgo = 2))
)

test "pays rewards and collateral to host":
test "pays rewards and returns collateral to host":
await market.requestStorage(request)

let address = await host.getAddress()
switchAccount(host)
await market.reserveSlot(request.id, 0.uint64)
await market.fillSlot(request.id, 0.uint64, proof, request.ask.collateralPerSlot)
let filledAt = (await ethProvider.currentTime())
let filledAt = await ethProvider.blockTime(BlockTag.latest)

for slotIndex in 1 ..< request.ask.slots:
await market.reserveSlot(request.id, slotIndex.uint64)
Expand Down Expand Up @@ -629,7 +629,7 @@ ethersuite "On-Chain Market":
check collateral ==
request.ask.collateralPerSlot - (request.ask.collateralPerSlot * 10).div(100.u256)

test "the request is added in cache after the fist access":
test "the request is added to cache after the first access":
await market.requestStorage(request)

check market.requestCache.contains($request.id) == false
Expand Down
1 change: 0 additions & 1 deletion tests/integration/codexclient.nim
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import std/httpclient
import std/strutils

from pkg/libp2p import Cid, `$`, init
Expand Down
81 changes: 28 additions & 53 deletions tests/integration/testrestapivalidation.nim
Original file line number Diff line number Diff line change
@@ -1,45 +1,23 @@
import std/httpclient
import std/times
import pkg/ethers
import pkg/codex/manifest
import pkg/codex/conf
import pkg/codex/contracts
from pkg/codex/stores/repostore/types import DefaultQuotaBytes
import ../asynctest
import ../checktest
import ../examples
import ../codex/examples
import ./codexconfig
import ./codexprocess
import ./codexclient
import ./multinodes

from ./multinodes import Role, getTempDirName, jsonRpcProviderUrl, nextFreePort
multinodesuite "Rest API validation":
let config = NodeConfigs(clients: CodexConfigs.init(nodes = 1).some)
var client: CodexClient

# This suite allows to run fast the basic rest api validation.
# It starts only one node for all the checks in order to speed up
# the execution.
asyncchecksuite "Rest API validation":
var node: CodexProcess
var config = CodexConfigs.init(nodes = 1).configs[0]
let starttime = now().format("yyyy-MM-dd'_'HH:mm:ss")
let nodexIdx = 0
let datadir = getTempDirName(starttime, Role.Client, nodexIdx)
setup:
client = clients()[0].client

config.addCliOption("--api-port", $(waitFor nextFreePort(8081)))
config.addCliOption("--data-dir", datadir)
config.addCliOption("--nat", "none")
config.addCliOption("--listen-addrs", "/ip4/127.0.0.1/tcp/0")
config.addCliOption("--disc-port", $(waitFor nextFreePort(8081)))
config.addCliOption(StartUpCmd.persistence, "--eth-provider", jsonRpcProviderUrl)
config.addCliOption(StartUpCmd.persistence, "--eth-account", $EthAddress.example)

node =
waitFor CodexProcess.startNode(config.cliArgs, config.debugEnabled, $Role.Client)

waitFor node.waitUntilStarted()

let client = node.client()

test "should return 422 when attempting delete of non-existing dataset":
test "should return 422 when attempting delete of non-existing dataset", config:
let data = await RandomChunker.example(blocks = 2)
let cid = (await client.upload(data)).get
let duration = 100.uint64
Expand All @@ -58,7 +36,7 @@ asyncchecksuite "Rest API validation":
check responseBefore.status == 422
check (await responseBefore.body) == "Tolerance needs to be bigger then zero"

test "request storage fails for datasets that are too small":
test "request storage fails for datasets that are too small", config:
let cid = (await client.upload("some file contents")).get
let response = (
await client.requestStorageRaw(
Expand All @@ -77,7 +55,7 @@ asyncchecksuite "Rest API validation":
"Dataset too small for erasure parameters, need at least " &
$(2 * DefaultBlockSize.int) & " bytes"

test "request storage fails if nodes and tolerance aren't correct":
test "request storage fails if nodes and tolerance aren't correct", config:
let data = await RandomChunker.example(blocks = 2)
let cid = (await client.upload(data)).get
let duration = 100.uint64
Expand All @@ -101,7 +79,7 @@ asyncchecksuite "Rest API validation":
check (await responseBefore.body) ==
"Invalid parameters: parameters must satify `1 < (nodes - tolerance) ≥ tolerance`"

test "request storage fails if tolerance > nodes (underflow protection)":
test "request storage fails if tolerance > nodes (underflow protection)", config:
let data = await RandomChunker.example(blocks = 2)
let cid = (await client.upload(data)).get
let duration = 100.uint64
Expand All @@ -122,21 +100,21 @@ asyncchecksuite "Rest API validation":
check responseBefore.status == 422
check (await responseBefore.body) == "Tolerance needs to be bigger then zero"

test "upload fails if content disposition contains bad filename":
test "upload fails if content disposition contains bad filename", config:
let headers = @[("Content-Disposition", "attachment; filename=\"exam*ple.txt\"")]
let response = await client.uploadRaw("some file contents", headers)

check response.status == 422
check (await response.body) == "The filename is not valid."

test "upload fails if content type is invalid":
test "upload fails if content type is invalid", config:
let headers = @[("Content-Type", "hello/world")]
let response = await client.uploadRaw("some file contents", headers)

check response.status == 422
check (await response.body) == "The MIME type 'hello/world' is not valid."

test "updating non-existing availability":
test "updating non-existing availability", config:
let nonExistingResponse = await client.patchAvailabilityRaw(
AvailabilityId.example,
duration = 100.uint64.some,
Expand All @@ -145,7 +123,7 @@ asyncchecksuite "Rest API validation":
)
check nonExistingResponse.status == 404

test "updating availability - freeSize is not allowed to be changed":
test "updating availability - freeSize is not allowed to be changed", config:
let availability = (
await client.postAvailability(
totalSize = 140000.uint64,
Expand All @@ -159,7 +137,7 @@ asyncchecksuite "Rest API validation":
check freeSizeResponse.status == 422
check "not allowed" in (await freeSizeResponse.body)

test "creating availability above the node quota returns 422":
test "creating availability above the node quota returns 422", config:
let response = await client.postAvailabilityRaw(
totalSize = 24000000000.uint64,
duration = 200.uint64,
Expand All @@ -170,7 +148,7 @@ asyncchecksuite "Rest API validation":
check response.status == 422
check (await response.body) == "Not enough storage quota"

test "updating availability above the node quota returns 422":
test "updating availability above the node quota returns 422", config:
let availability = (
await client.postAvailability(
totalSize = 140000.uint64,
Expand All @@ -186,7 +164,7 @@ asyncchecksuite "Rest API validation":
check response.status == 422
check (await response.body) == "Not enough storage quota"

test "creating availability when total size is zero returns 422":
test "creating availability when total size is zero returns 422", config:
let response = await client.postAvailabilityRaw(
totalSize = 0.uint64,
duration = 200.uint64,
Expand All @@ -197,7 +175,7 @@ asyncchecksuite "Rest API validation":
check response.status == 422
check (await response.body) == "Total size must be larger then zero"

test "updating availability when total size is zero returns 422":
test "updating availability when total size is zero returns 422", config:
let availability = (
await client.postAvailability(
totalSize = 140000.uint64,
Expand All @@ -212,7 +190,7 @@ asyncchecksuite "Rest API validation":
check response.status == 422
check (await response.body) == "Total size must be larger then zero"

test "creating availability when total size is negative returns 422":
test "creating availability when total size is negative returns 422", config:
let json =
%*{
"totalSize": "-1",
Expand All @@ -225,7 +203,7 @@ asyncchecksuite "Rest API validation":
check response.status == 400
check (await response.body) == "Parsed integer outside of valid range"

test "updating availability when total size is negative returns 422":
test "updating availability when total size is negative returns 422", config:
let availability = (
await client.postAvailability(
totalSize = 140000.uint64,
Expand All @@ -243,7 +221,7 @@ asyncchecksuite "Rest API validation":
check response.status == 400
check (await response.body) == "Parsed integer outside of valid range"

test "request storage fails if tolerance is zero":
test "request storage fails if tolerance is zero", config:
let data = await RandomChunker.example(blocks = 2)
let cid = (await client.upload(data)).get
let duration = 100.uint64
Expand All @@ -264,7 +242,7 @@ asyncchecksuite "Rest API validation":
check responseBefore.status == 422
check (await responseBefore.body) == "Tolerance needs to be bigger then zero"

test "request storage fails if duration exceeds limit":
test "request storage fails if duration exceeds limit", config:
let data = await RandomChunker.example(blocks = 2)
let cid = (await client.upload(data)).get
let duration = (31 * 24 * 60 * 60).uint64
Expand All @@ -286,7 +264,7 @@ asyncchecksuite "Rest API validation":
check responseBefore.status == 422
check "Duration exceeds limit of" in (await responseBefore.body)

test "request storage fails if expiry is zero":
test "request storage fails if expiry is zero", config:
let data = await RandomChunker.example(blocks = 2)
let cid = (await client.upload(data)).get
let duration = 100.uint64
Expand All @@ -306,7 +284,7 @@ asyncchecksuite "Rest API validation":
check (await responseBefore.body) ==
"Expiry must be greater than zero and less than the request's duration"

test "request storage fails if proof probability is zero":
test "request storage fails if proof probability is zero", config:
let data = await RandomChunker.example(blocks = 2)
let cid = (await client.upload(data)).get
let duration = 100.uint64
Expand All @@ -325,7 +303,7 @@ asyncchecksuite "Rest API validation":
check responseBefore.status == 422
check (await responseBefore.body) == "Proof probability must be greater than zero"

test "request storage fails if price per byte per second is zero":
test "request storage fails if price per byte per second is zero", config:
let data = await RandomChunker.example(blocks = 2)
let cid = (await client.upload(data)).get
let duration = 100.uint64
Expand All @@ -345,7 +323,7 @@ asyncchecksuite "Rest API validation":
check (await responseBefore.body) ==
"Price per byte per second must be greater than zero"

test "request storage fails if collareral per byte is zero":
test "request storage fails if collareral per byte is zero", config:
let data = await RandomChunker.example(blocks = 2)
let cid = (await client.upload(data)).get
let duration = 100.uint64
Expand All @@ -364,7 +342,7 @@ asyncchecksuite "Rest API validation":
check responseBefore.status == 422
check (await responseBefore.body) == "Collateral per byte must be greater than zero"

test "creating availability fails when until is negative":
test "creating availability fails when until is negative", config:
let totalSize = 12.uint64
let minPricePerBytePerSecond = 1.u256
let totalCollateral = totalSize.u256 * minPricePerBytePerSecond
Expand All @@ -379,6 +357,3 @@ asyncchecksuite "Rest API validation":
check:
response.status == 422
(await response.body) == "Cannot set until to a negative value"

waitFor node.stop()
node.removeDataDir()