Repository navigation
Expand file tree
/
Copy pathplugin-mobile-session.test.ts
More file actions
150 lines (150 loc) · 5.19 KB
/
Copy pathplugin-mobile-session.test.ts
File metadata and controls
150 lines (150 loc) · 5.19 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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
import { it, expect } from "vitest";
import { mkdtemp, mkdir, writeFile, rm } from "node:fs/promises";
import { tmpdir } from "node:os";
import { join } from "node:path";
import { createApp } from "@server/app/create-app.js";
import { createUser } from "@core/modules/identity/passwords.js";
import { openTestDatabase } from "./database.js";
it("binds mobile tickets to one plugin, denies replay and host APIs, and revokes on parent logout", async () => {
const db = await openTestDatabase({ driver: "sqlite", path: ":memory:" }),
root = await mkdtemp(join(tmpdir(), "doca-mobile-plugin-")),
pkg = join(root, "example.mail");
await mkdir(join(pkg, "web"), { recursive: true });
const manifest = {
schemaVersion: 1,
id: "example.mail",
version: "1.0.0",
displayName: "Mail",
sdkRange: "^0.2.0",
};
await writeFile(
join(pkg, "package.json"),
JSON.stringify({
name: "@example/mail",
version: "1.0.0",
type: "module",
doca: {
dataVersion: "1",
storage: "host",
manifest: "./manifest.json",
server: "./server.js",
web: { directory: "./web", entry: "./index.js" },
mobileHostRange: "^1.0.0",
navigation: [
{
id: "example.mail.inbox",
title: { zh: "邮箱", en: "Mail" },
icon: "mail",
webPath: "/plugins/example.mail/inbox",
mobile: true,
allowedSlots: ["web.left", "mobile.drawer", "mobile.more"],
defaults: ["mobile.drawer"],
order: 1,
},
],
},
}),
);
await writeFile(join(pkg, "manifest.json"), JSON.stringify(manifest));
await writeFile(
join(pkg, "server.js"),
`export default()=>({manifest:${JSON.stringify(manifest)},async uninstall(){}})`,
);
await writeFile(join(pkg, "web/index.js"), "export default()=>({})");
const app = await createApp(db, {
origin: "http://127.0.0.1:39201",
pluginDirectory: root,
});
const headers = { host: "127.0.0.1:39201", origin: "http://127.0.0.1:39201" };
try {
await createUser(
db,
{
login: "mobile-admin",
displayName: "Admin",
password: "mobile-admin-password",
},
{ bootstrap: true },
);
const login = await app.inject({
method: "POST",
url: "/api/v1/auth/login",
headers: { ...headers, "x-doca-client": "mobile" },
payload: { login: "mobile-admin", password: "mobile-admin-password" },
});
const token = login.json().sessionToken;
expect(token).toBeTruthy();
const ticket = await app.inject({
method: "POST",
url: "/api/v1/plugins-mobile/ticket",
headers: { ...headers, authorization: `Bearer ${token}` },
payload: { entryId: "example.mail.inbox" },
});
expect(ticket.statusCode).toBe(200);
const redeem = () =>
app.inject({
method: "POST",
url: "/api/v1/plugins-mobile/redeem",
headers,
payload: { ticket: ticket.json().ticket },
});
const response = await redeem();
expect(response.statusCode).toBe(200);
expect((await redeem()).statusCode).toBe(401);
const cookie = String(response.headers["set-cookie"]).split(";")[0]!;
expect(
(
await app.inject({
url: "/api/v1/admin/plugins",
headers: { ...headers, cookie },
})
).statusCode,
).toBe(403);
expect(
(
await app.inject({
url: "/api/v1/plugins/other.plugin/messages",
headers: { ...headers, cookie },
})
).statusCode,
).toBe(403);
const bootstrap = await app.inject({
url: "/api/v1/bootstrap",
headers: { ...headers, cookie },
});
expect(bootstrap.statusCode).toBe(200);
expect(bootstrap.json().plugins.map((p: any) => p.id)).toEqual([
"example.mail",
]);
const publicCall = (pluginId: string, operation: string, payload: Record<string, unknown> = {}) => app.inject({
method: "POST", url: `/api/v1/plugin-platform/${pluginId}/${operation}`, headers: { ...headers, cookie }, payload,
});
const self = await publicCall("example.mail", "users.me");
expect(self.statusCode, self.body).toBe(200);
expect(self.json()).toMatchObject({ login: "mobile-admin" });
expect(self.body).not.toContain("password_hash");
const search = await publicCall("example.mail", "users.searchPage", { query: "" });
expect(search.statusCode, search.body).toBe(200);
expect(search.headers["cache-control"]).toBe("no-store");
expect((await publicCall("other.plugin", "users.me")).statusCode).toBe(403);
expect((await publicCall("example.mail", "users.list")).statusCode).toBe(404);
expect((await publicCall("example.mail", "users.searchPage", { query: "", principal: { id: "other" } })).statusCode).toBe(400);
await app.inject({
method: "POST",
url: "/api/v1/auth/logout",
headers: { ...headers, authorization: `Bearer ${token}` },
});
expect(
(
await app.inject({
url: "/api/v1/bootstrap",
headers: { ...headers, cookie },
})
).statusCode,
).toBe(401);
} finally {
await app.close();
await db.destroy();
await rm(root, { recursive: true, force: true });
}
});