Skip to content

Commit

Permalink
sort out profiles in http-api tests
Browse files Browse the repository at this point in the history
  • Loading branch information
finn-block committed Nov 17, 2023
1 parent 367a7aa commit 5764b43
Showing 1 changed file with 64 additions and 62 deletions.
126 changes: 64 additions & 62 deletions tests/http-api.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 () {
Expand Down Expand Up @@ -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,
}),
});

Expand Down Expand Up @@ -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,
}),
});

Expand All @@ -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 () {
Expand All @@ -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,
}),
});

Expand All @@ -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,
}),
});

Expand Down Expand Up @@ -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);
});

Expand Down

0 comments on commit 5764b43

Please sign in to comment.