| 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: { pushEnabled: true, dailyReminderTime: '08:30' } })
|
| 34 |
|
| 35 | expect(res.status).toBe(200)
|
| 36 | expect(res.body.user.pushEnabled).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('schedules the daily reminder even when push is disabled', async () => {
|
| 53 | const user = await createTestUser(db, {
|
| 54 | email: 'nopush@example.com',
|
| 55 | pushEnabled: false,
|
| 56 | })
|
| 57 | const token = generateTestToken(user.id, user.email)
|
| 58 |
|
| 59 | const res = await request(app)
|
| 60 | .patch('/users/me')
|
| 61 | .set('Authorization', `Bearer ${token}`)
|
| 62 | .send({ user: { dailyReminderTime: '08:30' } })
|
| 63 |
|
| 64 | expect(res.status).toBe(200)
|
| 65 |
|
| 66 | expect(recordedJobs).toContainEqual(
|
| 67 | expect.objectContaining({
|
| 68 | queue: 'daily-reminders',
|
| 69 | name: DailyReminderJobName.Send,
|
| 70 | data: { userId: user.id, scheduledTime: '08:30' },
|
| 71 | })
|
| 72 | )
|
| 73 | })
|
| 74 |
|
| 75 | it('updates the weekly resume preference', async () => {
|
| 76 | const user = await createTestUser(db, { email: 'prefs@example.com' })
|
| 77 | const token = generateTestToken(user.id, user.email)
|
| 78 |
|
| 79 | const res = await request(app)
|
| 80 | .patch('/users/me')
|
| 81 | .set('Authorization', `Bearer ${token}`)
|
| 82 | .send({ user: { weeklyResumeEnabled: true } })
|
| 83 |
|
| 84 | expect(res.status).toBe(200)
|
| 85 | expect(res.body.user.weeklyResumeEnabled).toBe(true)
|
| 86 | expectPublicUser(res.body.user)
|
| 87 |
|
| 88 | const dbUser = await db.user.findUnique({ where: { id: user.id } })
|
| 89 | expect(dbUser!.weeklyResumeEnabled).toBe(true)
|
| 90 | })
|
| 91 |
|
| 92 | it('returns 400 for a non-boolean weeklyResumeEnabled', async () => {
|
| 93 | const user = await createTestUser(db, { email: 'prefs@example.com' })
|
| 94 | const token = generateTestToken(user.id, user.email)
|
| 95 |
|
| 96 | const res = await request(app)
|
| 97 | .patch('/users/me')
|
| 98 | .set('Authorization', `Bearer ${token}`)
|
| 99 | .send({ user: { weeklyResumeEnabled: 'yes' } })
|
| 100 |
|
| 101 | expect(res.status).toBe(400)
|
| 102 | })
|
| 103 |
|
| 104 | it('accepts a null reminder time and schedules nothing', async () => {
|
| 105 | const user = await createTestUser(db, { email: 'prefs@example.com' })
|
| 106 | const token = generateTestToken(user.id, user.email)
|
| 107 |
|
| 108 | const res = await request(app)
|
| 109 | .patch('/users/me')
|
| 110 | .set('Authorization', `Bearer ${token}`)
|
| 111 | .send({ user: { dailyReminderTime: null } })
|
| 112 |
|
| 113 | expect(res.status).toBe(200)
|
| 114 | expect(res.body.user.dailyReminderTime).toBeNull()
|
| 115 | expect(recordedJobs).toHaveLength(0)
|
| 116 | })
|
| 117 |
|
| 118 | it('returns 400 for a malformed time', async () => {
|
| 119 | const user = await createTestUser(db, { email: 'prefs@example.com' })
|
| 120 | const token = generateTestToken(user.id, user.email)
|
| 121 |
|
| 122 | const res = await request(app)
|
| 123 | .patch('/users/me')
|
| 124 | .set('Authorization', `Bearer ${token}`)
|
| 125 | .send({ user: { dailyReminderTime: '9:5' } })
|
| 126 |
|
| 127 | expect(res.status).toBe(400)
|
| 128 | expect(res.body.fields).toBeTruthy()
|
| 129 | })
|
| 130 |
|
| 131 | it('returns 400 for a non-boolean pushEnabled', async () => {
|
| 132 | const user = await createTestUser(db, { email: 'prefs@example.com' })
|
| 133 | const token = generateTestToken(user.id, user.email)
|
| 134 |
|
| 135 | const res = await request(app)
|
| 136 | .patch('/users/me')
|
| 137 | .set('Authorization', `Bearer ${token}`)
|
| 138 | .send({ user: { pushEnabled: 'yes' } })
|
| 139 |
|
| 140 | expect(res.status).toBe(400)
|
| 141 | })
|
| 142 |
|
| 143 | it('returns 401 without a token', async () => {
|
| 144 | const res = await request(app)
|
| 145 | .patch('/users/me')
|
| 146 | .send({ user: { pushEnabled: false } })
|
| 147 |
|
| 148 | expect(res.status).toBe(401)
|
| 149 | })
|
| 150 | })
|