Skip to content

Commit

Permalink
fix: EventResult should await OK response (WebOfTrust#299)
Browse files Browse the repository at this point in the history
* fix: EventResult should await OK response

* refactor: pretty run

* test: ensure rejected response is caught
  • Loading branch information
iFergal authored Jan 7, 2025
1 parent 332a46b commit cddb007
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 11 deletions.
17 changes: 6 additions & 11 deletions src/keri/app/aiding.ts
Original file line number Diff line number Diff line change
Expand Up @@ -266,7 +266,7 @@ export class Identifier {
jsondata[algo] = keeper.params();

this.client.pidx = this.client.pidx + 1;
const res = this.client.fetch('/identifiers', 'POST', jsondata);
const res = await this.client.fetch('/identifiers', 'POST', jsondata);
return new EventResult(serder, sigs, res);
}

Expand Down Expand Up @@ -447,7 +447,7 @@ export class Identifier {
sigs: sigs,
};

const res = this.client.fetch(
const res = await this.client.fetch(
'/identifiers/' + name + '/endroles',
'POST',
jsondata
Expand Down Expand Up @@ -500,16 +500,12 @@ export class Identifier {
export class EventResult {
private readonly _serder: Serder;
private readonly _sigs: string[];
private readonly promise: Promise<Response> | Response;
private readonly response: Response;

constructor(
serder: Serder,
sigs: string[],
promise: Promise<Response> | Response
) {
constructor(serder: Serder, sigs: string[], response: Response) {
this._serder = serder;
this._sigs = sigs;
this.promise = promise;
this.response = response;
}

get serder() {
Expand All @@ -521,7 +517,6 @@ export class EventResult {
}

async op(): Promise<any> {
const res = await this.promise;
return await res.json();
return await this.response.json();
}
}
18 changes: 18 additions & 0 deletions test/app/aiding.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,16 @@ describe('Aiding', () => {
assert.deepEqual(lastCall.body.salty.transferable, true);
});

it('Should throw error if fetch call fails when creating identifier', async () => {
const error = new Error(`Fail ${randomUUID()}`);
client.fetch.mockRejectedValue(error);
await expect(
client
.identifiers()
.create('aid1', { bran: '0123456789abcdefghijk' })
).rejects.toThrow(error);
});

it('Can rotate salty identifier', async () => {
const aid1 = await createMockIdentifierState('aid1', bran, {});
client.fetch.mockResolvedValueOnce(Response.json(aid1));
Expand Down Expand Up @@ -346,6 +356,14 @@ describe('Aiding', () => {
});
});

it('Should throw error if fetch call fails when adding end role', async () => {
const error = new Error(`Fail ${randomUUID()}`);
client.fetch.mockRejectedValue(error);
await expect(
client.identifiers().addEndRole('aid1', 'agent')
).rejects.toThrow(error);
});

it('Can get members', async () => {
client.fetch.mockResolvedValue(Response.json({}));
await client.identifiers().members('aid1');
Expand Down

0 comments on commit cddb007

Please sign in to comment.