From 40b7bea9822b5bfbcfaa5616daa480da56d89910 Mon Sep 17 00:00:00 2001 From: Tom Moor Date: Sat, 3 May 2025 15:13:29 -0400 Subject: [PATCH] test: Add tests for oauthAuthentications endpoints --- .../oauthAuthentications.test.ts.snap | 19 ++ .../oauthAuthentications.test.ts | 194 ++++++++++++++++++ .../oauthAuthentications.ts | 4 +- server/test/factories.ts | 10 +- server/utils/oauth/OAuthInterface.ts | 10 +- 5 files changed, 228 insertions(+), 9 deletions(-) create mode 100644 server/routes/api/oauthAuthentications/__snapshots__/oauthAuthentications.test.ts.snap create mode 100644 server/routes/api/oauthAuthentications/oauthAuthentications.test.ts diff --git a/server/routes/api/oauthAuthentications/__snapshots__/oauthAuthentications.test.ts.snap b/server/routes/api/oauthAuthentications/__snapshots__/oauthAuthentications.test.ts.snap new file mode 100644 index 0000000000..b18e9a4571 --- /dev/null +++ b/server/routes/api/oauthAuthentications/__snapshots__/oauthAuthentications.test.ts.snap @@ -0,0 +1,19 @@ +// Jest Snapshot v1, https://goo.gl/fbAQLP + +exports[`oauthAuthentications.delete should require authentication 1`] = ` +{ + "error": "authentication_required", + "message": "Authentication required", + "ok": false, + "status": 401, +} +`; + +exports[`oauthAuthentications.list should require authentication 1`] = ` +{ + "error": "authentication_required", + "message": "Authentication required", + "ok": false, + "status": 401, +} +`; diff --git a/server/routes/api/oauthAuthentications/oauthAuthentications.test.ts b/server/routes/api/oauthAuthentications/oauthAuthentications.test.ts new file mode 100644 index 0000000000..e8a6edc566 --- /dev/null +++ b/server/routes/api/oauthAuthentications/oauthAuthentications.test.ts @@ -0,0 +1,194 @@ +import { OAuthClient, OAuthAuthentication } from "@server/models"; +import { + buildOAuthAuthentication, + buildTeam, + buildUser, +} from "@server/test/factories"; +import { getTestServer } from "@server/test/support"; + +const server = getTestServer(); + +describe("oauthAuthentications.list", () => { + it("should require authentication", async () => { + const res = await server.post("/api/oauthAuthentications.list"); + const body = await res.json(); + expect(res.status).toEqual(401); + expect(body).toMatchSnapshot(); + }); + + it("should return list of oauth authentications for user", async () => { + const team = await buildTeam(); + const user = await buildUser({ teamId: team.id }); + const oauthClient = await OAuthClient.create({ + teamId: team.id, + createdById: user.id, + name: "Test Client", + redirectUris: ["https://example.com/callback"], + }); + + await buildOAuthAuthentication({ + oauthClientId: oauthClient.id, + user, + scope: ["read"], + }); + + const res = await server.post("/api/oauthAuthentications.list", { + body: { + token: user.getJwtToken(), + }, + }); + + const body = await res.json(); + expect(res.status).toEqual(200); + expect(body.data.length).toEqual(1); + expect(body.data[0].id).toBeDefined(); + expect(body.data[0].oauthClient.name).toEqual("Test Client"); + expect(body.policies).toBeDefined(); + }); + + it("should only return authentications for requesting user", async () => { + const team = await buildTeam(); + const user = await buildUser({ teamId: team.id }); + const anotherUser = await buildUser({ teamId: team.id }); + const oauthClient = await OAuthClient.create({ + teamId: team.id, + createdById: user.id, + name: "Test Client", + redirectUris: ["https://example.com/callback"], + }); + + await buildOAuthAuthentication({ + oauthClientId: oauthClient.id, + user: anotherUser, + scope: ["read"], + }); + + const res = await server.post("/api/oauthAuthentications.list", { + body: { + token: user.getJwtToken(), + }, + }); + + const body = await res.json(); + expect(res.status).toEqual(200); + expect(body.data.length).toEqual(0); + }); +}); + +describe("oauthAuthentications.delete", () => { + it("should require authentication", async () => { + const res = await server.post("/api/oauthAuthentications.delete"); + const body = await res.json(); + expect(res.status).toEqual(401); + expect(body).toMatchSnapshot(); + }); + + it("should delete all authentications for a client without scope", async () => { + const team = await buildTeam(); + const user = await buildUser({ teamId: team.id }); + const oauthClient = await OAuthClient.create({ + teamId: team.id, + createdById: user.id, + name: "Test Client", + redirectUris: ["https://example.com/callback"], + }); + + await buildOAuthAuthentication({ + oauthClientId: oauthClient.id, + user, + scope: ["read"], + }); + + const res = await server.post("/api/oauthAuthentications.delete", { + body: { + token: user.getJwtToken(), + oauthClientId: oauthClient.id, + }, + }); + + const body = await res.json(); + expect(res.status).toEqual(200); + expect(body.success).toBe(true); + + const auths = await OAuthAuthentication.findAll({ + where: { + userId: user.id, + oauthClientId: oauthClient.id, + }, + }); + expect(auths.length).toEqual(0); + }); + + it("should delete matching authentications for a client with scope", async () => { + const team = await buildTeam(); + const user = await buildUser({ teamId: team.id }); + const oauthClient = await OAuthClient.create({ + teamId: team.id, + createdById: user.id, + name: "Test Client", + redirectUris: ["https://example.com/callback"], + }); + + await buildOAuthAuthentication({ + oauthClientId: oauthClient.id, + user, + scope: ["read"], + }); + await buildOAuthAuthentication({ + oauthClientId: oauthClient.id, + user, + scope: ["write"], + }); + + const res = await server.post("/api/oauthAuthentications.delete", { + body: { + token: user.getJwtToken(), + oauthClientId: oauthClient.id, + scope: ["read"], + }, + }); + + const body = await res.json(); + expect(res.status).toEqual(200); + expect(body.success).toBe(true); + + const auths = await OAuthAuthentication.findAll({ + where: { + userId: user.id, + oauthClientId: oauthClient.id, + }, + }); + expect(auths.length).toEqual(1); + expect(auths[0].scope[0]).toEqual("write"); + }); + + it("should only delete authentications for requesting user", async () => { + const team = await buildTeam(); + const user = await buildUser({ teamId: team.id }); + const anotherUser = await buildUser({ teamId: team.id }); + const oauthClient = await OAuthClient.create({ + teamId: team.id, + createdById: user.id, + name: "Test Client", + redirectUris: ["https://example.com/callback"], + }); + + const otherAuth = await buildOAuthAuthentication({ + oauthClientId: oauthClient.id, + user: anotherUser, + scope: ["read"], + }); + + await server.post("/api/oauthAuthentications.delete", { + body: { + token: user.getJwtToken(), + oauthClientId: oauthClient.id, + scope: "read", + }, + }); + + // Verify other user's auth still exists + const auth = await OAuthAuthentication.findByPk(otherAuth.id); + expect(auth).not.toBeNull(); + }); +}); diff --git a/server/routes/api/oauthAuthentications/oauthAuthentications.ts b/server/routes/api/oauthAuthentications/oauthAuthentications.ts index 91dda471f2..ca7c697482 100644 --- a/server/routes/api/oauthAuthentications/oauthAuthentications.ts +++ b/server/routes/api/oauthAuthentications/oauthAuthentications.ts @@ -66,12 +66,12 @@ router.post( transaction(), async (ctx: APIContext) => { const { user } = ctx.state.auth; - const { oauthClientId, scope } = ctx.request.body; + const { oauthClientId, scope } = ctx.input.body; const oauthAuthentications = await OAuthAuthentication.findAll({ where: { userId: user.id, oauthClientId, - scope, + ...(scope ? { scope } : {}), }, transaction: ctx.state.transaction, }); diff --git a/server/test/factories.ts b/server/test/factories.ts index eaa41725f7..f215227bec 100644 --- a/server/test/factories.ts +++ b/server/test/factories.ts @@ -756,15 +756,19 @@ export async function buildOAuthAuthorizationCode( } export async function buildOAuthAuthentication({ + oauthClientId, user, scope, }: { + oauthClientId?: string; user: User; scope: string[]; }) { - const oauthClient = await buildOAuthClient({ - teamId: user.teamId, - }); + const oauthClient = oauthClientId + ? await OAuthClient.findByPk(oauthClientId, { rejectOnEmpty: true }) + : await buildOAuthClient({ + teamId: user.teamId, + }); const oauthInterfaceClient = { id: oauthClient.clientId, grants: ["authorization_code"], diff --git a/server/utils/oauth/OAuthInterface.ts b/server/utils/oauth/OAuthInterface.ts index b0211b3d4f..7479f8fd1c 100644 --- a/server/utils/oauth/OAuthInterface.ts +++ b/server/utils/oauth/OAuthInterface.ts @@ -139,10 +139,12 @@ export const OAuthInterface: RefreshTokenModel & }, async saveToken(token, client, user) { - const accessToken = token.accessToken; - const refreshToken = token.refreshToken; - const accessTokenExpiresAt = token.accessTokenExpiresAt; - const refreshTokenExpiresAt = token.refreshTokenExpiresAt; + const { + accessToken, + refreshToken, + accessTokenExpiresAt, + refreshTokenExpiresAt, + } = token; const accessTokenHash = hash(accessToken); const refreshTokenHash = refreshToken ? hash(refreshToken) : undefined;