authenticatedApplication.ts 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. import type Koa from 'koa'
  2. import {
  3. createApplication,
  4. type ApplicationOptions,
  5. type ApplicationRuntime,
  6. } from '../../src/server/app.js'
  7. import type { ServerConfig } from '../../src/server/config.js'
  8. import { LocalAuthStore, type LocalUser } from '../../src/server/localAuth.js'
  9. const TEST_ADMIN: LocalUser = {
  10. id: 'test-admin',
  11. username: 'test-admin',
  12. role: 'admin',
  13. enabled: true,
  14. mustChangePassword: false,
  15. createdAt: 1,
  16. updatedAt: 1,
  17. }
  18. const TEST_USER: LocalUser = {
  19. ...TEST_ADMIN,
  20. id: 'test-user',
  21. username: 'test-user',
  22. role: 'user',
  23. }
  24. class AuthenticatedTestStore extends LocalAuthStore {
  25. override currentFromCookieHeader(_cookie: string | undefined): LocalUser { return TEST_ADMIN }
  26. override current(_ctx: Koa.Context): LocalUser { return TEST_ADMIN }
  27. override require(_ctx: Koa.Context, _allowPasswordChange = false): LocalUser { return TEST_ADMIN }
  28. override requireAdmin(_ctx: Koa.Context): LocalUser { return TEST_ADMIN }
  29. }
  30. class UserAuthenticatedTestStore extends LocalAuthStore {
  31. override currentFromCookieHeader(_cookie: string | undefined): LocalUser { return TEST_USER }
  32. override current(_ctx: Koa.Context): LocalUser { return TEST_USER }
  33. override require(_ctx: Koa.Context, _allowPasswordChange = false): LocalUser { return TEST_USER }
  34. }
  35. export function createAuthenticatedApplication(
  36. options: ApplicationOptions & { config: ServerConfig },
  37. ): ApplicationRuntime {
  38. return createApplication({
  39. ...options,
  40. auth: new AuthenticatedTestStore(options.config.home, Boolean(options.config.tlsCert)),
  41. })
  42. }
  43. export function createUserAuthenticatedApplication(
  44. options: ApplicationOptions & { config: ServerConfig },
  45. ): ApplicationRuntime {
  46. return createApplication({
  47. ...options,
  48. auth: new UserAuthenticatedTestStore(options.config.home, Boolean(options.config.tlsCert)),
  49. })
  50. }