-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Expand file tree
/
Copy pathcontext.server.test.ts
More file actions
128 lines (100 loc) · 3.27 KB
/
Copy pathcontext.server.test.ts
File metadata and controls
128 lines (100 loc) · 3.27 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
import { beforeEach, describe, expect, test, vi } from "vitest";
const env = vi.hoisted(() => ({
TRPC_SERVER_API_TOKEN: undefined as string | undefined,
PUBLISHER_TOKEN: undefined as string | undefined,
POSTGREST_URL: "http://localhost:3000",
POSTGREST_API_KEY: "",
PUBLISHER_HOST: "wstd.work",
}));
const authenticator = vi.hoisted(() => ({
isAuthenticated: vi.fn(),
}));
const builderAuthenticator = vi.hoisted(() => ({
isAuthenticated: vi.fn(),
}));
const isBuilder = vi.hoisted(() => vi.fn(() => false));
const isCanvas = vi.hoisted(() => vi.fn(() => false));
vi.mock("~/env/env.server", () => ({
default: env,
}));
vi.mock("~/env/env.static.server", () => ({
staticEnv: {},
}));
vi.mock("~/services/auth.server", () => ({
authenticator,
}));
vi.mock("~/services/builder-auth.server", () => ({
builderAuthenticator,
}));
vi.mock("~/services/trpc.server", () => ({
trpcSharedClient: {
domain: {},
deployment: {},
},
}));
vi.mock("~/shared/entri/entri-api.server", () => ({
entryApi: {},
}));
vi.mock("@webstudio-is/plans", () => ({
defaultPlanFeatures: {},
}));
vi.mock("@webstudio-is/plans/index.server", () => ({
getAuthorizationOwnerId: vi.fn(),
getPlanInfo: vi.fn(),
}));
vi.mock("@webstudio-is/postgrest/index.server", () => ({
createClient: vi.fn(),
}));
vi.mock("~/services/session.server", () => ({
readLoginSessionBloomFilter: vi.fn(),
}));
vi.mock("./router-utils", () => ({
isBuilder,
isCanvas,
}));
vi.mock("@webstudio-is/http-client", () => ({
parseBuilderUrl: vi.fn(),
}));
import { extractAuthFromRequest } from "./context.server";
describe("extractAuthFromRequest", () => {
beforeEach(() => {
env.TRPC_SERVER_API_TOKEN = undefined;
env.PUBLISHER_TOKEN = undefined;
authenticator.isAuthenticated.mockResolvedValue(undefined);
builderAuthenticator.isAuthenticated.mockResolvedValue(undefined);
isBuilder.mockReturnValue(false);
isCanvas.mockReturnValue(false);
});
test("does not treat an empty service token as service authorization", async () => {
env.TRPC_SERVER_API_TOKEN = "";
const request = new Request("https://webstudio.is/trpc", {
headers: { Authorization: "" },
});
const auth = await extractAuthFromRequest(request);
expect(auth.isServiceCall).toBe(false);
});
test("accepts a matching non-empty service token", async () => {
env.PUBLISHER_TOKEN = "service-token";
const request = new Request("https://webstudio.is/trpc", {
headers: { Authorization: "service-token" },
});
const auth = await extractAuthFromRequest(request);
expect(auth.isServiceCall).toBe(true);
});
test("accepts a matching bearer service token", async () => {
env.PUBLISHER_TOKEN = "service-token";
const request = new Request("https://webstudio.is/trpc", {
headers: { Authorization: "Bearer service-token" },
});
const auth = await extractAuthFromRequest(request);
expect(auth.isServiceCall).toBe(true);
});
test("accepts legacy trpc service token", async () => {
env.TRPC_SERVER_API_TOKEN = "service-token";
const request = new Request("https://webstudio.is/trpc", {
headers: { Authorization: "service-token" },
});
const auth = await extractAuthFromRequest(request);
expect(auth.isServiceCall).toBe(true);
});
});