| 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 | weeklyResumeEnabled: false,
|
| 24 | onboardingSeenAt: null,
|
| 25 | termsAcceptedAt: new Date(),
|
| 26 | termsVersion: '1',
|
| 27 | } as User
|
| 28 |
|
| 29 | describe('publicUser', () => {
|
| 30 | it('keeps the profile fields the client needs', () => {
|
| 31 | expect(publicUser(row)).toMatchObject({
|
| 32 | id: 7,
|
| 33 | email: 'pedro@example.com',
|
| 34 | firstName: 'Pedro',
|
| 35 | active: true,
|
| 36 | weeklyResumeEnabled: false,
|
| 37 | onboardingSeenAt: null,
|
| 38 | })
|
| 39 | })
|
| 40 |
|
| 41 | it('never carries a secret off the server', () => {
|
| 42 | const safe = publicUser(row) as Record<string, unknown>
|
| 43 |
|
| 44 | for (const field of [
|
| 45 | 'encryptedPassword',
|
| 46 | 'passwordResetToken',
|
| 47 | 'passwordResetExpires',
|
| 48 | 'activationCode',
|
| 49 | 'activationCodeExpiresAt',
|
| 50 | 'pushToken',
|
| 51 | 'termsAcceptedAt',
|
| 52 | 'termsVersion',
|
| 53 | ]) {
|
| 54 | expect(safe).not.toHaveProperty(field)
|
| 55 | }
|
| 56 | })
|
| 57 | })
|