eletrotupi / tcc/ api/tests/users/preferences.test.tsdafa6f8a8e3b0e51605fc953a979d50ff6085b26
4.0 KB | 127 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, scheduledTime: '08:30' },
48
      })
49
    )
50
  })
51

52
  it('updates the weekly resume preference', 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: { weeklyResumeEnabled: true } })
60

61
    expect(res.status).toBe(200)
62
    expect(res.body.user.weeklyResumeEnabled).toBe(true)
63
    expectPublicUser(res.body.user)
64

65
    const dbUser = await db.user.findUnique({ where: { id: user.id } })
66
    expect(dbUser!.weeklyResumeEnabled).toBe(true)
67
  })
68

69
  it('returns 400 for a non-boolean weeklyResumeEnabled', async () => {
70
    const user = await createTestUser(db, { email: 'prefs@example.com' })
71
    const token = generateTestToken(user.id, user.email)
72

73
    const res = await request(app)
74
      .patch('/users/me')
75
      .set('Authorization', `Bearer ${token}`)
76
      .send({ user: { weeklyResumeEnabled: 'yes' } })
77

78
    expect(res.status).toBe(400)
79
  })
80

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

85
    const res = await request(app)
86
      .patch('/users/me')
87
      .set('Authorization', `Bearer ${token}`)
88
      .send({ user: { dailyReminderTime: null } })
89

90
    expect(res.status).toBe(200)
91
    expect(res.body.user.dailyReminderTime).toBeNull()
92
    expect(recordedJobs).toHaveLength(0)
93
  })
94

95
  it('returns 400 for a malformed time', async () => {
96
    const user = await createTestUser(db, { email: 'prefs@example.com' })
97
    const token = generateTestToken(user.id, user.email)
98

99
    const res = await request(app)
100
      .patch('/users/me')
101
      .set('Authorization', `Bearer ${token}`)
102
      .send({ user: { dailyReminderTime: '9:5' } })
103

104
    expect(res.status).toBe(400)
105
    expect(res.body.fields).toBeTruthy()
106
  })
107

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

112
    const res = await request(app)
113
      .patch('/users/me')
114
      .set('Authorization', `Bearer ${token}`)
115
      .send({ user: { notificationsEnabled: 'yes' } })
116

117
    expect(res.status).toBe(400)
118
  })
119

120
  it('returns 401 without a token', async () => {
121
    const res = await request(app)
122
      .patch('/users/me')
123
      .send({ user: { notificationsEnabled: false } })
124

125
    expect(res.status).toBe(401)
126
  })
127
})