| 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 | } from '../test-helper'
|
| 11 |
|
| 12 | import { recordedJobs, resetQueueMocks } from '../mocks/queue'
|
| 13 |
|
| 14 | const app = createApp()
|
| 15 | const db = buildPrisma()
|
| 16 |
|
| 17 | afterAll(() => db.$disconnect())
|
| 18 | beforeEach(async () => {
|
| 19 | await cleanupTestDb(db)
|
| 20 |
|
| 21 | resetQueueMocks()
|
| 22 | })
|
| 23 |
|
| 24 | describe('PATCH /users/me', () => {
|
| 25 | it('updates notification preferences and schedules a reminder', async () => {
|
| 26 | const user = await createTestUser(db, { email: 'prefs@example.com' })
|
| 27 | const token = generateTestToken(user.id, user.email)
|
| 28 |
|
| 29 | const res = await request(app)
|
| 30 | .patch('/users/me')
|
| 31 | .set('Authorization', `Bearer ${token}`)
|
| 32 | .send({ user: { notificationsEnabled: true, dailyReminderTime: '08:30' } })
|
| 33 |
|
| 34 | expect(res.status).toBe(200)
|
| 35 | expect(res.body.user.notificationsEnabled).toBe(true)
|
| 36 | expect(res.body.user.dailyReminderTime).toBe('08:30')
|
| 37 |
|
| 38 | const dbUser = await db.user.findUnique({ where: { id: user.id } })
|
| 39 | expect(dbUser!.dailyReminderTime).toBe('08:30')
|
| 40 |
|
| 41 | expect(recordedJobs).toContainEqual(
|
| 42 | expect.objectContaining({
|
| 43 | queue: 'daily-reminders',
|
| 44 | name: DailyReminderJobName.Send,
|
| 45 | data: { userId: user.id },
|
| 46 | })
|
| 47 | )
|
| 48 | })
|
| 49 |
|
| 50 | it('accepts a null reminder time and schedules nothing', async () => {
|
| 51 | const user = await createTestUser(db, { email: 'prefs@example.com' })
|
| 52 | const token = generateTestToken(user.id, user.email)
|
| 53 |
|
| 54 | const res = await request(app)
|
| 55 | .patch('/users/me')
|
| 56 | .set('Authorization', `Bearer ${token}`)
|
| 57 | .send({ user: { dailyReminderTime: null } })
|
| 58 |
|
| 59 | expect(res.status).toBe(200)
|
| 60 | expect(res.body.user.dailyReminderTime).toBeNull()
|
| 61 | expect(recordedJobs).toHaveLength(0)
|
| 62 | })
|
| 63 |
|
| 64 | it('returns 400 for a malformed time', async () => {
|
| 65 | const user = await createTestUser(db, { email: 'prefs@example.com' })
|
| 66 | const token = generateTestToken(user.id, user.email)
|
| 67 |
|
| 68 | const res = await request(app)
|
| 69 | .patch('/users/me')
|
| 70 | .set('Authorization', `Bearer ${token}`)
|
| 71 | .send({ user: { dailyReminderTime: '9:5' } })
|
| 72 |
|
| 73 | expect(res.status).toBe(400)
|
| 74 | expect(res.body.fields).toBeTruthy()
|
| 75 | })
|
| 76 |
|
| 77 | it('returns 400 for a non-boolean notificationsEnabled', async () => {
|
| 78 | const user = await createTestUser(db, { email: 'prefs@example.com' })
|
| 79 | const token = generateTestToken(user.id, user.email)
|
| 80 |
|
| 81 | const res = await request(app)
|
| 82 | .patch('/users/me')
|
| 83 | .set('Authorization', `Bearer ${token}`)
|
| 84 | .send({ user: { notificationsEnabled: 'yes' } })
|
| 85 |
|
| 86 | expect(res.status).toBe(400)
|
| 87 | })
|
| 88 |
|
| 89 | it('returns 401 without a token', async () => {
|
| 90 | const res = await request(app)
|
| 91 | .patch('/users/me')
|
| 92 | .send({ user: { notificationsEnabled: false } })
|
| 93 |
|
| 94 | expect(res.status).toBe(401)
|
| 95 | })
|
| 96 | })
|