Skip to content

The Postgres development platform. Supabase gives you a dedicated Postgres database to build your web, mobile, and AI applications.

13.0 KB
Raw Blame
import { randomUUID } from 'crypto'
import { afterAll, expect, test } from 'vitest'

import { getPaginatedUsersSQL } from '../../../src/sql/studio/auth/get-users-paginated'
import { cleanupRoot, createDatabaseWithAuthSchema, createTestDatabase } from '../../db/utils'

afterAll(async () => {
  await cleanupRoot()
})

const withTestDatabase = (
  name: string,
  fn: (db: Awaited<ReturnType<typeof createTestDatabase>>) => Promise<void>
) => {
  test(name, async () => {
    const db = await createTestDatabase()

    // Initialize minimal auth schema needed for user pagination tests
    await createDatabaseWithAuthSchema(db, { includeIdentities: true })

    try {
      await fn(db)
    } finally {
      await db.cleanup()
    }
  })
}

withTestDatabase('returns paginated users with default settings', async ({ executeQuery }) => {
  // Insert 10 users with different created_at times
  for (let i = 0; i < 10; i++) {
    await executeQuery(`
      INSERT INTO auth.users (id, email, instance_id, created_at)
      VALUES (
        '${randomUUID()}',
        'user${i}@supabase.io',
        '00000000-0000-0000-0000-000000000000',
        NOW() + INTERVAL '${i} minutes'
      )
    `)
  }

  // Get first page (default limit 50, page 0)
  const sql = getPaginatedUsersSQL({ sort: 'created_at', order: 'desc' })
  const result = await executeQuery<Array<{ email: string }>>(sql)

  expect(result.length).toBe(10)
  // Should be ordered by created_at desc, so user9 should be first
  expect(result[0].email).toBe('[email protected]')
  expect(result[9].email).toBe('[email protected]')
})

withTestDatabase('respects custom limit and pagination', async ({ executeQuery }) => {
  // Insert 30 users
  for (let i = 0; i < 30; i++) {
    await executeQuery(`
      INSERT INTO auth.users (id, email, instance_id, created_at)
      VALUES (
        '${randomUUID()}',
        'user${String(i).padStart(2, '0')}@supabase.io',
        '00000000-0000-0000-0000-000000000000',
        NOW() + INTERVAL '${i} minutes'
      )
    `)
  }

  // Get page 1 with limit 10 (should skip first 10, get next 10)
  const sql = getPaginatedUsersSQL({ sort: 'created_at', order: 'desc', limit: 10, page: 1 })
  const result = await executeQuery<Array<{ email: string }>>(sql)

  expect(result.length).toBe(10)
  // Page 1 should start at user19 (0-indexed, skipping first 10)
  expect(result[0].email).toBe('[email protected]')
  expect(result[9].email).toBe('[email protected]')
})

withTestDatabase('sorts by email in ascending order', async ({ executeQuery }) => {
  await executeQuery(`
    INSERT INTO auth.users (id, email, instance_id, created_at)
    VALUES 
      ('${randomUUID()}', '[email protected]', '00000000-0000-0000-0000-000000000000', NOW()),
      ('${randomUUID()}', '[email protected]', '00000000-0000-0000-0000-000000000000', NOW()),
      ('${randomUUID()}', '[email protected]', '00000000-0000-0000-0000-000000000000', NOW())
  `)

  const sql = getPaginatedUsersSQL({ sort: 'email', order: 'asc', limit: 10 })
  const result = await executeQuery<Array<{ email: string }>>(sql)

  expect(result.length).toBe(3)
  expect(result[0].email).toBe('[email protected]')
  expect(result[1].email).toBe('[email protected]')
  expect(result[2].email).toBe('[email protected]')
})

withTestDatabase('filters by keywords across multiple fields', async ({ executeQuery }) => {
  const searchUserId = randomUUID()

  await executeQuery(`
    INSERT INTO auth.users (id, email, phone, instance_id, raw_user_meta_data, created_at)
    VALUES 
      ('${searchUserId}', '[email protected]', NULL, '00000000-0000-0000-0000-000000000000', '{"full_name": "John Doe"}', NOW()),
      ('${randomUUID()}', '[email protected]', '+1234567890', '00000000-0000-0000-0000-000000000000', '{"full_name": "Jane Smith"}', NOW()),
      ('${randomUUID()}', '[email protected]', '+9876543210', '00000000-0000-0000-0000-000000000000', '{"full_name": "Bob Jones"}', NOW())
  `)

  // Search by keyword "john" - should find john.doe by email
  const emailSql = getPaginatedUsersSQL({ sort: 'created_at', order: 'desc', keywords: 'john' })
  const emailResult = await executeQuery<Array<{ email: string }>>(emailSql)
  expect(emailResult.length).toBe(1)
  expect(emailResult[0].email).toBe('[email protected]')

  // Search by phone
  const phoneSql = getPaginatedUsersSQL({ sort: 'created_at', order: 'desc', keywords: '1234' })
  const phoneResult = await executeQuery<Array<{ phone: string }>>(phoneSql)
  expect(phoneResult.length).toBe(1)
  expect(phoneResult[0].phone).toBe('+1234567890')

  // Search by ID prefix
  const idPrefix = searchUserId.substring(0, 8)
  const idSql = getPaginatedUsersSQL({ sort: 'created_at', order: 'desc', keywords: idPrefix })
  const idResult = await executeQuery<Array<{ email: string }>>(idSql)
  expect(idResult.length).toBe(1)
  expect(idResult[0].email).toBe('[email protected]')
})

withTestDatabase('filters verified and unverified users', async ({ executeQuery }) => {
  const now = new Date().toISOString()

  await executeQuery(`
    INSERT INTO auth.users (id, email, instance_id, email_confirmed_at, created_at)
    VALUES 
      ('${randomUUID()}', '[email protected]', '00000000-0000-0000-0000-000000000000', '${now}', NOW()),
      ('${randomUUID()}', '[email protected]', '00000000-0000-0000-0000-000000000000', '${now}', NOW()),
      ('${randomUUID()}', '[email protected]', '00000000-0000-0000-0000-000000000000', NULL, NOW())
  `)

  // Filter verified users
  const verifiedSql = getPaginatedUsersSQL({
    sort: 'created_at',
    order: 'desc',
    verified: 'verified',
  })
  const verifiedResult = await executeQuery<Array<{ email: string }>>(verifiedSql)
  expect(verifiedResult.length).toBe(2)

  // Filter unverified users
  const unverifiedSql = getPaginatedUsersSQL({
    sort: 'created_at',
    order: 'desc',
    verified: 'unverified',
  })
  const unverifiedResult = await executeQuery<Array<{ email: string }>>(unverifiedSql)
  expect(unverifiedResult.length).toBe(1)
  expect(unverifiedResult[0].email).toBe('[email protected]')
})

withTestDatabase('filters anonymous users', async ({ executeQuery }) => {
  await executeQuery(`
    INSERT INTO auth.users (id, email, instance_id, is_anonymous, created_at)
    VALUES 
      ('${randomUUID()}', '[email protected]', '00000000-0000-0000-0000-000000000000', true, NOW()),
      ('${randomUUID()}', '[email protected]', '00000000-0000-0000-0000-000000000000', true, NOW()),
      ('${randomUUID()}', '[email protected]', '00000000-0000-0000-0000-000000000000', false, NOW())
  `)

  const sql = getPaginatedUsersSQL({ sort: 'created_at', order: 'desc', verified: 'anonymous' })
  const result = await executeQuery<Array<{ email: string }>>(sql)
  expect(result.length).toBe(2)
})

withTestDatabase('filters by providers', async ({ executeQuery }) => {
  const user1 = randomUUID()
  const user2 = randomUUID()
  const user3 = randomUUID()

  await executeQuery(`
    INSERT INTO auth.users (id, email, instance_id, raw_app_meta_data, created_at)
    VALUES 
      ('${user1}', '[email protected]', '00000000-0000-0000-0000-000000000000', '{"providers": ["google"]}', NOW()),
      ('${user2}', '[email protected]', '00000000-0000-0000-0000-000000000000', '{"providers": ["google"]}', NOW()),
      ('${user3}', '[email protected]', '00000000-0000-0000-0000-000000000000', '{"providers": ["github"]}', NOW())
  `)

  // Also insert identities for the users
  await executeQuery(`
    INSERT INTO auth.identities (id, user_id, provider, identity_data, created_at)
    VALUES 
      ('google1', '${user1}', 'google', '{}', NOW()),
      ('google2', '${user2}', 'google', '{}', NOW()),
      ('github1', '${user3}', 'github', '{}', NOW())
  `)

  // Filter by google provider
  const googleSql = getPaginatedUsersSQL({
    sort: 'created_at',
    order: 'desc',
    providers: ['google'],
  })
  const googleResult = await executeQuery<Array<{ email: string }>>(googleSql)
  expect(googleResult.length).toBe(2)

  // Filter by multiple providers
  const multipleSql = getPaginatedUsersSQL({
    sort: 'created_at',
    order: 'desc',
    providers: ['google', 'github'],
  })
  const multipleResult = await executeQuery<Array<{ email: string }>>(multipleSql)
  expect(multipleResult.length).toBe(3)
})

withTestDatabase('combines multiple filters', async ({ executeQuery }) => {
  const now = new Date().toISOString()

  await executeQuery(`
    INSERT INTO auth.users (id, email, instance_id, email_confirmed_at, raw_app_meta_data, created_at)
    VALUES 
      ('${randomUUID()}', '[email protected]', '00000000-0000-0000-0000-000000000000', '${now}', '{"providers": ["google"]}', NOW()),
      ('${randomUUID()}', '[email protected]', '00000000-0000-0000-0000-000000000000', '${now}', '{"providers": ["github"]}', NOW()),
      ('${randomUUID()}', '[email protected]', '00000000-0000-0000-0000-000000000000', NULL, '{"providers": ["google"]}', NOW())
  `)

  // Combine verified filter with provider and keyword
  const sql = getPaginatedUsersSQL({
    sort: 'created_at',
    order: 'desc',
    keywords: 'verified',
    verified: 'verified',
    providers: ['google'],
  })
  const result = await executeQuery<Array<{ email: string }>>(sql)
  expect(result.length).toBe(1)
  expect(result[0].email).toBe('[email protected]')
})

withTestDatabase('optimized email search works correctly', async ({ executeQuery }) => {
  await executeQuery(`
    INSERT INTO auth.users (id, email, instance_id, created_at)
    VALUES 
      ('${randomUUID()}', '[email protected]', '00000000-0000-0000-0000-000000000000', NOW()),
      ('${randomUUID()}', '[email protected]', '00000000-0000-0000-0000-000000000000', NOW()),
      ('${randomUUID()}', '[email protected]', '00000000-0000-0000-0000-000000000000', NOW())
  `)

  // Optimized search by email prefix
  const sql = getPaginatedUsersSQL({
    sort: 'created_at',
    order: 'desc',
    column: 'email',
    keywords: 'ali',
    limit: 10,
  })
  const result = await executeQuery<Array<{ email: string }>>(sql)
  expect(result.length).toBe(2)
  expect(result[0].email).toBe('[email protected]')
  expect(result[1].email).toBe('[email protected]')
})

withTestDatabase('optimized phone search works correctly', async ({ executeQuery }) => {
  await executeQuery(`
    INSERT INTO auth.users (id, phone, instance_id, created_at)
    VALUES 
      ('${randomUUID()}', '+1234567890', '00000000-0000-0000-0000-000000000000', NOW()),
      ('${randomUUID()}', '+1234999999', '00000000-0000-0000-0000-000000000000', NOW()),
      ('${randomUUID()}', '+9876543210', '00000000-0000-0000-0000-000000000000', NOW())
  `)

  // Optimized search by phone prefix
  const sql = getPaginatedUsersSQL({
    sort: 'created_at',
    order: 'desc',
    column: 'phone',
    keywords: '+1234',
    limit: 10,
  })
  const result = await executeQuery<Array<{ phone: string }>>(sql)
  expect(result.length).toBe(2)
})

withTestDatabase('optimized id search works correctly', async ({ executeQuery }) => {
  const userId1 = 'abc12300-0000-4000-8000-000000000001'
  const userId2 = 'abc12300-0000-4000-8000-000000000002'
  const userId3 = 'def45600-0000-4000-8000-000000000003'

  await executeQuery(`
    INSERT INTO auth.users (id, email, instance_id, created_at)
    VALUES 
      ('${userId1}', '[email protected]', '00000000-0000-0000-0000-000000000000', NOW()),
      ('${userId2}', '[email protected]', '00000000-0000-0000-0000-000000000000', NOW()),
      ('${userId3}', '[email protected]', '00000000-0000-0000-0000-000000000000', NOW())
  `)

  // Optimized search by id prefix
  const sql = getPaginatedUsersSQL({
    sort: 'created_at',
    order: 'desc',
    column: 'id',
    keywords: 'abc123',
    limit: 10,
  })
  const result = await executeQuery<Array<{ id: string }>>(sql)
  expect(result.length).toBe(2)
  expect(result[0].id).toBe(userId1)
  expect(result[1].id).toBe(userId2)

  // Exact UUID match
  const exactSql = getPaginatedUsersSQL({
    sort: 'created_at',
    order: 'desc',
    column: 'id',
    keywords: userId1,
    limit: 10,
  })
  const exactResult = await executeQuery<Array<{ id: string }>>(exactSql)
  expect(exactResult.length).toBe(1)
  expect(exactResult[0].id).toBe(userId1)
})

withTestDatabase('includes provider information from identities', async ({ executeQuery }) => {
  const userId = randomUUID()

  await executeQuery(`
    INSERT INTO auth.users (id, email, instance_id, created_at)
    VALUES ('${userId}', '[email protected]', '00000000-0000-0000-0000-000000000000', NOW())
  `)

  // Insert multiple identities for the user
  await executeQuery(`
    INSERT INTO auth.identities (id, user_id, provider, identity_data, created_at)
    VALUES 
      ('google1', '${userId}', 'google', '{}', NOW()),
      ('github1', '${userId}', 'github', '{}', NOW())
  `)

  const sql = getPaginatedUsersSQL({ sort: 'created_at', order: 'desc', limit: 10 })
  const result = await executeQuery<Array<{ email: string; providers: string[] }>>(sql)

  expect(result.length).toBe(1)
  expect(result[0].email).toBe('[email protected]')
  expect(result[0].providers).toHaveLength(2)
  expect(result[0].providers).toContain('google')
  expect(result[0].providers).toContain('github')
})