eletrotupi / tcc/ api/tests/users/preferences.test.tsff12ba99add07246bdaf39737d2c64072be1cfbf
3.0 KB | 98 lines Raw
1
import { describe, it, expect, afterAll, beforeEach } from 'vitest'
2
import request from 'supertest'
3
import { createApp } from '@app/createApp'
4
import { DailyReminderJobName } from '@app/lib/queue/types'
5
import {
6
  buildPrisma,
7
  cleanupTestDb,
8
  createTestUser,
9
  generateTestToken,
10
  expectPublicUser,
11
} from '../test-helper'
12

13
import { recordedJobs, resetQueueMocks } from '../mocks/queue'
14

15
const app = createApp()
16
const db = buildPrisma()
17

18
afterAll(() => db.$disconnect())
19
beforeEach(async () => {
20
  await cleanupTestDb(db)
21

22
  resetQueueMocks()
23
})
24

25
describe('PATCH /users/me', () => {
26
  it('updates notification preferences and schedules a reminder', async () => {
27
    const user = await createTestUser(db, { email: 'prefs@example.com' })
28
    const token = generateTestToken(user.id, user.email)
29

30
    const res = await request(app)
31
      .patch('/users/me')
32
      .set('Authorization', `Bearer ${token}`)
33
      .send({ user: { notificationsEnabled: true, dailyReminderTime: '08:30' } })
34

35
    expect(res.status).toBe(200)
36
    expect(res.body.user.notificationsEnabled).toBe(true)
37
    expect(res.body.user.dailyReminderTime).toBe('08:30')
38
    expectPublicUser(res.body.user)
39

40
    const dbUser = await db.user.findUnique({ where: { id: user.id } })
41
    expect(dbUser!.dailyReminderTime).toBe('08:30')
42

43
    expect(recordedJobs).toContainEqual(
44
      expect.objectContaining({
45
        queue: 'daily-reminders',
46
        name: DailyReminderJobName.Send,
47
        data: { userId: user.id },
48
      })
49
    )
50
  })
51

52
  it('accepts a null reminder time and schedules nothing', async () => {
53
    const user = await createTestUser(db, { email: 'prefs@example.com' })
54
    const token = generateTestToken(user.id, user.email)
55

56
    const res = await request(app)
57
      .patch('/users/me')
58
      .set('Authorization', `Bearer ${token}`)
59
      .send({ user: { dailyReminderTime: null } })
60

61
    expect(res.status).toBe(200)
62
    expect(res.body.user.dailyReminderTime).toBeNull()
63
    expect(recordedJobs).toHaveLength(0)
64
  })
65

66
  it('returns 400 for a malformed time', async () => {
67
    const user = await createTestUser(db, { email: 'prefs@example.com' })
68
    const token = generateTestToken(user.id, user.email)
69

70
    const res = await request(app)
71
      .patch('/users/me')
72
      .set('Authorization', `Bearer ${token}`)
73
      .send({ user: { dailyReminderTime: '9:5' } })
74

75
    expect(res.status).toBe(400)
76
    expect(res.body.fields).toBeTruthy()
77
  })
78

79
  it('returns 400 for a non-boolean notificationsEnabled', async () => {
80
    const user = await createTestUser(db, { email: 'prefs@example.com' })
81
    const token = generateTestToken(user.id, user.email)
82

83
    const res = await request(app)
84
      .patch('/users/me')
85
      .set('Authorization', `Bearer ${token}`)
86
      .send({ user: { notificationsEnabled: 'yes' } })
87

88
    expect(res.status).toBe(400)
89
  })
90

91
  it('returns 401 without a token', async () => {
92
    const res = await request(app)
93
      .patch('/users/me')
94
      .send({ user: { notificationsEnabled: false } })
95

96
    expect(res.status).toBe(401)
97
  })
98
})