|
| 1 | +/** |
| 2 | + * @vitest-environment node |
| 3 | + */ |
| 4 | +import { |
| 5 | + createMockRequest, |
| 6 | + dbChainMock, |
| 7 | + dbChainMockFns, |
| 8 | + queueTableRows, |
| 9 | + resetDbChainMock, |
| 10 | + schemaMock, |
| 11 | +} from '@sim/testing' |
| 12 | +import { beforeEach, describe, expect, it, vi } from 'vitest' |
| 13 | + |
| 14 | +const { mockGetSession } = vi.hoisted(() => ({ mockGetSession: vi.fn() })) |
| 15 | + |
| 16 | +vi.mock('@sim/db', () => ({ ...dbChainMock, ...schemaMock })) |
| 17 | +vi.mock('@/lib/auth', () => ({ getSession: mockGetSession })) |
| 18 | + |
| 19 | +import { DELETE } from '@/app/api/auth/sso/providers/[providerId]/route' |
| 20 | + |
| 21 | +const context = { params: Promise.resolve({ providerId: 'acme-okta' }) } |
| 22 | +const request = () => createMockRequest('DELETE') |
| 23 | + |
| 24 | +describe('DELETE /api/auth/sso/providers/[providerId]', () => { |
| 25 | + beforeEach(() => { |
| 26 | + vi.clearAllMocks() |
| 27 | + resetDbChainMock() |
| 28 | + mockGetSession.mockResolvedValue({ user: { id: 'u1' } }) |
| 29 | + dbChainMockFns.returning.mockResolvedValue([{ id: 'row-1' }]) |
| 30 | + }) |
| 31 | + |
| 32 | + it('requires a session', async () => { |
| 33 | + mockGetSession.mockResolvedValue(null) |
| 34 | + const res = await DELETE(request(), context) |
| 35 | + expect(res.status).toBe(401) |
| 36 | + expect(dbChainMockFns.delete).not.toHaveBeenCalled() |
| 37 | + }) |
| 38 | + |
| 39 | + it('answers 404 for an unknown provider', async () => { |
| 40 | + queueTableRows(schemaMock.ssoProvider, []) |
| 41 | + const res = await DELETE(request(), context) |
| 42 | + expect(res.status).toBe(404) |
| 43 | + expect(dbChainMockFns.delete).not.toHaveBeenCalled() |
| 44 | + }) |
| 45 | + |
| 46 | + it("refuses an organization provider to a member who is not the organization's admin", async () => { |
| 47 | + queueTableRows(schemaMock.ssoProvider, [ |
| 48 | + { id: 'row-1', organizationId: 'org1', userId: 'u-other', domain: 'acme.com' }, |
| 49 | + ]) |
| 50 | + queueTableRows(schemaMock.member, [{ role: 'member' }]) |
| 51 | + const res = await DELETE(request(), context) |
| 52 | + expect(res.status).toBe(403) |
| 53 | + expect(dbChainMockFns.delete).not.toHaveBeenCalled() |
| 54 | + }) |
| 55 | + |
| 56 | + it('lets an organization admin delete a provider another admin created', async () => { |
| 57 | + queueTableRows(schemaMock.ssoProvider, [ |
| 58 | + { id: 'row-1', organizationId: 'org1', userId: 'u-other', domain: 'acme.com' }, |
| 59 | + ]) |
| 60 | + queueTableRows(schemaMock.member, [{ role: 'admin' }]) |
| 61 | + const res = await DELETE(request(), context) |
| 62 | + expect(res.status).toBe(200) |
| 63 | + await expect(res.json()).resolves.toEqual({ success: true, providerId: 'acme-okta' }) |
| 64 | + expect(dbChainMockFns.delete).toHaveBeenCalledWith(schemaMock.ssoProvider) |
| 65 | + }) |
| 66 | + |
| 67 | + it('lets only the creator delete a personal provider', async () => { |
| 68 | + queueTableRows(schemaMock.ssoProvider, [ |
| 69 | + { id: 'row-1', organizationId: null, userId: 'u-other', domain: 'acme.com' }, |
| 70 | + ]) |
| 71 | + const refused = await DELETE(request(), context) |
| 72 | + expect(refused.status).toBe(403) |
| 73 | + |
| 74 | + resetDbChainMock() |
| 75 | + dbChainMockFns.returning.mockResolvedValue([{ id: 'row-1' }]) |
| 76 | + queueTableRows(schemaMock.ssoProvider, [ |
| 77 | + { id: 'row-1', organizationId: null, userId: 'u1', domain: 'acme.com' }, |
| 78 | + ]) |
| 79 | + const allowed = await DELETE(request(), context) |
| 80 | + expect(allowed.status).toBe(200) |
| 81 | + }) |
| 82 | + |
| 83 | + it.each([129, 256])('deletes a provider with a %i-character ID', async (length) => { |
| 84 | + const providerId = 'a'.repeat(length) |
| 85 | + queueTableRows(schemaMock.ssoProvider, [ |
| 86 | + { id: 'row-1', organizationId: 'org1', userId: 'u1', domain: 'acme.com' }, |
| 87 | + ]) |
| 88 | + queueTableRows(schemaMock.member, [{ role: 'owner' }]) |
| 89 | + |
| 90 | + const res = await DELETE(request(), { params: Promise.resolve({ providerId }) }) |
| 91 | + |
| 92 | + expect(res.status).toBe(200) |
| 93 | + await expect(res.json()).resolves.toEqual({ success: true, providerId }) |
| 94 | + expect(dbChainMockFns.delete).toHaveBeenCalledWith(schemaMock.ssoProvider) |
| 95 | + }) |
| 96 | + |
| 97 | + it('answers 404 when the row vanished between the check and the delete', async () => { |
| 98 | + queueTableRows(schemaMock.ssoProvider, [ |
| 99 | + { id: 'row-1', organizationId: 'org1', userId: 'u1', domain: 'acme.com' }, |
| 100 | + ]) |
| 101 | + queueTableRows(schemaMock.member, [{ role: 'owner' }]) |
| 102 | + dbChainMockFns.returning.mockResolvedValue([]) |
| 103 | + const res = await DELETE(request(), context) |
| 104 | + expect(res.status).toBe(404) |
| 105 | + }) |
| 106 | +}) |
0 commit comments