11import { describe , expect , it } from 'vitest' ;
22import { 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
78describe ( '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