-
-
Notifications
You must be signed in to change notification settings - Fork 197
/
Copy pathios-entitlements-service.ts
221 lines (191 loc) · 7.06 KB
/
ios-entitlements-service.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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
import * as temp from "temp";
import { EOL } from "os";
import { assert } from "chai";
import { IOSEntitlementsService } from "../lib/services/ios-entitlements-service";
import * as yok from "../lib/common/yok";
import * as stubs from "./stubs";
import * as FsLib from "../lib/common/file-system";
import * as MobileHelperLib from "../lib/common/mobile/mobile-helper";
import * as DevicePlatformsConstantsLib from "../lib/common/mobile/device-platforms-constants";
import * as ErrorsLib from "../lib/common/errors";
import * as path from "path";
import { IProjectData } from "../lib/definitions/project";
import { IInjector } from "../lib/common/definitions/yok";
import { IFileSystem } from "../lib/common/declarations";
// start tracking temporary folders/files
temp.track();
describe("IOSEntitlements Service Tests", () => {
const createTestInjector = (): IInjector => {
const testInjector = new yok.Yok();
testInjector.register("platformsDataService", stubs.NativeProjectDataStub);
testInjector.register("projectData", stubs.ProjectDataStub);
testInjector.register("logger", stubs.LoggerStub);
testInjector.register("iOSEntitlementsService", IOSEntitlementsService);
testInjector.register("fs", FsLib.FileSystem);
testInjector.register("mobileHelper", MobileHelperLib.MobileHelper);
testInjector.register(
"devicePlatformsConstants",
DevicePlatformsConstantsLib.DevicePlatformsConstants
);
testInjector.register("errors", ErrorsLib.Errors);
testInjector.register("pluginsService", {
getAllInstalledPlugins: async (): Promise<any[]> => [],
});
testInjector.register("tempService", stubs.TempServiceStub);
testInjector.register("options", {});
return testInjector;
};
let injector: IInjector;
let projectData: IProjectData;
let fs: IFileSystem;
let iOSEntitlementsService: IOSEntitlementsService;
let destinationFilePath: string;
beforeEach(() => {
injector = createTestInjector();
projectData = injector.resolve<IProjectData>("projectData");
projectData.projectName = "testApp";
projectData.platformsDir = temp.mkdirSync("platformsDir");
projectData.projectDir = temp.mkdirSync("projectDir");
projectData.appDirectoryPath = projectData.getAppDirectoryPath();
projectData.appResourcesDirectoryPath =
projectData.getAppResourcesDirectoryPath();
fs = injector.resolve("$fs");
iOSEntitlementsService = injector.resolve("iOSEntitlementsService");
destinationFilePath =
iOSEntitlementsService.getPlatformsEntitlementsPath(projectData);
});
describe("Ensure paths constructed are correct", () => {
it("Ensure destination entitlements relative path is calculated correctly.", () => {
const expected = path.join("testApp", "testApp.entitlements");
const actual =
iOSEntitlementsService.getPlatformsEntitlementsRelativePath(
projectData
);
assert.equal(actual, expected);
});
it("Ensure full path to entitlements in platforms dir is correct", () => {
const expected = path.join(
projectData.platformsDir,
"ios",
"testApp",
"testApp.entitlements"
);
const actual =
iOSEntitlementsService.getPlatformsEntitlementsPath(projectData);
assert.equal(actual, expected);
});
});
describe("Merge", () => {
const defaultAppResourcesEntitlementsContent = `<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>aps-environment</key>
<string>development</string>
</dict>
</plist>`;
const defaultPluginEntitlementsContent = `<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>aps-environment</key>
<string>production</string>
</dict>
</plist>`;
const namedAppResourcesEntitlementsContent = `<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>nameKey</key>
<string>appResources</string>
</dict>
</plist>`;
const mergedEntitlementsContent = `<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>aps-environment</key>
<string>production</string>
<key>nameKey</key>
<string>appResources</string>
</dict>
</plist>`;
function assertContent(actual: string, expected: string) {
const strip = (x: string) => {
return x.replace(EOL, "").trim();
};
assert.equal(strip(actual), strip(expected));
}
it("Merge does not create a default entitlements file.", async () => {
// act
await iOSEntitlementsService.merge(projectData);
// assert
assert.isFalse(fs.exists(destinationFilePath));
});
it("Merge uses the entitlements from App_Resources folder", async () => {
const appResourcesEntitlement = (<any>(
iOSEntitlementsService
)).getDefaultAppEntitlementsPath(projectData);
fs.writeFile(
appResourcesEntitlement,
defaultAppResourcesEntitlementsContent
);
// act
await iOSEntitlementsService.merge(projectData);
// assert
const actual = fs.readText(destinationFilePath);
assertContent(actual, defaultAppResourcesEntitlementsContent);
});
it("Merge uses the entitlements file from a Plugin", async () => {
const pluginsService = injector.resolve("pluginsService");
const testPluginFolderPath = temp.mkdirSync("testPlugin");
pluginsService.getAllInstalledPlugins = async () => [
{
pluginPlatformsFolderPath: (platform: string) => {
return testPluginFolderPath;
},
},
];
const pluginAppEntitlementsPath = path.join(
testPluginFolderPath,
IOSEntitlementsService.DefaultEntitlementsName
);
fs.writeFile(pluginAppEntitlementsPath, defaultPluginEntitlementsContent);
// act
await iOSEntitlementsService.merge(projectData);
// assert
const actual = fs.readText(destinationFilePath);
assertContent(actual, defaultPluginEntitlementsContent);
});
it("Merge uses App_Resources and Plugins and merges all keys", async () => {
// setup app resoruces
const appResourcesEntitlement = (<any>(
iOSEntitlementsService
)).getDefaultAppEntitlementsPath(projectData);
fs.writeFile(
appResourcesEntitlement,
namedAppResourcesEntitlementsContent
);
// setup plugin entitlements
const pluginsService = injector.resolve("pluginsService");
const testPluginFolderPath = temp.mkdirSync("testPlugin");
pluginsService.getAllInstalledPlugins = async () => [
{
pluginPlatformsFolderPath: (platform: string) => {
return testPluginFolderPath;
},
},
];
const pluginAppEntitlementsPath = path.join(
testPluginFolderPath,
IOSEntitlementsService.DefaultEntitlementsName
);
fs.writeFile(pluginAppEntitlementsPath, defaultPluginEntitlementsContent);
// act
await iOSEntitlementsService.merge(projectData);
// assert
const actual = fs.readText(destinationFilePath);
assertContent(actual, mergedEntitlementsContent);
});
});
});