Skip to content

armgit / papra

Public Imported from GitHub
0

The minimalistic document archiving platform.

3.2 KB
Raw Blame
import { inspectRoutes } from 'hono/dev';
import { describe, expect, test } from 'vitest';
import { overrideConfig } from '../config/config.test-utils';
import { SHARE_LINK_TOKEN_LENGTH } from '../document-share-links/document-share-links.constants';
import { createInMemoryDatabase } from './database/database.test-utils';
import { createServer } from './server';
import { createTestServerDependencies } from './server.test-utils';

function setValidParams(path: string) {
  const newPath = path
    .replaceAll(':organizationId', 'org_111111111111111111111111')
    .replaceAll(':userId', 'user_222222222222222222222222')
    .replaceAll(':documentId', 'doc_333333333333333333333333')
    .replaceAll(':tagId', 'tag_444444444444444444444444')
    .replaceAll(':taggingRuleId', 'rule_555555555555555555555555')
    .replaceAll(':intakeEmailId', 'email_666666666666666666666666')
    .replaceAll(':apiKeyId', 'api_key_777777777777777777777777')
    .replaceAll(':webhookId', 'wbh_888888888888888888888888')
    .replaceAll(':memberId', 'org_mem_999999999999999999999999')
    .replaceAll(':invitationId', 'inv_101010101010101010101010')
    .replaceAll(':propertyDefinitionId', 'cpd_111111111111111111111111')
    .replaceAll(':optionId', 'cpso_222222222222222222222222')
    .replaceAll(':shareLinkId', 'dsl_333333333333333333333333')
    .replaceAll(':shareLinkToken', 't'.repeat(SHARE_LINK_TOKEN_LENGTH))
    .replaceAll(':documentViewId', 'dv_444444444444444444444444')
    .replaceAll(':planEntitlementId', 'pla_ent_555555555555555555555555')
    .replaceAll(':type', 'selfhst-premium');

  // throw if there are any remaining params
  if (newPath.match(/:\w+/g)) {
    throw new Error(`Add a dummy value for the params in ${path}`);
  }

  return newPath;
}

describe('server routes', () => {
  test('all routes should respond with a 401 when non-authenticated, except for public and auth-related routes', async () => {
    const { db } = await createInMemoryDatabase();
    const { app } = createServer(createTestServerDependencies({ db, config: overrideConfig() }));

    const publicRoutes = [
      // Authentication is handled by payload signature verification
      'POST /api/intake-emails/ingest',

      // Stripe stuff
      'POST /api/stripe/webhook',

      // Document share link access
      'GET /api/share-links/:shareLinkToken/document',
      'POST /api/share-links/:shareLinkToken/verify',
      'GET /api/share-links/:shareLinkToken/document/file',
    ];

    // Excluding auth routes that are managed by better-auth
    const authRoutesPrefix = '/api/auth';

    const routes = inspectRoutes(app)
      .filter((route) => !route.isMiddleware)
      .filter((route) => !publicRoutes.includes(`${route.method} ${route.path}`))
      .filter((route) => !route.path.startsWith(authRoutesPrefix));

    for (const route of routes) {
      const response = await app.request(setValidParams(route.path), {
        method: route.method,
      });

      expect(response.status).to.eql(
        401,
        `Route ${route.method} ${route.path} did not return 401 when not authenticated`,
      );
      expect(await response.json()).to.eql({
        error: {
          message: 'Unauthorized',
          code: 'auth.unauthorized',
        },
      });
    }
  });
});