throw error for get usage APIs (#4608)

This commit is contained in:
Reynaldi Chernando authored and GitHub committed 2026-11-12 20:47:03 -07:00
1 parent 0694b76751
commit bdb88507df
2 files changed
+62 -6

No files matched your search

+27
View File
@@ -789,6 +789,15 @@ export class AuthModule extends PuterModule {
includePuterAuth: true,
logContext: { service: 'auth', operation: 'usage', params: {} },
});
if (!resp.ok) {
const body = await resp.json().catch(() => ({}));
throw {
status: resp.status,
message: body?.message ?? body?.error
?? `getMonthlyUsage failed with status ${resp.status}`,
...(body?.code ? { code: body.code } : {}),
};
}
return await resp.json();
}
@@ -815,6 +824,15 @@ export class AuthModule extends PuterModule {
},
},
);
if (!resp.ok) {
const body = await resp.json().catch(() => ({}));
throw {
status: resp.status,
message: body?.message ?? body?.error
?? `getDetailedAppUsage failed with status ${resp.status}`,
...(body?.code ? { code: body.code } : {}),
};
}
return await resp.json();
}
@@ -835,6 +853,15 @@ export class AuthModule extends PuterModule {
params: {},
},
});
if (!resp.ok) {
const body = await resp.json().catch(() => ({}));
throw {
status: resp.status,
message: body?.message ?? body?.error
?? `getGlobalUsage failed with status ${resp.status}`,
...(body?.code ? { code: body.code } : {}),
};
}
return await resp.json();
}
}
+35 -6
View File
@@ -454,13 +454,42 @@ export default suite('auth', {
// -- Usage reporting --
'deployment-wide usage is refused for a non-admin': async (t) => {
const usage = (await (
t.puter.auth as unknown as {
getGlobalUsage: () => Promise<Record<string, unknown>>;
const error = (await t.assert.rejects(() =>
(
t.puter.auth as unknown as {
getGlobalUsage: () => Promise<Record<string, unknown>>;
}
).getGlobalUsage(),
)) as { status?: number; code?: string; message?: string };
t.assert.equal(error.status, 403);
t.assert.equal(error.code, 'forbidden');
t.assert.equal(error.message, 'Only admins may request this resource');
},
'getDetailedAppUsage rejects for an unknown app': async (t) => {
const error = (await t.assert.rejects(() =>
t.puter.auth.getDetailedAppUsage(`no-such-app-${t.puter.randName()}`),
)) as { status?: number; message?: string };
t.assert.equal(error.status, 404);
t.assert.equal(error.message, 'App not found');
},
// Web handles invalid tokens with a sign-in popup.
'getMonthlyUsage rejects with a malformed token': {
platforms: ['node', 'workerd'],
fn: async (t) => {
const p = internals(t);
const token = p.authToken;
p.authToken = 'auth-suite-not-a-real-token';
try {
const error = (await t.assert.rejects(() =>
t.puter.auth.getMonthlyUsage(),
)) as { status?: number };
t.assert.equal(error.status, 401);
} finally {
p.authToken = token;
}
).getGlobalUsage()) as { code?: string; message?: string };
t.assert.equal(usage.code, 'forbidden');
t.assert.equal(usage.message, 'Only admins may request this resource');
},
},
// -- Reauth coordination --