Skip to content

Commit

Permalink
feat: more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
guru-web3 committed Jan 3, 2024
1 parent 3f0b499 commit 39c9cf8
Showing 1 changed file with 52 additions and 0 deletions.
52 changes: 52 additions & 0 deletions test/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -315,6 +315,7 @@ describe("API-calls", function () {
assert.strictEqual(releaseStatus, 2);
});
});

describe("/get_or_set_nonce", function () {
let privKey;
before(function () {
Expand Down Expand Up @@ -347,6 +348,32 @@ describe("API-calls", function () {
}
});

it("#it should reject if the timestamp is missing and signature is present", async function () {
const msg = "getOrSetNonce";
const data = "getOrSetNonce";
const metadataParams = generateGetOrSetNonceParams(msg, data, privKey);
metadataParams.set_data.timestamp = "";
try {
await post(`${server}/get_or_set_nonce`, metadataParams);
} catch (err) {
const val = await err.json();
assert.deepStrictEqual(val.statusCode, 400);
assert.deepStrictEqual(val.message, "Validation failed");
}
});

it("#it should reject if the timestamp is old", async function () {
const msg = "getOrSetNonce";
const data = "getOrSetNonce";
const metadataParams = generateGetOrSetNonceParams(msg, data, privKey);
metadataParams.set_data.timestamp = new BN(~~(Date.now() / 1000) - 95).toString(16); // set old timestimpe
try {
await post(`${server}/get_or_set_nonce`, metadataParams);
} catch (err) {
const val = await err.json();
assert.deepStrictEqual(val.error.timestamp, "Message has been signed more than 90s ago");
}
});
it("#it should set new nonce for new user with ed25519 key, when validation is correct", async function () {
const msg = "getOrSetNonce";
const data = "getOrSetNonce";
Expand All @@ -361,5 +388,30 @@ describe("API-calls", function () {
console.log({ val });
}
});

it("#it should get existing nonce for user when validation is correct", async function () {
const msg = "getOrSetNonce";
const data = "getOrSetNonce";
const metadataParams = generateGetOrSetNonceParams(msg, data, privKey);

// set nonce
const setResult = await post(`${server}/get_or_set_nonce`, metadataParams);
assert.isString(setResult.nonce);
assert.isObject(setResult.pubNonce);

// get nonce
const getResult = await post(`${server}/get_or_set_nonce`, metadataParams);
assert.equal(getResult.nonce, setResult.nonce);
assert.deepEqual(getResult.pubNonce, setResult.pubNonce);
});

it("#it should get set custom passed nonce for user when validation is correct", async function () {
const msg = "getOrSetNonce";
const data = "getOrSetNonce";
const metadataParams = generateGetOrSetNonceParams(msg, data, privKey);
const setResult = await post(`${server}/get_or_set_nonce`, metadataParams);
assert.isString(setResult.nonce);
assert.isObject(setResult.pubNonce);
});
});
});

0 comments on commit 39c9cf8

Please sign in to comment.