Repository navigation
Expand file tree
/
Copy pathapiAuthScope.test.ts
More file actions
215 lines (195 loc) · 6.62 KB
/
Copy pathapiAuthScope.test.ts
File metadata and controls
215 lines (195 loc) · 6.62 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
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
import { beforeEach, describe, expect, it, vi } from "vitest";
import {
authenticateApiKeyRequest,
authenticateApiKeyWithScope,
authenticateRequestWithScopedApiKey,
} from "~/services/apiAuth.server";
const authorizeBearer = vi.fn();
beforeEach(() => {
vi.clearAllMocks();
});
describe("authenticateApiKeyWithScope", () => {
it("returns 401 without a bearer credential", async () => {
const result = await authenticateApiKeyWithScope(
new Request("https://example.com"),
{ action: "read", resource: { type: "envvars" } },
authorizeBearer
);
expect(result).toEqual({
ok: false,
status: 401,
error: "Invalid or Missing API key",
});
expect(authorizeBearer).not.toHaveBeenCalled();
});
it.each([
{ status: 401 as const, error: "Invalid API key" },
{ status: 403 as const, error: "Unauthorized" },
])("preserves controller $status failures", async (failure) => {
authorizeBearer.mockResolvedValueOnce({ ok: false, ...failure });
const request = new Request("https://example.com", {
headers: { Authorization: "Bearer tr_test_key" },
});
await expect(
authenticateApiKeyWithScope(
request,
{ action: "write", resource: { type: "deployments" } },
authorizeBearer
)
).resolves.toEqual({ ok: false, ...failure });
});
it("bridges controller success into the legacy private authentication shape", async () => {
const environment = { id: "env_123" };
const ability = { can: vi.fn(() => true), canSuper: vi.fn(() => true) };
authorizeBearer.mockResolvedValueOnce({
ok: true,
environment,
ability,
subject: { type: "apiKey", apiKeyId: "key_123" },
});
const request = new Request("https://example.com", {
headers: { Authorization: "Bearer tr_test_key" },
});
const result = await authenticateApiKeyWithScope(
request,
{ action: "read", resource: { type: "envvars" }, allowJWT: true },
authorizeBearer
);
expect(authorizeBearer).toHaveBeenCalledWith(
request,
{ action: "read", resource: { type: "envvars" } },
{ allowJWT: true, allowPreviewParent: false }
);
expect(result).toEqual({
ok: true,
authentication: {
ok: true,
apiKey: "tr_test_key",
type: "PRIVATE",
environment,
ability,
},
});
});
it("allows branch creation to authenticate against the Preview parent", async () => {
const environment = { id: "env_preview" };
const ability = { can: vi.fn(() => true), canSuper: vi.fn(() => true) };
authorizeBearer.mockResolvedValueOnce({
ok: true,
environment,
ability,
subject: { type: "apiKey", apiKeyId: "key_123" },
});
const request = new Request("https://example.com", {
headers: { Authorization: "Bearer tr_preview_sk_test" },
});
await expect(
authenticateApiKeyWithScope(
request,
{
action: "write",
resource: { type: "branches" },
allowPreviewParent: true,
},
authorizeBearer
)
).resolves.toMatchObject({ ok: true });
expect(authorizeBearer).toHaveBeenCalledWith(
request,
{ action: "write", resource: { type: "branches" } },
{ allowJWT: false, allowPreviewParent: true }
);
});
it("authenticates a valid API key without a resource check", async () => {
const environment = { id: "env_123" };
const ability = { can: vi.fn(() => false), canSuper: vi.fn(() => false) };
const authenticateBearer = vi.fn().mockResolvedValueOnce({
ok: true,
environment,
ability,
subject: { type: "apiKey", apiKeyId: "key_123" },
});
const request = new Request("https://example.com", {
headers: { Authorization: "Bearer tr_prod_sk_test" },
});
await expect(
authenticateApiKeyRequest(request, { allowPreviewParent: true }, authenticateBearer)
).resolves.toMatchObject({
ok: true,
authentication: { apiKey: "tr_prod_sk_test", environment },
});
expect(ability.can).not.toHaveBeenCalled();
});
it("returns authorization failures from the controller", async () => {
const ability = { can: vi.fn(() => false), canSuper: vi.fn(() => false) };
authorizeBearer.mockResolvedValueOnce({
ok: false,
status: 403,
error: "Unauthorized",
});
const request = new Request("https://example.com", {
headers: { Authorization: "Bearer tr_prod_sk_test" },
});
await expect(
authenticateApiKeyWithScope(
request,
{ action: "write", resource: { type: "deployments" } },
authorizeBearer
)
).resolves.toEqual({ ok: false, status: 403, error: "Unauthorized" });
expect(ability.can).not.toHaveBeenCalled();
});
});
describe("authenticateRequestWithScopedApiKey", () => {
const options = {
personalAccessToken: true as const,
organizationAccessToken: true as const,
apiKey: {
action: "write",
resource: { type: "branches" },
allowPreviewParent: true,
},
};
it("keeps user and organization tokens on the legacy path", async () => {
const authentication = {
type: "personalAccessToken",
result: { userId: "user_123" },
} as const;
const authenticateRequest = vi.fn().mockResolvedValueOnce(authentication);
const authenticateApiKeyWithScope = vi.fn();
await expect(
authenticateRequestWithScopedApiKey(new Request("https://example.com"), options, {
authenticateRequest,
authenticateApiKeyWithScope,
})
).resolves.toEqual({ ok: true, authentication });
expect(authenticateRequest).toHaveBeenCalledWith(expect.any(Request), {
personalAccessToken: true,
organizationAccessToken: true,
apiKey: false,
});
expect(authenticateApiKeyWithScope).not.toHaveBeenCalled();
});
it("uses scoped RBAC authentication for API keys", async () => {
const apiKeyAuthentication = {
ok: true,
apiKey: "tr_preview_sk_test",
type: "PRIVATE",
environment: {},
} as const;
const authenticateRequest = vi.fn().mockResolvedValueOnce(undefined);
const authenticateApiKeyWithScope = vi
.fn()
.mockResolvedValueOnce({ ok: true, authentication: apiKeyAuthentication });
await expect(
authenticateRequestWithScopedApiKey(new Request("https://example.com"), options, {
authenticateRequest,
authenticateApiKeyWithScope,
})
).resolves.toEqual({
ok: true,
authentication: { type: "apiKey", result: apiKeyAuthentication },
});
expect(authenticateApiKeyWithScope).toHaveBeenCalledWith(expect.any(Request), options.apiKey);
});
});