localAuth.test.ts 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. import { mkdtempSync, readFileSync, rmSync } from 'node:fs'
  2. import { tmpdir } from 'node:os'
  3. import { join } from 'node:path'
  4. import type Koa from 'koa'
  5. import { afterEach, describe, expect, it } from 'vitest'
  6. import { LocalAuthStore } from '../../src/server/localAuth.js'
  7. const homes: string[] = []
  8. function context(cookie = ''): Koa.Context {
  9. const headers: Record<string, string | string[]> = {}
  10. return {
  11. state: {},
  12. get: (name: string) => name.toLowerCase() === 'cookie' ? cookie : '',
  13. set: (name: string, value: string | string[]) => { headers[name.toLowerCase()] = value },
  14. response: { headers },
  15. } as unknown as Koa.Context
  16. }
  17. function issuedCookie(ctx: Koa.Context): string {
  18. const value = ctx.response.headers['set-cookie']
  19. const raw = Array.isArray(value) ? value.at(-1) : String(value)
  20. return raw.split(';', 1)[0]!
  21. }
  22. afterEach(() => {
  23. for (const home of homes.splice(0)) rmSync(home, { recursive: true, force: true })
  24. })
  25. describe('15300 local users', () => {
  26. it('requires an explicit first administrator and persists its session', () => {
  27. const home = mkdtempSync(join(tmpdir(), 'yaoyao-local-auth-'))
  28. homes.push(home)
  29. const store = new LocalAuthStore(home)
  30. expect(store.setupRequired).toBe(true)
  31. const setup = context()
  32. const admin = store.setupAdmin(setup, 'owner', 'new-password')
  33. expect(admin).toMatchObject({ username: 'owner', role: 'admin', mustChangePassword: false })
  34. expect(store.setupRequired).toBe(false)
  35. expect(() => store.setupAdmin(context(), 'second', 'new-password')).toThrow(/已经初始化/)
  36. expect(store.require(context(issuedCookie(setup))).username).toBe('owner')
  37. const restarted = new LocalAuthStore(home)
  38. expect(restarted.require(context(issuedCookie(setup))).username).toBe('owner')
  39. })
  40. it('lets the administrator create, reset, disable and delete ordinary users', () => {
  41. const home = mkdtempSync(join(tmpdir(), 'yaoyao-local-auth-'))
  42. homes.push(home)
  43. const store = new LocalAuthStore(home)
  44. const first = context()
  45. store.setupAdmin(first, 'admin', 'new-password')
  46. const admin = store.require(context(issuedCookie(first)))
  47. const user = store.create(admin, 'member', 'temporary-password')
  48. expect(user.mustChangePassword).toBe(true)
  49. expect(store.updateUser(admin, user.id, { enabled: false }).enabled).toBe(false)
  50. store.deleteUser(admin, user.id)
  51. expect(store.list(admin).map(item => item.username)).toEqual(['admin'])
  52. })
  53. it('persists an account image without invalidating the existing session', () => {
  54. const home = mkdtempSync(join(tmpdir(), 'yaoyao-local-auth-')); homes.push(home)
  55. const store = new LocalAuthStore(home), login = context()
  56. store.setupAdmin(login, 'admin', 'new-password')
  57. const active = context(issuedCookie(login))
  58. const avatar = 'data:image/png;base64,aGVsbG8='
  59. expect(store.setAvatar(active, avatar).avatar).toBe(avatar)
  60. expect(store.require(active).avatar).toBe(avatar)
  61. expect(new LocalAuthStore(home).require(context(issuedCookie(login))).avatar).toBe(avatar)
  62. expect(() => store.setAvatar(active, 'https://example.test/avatar.png')).toThrow(/PNG、JPEG/)
  63. expect(store.setAvatar(active, null).avatar).toBeUndefined()
  64. })
  65. it('encrypts the 9119 service password at rest', () => {
  66. const home = mkdtempSync(join(tmpdir(), 'yaoyao-local-auth-'))
  67. homes.push(home)
  68. const store = new LocalAuthStore(home)
  69. const first = context()
  70. store.setupAdmin(first, 'admin', 'new-password')
  71. const admin = store.require(context(issuedCookie(first)))
  72. store.setUpstreamCredentials(admin, { username: 'service', password: 'very-secret' })
  73. expect(store.upstreamCredentials()).toEqual({ username: 'service', password: 'very-secret' })
  74. expect(readFileSync(join(home, 'upstream-credentials.enc'), 'utf8')).not.toContain('very-secret')
  75. })
  76. })