Repository navigation
Expand file tree
/
Copy pathwithOfflineBackupRules.test.js
More file actions
135 lines (119 loc) · 5.86 KB
/
Copy pathwithOfflineBackupRules.test.js
File metadata and controls
135 lines (119 loc) · 5.86 KB
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
/* global describe, it, expect, __dirname */
// Plain-JS test like withCarPlay.test.js, so no-undef needs the jest globals named.
const fs = require("fs");
const os = require("os");
const path = require("path");
const {
withOfflineBackupRules, addOfflineExcludes, secureStoreRules, OFFLINE_EXCLUDE, BACKUP_RULES, EXTRACTION_RULES,
} = require("./withOfflineBackupRules");
// The rule files the manifest references, read from the installed
// expo-secure-store - exactly what the build merges.
const projectRoot = path.join(__dirname, "..");
const pkgDir = path.dirname(require.resolve("expo-secure-store/package.json", { paths: [projectRoot] }));
const REAL_BACKUP = fs.readFileSync(path.join(pkgDir, "android/src/main/res/xml/secure_store_backup_rules.xml"), "utf8");
const REAL_EXTRACTION = fs.readFileSync(path.join(pkgDir, "android/src/main/res/xml/secure_store_data_extraction_rules.xml"), "utf8");
// Rules with no <include> at all: Android backs up everything but what is excluded.
const NO_INCLUDE_BACKUP = `<?xml version="1.0" encoding="utf-8"?>
<full-backup-content>
<exclude domain="sharedpref" path="SecureStore"/>
</full-backup-content>
`;
const NO_INCLUDE_EXTRACTION = `<?xml version="1.0" encoding="utf-8"?>
<data-extraction-rules>
<cloud-backup>
<exclude domain="sharedpref" path="SecureStore"/>
</cloud-backup>
<device-transfer>
<exclude domain="sharedpref" path="SecureStore"/>
</device-transfer>
</data-extraction-rules>
`;
// A file-domain include that covers offline/.
const FILE_INCLUDE_BACKUP = `<?xml version="1.0" encoding="utf-8"?>
<full-backup-content>
<include domain="file" path="."/>
</full-backup-content>
`;
const count = (s, sub) => s.split(sub).length - 1;
function baseConfig() {
return { name: "dosya", slug: "dosya", android: { package: "dev.dosya" } };
}
function manifestWith(attrs) {
return { manifest: { application: [{ $: { "android:name": ".MainApplication", ...attrs } }] } };
}
async function runDangerous() {
const config = withOfflineBackupRules(baseConfig());
const platformProjectRoot = fs.mkdtempSync(path.join(os.tmpdir(), "offline-backup-plugin-"));
await config.mods.android.dangerous({
...config,
modResults: {},
modRequest: { platformProjectRoot, projectRoot, platform: "android" },
});
return path.join(platformProjectRoot, "app", "src", "main", "res", "xml");
}
describe("withOfflineBackupRules", () => {
it("reads the real expo-secure-store rules, which include only sharedpref", () => {
expect(secureStoreRules("secure_store_backup_rules.xml", projectRoot)).toBe(REAL_BACKUP);
expect(REAL_BACKUP).toContain('<include domain="sharedpref" path="."/>');
expect(REAL_EXTRACTION).toContain('<include domain="sharedpref" path="."/>');
});
it("leaves expo-secure-store's rules untouched: their includes already keep offline/ out", () => {
// An exclude outside every include fails lint's FullBackupContent (fatal in lintVitalRelease).
expect(addOfflineExcludes(REAL_BACKUP)).toBe(REAL_BACKUP);
expect(addOfflineExcludes(REAL_EXTRACTION)).toBe(REAL_EXTRACTION);
});
it("with the real rules, writes no override files at all", async () => {
const xmlDir = await runDangerous();
expect(fs.existsSync(path.join(xmlDir, "secure_store_backup_rules.xml"))).toBe(false);
expect(fs.existsSync(path.join(xmlDir, "secure_store_data_extraction_rules.xml"))).toBe(false);
});
it("adds the exclude to full-backup-content when there is no include", () => {
const out = addOfflineExcludes(NO_INCLUDE_BACKUP);
expect(count(out, OFFLINE_EXCLUDE)).toBe(1);
expect(out.indexOf(OFFLINE_EXCLUDE)).toBeLessThan(out.indexOf("</full-backup-content>"));
expect(out).toContain('<exclude domain="sharedpref" path="SecureStore"/>');
});
it("adds it to both cloud-backup and device-transfer when they have no include", () => {
const out = addOfflineExcludes(NO_INCLUDE_EXTRACTION);
const cloud = out.slice(out.indexOf("<cloud-backup>"), out.indexOf("</cloud-backup>"));
const transfer = out.slice(out.indexOf("<device-transfer>"), out.indexOf("</device-transfer>"));
expect(cloud).toContain(OFFLINE_EXCLUDE);
expect(transfer).toContain(OFFLINE_EXCLUDE);
});
it("adds it where a file-domain include covers offline/", () => {
expect(count(addOfflineExcludes(FILE_INCLUDE_BACKUP), OFFLINE_EXCLUDE)).toBe(1);
});
it("is idempotent", () => {
const once = addOfflineExcludes(NO_INCLUDE_EXTRACTION);
expect(addOfflineExcludes(once)).toBe(once);
});
it("leaves the manifest pointing at the secure-store rules after that plugin ran", async () => {
const config = withOfflineBackupRules(baseConfig());
const result = await config.mods.android.manifest({
...config,
modResults: manifestWith({ "android:fullBackupContent": BACKUP_RULES, "android:dataExtractionRules": EXTRACTION_RULES }),
modRequest: { platform: "android" },
});
const app = result.modResults.manifest.application[0].$;
expect(app["android:fullBackupContent"]).toBe(BACKUP_RULES);
expect(app["android:dataExtractionRules"]).toBe(EXTRACTION_RULES);
});
it("points a manifest with no rules at them", async () => {
const config = withOfflineBackupRules(baseConfig());
const result = await config.mods.android.manifest({
...config,
modResults: manifestWith({}),
modRequest: { platform: "android" },
});
const app = result.modResults.manifest.application[0].$;
expect(app["android:fullBackupContent"]).toBe(BACKUP_RULES);
expect(app["android:dataExtractionRules"]).toBe(EXTRACTION_RULES);
});
it("is registered in app.config.ts after expo-secure-store", () => {
const src = fs.readFileSync(path.join(__dirname, "..", "app.config.ts"), "utf8");
const secure = src.indexOf('"expo-secure-store"');
const ours = src.indexOf('"./plugins/withOfflineBackupRules"');
expect(secure).toBeGreaterThan(-1);
expect(ours).toBeGreaterThan(secure);
});
});