Skip to content

Commit

Permalink
Merge pull request #14 from adobe/test_multi_auth
Browse files Browse the repository at this point in the history
Add a test case for multi auth with anon user
  • Loading branch information
karlpauls authored Feb 7, 2024
2 parents a251184 + dd81a97 commit a378336
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/utils/auth.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ export async function getUsers(req, env) {
// If we have an empty token there was an anon user in the session
if (!token || token.trim().length === 0) {
users.push({ email: 'anonymous' });
// eslint-disable-next-line no-continue
continue;
}
const { user_id: userId, created_at: createdAt, expires_in: expiresIn } = decodeJwt(token);
const expires = Number(createdAt) + Number(expiresIn);
Expand Down
6 changes: 6 additions & 0 deletions test/utils/auth.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,12 @@ describe('Dark Alley auth', () => {
assert.strictEqual(users[0].email, '[email protected]');
});

it('authorized with user if email matches and anonymous if present', async () => {
const users = await getUsers(reqs.siteMulti, env);
assert.strictEqual(users[0].email, 'anonymous')
assert.strictEqual(users[1].email, '[email protected]');
});

it('anonymous if ims fails', async () => {
const users = await getUsers(reqs.media, env);
assert.strictEqual(users[0].email, 'anonymous');
Expand Down
7 changes: 7 additions & 0 deletions test/utils/mocks/req.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,12 @@ const optsWithExpAuth = {
}),
};

const optsWithMultiAuthAnon = {
headers: new Headers({
'Authorization': `,Bearer [email protected]`
}),
};

const optsWithForceFail = {
headers: new Headers({
'x-mock-fail': true,
Expand All @@ -44,6 +50,7 @@ const reqs = {
folder: new Request('https://da.live/source/cq/Geometrixx/NFT/', optsWithExpAuth),
file: new Request('https://da.live/source/cq/Geometrixx/NFT/Outreach.html', optsWithEmptyBearer),
media: new Request('https://da.live/source/cq/Geometrixx/NFT/blockchain.png', optsWithForceFail),
siteMulti: new Request('https://da.live/source/cq/Geometrixx', optsWithMultiAuthAnon),
};

export default reqs;

0 comments on commit a378336

Please sign in to comment.