|
| 1 | +/** |
| 2 | + * ------------------------------------------------------------------------------------------- |
| 3 | + * Copyright (c) Microsoft Corporation. All Rights Reserved. Licensed under the MIT License. |
| 4 | + * See License in the project root for license information. |
| 5 | + * ------------------------------------------------------------------------------------------- |
| 6 | + */ |
| 7 | + |
| 8 | +import { IProvider, IProviderAccount, ProviderState } from '@microsoft/mgt-element'; |
| 9 | +import { MockGraph } from './MockGraph'; |
| 10 | + |
| 11 | +/** |
| 12 | + * Mock Provider access token for Microsoft Graph APIs |
| 13 | + * |
| 14 | + * @export |
| 15 | + * @class MockProvider |
| 16 | + * @extends {IProvider} |
| 17 | + */ |
| 18 | +export class MockProvider extends IProvider { |
| 19 | + // tslint:disable-next-line: completed-docs |
| 20 | + public provider: any; |
| 21 | + |
| 22 | + private _mockGraphPromise: Promise<MockGraph>; |
| 23 | + |
| 24 | + /** |
| 25 | + * new instance of mock graph provider |
| 26 | + * |
| 27 | + * @memberof MockProvider |
| 28 | + */ |
| 29 | + public graph: MockGraph; |
| 30 | + constructor(signedIn: boolean = false, signedInAccounts: IProviderAccount[] = []) { |
| 31 | + super(); |
| 32 | + this._mockGraphPromise = MockGraph.create(this); |
| 33 | + const enableMultipleLogin = Boolean(signedInAccounts.length); |
| 34 | + this.isMultipleAccountSupported = enableMultipleLogin; |
| 35 | + this.isMultipleAccountDisabled = !enableMultipleLogin; |
| 36 | + this._accounts = signedInAccounts; |
| 37 | + |
| 38 | + this.initializeMockGraph(signedIn); |
| 39 | + } |
| 40 | + |
| 41 | + /** |
| 42 | + * Indicates if the MockProvider is configured to support multi account mode |
| 43 | + * This is only true if the Mock provider has been configured with signedInAccounts in the constructor |
| 44 | + * |
| 45 | + * @readonly |
| 46 | + * @type {boolean} |
| 47 | + * @memberof MockProvider |
| 48 | + */ |
| 49 | + public get isMultiAccountSupportedAndEnabled(): boolean { |
| 50 | + return !this.isMultipleAccountDisabled && this.isMultipleAccountSupported; |
| 51 | + } |
| 52 | + |
| 53 | + private _accounts: IProviderAccount[] = []; |
| 54 | + /** |
| 55 | + * Returns the array of accounts the MockProviders has been configured with |
| 56 | + * |
| 57 | + * @return {*} {IProviderAccount[]} |
| 58 | + * @memberof MockProvider |
| 59 | + */ |
| 60 | + public getAllAccounts?(): IProviderAccount[] { |
| 61 | + return this._accounts; |
| 62 | + } |
| 63 | + |
| 64 | + /** |
| 65 | + * Returns the first account in the set of accounts the MockProvider has been configured with |
| 66 | + * |
| 67 | + * @return {*} {IProviderAccount} |
| 68 | + * @memberof MockProvider |
| 69 | + */ |
| 70 | + public getActiveAccount?(): IProviderAccount { |
| 71 | + if (this._accounts.length) { |
| 72 | + return this._accounts[0]; |
| 73 | + } |
| 74 | + } |
| 75 | + |
| 76 | + /** |
| 77 | + * sets Provider state to SignedIn |
| 78 | + * |
| 79 | + * @returns {Promise<void>} |
| 80 | + * @memberof MockProvider |
| 81 | + */ |
| 82 | + public async login(): Promise<void> { |
| 83 | + this.setState(ProviderState.Loading); |
| 84 | + await this._mockGraphPromise; |
| 85 | + await new Promise(resolve => setTimeout(resolve, 3000)); |
| 86 | + this.setState(ProviderState.SignedIn); |
| 87 | + } |
| 88 | + |
| 89 | + /** |
| 90 | + * sets Provider state to signed out |
| 91 | + * |
| 92 | + * @returns {Promise<void>} |
| 93 | + * @memberof MockProvider |
| 94 | + */ |
| 95 | + public async logout(): Promise<void> { |
| 96 | + this.setState(ProviderState.Loading); |
| 97 | + await this._mockGraphPromise; |
| 98 | + await new Promise(resolve => setTimeout(resolve, 3000)); |
| 99 | + this.setState(ProviderState.SignedOut); |
| 100 | + } |
| 101 | + |
| 102 | + /** |
| 103 | + * Promise returning token from graph.microsoft.com |
| 104 | + * |
| 105 | + * @returns {Promise<string>} |
| 106 | + * @memberof MockProvider |
| 107 | + */ |
| 108 | + public getAccessToken(): Promise<string> { |
| 109 | + return Promise.resolve('{token:https://graph.microsoft.com/}'); |
| 110 | + } |
| 111 | + |
| 112 | + /** |
| 113 | + * Name used for analytics |
| 114 | + * |
| 115 | + * @readonly |
| 116 | + * @memberof IProvider |
| 117 | + */ |
| 118 | + public get name() { |
| 119 | + return 'MgtMockProvider'; |
| 120 | + } |
| 121 | + |
| 122 | + private async initializeMockGraph(signedIn: boolean = false) { |
| 123 | + this.graph = await this._mockGraphPromise; |
| 124 | + |
| 125 | + if (signedIn) { |
| 126 | + this.setState(ProviderState.SignedIn); |
| 127 | + } else { |
| 128 | + this.setState(ProviderState.SignedOut); |
| 129 | + } |
| 130 | + } |
| 131 | +} |
0 commit comments