-
Notifications
You must be signed in to change notification settings - Fork 31
/
access-control.test.ts
234 lines (214 loc) · 7.46 KB
/
access-control.test.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
222
223
224
225
226
227
228
229
230
231
232
233
234
import serverPromise, { TestServer } from "../test-server";
import { TestClient, clearDatabase, setupUsers } from "../test-helpers";
import { SigningKey, SigningKeyResponsePayload, User } from "../schema/types";
import { WithID } from "../store/types";
import { Asset } from "../schema/types";
import { v4 as uuid } from "uuid";
import { db } from "../store";
import { generateUniquePlaybackId } from "./generate-keys";
// includes auth file tests
let server: TestServer;
let mockAdminUserInput: User;
let mockNonAdminUserInput: User;
// jest.setTimeout(70000)
beforeAll(async () => {
server = await serverPromise;
mockAdminUserInput = {
email: "user_admin@gmail.com",
password: "x".repeat(64),
};
mockNonAdminUserInput = {
email: "user_non_admin@gmail.com",
password: "y".repeat(64),
};
});
afterEach(async () => {
await clearDatabase(server);
});
describe("controllers/signing-key", () => {
describe("basic CRUD with JWT authorization", () => {
let client: TestClient;
let adminUser: User;
let adminToken: string;
let nonAdminUser: User;
let nonAdminToken: string;
let signingKey: WithID<SigningKey>;
let gatedPlaybackId: string;
let publicPlaybackId: string;
let gatedAsset: WithID<Asset>;
beforeEach(async () => {
({ client, adminUser, adminToken, nonAdminUser, nonAdminToken } =
await setupUsers(server, mockAdminUserInput, mockNonAdminUserInput));
client.jwtAuth = nonAdminToken;
let created: SigningKeyResponsePayload = await client
.post("/access-control/signing-key")
.then((res) => res.json());
let res = await client.get(`/access-control/signing-key/${created.id}`);
signingKey = await res.json();
const res2 = await client.post("/stream", {
name: "test",
playbackPolicy: {
type: "jwt",
},
});
const stream = await res2.json();
gatedPlaybackId = stream.playbackId;
const res3 = await client.post("/stream", {
name: "test",
playbackPolicy: {
type: "public",
},
});
const publicStream = await res3.json();
publicPlaybackId = publicStream.playbackId;
let id = uuid();
gatedAsset = await db.asset.create({
id,
name: "test-storage",
source: { type: "directUpload" },
createdAt: Date.now(),
playbackId: await generateUniquePlaybackId(id),
playbackPolicy: {
type: "jwt",
},
status: {
phase: "ready",
updatedAt: Date.now(),
},
userId: nonAdminUser.id,
});
});
it("should create a gate stream and allow playback with given public key", async () => {
client.jwtAuth = adminToken;
const res2 = await client.post("/access-control/gate", {
stream: `video+${gatedPlaybackId}`,
pub: signingKey.publicKey,
type: "jwt",
});
expect(res2.status).toBe(204);
});
it("should not allow playback on not existing streams or public keys", async () => {
client.jwtAuth = adminToken;
const res = await client.post("/access-control/gate", {
stream: `video+0000000000123`,
pub: signingKey.publicKey,
type: "jwt",
});
expect(res.status).toBe(404);
const res2 = await client.post("/access-control/gate", {
stream: `video+${gatedPlaybackId}`,
pub: "00000000000000",
type: "jwt",
});
expect(res2.status).toBe(403);
});
it("should not allow playback if stream is gated an pub is missing", async () => {
client.jwtAuth = adminToken;
const res2 = await client.post("/access-control/gate", {
stream: `video+${gatedPlaybackId}`,
type: "jwt",
});
expect(res2.status).toBe(403);
});
it("should not allow playback if stream and public key does not share the same owner", async () => {
client.jwtAuth = adminToken;
const otherStream = await client.post("/stream", {
name: "test",
playbackPolicy: {
type: "jwt",
},
});
const otherStreamPlaybackId = (await otherStream.json()).playbackId;
const res = await client.post("/access-control/gate", {
stream: `video+${otherStreamPlaybackId}`,
pub: signingKey.publicKey,
type: "jwt",
});
expect(res.status).toBe(404);
});
it("should allow playback on public playbackId with and without a public key provided", async () => {
client.jwtAuth = adminToken;
const res = await client.post("/access-control/gate", {
stream: `video+${publicPlaybackId}`,
type: "jwt",
});
expect(res.status).toBe(204);
client.jwtAuth = nonAdminToken;
const stream = await client.post("/stream", {
name: "test",
});
client.jwtAuth = adminToken;
const streamPlaybackId = (await stream.json()).playbackId;
const res2 = await client.post("/access-control/gate", {
stream: `video+${streamPlaybackId}`,
type: "jwt",
});
expect(res2.status).toBe(204);
});
it("should allow playback if playbackPolicy is not specified", async () => {
client.jwtAuth = nonAdminToken;
const stream = await client.post("/stream", {
name: "test",
});
client.jwtAuth = adminToken;
const streamPlaybackId = (await stream.json()).playbackId;
const res = await client.post("/access-control/gate", {
stream: `video+${streamPlaybackId}`,
type: "jwt",
pub: signingKey.publicKey,
});
expect(res.status).toBe(204);
});
it("should allow playback on gated asset", async () => {
client.jwtAuth = adminToken;
const res = await client.post("/access-control/gate", {
stream: `video+${gatedAsset.playbackId}`,
type: "jwt",
pub: signingKey.publicKey,
});
expect(res.status).toBe(204);
});
it("should not allow playback if user is suspended", async () => {
client.jwtAuth = adminToken;
const res = await client.post("/access-control/gate", {
stream: `video+${gatedAsset.playbackId}`,
type: "jwt",
pub: signingKey.publicKey,
});
expect(res.status).toBe(204);
await db.user.update(gatedAsset.userId, { suspended: true });
const res2 = await client.post("/access-control/gate", {
stream: `video+${gatedAsset.playbackId}`,
type: "jwt",
pub: signingKey.publicKey,
});
expect(res2.status).toBe(404);
await db.user.update(gatedAsset.userId, { suspended: false });
});
it("should not allow playback if signing key is deleted or disabled", async () => {
client.jwtAuth = nonAdminToken;
await client.patch(`/access-control/signing-key/${signingKey.id}`, {
disabled: true,
});
client.jwtAuth = adminToken;
const res = await client.post("/access-control/gate", {
stream: `video+${gatedPlaybackId}`,
type: "jwt",
pub: signingKey.publicKey,
});
expect(res.status).toBe(403);
client.jwtAuth = nonAdminToken;
await client.patch(`/access-control/signing-key/${signingKey.id}`, {
disabled: false,
});
await client.delete(`/access-control/signing-key/${signingKey.id}`);
client.jwtAuth = adminToken;
const res2 = await client.post("/access-control/gate", {
stream: `video+${gatedPlaybackId}`,
type: "jwt",
pub: signingKey.publicKey,
});
expect(res2.status).toBe(403);
});
});
});