diff --git a/tests/http-api.spec.ts b/tests/http-api.spec.ts index c19279e..3b75ddc 100644 --- a/tests/http-api.spec.ts +++ b/tests/http-api.spec.ts @@ -58,8 +58,11 @@ describe('http api', function () { dwn = testdwn.dwn; tenantGate = testdwn.tenantGate; - profile = await createProfile(); httpApi = new HttpApi(dwn, tenantGate); + + await tenantGate.initialize(); + profile = await createProfile(); + tenantGate.authorizeTenant(profile.did); }); beforeEach(async function () { @@ -105,13 +108,14 @@ describe('http api', function () { response = generateNonce(5); } + const p = await createProfile(); const submitResponse = await fetch('http://localhost:3000/register/pow', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ challenge: body.challenge, response: response, - did: profile.did, + did: p.did, }), }); @@ -139,13 +143,14 @@ describe('http api', function () { response = generateNonce(5); } + const p = await createProfile(); const submitResponse = await fetch('http://localhost:3000/register/pow', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ challenge: body.challenge, response: response, - did: profile.did, + did: p.did, }), }); @@ -154,67 +159,61 @@ describe('http api', function () { it('increase complexity as more challenges are completed', async function () { for (let i = 1; i <= 60; i++) { - const p = await createProfile(); - if (i < 59) { - tenantGate.authorizeTenant(p.did); - continue; - } + tenantGate.authorizeTenant((await createProfile()).did); + } - const challengeResponse = await fetch( - 'http://localhost:3000/register/pow', - ); - expect(challengeResponse.status).to.equal(200); - const body = (await challengeResponse.json()) as { - challenge: string; - complexity: number; - }; - expect(body.challenge.length).to.equal(16); - - // solve the challenge - let response = ''; - let iterations = 0; - const start = Date.now(); - while (!checkNonce(body.challenge, response, body.complexity)) { - response = generateNonce(5); - iterations++; - if (iterations % 10000000 == 0) { - console.log( - 'complexity:', - body.complexity, - 'iteration count:', - iterations, - 'duration:', - Date.now() - start, - 'ms', - ); - } + const p = await createProfile(); + const challengeResponse = await fetch( + 'http://localhost:3000/register/pow', + ); + expect(challengeResponse.status).to.equal(200); + const body = (await challengeResponse.json()) as { + challenge: string; + complexity: number; + }; + expect(body.challenge.length).to.equal(16); + + // solve the challenge + let response = ''; + let iterations = 0; + const start = Date.now(); + while (!checkNonce(body.challenge, response, body.complexity)) { + response = generateNonce(5); + iterations++; + if (iterations % 10000000 == 0) { + console.log( + 'complexity:', + body.complexity, + 'iteration count:', + iterations, + 'duration:', + Date.now() - start, + 'ms', + ); } + } - console.log( - 'complexity:', - body.complexity, - 'iteration count:', - iterations, - 'duration:', - Date.now() - start, - 'ms', - ); - - const submitResponse = await fetch( - 'http://localhost:3000/register/pow', - { - method: 'POST', - headers: { 'Content-Type': 'application/json' }, - body: JSON.stringify({ - challenge: body.challenge, - response: response, - did: p.did, - }), - }, - ); + console.log( + 'complexity:', + body.complexity, + 'iteration count:', + iterations, + 'duration:', + Date.now() - start, + 'ms', + ); - expect(submitResponse.status).to.equal(200); - } + const submitResponse = await fetch('http://localhost:3000/register/pow', { + method: 'POST', + headers: { 'Content-Type': 'application/json' }, + body: JSON.stringify({ + challenge: body.challenge, + response: response, + did: p.did, + }), + }); + + expect(submitResponse.status).to.equal(200); }).timeout(120000); it('rejects an invalid nonce', async function () { @@ -236,13 +235,14 @@ describe('http api', function () { response = generateNonce(5); } + const p = await createProfile(); const submitResponse = await fetch('http://localhost:3000/register/pow', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ challenge: body.challenge, response: response, - did: profile.did, + did: p.did, }), }); @@ -258,13 +258,14 @@ describe('http api', function () { response = generateNonce(5); } + const p = await createProfile(); const submitResponse = await fetch('http://localhost:3000/register/pow', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ challenge: challenge, response: response, - did: profile.did, + did: p.did, }), }); @@ -496,6 +497,7 @@ describe('http api', function () { expect(body.error).to.not.exist; const { reply } = body.result; + console.log(reply); expect(reply.status.code).to.equal(202); });