import { Authentication } from '../../src/authentication/authentication' ; import { AuthorizationEngine } from '../../src/authorization/authorization-engine' ; import { Feed } from '../../src/feed/feed' ; import { LoginResponse } from '../../src/http/messages' ; import { Query } from '../../src/query/query' ; import { FactEnvelope , FactRecord , FactReference } from '../../src/storage' ; import { AuthorizationRules } from '../authorization/authorizationRules' ; export class AuthenticationTest implements Authentication { private authorizationEngine : AuthorizationEngine | null ; constructor ( private inner , authorizationRules , private userFact , private deviceFact ) { this . authorizationEngine = authorizationRules && new AuthorizationEngine ( authorizationRules , inner ) ; } async login ( ) { if ( ! this . userFact ) { throw new Error ( "No logged in user." ) ; } return < LoginResponse > { userFact : this . userFact , profile : { displayName : "Test user" } } ; } async local ( ) { if ( ! this . deviceFact ) { throw new Error ( "No persistent device." ) ; } return this . deviceFact ; } from ( fact , query ) { return this . inner . from ( fact , query ) ; } async save ( envelopes ) { await this . authorize ( envelopes ) ; return await this . inner . save ( envelopes ) ; } query ( start , query ) { return this . inner . query ( start , query ) ; } exists ( fact ) { return this . inner . exists ( fact ) ; } load ( references ) { return this . inner . load ( references ) ; } private async authorize ( envelopes ) { if ( this . authorizationEngine ) { await this . authorizationEngine . authorizeFacts ( envelopes . map ( e => e . fact ) , this . userFact ) ; } } }