import { execute , parse } from 'graphql' ; import { makeSchema , objectType } from 'nexus' ; import { fieldAuthenticationPlugin , FieldAuthenticationPluginConfig } from '../src' ; const user = { id : '8a291df8-bbcf-11ea-9db0-ff6b23713451' , name : 'user-a' , } ; const User = objectType ( { name : 'User' , definition ( t ) { t . id ( 'id' ) ; t . string ( 'name' ) ; } , } ) ; const QueryAll = parse ( `

  fragment userInfo on User {
    id
    name
  }

  query QueryAll {
    me1 {
      ...userInfo
    }
    me2 {
      ...userInfo
    }
    me3 {
      ...userInfo
    }
    me4 {
      ...userInfo
    }
    me5 {
      ...userInfo
    }
    me6 {
      ...userInfo
    }
    me7 {
      ...userInfo
    }
    me8 {
      ...userInfo
    }
    me9 {
      ...userInfo
    }
    me10 {
      ...userInfo
    }
  }` ) ; const loggedOutUser = { id : 'unauthenticated' , name : 'guest' , } ; const fakeUser = { id : 'totally-different-than-the-logged' , name : 'another-user' , } ; const testSchema = ( pluginConfig , outputs = false ) => makeSchema ( { outputs , types : [ User , objectType ( { name : 'Query' , definition ( t ) { t . field ( 'me1' , { type : User , resolve : ( ) => user , } ) ; t . field ( 'me2' , { type : User , authentication : true , resolve : ( ) => user , } ) ; t . field ( 'me3' , { type : User , authentication : false , resolve : ( ) => user , } ) ; t . field ( 'me4' , { type : User , authentication : [ true , loggedOutUser ] , resolve : ( ) => user , } ) ; t . field ( 'me5' , { type : User , authentication : [ false , fakeUser ] , resolve : ( ) => user , } ) ; t . field ( 'me6' , { type : User , authentication : [ true , new Error ( 'Invalid - Unauthenticated' ) ] , resolve : ( ) => user , } ) ; t . field ( 'me7' , { type : User , authentication : [ false , new Error ( 'Invalid - Authenticated' ) ] , resolve : ( ) => user , } ) ; t . field ( 'me8' , { type : User , authentication : ( ) => true , } ) ; t . field ( 'me9' , { type : User , authentication : ( ) => { throw new Error ( 'You shall not pass' ) ; } , } ) ; t . field ( 'me10' , { type : User , authentication : { something : true } , } ) ; } , } ) , ] , plugins : [ fieldAuthenticationPlugin ( pluginConfig ) ] , nonNullDefaults : { input : false , output : false , } , } ) ; describe ( 'fieldAuthentication' , ( ) => { describe ( 'basic behavior' , ( ) => { it ( 'should resolve all fields correctly with default plugin values - unauthenticated' , async ( ) => { const schema = testSchema ( { } ) ; const data = await execute ( { schema , document : QueryAll , } ) ; expect ( data ) . toMatchSnapshot ( ) ; } ) ; it ( 'should resolve all fields correctly with default plugin values - authenticated' , async ( ) => { const schema = testSchema ( { } ) ; const data = await execute ( { schema , contextValue : { state : { user : 'something' , } , } , document : QueryAll , } ) ; expect ( data ) . toMatchSnapshot ( ) ; } ) ; it ( 'throwErrorOnFailedAuthenticationByDefault = true / defaultErrorMessage = something' , async ( ) => { const schema = testSchema ( { throwErrorOnFailedAuthenticationByDefault : true , defaultErrorMessage : 'You need to be authenticated' , } ) ; const data = await execute ( { schema , contextValue : { state : { user : 'something' , } , } , document : QueryAll , } ) ; expect ( data ) . toMatchSnapshot ( ) ; } ) ; it ( 'format error returning non error value' , async ( ) => { const schema = testSchema ( { throwErrorOnFailedAuthenticationByDefault : true , defaultErrorMessage : 'You need to be authenticated' , formatError : ( ) => "I'm not an error" , } ) ; const errorLogger = jest . fn ( ) ; const data = await execute ( { schema , contextValue : { state : { user : 'something' , } , logger : { error : errorLogger , } , } , document : QueryAll , } ) ; expect ( data ) . toMatchSnapshot ( ) ; expect ( errorLogger ) . toHaveBeenCalled ( ) ; } ) ; } ) ; } ) ;