|
| 1 | +import temp = require("temp"); |
| 2 | +import { EOL } from "os"; |
| 3 | +import { assert } from "chai"; |
| 4 | +import { IOSEntitlementsService } from "../lib/services/ios-entitlements-service"; |
| 5 | +import * as yok from "../lib/common/yok"; |
| 6 | +import * as stubs from "./stubs"; |
| 7 | +import * as FsLib from "../lib/common/file-system"; |
| 8 | +import * as path from "path"; |
| 9 | + |
| 10 | +// start tracking temporary folders/files |
| 11 | +temp.track(); |
| 12 | + |
| 13 | +describe("IOSEntitlements Service Tests", () => { |
| 14 | + const createTestInjector = (): IInjector => { |
| 15 | + const testInjector = new yok.Yok(); |
| 16 | + |
| 17 | + testInjector.register('platformsData', stubs.PlatformsDataStub); |
| 18 | + testInjector.register("logger", stubs.LoggerStub); |
| 19 | + testInjector.register('iOSEntitlementsService', IOSEntitlementsService); |
| 20 | + |
| 21 | + testInjector.register("fs", FsLib.FileSystem); |
| 22 | + |
| 23 | + testInjector.register("pluginsService", { |
| 24 | + getAllInstalledPlugins: async () => [] |
| 25 | + }); |
| 26 | + |
| 27 | + return testInjector; |
| 28 | + }; |
| 29 | + |
| 30 | + let injector: IInjector, |
| 31 | + platformsData: any, |
| 32 | + projectData: IProjectData, |
| 33 | + fs: IFileSystem, |
| 34 | + iOSEntitlementsService: IOSEntitlementsService, |
| 35 | + destinationFilePath: string; |
| 36 | + |
| 37 | + beforeEach(function() { |
| 38 | + injector = createTestInjector(); |
| 39 | + |
| 40 | + platformsData = injector.resolve("platformsData"); |
| 41 | + projectData = <IProjectData>platformsData.getPlatformData(); |
| 42 | + projectData.projectName = 'testApp'; |
| 43 | + |
| 44 | + projectData.platformsDir = temp.mkdirSync("platformsDir"); |
| 45 | + projectData.projectDir = temp.mkdirSync("projectDir"); |
| 46 | + |
| 47 | + fs = injector.resolve("$fs"); |
| 48 | + |
| 49 | + iOSEntitlementsService = injector.resolve("iOSEntitlementsService"); |
| 50 | + destinationFilePath = iOSEntitlementsService.getPlatformsEntitlementsPath(projectData); |
| 51 | + }); |
| 52 | + |
| 53 | + describe("Ensure paths constructed are correct", () => { |
| 54 | + it("Ensure destination entitlements relative path is calculated correctly.", () => { |
| 55 | + const expected = "testApp/testApp.entitlements"; |
| 56 | + let actual = iOSEntitlementsService.getPlatformsEntitlementsRelativePath(projectData); |
| 57 | + assert.equal(actual, expected); |
| 58 | + }); |
| 59 | + }); |
| 60 | + |
| 61 | + describe("Merge", () => { |
| 62 | + const defaultPlistContent = `<?xml version="1.0" encoding="UTF-8"?> |
| 63 | +<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"> |
| 64 | +<plist version="1.0"> |
| 65 | + <dict/> |
| 66 | +</plist>`; |
| 67 | + const defaultAppResourcesEntitlementsContent = `<?xml version="1.0" encoding="UTF-8"?> |
| 68 | +<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"> |
| 69 | +<plist version="1.0"> |
| 70 | + <dict> |
| 71 | + <key>aps-environment</key> |
| 72 | + <string>development</string> |
| 73 | + </dict> |
| 74 | +</plist>`; |
| 75 | + const defaultPluginEntitlementsContent = `<?xml version="1.0" encoding="UTF-8"?> |
| 76 | +<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"> |
| 77 | +<plist version="1.0"> |
| 78 | + <dict> |
| 79 | + <key>aps-environment</key> |
| 80 | + <string>production</string> |
| 81 | + </dict> |
| 82 | +</plist>`; |
| 83 | + |
| 84 | + function assertContent(actual: string, expected: string) { |
| 85 | + let strip = (x: string) => { |
| 86 | + return x.replace(EOL, '').trim(); |
| 87 | + }; |
| 88 | + assert.equal(strip(actual), strip(expected)); |
| 89 | + } |
| 90 | + |
| 91 | + it("Merge creates a default entitlements file.", async () => { |
| 92 | + // act |
| 93 | + await iOSEntitlementsService.merge(projectData); |
| 94 | + |
| 95 | + // assert |
| 96 | + let actual = fs.readText(destinationFilePath); |
| 97 | + assertContent(actual, defaultPlistContent); |
| 98 | + }); |
| 99 | + |
| 100 | + it("Merge uses the entitlements from App_Resources folder", async () => { |
| 101 | + let appResourcesEntitlement = (<any>iOSEntitlementsService).getDefaultAppEntitlementsPath(projectData); |
| 102 | + fs.writeFile(appResourcesEntitlement, defaultAppResourcesEntitlementsContent); |
| 103 | + |
| 104 | + // act |
| 105 | + await iOSEntitlementsService.merge(projectData); |
| 106 | + |
| 107 | + // assert |
| 108 | + let actual = fs.readText(destinationFilePath); |
| 109 | + assertContent(actual, defaultAppResourcesEntitlementsContent); |
| 110 | + }); |
| 111 | + |
| 112 | + it("Merge uses the entitlements file from a Plugin", async () => { |
| 113 | + let pluginsService = injector.resolve("pluginsService"); |
| 114 | + let testPluginFolderPath = temp.mkdirSync("testPlugin"); |
| 115 | + pluginsService.getAllInstalledPlugins = async() => [{ |
| 116 | + pluginPlatformsFolderPath: (platform: string) => { |
| 117 | + return testPluginFolderPath; |
| 118 | + } |
| 119 | + }]; |
| 120 | + let pluginAppEntitlementsPath = path.join(testPluginFolderPath, IOSEntitlementsService.DefaultEntitlementsName); |
| 121 | + fs.writeFile(pluginAppEntitlementsPath, defaultPluginEntitlementsContent); |
| 122 | + |
| 123 | + // act |
| 124 | + await iOSEntitlementsService.merge(projectData); |
| 125 | + |
| 126 | + // assert |
| 127 | + let actual = fs.readText(destinationFilePath); |
| 128 | + assertContent(actual, defaultPluginEntitlementsContent); |
| 129 | + }); |
| 130 | + }); |
| 131 | +}); |
0 commit comments