|
| 1 | +import { describe, it, expect, beforeEach, vi } from 'vitest'; |
| 2 | +import Fastify from 'fastify'; |
| 3 | +import { publicRoutes } from '../routes/public.js'; |
| 4 | +import type { PrismaClient } from '@prisma/client'; |
| 5 | + |
| 6 | +const mockPrisma = { |
| 7 | + usernameRedirect: { |
| 8 | + findUnique: vi.fn(), |
| 9 | + }, |
| 10 | + user: { |
| 11 | + findUnique: vi.fn(), |
| 12 | + }, |
| 13 | + cardView: { |
| 14 | + create: vi.fn().mockReturnValue({ catch: vi.fn() }), |
| 15 | + }, |
| 16 | + followLog: { |
| 17 | + findMany: vi.fn().mockResolvedValue([]), |
| 18 | + }, |
| 19 | +}; |
| 20 | + |
| 21 | +async function buildApp() { |
| 22 | + const app = Fastify(); |
| 23 | + app.decorate('prisma', mockPrisma as unknown as PrismaClient); |
| 24 | + app.register(publicRoutes, { prefix: '/api/public' }); |
| 25 | + await app.ready(); |
| 26 | + return app; |
| 27 | +} |
| 28 | + |
| 29 | +describe('Username Redirects Routing', () => { |
| 30 | + beforeEach(() => { |
| 31 | + vi.clearAllMocks(); |
| 32 | + }); |
| 33 | + |
| 34 | + it('performs a 301 redirect to the new username for recently changed usernames', async () => { |
| 35 | + const app = buildApp(); |
| 36 | + mockPrisma.usernameRedirect.findUnique.mockImplementation(({ where }: any) => { |
| 37 | + if (where.oldUsername === 'olduser') { |
| 38 | + return Promise.resolve({ |
| 39 | + oldUsername: 'olduser', |
| 40 | + newUsername: 'newuser', |
| 41 | + createdAt: new Date(), |
| 42 | + }); |
| 43 | + } |
| 44 | + return Promise.resolve(null); |
| 45 | + }); |
| 46 | + |
| 47 | + const appInstance = await app; |
| 48 | + const res = await appInstance.inject({ |
| 49 | + method: 'GET', |
| 50 | + url: '/api/public/olduser', |
| 51 | + }); |
| 52 | + |
| 53 | + expect(res.statusCode).toBe(301); |
| 54 | + expect(res.headers.location).toBe('/api/public/newuser'); |
| 55 | + }); |
| 56 | + |
| 57 | + it('does not redirect and returns 404/200 if username is not in redirects', async () => { |
| 58 | + const app = buildApp(); |
| 59 | + mockPrisma.usernameRedirect.findUnique.mockResolvedValue(null); |
| 60 | + mockPrisma.user.findUnique.mockResolvedValue(null); |
| 61 | + |
| 62 | + const appInstance = await app; |
| 63 | + const res = await appInstance.inject({ |
| 64 | + method: 'GET', |
| 65 | + url: '/api/public/nonexistent', |
| 66 | + }); |
| 67 | + |
| 68 | + expect(res.statusCode).toBe(404); |
| 69 | + }); |
| 70 | + |
| 71 | + it('does not redirect if the redirect is older than 90 days', async () => { |
| 72 | + const app = buildApp(); |
| 73 | + const ninetyOneDaysAgo = new Date(); |
| 74 | + ninetyOneDaysAgo.setDate(ninetyOneDaysAgo.getDate() - 91); |
| 75 | + |
| 76 | + mockPrisma.usernameRedirect.findUnique.mockResolvedValue({ |
| 77 | + oldUsername: 'olduser', |
| 78 | + newUsername: 'newuser', |
| 79 | + createdAt: ninetyOneDaysAgo, |
| 80 | + }); |
| 81 | + mockPrisma.user.findUnique.mockResolvedValue(null); |
| 82 | + |
| 83 | + const appInstance = await app; |
| 84 | + const res = await appInstance.inject({ |
| 85 | + method: 'GET', |
| 86 | + url: '/api/public/olduser', |
| 87 | + }); |
| 88 | + |
| 89 | + expect(res.statusCode).toBe(404); |
| 90 | + }); |
| 91 | + |
| 92 | + it('resolves multi-step redirect chains recursively', async () => { |
| 93 | + const app = buildApp(); |
| 94 | + mockPrisma.usernameRedirect.findUnique.mockImplementation(({ where }: any) => { |
| 95 | + if (where.oldUsername === 'userA') { |
| 96 | + return Promise.resolve({ |
| 97 | + oldUsername: 'userA', |
| 98 | + newUsername: 'userB', |
| 99 | + createdAt: new Date(), |
| 100 | + }); |
| 101 | + } |
| 102 | + if (where.oldUsername === 'userB') { |
| 103 | + return Promise.resolve({ |
| 104 | + oldUsername: 'userB', |
| 105 | + newUsername: 'userC', |
| 106 | + createdAt: new Date(), |
| 107 | + }); |
| 108 | + } |
| 109 | + return Promise.resolve(null); |
| 110 | + }); |
| 111 | + |
| 112 | + const appInstance = await app; |
| 113 | + const res = await appInstance.inject({ |
| 114 | + method: 'GET', |
| 115 | + url: '/api/public/userA/qr?size=300', |
| 116 | + }); |
| 117 | + |
| 118 | + expect(res.statusCode).toBe(301); |
| 119 | + expect(res.headers.location).toBe('/api/public/userC/qr?size=300'); |
| 120 | + }); |
| 121 | + |
| 122 | + it('guards against infinite loops in redirect chains', async () => { |
| 123 | + const app = buildApp(); |
| 124 | + mockPrisma.usernameRedirect.findUnique.mockImplementation(({ where }: any) => { |
| 125 | + if (where.oldUsername === 'userA') { |
| 126 | + return Promise.resolve({ |
| 127 | + oldUsername: 'userA', |
| 128 | + newUsername: 'userB', |
| 129 | + createdAt: new Date(), |
| 130 | + }); |
| 131 | + } |
| 132 | + if (where.oldUsername === 'userB') { |
| 133 | + return Promise.resolve({ |
| 134 | + oldUsername: 'userB', |
| 135 | + newUsername: 'userA', |
| 136 | + createdAt: new Date(), |
| 137 | + }); |
| 138 | + } |
| 139 | + return Promise.resolve(null); |
| 140 | + }); |
| 141 | + mockPrisma.user.findUnique.mockResolvedValue(null); |
| 142 | + |
| 143 | + const appInstance = await app; |
| 144 | + const res = await appInstance.inject({ |
| 145 | + method: 'GET', |
| 146 | + url: '/api/public/userA', |
| 147 | + }); |
| 148 | + |
| 149 | + expect(res.statusCode).toBe(301); |
| 150 | + expect(res.headers.location).toBe('/api/public/userB'); |
| 151 | + }); |
| 152 | +}); |
0 commit comments