Skip to content

A desktop coding agent with free access to frontier models.

TypeScript
3.3 KB
Raw Blame
import { beforeAll, expect, mock, test } from "bun:test"
import {
  AuthSessionTable,
  OAuthAccessTokenTable,
  OAuthConsentTable,
  OAuthRefreshTokenTable,
} from "@omnirush-ee/den-db/schema"

const selectedRows = {
  sessions: [{ id: "session_one" }, { id: "session_two" }],
  oauthAccessTokens: [{ id: "oauth_access_one" }],
  oauthConsents: [{ id: "oauth_consent_one" }],
  oauthRefreshTokens: [{ id: "oauth_refresh_one" }],
}

const deleteCalls: unknown[] = []
const updateCalls: { table: unknown; value: unknown }[] = []
let selectCalls = 0

function rowsForTable(table: unknown) {
  if (table === AuthSessionTable) {
    return selectedRows.sessions
  }
  if (table === OAuthAccessTokenTable) {
    return selectedRows.oauthAccessTokens
  }
  if (table === OAuthRefreshTokenTable) {
    return selectedRows.oauthRefreshTokens
  }
  if (table === OAuthConsentTable) {
    return selectedRows.oauthConsents
  }
  return []
}

function resetCalls() {
  deleteCalls.length = 0
  updateCalls.length = 0
  selectCalls = 0
}

let credentialRevocationModule: typeof import("../src/credential-revocation.js")

beforeAll(async () => {
  process.env.DATABASE_URL = process.env.DATABASE_URL ?? "mysql://root:[email protected]:3306/omnirush_test"
  process.env.DEN_DB_ENCRYPTION_KEY = process.env.DEN_DB_ENCRYPTION_KEY ?? "x".repeat(32)
  process.env.BETTER_AUTH_SECRET = process.env.BETTER_AUTH_SECRET ?? "y".repeat(32)
  process.env.BETTER_AUTH_URL = process.env.BETTER_AUTH_URL ?? "http://127.0.0.1:8790"

  mock.module("../src/db.js", () => ({
    db: {
      select: () => ({
        from: (table: unknown) => ({
          where: () => {
            selectCalls += 1
            return Promise.resolve(rowsForTable(table))
          },
        }),
      }),
      delete: (table: unknown) => ({
        where: () => {
          deleteCalls.push(table)
          return Promise.resolve()
        },
      }),
      update: (table: unknown) => ({
        set: (value: unknown) => ({
          where: () => {
            updateCalls.push({ table, value })
            return Promise.resolve()
          },
        }),
      }),
    },
  }))

  credentialRevocationModule = await import("../src/credential-revocation.js")
})

test("membership credential revocation deletes sessions and org-scoped OAuth access tokens", async () => {
  resetCalls()

  const counts = await credentialRevocationModule.revokeMembershipSessionCredentials({
    organizationId: "org_123",
    userId: "user_123",
  })

  expect(counts).toEqual({
    sessions: 2,
    oauthAccessTokens: 1,
    oauthRefreshTokens: 1,
  })
  expect(selectCalls).toBe(4)
  expect(deleteCalls).toEqual([
    AuthSessionTable,
    OAuthAccessTokenTable,
    OAuthConsentTable,
  ])
  expect(updateCalls).toHaveLength(1)
  expect(updateCalls[0]?.table).toBe(OAuthRefreshTokenTable)
  expect(updateCalls[0]?.value).toEqual({ revoked: expect.any(Date) })
})

test("membership credential revocation skips anonymous pending members", async () => {
  resetCalls()

  const counts = await credentialRevocationModule.revokeMembershipSessionCredentials({
    organizationId: "org_123",
    userId: null,
  })

  expect(counts).toEqual({
    sessions: 0,
    oauthAccessTokens: 0,
    oauthRefreshTokens: 0,
  })
  expect(selectCalls).toBe(0)
  expect(deleteCalls).toEqual([])
  expect(updateCalls).toEqual([])
})