|
| 1 | +import { describe, test } from 'vitest'; |
| 2 | + |
| 3 | +import { db } from '../../db.js'; |
| 4 | + |
| 5 | +describe('PUT /api/v1/users/:id', () => { |
| 6 | + test('updates the user with a new email address', async ({ expect }) => { |
| 7 | + let user = db.user.create({ email: '[email protected]' }); |
| 8 | + db.mswSession.create({ user }); |
| 9 | + |
| 10 | + let body = JSON.stringify({ user: { email: '[email protected]' } }); |
| 11 | + let response = await fetch(`/api/v1/users/${user.id}`, { method: 'PUT', body }); |
| 12 | + expect(response.status).toBe(200); |
| 13 | + expect(await response.json()).toEqual({ ok: true }); |
| 14 | + |
| 15 | + user = db.user.findFirst({ where: { id: user.id } }); |
| 16 | + expect(user.email).toBe('[email protected]'); |
| 17 | + expect(user.emailVerified).toBe(false); |
| 18 | + expect(user.emailVerificationToken).toBe('secret123'); |
| 19 | + }); |
| 20 | + |
| 21 | + test('updates the `publish_notifications` settings', async ({ expect }) => { |
| 22 | + let user = db.user.create(); |
| 23 | + db.mswSession.create({ user }); |
| 24 | + expect(user.publishNotifications).toBe(true); |
| 25 | + |
| 26 | + let body = JSON.stringify({ user: { publish_notifications: false } }); |
| 27 | + let response = await fetch(`/api/v1/users/${user.id}`, { method: 'PUT', body }); |
| 28 | + expect(response.status).toBe(200); |
| 29 | + expect(await response.json()).toEqual({ ok: true }); |
| 30 | + |
| 31 | + user = db.user.findFirst({ where: { id: user.id } }); |
| 32 | + expect(user.publishNotifications).toBe(false); |
| 33 | + }); |
| 34 | + |
| 35 | + test('returns 403 when not logged in', async ({ expect }) => { |
| 36 | + let user = db.user.create({ email: '[email protected]' }); |
| 37 | + |
| 38 | + let body = JSON.stringify({ user: { email: '[email protected]' } }); |
| 39 | + let response = await fetch(`/api/v1/users/${user.id}`, { method: 'PUT', body }); |
| 40 | + expect(response.status).toBe(403); |
| 41 | + expect(await response.json()).toEqual({ errors: [{ detail: 'must be logged in to perform that action' }] }); |
| 42 | + |
| 43 | + user = db.user.findFirst({ where: { id: user.id } }); |
| 44 | + expect(user.email).toBe('[email protected]'); |
| 45 | + }); |
| 46 | + |
| 47 | + test('returns 400 when requesting the wrong user id', async ({ expect }) => { |
| 48 | + let user = db.user.create({ email: '[email protected]' }); |
| 49 | + db.mswSession.create({ user }); |
| 50 | + |
| 51 | + let body = JSON.stringify({ user: { email: '[email protected]' } }); |
| 52 | + let response = await fetch(`/api/v1/users/wrong-id`, { method: 'PUT', body }); |
| 53 | + expect(response.status).toBe(400); |
| 54 | + expect(await response.json()).toEqual({ errors: [{ detail: 'current user does not match requested user' }] }); |
| 55 | + |
| 56 | + user = db.user.findFirst({ where: { id: user.id } }); |
| 57 | + expect(user.email).toBe('[email protected]'); |
| 58 | + }); |
| 59 | + |
| 60 | + test('returns 400 when sending an invalid payload', async ({ expect }) => { |
| 61 | + let user = db.user.create({ email: '[email protected]' }); |
| 62 | + db.mswSession.create({ user }); |
| 63 | + |
| 64 | + let body = JSON.stringify({}); |
| 65 | + let response = await fetch(`/api/v1/users/${user.id}`, { method: 'PUT', body }); |
| 66 | + expect(response.status).toBe(400); |
| 67 | + expect(await response.json()).toEqual({ errors: [{ detail: 'invalid json request' }] }); |
| 68 | + |
| 69 | + user = db.user.findFirst({ where: { id: user.id } }); |
| 70 | + expect(user.email).toBe('[email protected]'); |
| 71 | + }); |
| 72 | + |
| 73 | + test('returns 400 when sending an empty email address', async ({ expect }) => { |
| 74 | + let user = db.user.create({ email: '[email protected]' }); |
| 75 | + db.mswSession.create({ user }); |
| 76 | + |
| 77 | + let body = JSON.stringify({ user: { email: '' } }); |
| 78 | + let response = await fetch(`/api/v1/users/${user.id}`, { method: 'PUT', body }); |
| 79 | + expect(response.status).toBe(400); |
| 80 | + expect(await response.json()).toEqual({ errors: [{ detail: 'empty email rejected' }] }); |
| 81 | + |
| 82 | + user = db.user.findFirst({ where: { id: user.id } }); |
| 83 | + expect(user.email).toBe('[email protected]'); |
| 84 | + }); |
| 85 | +}); |
0 commit comments