import * as TypeMoq from "typemoq" ; import { Config } from "@bentley/bentleyjs-core" ; import { ContextRegistryClient , Project } from "@bentley/context-registry-client" ; import { EventHandler , EventSubscription , EventSubscriptionHandler , HubIModel , IModelHubClient , IModelHubEvent , IModelsHandler , } from "@bentley/imodelhub-client" ; import { BriefcaseDb } from "@bentley/imodeljs-backend" ; import { AgentAuthorizationClient } from "@bentley/backend-itwin-client" ; import { AccessToken , UserInfo } from "@bentley/itwin-client" ; import { BriefcaseProvider } from "../BriefcaseProvider" ; import { ChangeSummaryExtractor } from "../ChangeSummaryExtractor" ; /** Static test mock objects */ export class TestMockObjects { public static setupMockAppConfig ( ) { Config . App . merge ( { imjs_agent_client_id : "FAKE_CLIENT_ID" , imjs_agent_client_secret : "FAKE_CLIENT_SECRET" , imjs_agent_service_user_email : "FAKE_USER_EMAIL" , imjs_agent_service_user_password : "FAKE_USER_PASS" , imjs_agent_project_name : "FAKE_PROJECT_NAME" , imjs_agent_imodel_name : "FAKE_IMODEL_NAME" , agent_app_port : 3000 , agent_app_listen_time : 20 , imjs_buddi_resolve_url_using_region : "103" , imjs_default_relying_party_uri : "https://fake.com" , } ) ; } public static clearMockAppConfig ( ) { Config . App . remove ( "imjs_agent_client_id" ) ; Config . App . remove ( "imjs_agent_client_secret" ) ; Config . App . remove ( "imjs_agent_service_user_email" ) ; Config . App . remove ( "imjs_agent_service_user_password" ) ; Config . App . remove ( "imjs_agent_project_name" ) ; Config . App . remove ( "imjs_agent_imodel_name" ) ; Config . App . remove ( "agent_app_port" ) ; Config . App . remove ( "agent_app_listen_time" ) ; Config . App . remove ( "imjs_buddi_resolve_url_using_region" ) ; Config . App . remove ( "imjs_default_relying_party_uri" ) ; } public static clearMockAppConfigProjectName ( ) { Config . App . remove ( "imjs_agent_project_name" ) ; } public static readonly fakeAccessToken : string = "FAKE_ACCESS_TOKEN" ; public static getMockChangeSummaryExtractor ( ) { const mockExtractor = TypeMoq . Mock . ofType < ChangeSummaryExtractor > ( ) ; return mockExtractor . object ; } public static getMockContextRegistryClient ( ) { const mockContextRegistryClient = TypeMoq . Mock . ofType < ContextRegistryClient > ( ) ; const project = new Project ( ) ; project . wsgId = "FAKE_WSG_ID" ; mockContextRegistryClient . setup ( async ( _ ) => _ . getProject ( TypeMoq . It . isAny ( ) , TypeMoq . It . isAny ( ) ) ) . returns ( async ( ) => project ) ; return mockContextRegistryClient . object ; } public static getMockAgentAuthorizationClient ( throwsError = false ) { const mockOidcAgentClient = TypeMoq . Mock . ofType < AgentAuthorizationClient > ( ) ; if ( throwsError ) mockOidcAgentClient . setup ( async ( _ ) => _ . getAccessToken ( TypeMoq . It . isAny ( ) ) ) . throws ( new Error ( "Mock login failure" ) ) ; else mockOidcAgentClient . setup ( async ( _ ) => _ . getAccessToken ( TypeMoq . It . isAny ( ) ) ) . returns ( async ( ) => this . getFakeAccessToken ( ) ) ; return mockOidcAgentClient . object ; } public static getMockIModelDb ( ) { const mockIModelDb = TypeMoq . Mock . ofType < BriefcaseDb > ( ) ; return mockIModelDb . object ; } public static getMockBriefcaseProvider ( throwsError = false ) { const mockBriefcaseProvider = TypeMoq . Mock . ofType ( BriefcaseProvider ) ; if ( throwsError ) { mockBriefcaseProvider . setup ( async ( _ ) => _ . getBriefcase ( TypeMoq . It . isAny ( ) , TypeMoq . It . isAny ( ) , TypeMoq . It . isAny ( ) , TypeMoq . It . isAny ( ) ) ) . throws ( new Error ( "MOCK Briefcase provider failure" ) ) ; } else { mockBriefcaseProvider . setup ( async ( _ ) => _ . getBriefcase ( TypeMoq . It . isAny ( ) , TypeMoq . It . isAny ( ) , TypeMoq . It . isAny ( ) , TypeMoq . It . isAny ( ) ) ) . returns ( async ( ) => { return this . getMockIModelDb ( ) ; } ) ; } return mockBriefcaseProvider . object ; } public static getMockHubClient ( ) { const mockHubClient = TypeMoq . Mock . ofType ( IModelHubClient ) ; mockHubClient . setup ( ( _ ) => _ . events ) . returns ( ( ) => this . getMockEventHandler ( ) ) ; mockHubClient . setup ( ( _ ) => _ . iModels ) . returns ( ( ) => this . getMockIModelsHandler ( ) ) ; return mockHubClient . object ; } public static getMockIModelsHandler ( ) { const hubIModel = new HubIModel ( ) ; hubIModel . wsgId = "FAKE_WSG_ID" ; const hubIModels = [ hubIModel ] ; const mockIModelHandler = TypeMoq . Mock . ofType < IModelsHandler > ( ) ; mockIModelHandler . setup ( async ( _ ) => _ . get ( TypeMoq . It . isAny ( ) , TypeMoq . It . isAny ( ) , TypeMoq . It . isAny ( ) ) ) . returns ( async ( ) => hubIModels ) ; return mockIModelHandler . object ; } public static getMockEventHandler ( ) { const mockEventHandler = TypeMoq . Mock . ofType ( EventHandler ) ; mockEventHandler . setup ( ( _ ) => _ . subscriptions ) . returns ( ( ) => this . getMockEventSubscriptionHandler ( ) ) ; const listener = ( event ) => { event ; } ; mockEventHandler . setup ( ( _ ) => _ . createListener ( TypeMoq . It . isAny ( ) , TypeMoq . It . isAny ( ) , TypeMoq . It . isAny ( ) , TypeMoq . It . isAny ( ) , listener ) ) . returns ( ( ) => async ( ) => { } ) ; return mockEventHandler . object ; } public static getMockEventSubscriptionHandler ( ) { const mockEventSubscriptionHandler = TypeMoq . Mock . ofType ( EventSubscriptionHandler ) ; mockEventSubscriptionHandler . setup ( async ( _ ) => _ . create ( TypeMoq . It . isAny ( ) , TypeMoq . It . isAny ( ) , TypeMoq . It . isAny ( ) ) ) . returns ( async ( ) => this . getMockEventSubscription ( ) ) ; return mockEventSubscriptionHandler . object ; } public static getMockEventSubscription ( ) { const mockEventSubscription = TypeMoq . Mock . ofType ( EventSubscription ) ; return mockEventSubscription . object ; } public static getFakeAccessToken ( ) { const token = DummyAccessToken . fromForeignProjectAccessTokenJson ( this . fakeAccessToken ) ; return token ! ; } public static getFakeIModelId ( ) { return "FAKE_IMODEL_ID" ; } public static getFakeProjectId ( ) { return "FakePhysicalModelId" ; } } export class DummyAccessToken extends AccessToken { public static foreignProjectAccessTokenJsonProperty = "ForeignProjectAccessToken" ; internal public static fromForeignProjectAccessTokenJson ( foreignJsonStr ) { if ( ! foreignJsonStr . startsWith ( `{\"${ this . foreignProjectAccessTokenJsonProperty }\":` ) ) return undefined ; const props = JSON . parse ( foreignJsonStr ) ; if ( props [ this . foreignProjectAccessTokenJsonProperty ] === undefined ) return undefined ; const tok = new DummyAccessToken ( foreignJsonStr ) ; const userInfoJson = props [ this . foreignProjectAccessTokenJsonProperty ] . userInfo ; const userInfo = UserInfo . fromJson ( userInfoJson ) ; tok . setUserInfo ( userInfo ) ; return tok ; } }