@@ -30,12 +30,11 @@ import { expectedResponseHeaders, MockRequest } from "../../fixtures/mockrequest
3030import { runHandler } from "../../helper" ;
3131import { FULL_ENDPOINT , MINIMAL_V2_ENDPOINT , FULL_OPTIONS , FULL_TRIGGER } from "./fixtures" ;
3232import { onInit } from "../../../src/v2/core" ;
33- import { Handler } from "express" ;
3433import { genkit } from "genkit" ;
3534
3635function request ( args : {
3736 data ?: any ;
38- auth ?: Record < string , string > ;
37+ auth ?: Record < string , any > ;
3938 headers ?: Record < string , string > ;
4039 method ?: MockRequest [ "method" ] ;
4140} ) : any {
@@ -526,34 +525,92 @@ describe("onCall", () => {
526525 expect ( anonResp . status ) . to . equal ( 403 ) ;
527526 } ) ;
528527
529- it ( "should check hasClaim" , async ( ) => {
530- const anyValue = https . onCall (
531- {
532- authPolicy : https . hasClaim ( "meaning" ) ,
533- } ,
534- ( ) => "HHGTTG"
535- ) ;
536- const specificValue = https . onCall (
537- {
538- authPolicy : https . hasClaim ( "meaning" , "42" ) ,
539- } ,
540- ( ) => "HHGTG"
541- ) ;
542-
543- const cases : Array < { fn : Handler ; auth ?: Record < string , string > ; status : number } > = [
544- { fn : anyValue , auth : { meaning : "42" } , status : 200 } ,
545- { fn : anyValue , auth : { meaning : "43" } , status : 200 } ,
546- { fn : anyValue , auth : { order : "66" } , status : 403 } ,
547- { fn : anyValue , status : 403 } ,
548- { fn : specificValue , auth : { meaning : "42" } , status : 200 } ,
549- { fn : specificValue , auth : { meaning : "43" } , status : 403 } ,
550- { fn : specificValue , auth : { order : "66" } , status : 403 } ,
551- { fn : specificValue , status : 403 } ,
552- ] ;
553- for ( const test of cases ) {
554- const resp = await runHandler ( test . fn , request ( { auth : test . auth } ) ) ;
555- expect ( resp . status ) . to . equal ( test . status ) ;
556- }
528+ describe ( "hasClaim" , ( ) => {
529+ it ( "should check single claim with specific value" , async ( ) => {
530+ const func = https . onCall (
531+ {
532+ authPolicy : https . hasClaim ( "meaning" , "42" ) ,
533+ } ,
534+ ( ) => true
535+ ) ;
536+ const validResp = await runHandler ( func , request ( { auth : { meaning : "42" } } ) ) ;
537+ expect ( validResp . status ) . to . equal ( 200 ) ;
538+
539+ const noClaimResp = await runHandler ( func , request ( { auth : { } } ) ) ;
540+ expect ( noClaimResp . status ) . to . equal ( 403 ) ;
541+ } ) ;
542+
543+ it ( "should check single claim with default value (true)" , async ( ) => {
544+ const func = https . onCall (
545+ {
546+ authPolicy : https . hasClaim ( "admin" ) ,
547+ } ,
548+ ( ) => true
549+ ) ;
550+ const validResp = await runHandler ( func , request ( { auth : { admin : true } } ) ) ;
551+ expect ( validResp . status ) . to . equal ( 200 ) ;
552+
553+ const wrongTypeResp = await runHandler ( func , request ( { auth : { admin : "true" } } ) ) ;
554+ expect ( wrongTypeResp . status ) . to . equal ( 403 ) ;
555+
556+ const falseResp = await runHandler ( func , request ( { auth : { admin : false } } ) ) ;
557+ expect ( falseResp . status ) . to . equal ( 403 ) ;
558+
559+ const noClaimResp = await runHandler ( func , request ( { auth : { } } ) ) ;
560+ expect ( noClaimResp . status ) . to . equal ( 403 ) ;
561+ } ) ;
562+
563+ it ( "should check multiple claims with default value (true)" , async ( ) => {
564+ const func = https . onCall (
565+ {
566+ authPolicy : https . hasClaim ( [ "pro" , "eap" ] ) ,
567+ } ,
568+ ( ) => true
569+ ) ;
570+ const validResp = await runHandler ( func , request ( { auth : { pro : true , eap : true } } ) ) ;
571+ expect ( validResp . status ) . to . equal ( 200 ) ;
572+
573+ const missingResp = await runHandler ( func , request ( { auth : { pro : true } } ) ) ;
574+ expect ( missingResp . status ) . to . equal ( 403 ) ;
575+
576+ const wrongTypeResp = await runHandler (
577+ func ,
578+ request ( { auth : { pro : "true" , eap : "true" } } )
579+ ) ;
580+ expect ( wrongTypeResp . status ) . to . equal ( 403 ) ;
581+
582+ const noClaimResp = await runHandler ( func , request ( { auth : { } } ) ) ;
583+ expect ( noClaimResp . status ) . to . equal ( 403 ) ;
584+ } ) ;
585+
586+ it ( "should check multiple claims with specific values" , async ( ) => {
587+ const func = https . onCall (
588+ {
589+ authPolicy : https . hasClaim ( {
590+ meaning : 42 ,
591+ animal : "dolphin" ,
592+ } ) ,
593+ } ,
594+ ( ) => true
595+ ) ;
596+ const validResp = await runHandler (
597+ func ,
598+ request ( { auth : { meaning : 42 , animal : "dolphin" } } )
599+ ) ;
600+ expect ( validResp . status ) . to . equal ( 200 ) ;
601+
602+ const wrongTypeResp = await runHandler (
603+ func ,
604+ request ( { auth : { meaning : "42" , animal : "dolphin" } } )
605+ ) ;
606+ expect ( wrongTypeResp . status ) . to . equal ( 403 ) ;
607+
608+ const missingResp = await runHandler ( func , request ( { auth : { meaing : 42 } } ) ) ;
609+ expect ( missingResp . status ) . to . equal ( 403 ) ;
610+
611+ const noClaimResp = await runHandler ( func , request ( { auth : { } } ) ) ;
612+ expect ( noClaimResp . status ) . to . equal ( 403 ) ;
613+ } ) ;
557614 } ) ;
558615
559616 it ( "can be any callback" , async ( ) => {
@@ -569,6 +626,24 @@ describe("onCall", () => {
569626 const accessDenied = await runHandler ( divTwo , request ( { data : 1 } ) ) ;
570627 expect ( accessDenied . status ) . to . equal ( 403 ) ;
571628 } ) ;
629+
630+ it ( "should check emailVerified" , async ( ) => {
631+ const func = https . onCall (
632+ {
633+ authPolicy : https . emailVerified ( ) ,
634+ } ,
635+ ( ) => 42
636+ ) ;
637+
638+ const verifiedResp = await runHandler ( func , request ( { auth : { email_verified : true } } ) ) ;
639+ expect ( verifiedResp . status ) . to . equal ( 200 ) ;
640+
641+ const unverifiedResp = await runHandler ( func , request ( { auth : { email_verified : false } } ) ) ;
642+ expect ( unverifiedResp . status ) . to . equal ( 403 ) ;
643+
644+ const noAuthResp = await runHandler ( func , request ( { auth : { } } ) ) ;
645+ expect ( noAuthResp . status ) . to . equal ( 403 ) ;
646+ } ) ;
572647 } ) ;
573648} ) ;
574649
0 commit comments