Skip to content

Commit 28fb718

Browse files
authored
fix test coverage (#156)
1 parent 78a68cf commit 28fb718

File tree

2 files changed

+27
-20
lines changed

2 files changed

+27
-20
lines changed

test/storage/object/delete.test.js

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,7 @@
1212
import assert from 'node:assert';
1313
import esmock from 'esmock';
1414
import { mockClient } from 'aws-sdk-client-mock';
15-
import { DeleteObjectCommand, ListObjectsV2Command, S3Client } from '@aws-sdk/client-s3';
16-
import { getSignedUrl } from '@aws-sdk/s3-request-presigner';
15+
import { ListObjectsV2Command, S3Client } from '@aws-sdk/client-s3';
1716

1817
const s3Mock = mockClient(S3Client);
1918

@@ -252,11 +251,6 @@ describe('Object delete', () => {
252251
getSignedUrl: mockSignedUrl,
253252
}
254253
},
255-
{
256-
import: {
257-
fetch: async () => ({ status: 200 }),
258-
}
259-
}
260254
);
261255
s3Mock.on(ListObjectsV2Command).resolves({ Contents: [{ Key: 'foo/bar.html' }] });
262256
const resp = await deleteObjects(env, daCtx, {});
@@ -287,11 +281,6 @@ describe('Object delete', () => {
287281
getSignedUrl: mockSignedUrl,
288282
}
289283
},
290-
{
291-
import: {
292-
fetch: async () => ({ status: 200 }),
293-
}
294-
}
295284
);
296285
s3Mock.on(ListObjectsV2Command).resolves({ Contents: [{ Key: 'foo/bar.html' }], NextContinuationToken: 'token' });
297286
const resp = await deleteObjects(env, daCtx, {});

test/utils/auth.test.js

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,17 @@ import {
2929
const {
3030
setUser,
3131
getUsers,
32-
} = await esmock('../../src/utils/auth.js', { jose, import: { fetch } });
32+
} = await esmock('../../src/utils/auth.js', { jose });
33+
34+
async function withMockedFetch(act) {
35+
const savedFetch = globalThis.fetch;
36+
globalThis.fetch = fetch;
37+
try {
38+
await act();
39+
} finally {
40+
globalThis.fetch = savedFetch;
41+
}
42+
}
3343

3444
describe('DA auth', () => {
3545
describe('get user', async () => {
@@ -49,14 +59,18 @@ describe('DA auth', () => {
4959
});
5060

5161
it('authorized if email matches', async () => {
52-
const users = await getUsers(reqs.site, env);
53-
assert.strictEqual(users[0].email, 'aparker@geometrixx.info');
62+
await withMockedFetch(async () => {
63+
const users = await getUsers(reqs.site, env);
64+
assert.strictEqual(users[0].email, 'aparker@geometrixx.info');
65+
});
5466
});
5567

5668
it('authorized with user if email matches and anonymous if present', async () => {
57-
const users = await getUsers(reqs.siteMulti, env);
58-
assert.strictEqual(users[0].email, 'anonymous')
59-
assert.strictEqual(users[1].email, 'aparker@geometrixx.info');
69+
await withMockedFetch(async () => {
70+
const users = await getUsers(reqs.siteMulti, env);
71+
assert.strictEqual(users[0].email, 'anonymous')
72+
assert.strictEqual(users[1].email, 'aparker@geometrixx.info');
73+
});
6074
});
6175

6276
it('anonymous if ims fails', async () => {
@@ -71,8 +85,12 @@ describe('DA auth', () => {
7185
'Authorization': `Bearer aparker@geometrixx.info`,
7286
});
7387

74-
const userValStr = await setUser('aparker@geometrixx.info', 100, headers, env);
75-
const userValue = JSON.parse(userValStr);
88+
let userValue;
89+
90+
await withMockedFetch(async () => {
91+
const userValStr = await setUser('aparker@geometrixx.info', 100, headers, env);
92+
userValue = JSON.parse(userValStr);
93+
});
7694

7795
assert.strictEqual('aparker@geometrixx.info', userValue.email);
7896
assert.strictEqual('123', userValue.ident);

0 commit comments

Comments
 (0)