-
Notifications
You must be signed in to change notification settings - Fork 208
/
IModelTestUtils.ts
126 lines (104 loc) · 4.92 KB
/
IModelTestUtils.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
/*---------------------------------------------------------------------------------------------
* Copyright (c) 2019 Bentley Systems, Incorporated. All rights reserved.
* Licensed under the MIT License. See LICENSE.md in the project root for license terms.
*--------------------------------------------------------------------------------------------*/
import { assert } from "chai";
import { RpcManager, IModelReadRpcInterface } from "@bentley/imodeljs-common";
import { OpenMode, ClientRequestContext } from "@bentley/bentleyjs-core";
import { AccessToken, AuthorizationToken, ImsActiveSecureTokenClient, ImsDelegationSecureTokenClient, ConnectClient, Config, ImsUserCredentials } from "@bentley/imodeljs-clients";
import { IModelDb, IModelHost, IModelHostConfiguration, KnownLocations } from "@bentley/imodeljs-backend";
import { IModelJsFs, IModelJsFsStats } from "@bentley/imodeljs-backend/lib/IModelJsFs";
import * as path from "path";
import { IModelJsConfig } from "@bentley/config-loader/lib/IModelJsConfig";
IModelJsConfig.init(true /* suppress exception */, false /* suppress error message */, Config.App);
RpcManager.initializeInterface(IModelReadRpcInterface);
export interface IModelTestUtilsOpenOptions {
copyFilename?: string;
enableTransactions?: boolean;
openMode?: OpenMode;
}
/** Test users with various permissions */
export class TestUsers {
/** User with the typical permissions of the regular/average user - Co-Admin: No, Connect-Services-Admin: No */
public static get regular(): ImsUserCredentials {
return {
email: Config.App.getString("imjs_test_regular_user_name"),
password: Config.App.getString("imjs_test_regular_user_password"),
};
}
}
export class KnownTestLocations {
/** The directory where test assets are stored. Keep in mind that the test is playing the role of the app. */
public static get assetsDir(): string {
// Assume that we are running in nodejs
return path.join(__dirname, "assets");
}
/** The directory where tests can write. */
public static get outputDir(): string {
// Assume that we are running in nodejs
return path.join(__dirname, "output");
}
}
export class IModelTestUtils {
private static _connectClient: ConnectClient | undefined;
public static get connectClient(): ConnectClient {
if (!IModelTestUtils._connectClient)
IModelTestUtils._connectClient = new ConnectClient();
return IModelTestUtils._connectClient!;
}
public static async getTestUserAccessToken(userCredentials?: any): Promise<AccessToken> {
const requestContext = new ClientRequestContext();
if (userCredentials === undefined)
userCredentials = TestUsers.regular;
const authToken: AuthorizationToken = await (new ImsActiveSecureTokenClient()).getToken(requestContext, userCredentials.email, userCredentials.password);
assert(authToken);
const accessToken = await (new ImsDelegationSecureTokenClient()).getToken(requestContext, authToken!);
assert(accessToken);
return accessToken;
}
private static getStat(name: string) {
let stat: IModelJsFsStats | undefined;
try {
stat = IModelJsFs.lstatSync(name);
} catch (err) {
stat = undefined;
}
return stat;
}
public static openIModel(filename: string, opts?: IModelTestUtilsOpenOptions): IModelDb {
const destPath = KnownTestLocations.outputDir;
if (!IModelJsFs.existsSync(destPath))
IModelJsFs.mkdirSync(destPath);
if (opts === undefined)
opts = {};
const srcName = path.join(KnownTestLocations.assetsDir, filename);
const dbName = path.join(destPath, (opts.copyFilename ? opts.copyFilename! : filename));
const srcStat = IModelTestUtils.getStat(srcName);
const destStat = IModelTestUtils.getStat(dbName);
if (!srcStat || !destStat || srcStat.mtimeMs !== destStat.mtimeMs) {
IModelJsFs.copySync(srcName, dbName, { preserveTimestamps: true });
}
const iModel: IModelDb = IModelDb.openStandalone(dbName, opts.openMode, opts.enableTransactions); // could throw Error
assert.exists(iModel);
return iModel!;
}
// __PUBLISH_EXTRACT_START__ IModelHost.startup
public static startupIModelHost() {
// The host configuration.
// The defaults will work for most backends.
// Here is an example of how the briefcasesCacheDir property of the host configuration
// could be set from an environment variable, which could be set by a cloud deployment mechanism.
let briefcaseCacheDir = process.env.MY_SERVICE_BRIEFCASES_DIR;
if (briefcaseCacheDir === undefined) {
const tempDir = process.env.MY_SERVICE_TMP_DIR || KnownLocations.tmpdir;
briefcaseCacheDir = path.join(tempDir, "iModelJs_cache");
}
const imHostConfig = new IModelHostConfiguration();
imHostConfig.briefcaseCacheDir = briefcaseCacheDir;
// Start up IModelHost, supplying the configuration.
IModelHost.startup(imHostConfig);
}
// __PUBLISH_EXTRACT_END__
}
// Start the backend
IModelTestUtils.startupIModelHost();