eletrotupi / tcc/ api/tests/notifications/list.test.tsc901c35af9c84b6a29e5185545ced1129f33b8f4
4.9 KB | 153 lines Raw
1
import { describe, it, expect, afterAll, beforeEach } from 'vitest'
2
import request from 'supertest'
3
import { NotificationKind } from '@prisma/client'
4
import { createApp } from '@app/createApp'
5
import { buildPrisma, cleanupTestDb, createTestUser, generateTestToken } from '../test-helper'
6

7
const app = createApp()
8
const db = buildPrisma()
9

10
afterAll(() => db.$disconnect())
11
beforeEach(() => cleanupTestDb(db))
12

13
async function seedNotification(
14
  userId: number,
15
  overrides: {
16
    kind?: NotificationKind
17
    title?: string
18
    body?: string
19
    scheduledFor?: Date
20
    regimenId?: number
21
  } = {},
22
) {
23
  return db.notification.create({
24
    data: {
25
      userId,
26
      kind: overrides.kind ?? NotificationKind.DAILY_REMINDER,
27
      title: overrides.title ?? 'Como você está?',
28
      body: overrides.body ?? 'Registre seu humor para acompanhar sua saúde.',
29
      scheduledFor: overrides.scheduledFor ?? new Date(),
30
      regimenId: overrides.regimenId ?? null,
31
    },
32
  })
33
}
34

35
describe('GET /notifications', () => {
36
  it('requires authentication', async () => {
37
    const res = await request(app).get('/notifications')
38

39
    expect(res.status).toBe(401)
40
  })
41

42
  it('returns an empty page when the user has none', async () => {
43
    const user = await createTestUser(db)
44
    const token = generateTestToken(user.id, user.email)
45

46
    const res = await request(app)
47
      .get('/notifications')
48
      .set('Authorization', `Bearer ${token}`)
49

50
    expect(res.status).toBe(200)
51
    expect(res.body.notifications).toEqual([])
52
    expect(res.body.nextCursor).toBeNull()
53
  })
54

55
  it("returns the user's notifications newest first", async () => {
56
    const user = await createTestUser(db)
57
    const token = generateTestToken(user.id, user.email)
58

59
    const first = await seedNotification(user.id, { title: 'Primeira' })
60
    const second = await seedNotification(user.id, { title: 'Segunda' })
61
    const third = await seedNotification(user.id, { title: 'Terceira' })
62

63
    const res = await request(app)
64
      .get('/notifications')
65
      .set('Authorization', `Bearer ${token}`)
66

67
    expect(res.status).toBe(200)
68
    expect(res.body.notifications.map((n: { id: number }) => n.id)).toEqual([
69
      third.id,
70
      second.id,
71
      first.id,
72
    ])
73
  })
74

75
  it("never returns another user's notifications", async () => {
76
    const user = await createTestUser(db)
77
    const other = await createTestUser(db, { email: 'other@example.com' })
78
    const token = generateTestToken(user.id, user.email)
79

80
    await seedNotification(user.id, { title: 'Minha' })
81
    await seedNotification(other.id, { title: 'Dele' })
82

83
    const res = await request(app)
84
      .get('/notifications')
85
      .set('Authorization', `Bearer ${token}`)
86

87
    expect(res.status).toBe(200)
88
    expect(res.body.notifications).toHaveLength(1)
89
    expect(res.body.notifications[0].title).toBe('Minha')
90
  })
91

92
  it('pages newest first with a cursor', async () => {
93
    const user = await createTestUser(db)
94
    const token = generateTestToken(user.id, user.email)
95

96
    const oldest = await seedNotification(user.id, { title: 'Mais antiga' })
97
    const middle = await seedNotification(user.id, { title: 'Do meio' })
98
    const newest = await seedNotification(user.id, { title: 'Mais nova' })
99

100
    const firstPage = await request(app)
101
      .get('/notifications?limit=2')
102
      .set('Authorization', `Bearer ${token}`)
103

104
    expect(firstPage.status).toBe(200)
105
    expect(firstPage.body.notifications.map((n: { id: number }) => n.id)).toEqual([
106
      newest.id,
107
      middle.id,
108
    ])
109
    expect(firstPage.body.nextCursor).toBe(middle.id)
110

111
    const secondPage = await request(app)
112
      .get(`/notifications?limit=2&cursor=${firstPage.body.nextCursor}`)
113
      .set('Authorization', `Bearer ${token}`)
114

115
    expect(secondPage.status).toBe(200)
116
    expect(secondPage.body.notifications.map((n: { id: number }) => n.id)).toEqual([
117
      oldest.id,
118
    ])
119
    expect(secondPage.body.nextCursor).toBeNull()
120
  })
121

122
  it('serializes only the fields the inbox needs', async () => {
123
    const user = await createTestUser(db)
124
    const token = generateTestToken(user.id, user.email)
125

126
    await seedNotification(user.id, { title: 'Como você está?' })
127

128
    const res = await request(app)
129
      .get('/notifications')
130
      .set('Authorization', `Bearer ${token}`)
131

132
    const [notification] = res.body.notifications
133
    expect(notification).toMatchObject({
134
      kind: 'DAILY_REMINDER',
135
      title: 'Como você está?',
136
      body: 'Registre seu humor para acompanhar sua saúde.',
137
      regimenId: null,
138
    })
139
    expect(notification).not.toHaveProperty('userId')
140
    expect(notification).not.toHaveProperty('pushedAt')
141
  })
142

143
  it('rejects an out-of-range limit', async () => {
144
    const user = await createTestUser(db)
145
    const token = generateTestToken(user.id, user.email)
146

147
    const res = await request(app)
148
      .get('/notifications?limit=0')
149
      .set('Authorization', `Bearer ${token}`)
150

151
    expect(res.status).toBe(400)
152
  })
153
})