Skip to content

Commit f5fdec4

Browse files
committed
fix: make email allowlist test self-contained
Avoid test-only dependence on Worker globals and runtime alias class identity.
1 parent 61bc5f7 commit f5fdec4

1 file changed

Lines changed: 8 additions & 7 deletions

File tree

worker/utils/enforceAllowedEmail.test.ts

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
import { describe, expect, it } from 'vitest';
22
import { enforceAllowedEmail } from './authUtils';
3-
import { SecurityError } from 'shared/types/errors';
3+
type AuthEnv = Parameters<typeof enforceAllowedEmail>[0];
4+
type SecurityErrorLike = { statusCode: number; message: string };
45

5-
const envWith = (allowed: string) => ({ ALLOWED_EMAIL: allowed }) as unknown as Env;
6+
const envWith = (allowed: string) => ({ ALLOWED_EMAIL: allowed }) as AuthEnv;
67

78
describe('enforceAllowedEmail', () => {
89
it('is a no-op when ALLOWED_EMAIL is unset', () => {
9-
expect(() => enforceAllowedEmail({} as Env, 'anyone@example.com', 'oauth')).not.toThrow();
10+
expect(() => enforceAllowedEmail({} as AuthEnv, 'anyone@example.com', 'oauth')).not.toThrow();
1011
});
1112

1213
it('is a no-op when ALLOWED_EMAIL is empty', () => {
@@ -30,9 +31,9 @@ describe('enforceAllowedEmail', () => {
3031
enforceAllowedEmail(envWith('staff@cloudflare.com'), 'attacker@example.com', 'oauth');
3132
throw new Error('expected enforceAllowedEmail to throw');
3233
} catch (error) {
33-
expect(error).toBeInstanceOf(SecurityError);
34-
expect((error as SecurityError).statusCode).toBe(403);
35-
expect((error as SecurityError).message).toContain('to oauth');
34+
expect(error).toMatchObject({ name: 'SecurityError', statusCode: 403 });
35+
expect((error as SecurityErrorLike).statusCode).toBe(403);
36+
expect((error as SecurityErrorLike).message).toContain('to oauth');
3637
}
3738
});
3839

@@ -41,7 +42,7 @@ describe('enforceAllowedEmail', () => {
4142
enforceAllowedEmail(envWith('staff@cloudflare.com'), 'attacker@example.com', 'register');
4243
throw new Error('expected enforceAllowedEmail to throw');
4344
} catch (error) {
44-
expect((error as SecurityError).message).toContain('to register');
45+
expect((error as SecurityErrorLike).message).toContain('to register');
4546
}
4647
});
4748
});

0 commit comments

Comments
 (0)