| 1 | import { describe, it, expect, afterAll, beforeEach, vi } from 'vitest'
|
| 2 | import request from 'supertest'
|
| 3 | import { DeleteObjectCommand } from '@aws-sdk/client-s3'
|
| 4 | import { createApp } from '@app/createApp'
|
| 5 | import s3 from '@app/lib/s3'
|
| 6 | import {
|
| 7 | buildPrisma,
|
| 8 | cleanupTestDb,
|
| 9 | createTestUser,
|
| 10 | generateTestToken,
|
| 11 | } from '../test-helper'
|
| 12 |
|
| 13 | import { resetAvatarMock, setUploadedFile } from '../mocks/avatar'
|
| 14 |
|
| 15 | const app = createApp()
|
| 16 | const db = buildPrisma()
|
| 17 | const mockedSend = vi.mocked(s3.send)
|
| 18 |
|
| 19 | afterAll(() => db.$disconnect())
|
| 20 |
|
| 21 | beforeEach(async () => {
|
| 22 | await cleanupTestDb(db)
|
| 23 |
|
| 24 | resetAvatarMock()
|
| 25 | mockedSend.mockClear()
|
| 26 | })
|
| 27 |
|
| 28 | const uploadedFile = {
|
| 29 | location: 'https://cdn.example.com/avatars/new.png',
|
| 30 | key: 'avatars/new.png',
|
| 31 | originalname: 'new.png',
|
| 32 | size: 1234,
|
| 33 | mimetype: 'image/png',
|
| 34 | }
|
| 35 |
|
| 36 | describe('POST /users/me/avatar', () => {
|
| 37 | it('stores the uploaded file on the user', async () => {
|
| 38 | const user = await createTestUser(db)
|
| 39 | const token = generateTestToken(user.id, user.email)
|
| 40 |
|
| 41 | setUploadedFile(uploadedFile)
|
| 42 |
|
| 43 | const res = await request(app)
|
| 44 | .post('/users/me/avatar')
|
| 45 | .set('Authorization', `Bearer ${token}`)
|
| 46 |
|
| 47 | expect(res.status).toBe(200)
|
| 48 | expect(res.body.key).toBe('avatars/new.png')
|
| 49 | expect(res.body.user.avatarURL).toBe(uploadedFile.location)
|
| 50 | expect(res.body.user.avatarKey).toBe('avatars/new.png')
|
| 51 |
|
| 52 | const dbUser = await db.user.findUnique({ where: { id: user.id } })
|
| 53 |
|
| 54 | expect(dbUser!.avatarURL).toBe(uploadedFile.location)
|
| 55 | })
|
| 56 |
|
| 57 | it('deletes the previous object when replacing an avatar', async () => {
|
| 58 | const user = await createTestUser(db)
|
| 59 | await db.user.update({
|
| 60 | where: { id: user.id },
|
| 61 | data: {
|
| 62 | avatarKey: 'avatars/old.png',
|
| 63 | avatarURL: 'https://cdn.example.com/avatars/old.png'
|
| 64 | },
|
| 65 | })
|
| 66 |
|
| 67 | const token = generateTestToken(user.id, user.email)
|
| 68 | setUploadedFile(uploadedFile)
|
| 69 |
|
| 70 | const res = await request(app)
|
| 71 | .post('/users/me/avatar')
|
| 72 | .set('Authorization', `Bearer ${token}`)
|
| 73 |
|
| 74 | expect(res.status).toBe(200)
|
| 75 | expect(mockedSend).toHaveBeenCalledOnce()
|
| 76 | expect(mockedSend).toHaveBeenCalledWith(expect.any(DeleteObjectCommand))
|
| 77 | })
|
| 78 |
|
| 79 | it('returns 400 when no file is present', async () => {
|
| 80 | const user = await createTestUser(db)
|
| 81 | const token = generateTestToken(user.id, user.email)
|
| 82 |
|
| 83 | const res = await request(app)
|
| 84 | .post('/users/me/avatar')
|
| 85 | .set('Authorization', `Bearer ${token}`)
|
| 86 |
|
| 87 | expect(res.status).toBe(400)
|
| 88 | expect(mockedSend).not.toHaveBeenCalled()
|
| 89 | })
|
| 90 |
|
| 91 | it('returns 401 without a token', async () => {
|
| 92 | setUploadedFile(uploadedFile)
|
| 93 |
|
| 94 | const res = await request(app).post('/users/me/avatar')
|
| 95 | expect(res.status).toBe(401)
|
| 96 | })
|
| 97 | })
|
| 98 |
|
| 99 | describe('DELETE /users/me/avatar', () => {
|
| 100 | it('clears the stored avatar and deletes the object', async () => {
|
| 101 | const user = await createTestUser(db)
|
| 102 |
|
| 103 | await db.user.update({
|
| 104 | where: { id: user.id },
|
| 105 | data: {
|
| 106 | avatarKey: 'avatars/old.png',
|
| 107 | avatarURL: 'https://cdn.example.com/avatars/old.png'
|
| 108 | },
|
| 109 | })
|
| 110 | const token = generateTestToken(user.id, user.email)
|
| 111 |
|
| 112 | const res = await request(app)
|
| 113 | .delete('/users/me/avatar')
|
| 114 | .set('Authorization', `Bearer ${token}`)
|
| 115 |
|
| 116 | expect(res.status).toBe(200)
|
| 117 | expect(res.body.user.avatarKey).toBeNull()
|
| 118 | expect(res.body.user.avatarURL).toBeNull()
|
| 119 | expect(mockedSend).toHaveBeenCalledOnce()
|
| 120 |
|
| 121 | const dbUser = await db.user.findUnique({ where: { id: user.id } })
|
| 122 |
|
| 123 | expect(dbUser!.avatarKey).toBeNull()
|
| 124 | })
|
| 125 |
|
| 126 | it('is a no-op when there is no avatar to remove', async () => {
|
| 127 | const user = await createTestUser(db)
|
| 128 | const token = generateTestToken(user.id, user.email)
|
| 129 |
|
| 130 | const res = await request(app)
|
| 131 | .delete('/users/me/avatar')
|
| 132 | .set('Authorization', `Bearer ${token}`)
|
| 133 |
|
| 134 | expect(res.status).toBe(200)
|
| 135 | expect(res.body.user.id).toBe(user.id)
|
| 136 | expect(mockedSend).not.toHaveBeenCalled()
|
| 137 | })
|
| 138 |
|
| 139 | it('returns 401 without a token', async () => {
|
| 140 | const res = await request(app).delete('/users/me/avatar')
|
| 141 | expect(res.status).toBe(401)
|
| 142 | })
|
| 143 | })
|