Skip to content

Sync-in · Sovereign platform for file storage, sharing, synchronization, and collaboration.

2.1 KB
Raw Blame
import { USER_ROLE } from '../constants/user.js'
import { UserModel } from './user.model.js'

describe(UserModel.name, () => {
  it('shows hidden files by default and preserves an explicit opt-out', () => {
    const defaultUser = new UserModel({ id: 1, login: 'alice', role: USER_ROLE.USER })
    const optOutUser = new UserModel({ id: 2, login: 'bob', role: USER_ROLE.USER, preferences: { showHiddenFiles: false } })

    expect(defaultUser.preferences.showHiddenFiles).toBe(true)
    expect(optOutUser.preferences.showHiddenFiles).toBe(false)
  })

  it('serializes only public user properties', () => {
    const user = new UserModel(
      {
        id: 1,
        login: 'alice',
        role: USER_ROLE.USER,
        password: 'password-hash',
        externalId: 'external-subject',
        secrets: { twoFaSecret: 'totp-secret', appPasswords: [{ name: 'desktop', password: 'app-password' }] }
      } as any,
      false
    )
    user.exp = 123456

    const serialized = JSON.parse(JSON.stringify(user))

    expect(serialized).not.toHaveProperty('password')
    expect(serialized).not.toHaveProperty('secrets')
    expect(serialized).not.toHaveProperty('externalId')
    expect(serialized).not.toHaveProperty('exp')
    expect(serialized).toEqual(expect.objectContaining({ id: 1, login: 'alice', twoFaEnabled: true, appPasswords: 1 }))
  })

  it('drops private properties when restoring a cached user', () => {
    const user = UserModel.fromSerialized({
      id: 1,
      login: 'alice',
      role: USER_ROLE.USER,
      applications: [],
      twoFaEnabled: false,
      password: 'password-hash',
      secrets: { twoFaSecret: 'totp-secret' },
      externalId: 'external-subject',
      exp: 123456
    })

    expect(user).not.toHaveProperty('password')
    expect(user.secrets).toEqual({ twoFaSecret: false })
    expect(user.externalId).toBeUndefined()
    expect(user.exp).toBeUndefined()
    const serialized = JSON.parse(JSON.stringify(user))
    expect(serialized).not.toHaveProperty('secrets')
    expect(serialized).not.toHaveProperty('externalId')
    expect(serialized).not.toHaveProperty('exp')
  })
})