eletrotupi / tcc/ api/tests/serializers/user.test.tsff12ba99add07246bdaf39737d2c64072be1cfbf
1.4 KB | 55 lines Raw
1
import { describe, it, expect } from 'vitest'
2
import { User } from '@prisma/client'
3
import { publicUser } from '@app/lib/serializers/user'
4

5
const row = {
6
  id: 7,
7
  firstName: 'Pedro',
8
  lastName: 'Silva',
9
  email: 'pedro@example.com',
10
  encryptedPassword: 'bcrypt-hash',
11
  passwordResetToken: 'reset-secret',
12
  passwordResetExpires: new Date(),
13
  createdAt: new Date(),
14
  updatedAt: new Date(),
15
  avatarKey: 'avatars/pedro.png',
16
  avatarURL: 'https://cdn.example.com/avatars/pedro.png',
17
  activationCode: '123456',
18
  activationCodeExpiresAt: new Date(),
19
  active: true,
20
  pushToken: 'device-token',
21
  notificationsEnabled: true,
22
  dailyReminderTime: '08:30',
23
  onboardingSeenAt: null,
24
  termsAcceptedAt: new Date(),
25
  termsVersion: '1',
26
} as User
27

28
describe('publicUser', () => {
29
  it('keeps the profile fields the client needs', () => {
30
    expect(publicUser(row)).toMatchObject({
31
      id: 7,
32
      email: 'pedro@example.com',
33
      firstName: 'Pedro',
34
      active: true,
35
      onboardingSeenAt: null,
36
    })
37
  })
38

39
  it('never carries a secret off the server', () => {
40
    const safe = publicUser(row) as Record<string, unknown>
41

42
    for (const field of [
43
      'encryptedPassword',
44
      'passwordResetToken',
45
      'passwordResetExpires',
46
      'activationCode',
47
      'activationCodeExpiresAt',
48
      'pushToken',
49
      'termsAcceptedAt',
50
      'termsVersion',
51
    ]) {
52
      expect(safe).not.toHaveProperty(field)
53
    }
54
  })
55
})