-
Notifications
You must be signed in to change notification settings - Fork 171
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
238 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
import { prisma } from '../db.server' | ||
import { handleDestroyProject } from '../routes/projects.$id.destroy' | ||
import { | ||
createTestProject, | ||
createTestSession, | ||
createTestUser, | ||
newTestRequest, | ||
truncateTables, | ||
} from '../test-util' | ||
import { ApiError } from '../util/api.server' | ||
|
||
describe('handleDestroyProject', () => { | ||
afterEach(async () => { | ||
await truncateTables([ | ||
prisma.userDetails, | ||
prisma.persistentSession, | ||
prisma.project, | ||
prisma.projectID, | ||
]) | ||
}) | ||
|
||
beforeEach(async () => { | ||
await createTestUser(prisma, { id: 'foo' }) | ||
await createTestUser(prisma, { id: 'bar' }) | ||
await createTestSession(prisma, { key: 'the-key', userId: 'foo' }) | ||
await createTestProject(prisma, { | ||
id: 'one', | ||
ownerId: 'foo', | ||
title: 'project-one', | ||
}) | ||
await createTestProject(prisma, { | ||
id: 'two', | ||
ownerId: 'foo', | ||
title: 'project-two', | ||
deleted: true, | ||
}) | ||
await createTestProject(prisma, { | ||
id: 'three', | ||
ownerId: 'bar', | ||
title: 'project-three', | ||
deleted: true, | ||
}) | ||
}) | ||
|
||
it('requires a user', async () => { | ||
const fn = async () => | ||
handleDestroyProject(newTestRequest({ method: 'POST', authCookie: 'wrong-key' }), {}) | ||
await expect(fn).rejects.toThrow(ApiError) | ||
await expect(fn).rejects.toThrow('session not found') | ||
}) | ||
it('requires a valid id', async () => { | ||
const fn = async () => | ||
handleDestroyProject(newTestRequest({ method: 'POST', authCookie: 'the-key' }), {}) | ||
await expect(fn).rejects.toThrow(ApiError) | ||
await expect(fn).rejects.toThrow('id is null') | ||
}) | ||
it('requires a valid project', async () => { | ||
const fn = async () => { | ||
const req = newTestRequest({ method: 'POST', authCookie: 'the-key' }) | ||
return handleDestroyProject(req, { id: 'doesnt-exist' }) | ||
} | ||
|
||
await expect(fn).rejects.toThrow('Record to delete does not exist') | ||
}) | ||
it('requires ownership of the project', async () => { | ||
const fn = async () => { | ||
const req = newTestRequest({ method: 'POST', authCookie: 'the-key' }) | ||
return handleDestroyProject(req, { id: 'three' }) | ||
} | ||
|
||
await expect(fn).rejects.toThrow('Record to delete does not exist') | ||
}) | ||
it('requires soft-deletion of the project', async () => { | ||
const fn = async () => { | ||
const req = newTestRequest({ method: 'POST', authCookie: 'the-key' }) | ||
return handleDestroyProject(req, { id: 'one' }) | ||
} | ||
|
||
await expect(fn).rejects.toThrow('Record to delete does not exist') | ||
}) | ||
it('hard-deletes the project', async () => { | ||
const fn = async () => { | ||
const req = newTestRequest({ method: 'POST', authCookie: 'the-key' }) | ||
return handleDestroyProject(req, { id: 'two' }) | ||
} | ||
|
||
await fn() | ||
const got = await prisma.project.count({ where: { proj_id: 'two' } }) | ||
expect(got).toEqual(0) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import { prisma } from '../db.server' | ||
import { handleDestroyAllProjects } from '../routes/projects.destroy' | ||
import { | ||
createTestProject, | ||
createTestSession, | ||
createTestUser, | ||
newTestRequest, | ||
truncateTables, | ||
} from '../test-util' | ||
import { ApiError } from '../util/api.server' | ||
|
||
describe('handleDestroyAllProjects', () => { | ||
afterEach(async () => { | ||
await truncateTables([ | ||
prisma.userDetails, | ||
prisma.persistentSession, | ||
prisma.project, | ||
prisma.projectID, | ||
]) | ||
}) | ||
|
||
beforeEach(async () => { | ||
await createTestUser(prisma, { id: 'bob' }) | ||
await createTestUser(prisma, { id: 'alice' }) | ||
await createTestSession(prisma, { key: 'the-key', userId: 'bob' }) | ||
await createTestProject(prisma, { id: 'one', ownerId: 'bob' }) | ||
await createTestProject(prisma, { id: 'two', ownerId: 'bob', deleted: true }) | ||
await createTestProject(prisma, { id: 'three', ownerId: 'alice', deleted: true }) | ||
await createTestProject(prisma, { id: 'four', ownerId: 'bob' }) | ||
await createTestProject(prisma, { id: 'five', ownerId: 'bob' }) | ||
await createTestProject(prisma, { id: 'six', ownerId: 'bob', deleted: true }) | ||
}) | ||
|
||
it('requires a user', async () => { | ||
const fn = async () => | ||
handleDestroyAllProjects(newTestRequest({ method: 'POST', authCookie: 'wrong-key' }), {}) | ||
await expect(fn).rejects.toThrow(ApiError) | ||
await expect(fn).rejects.toThrow('session not found') | ||
}) | ||
it('hard-deletes all soft-deleted projects owned by the user', async () => { | ||
const fn = async () => { | ||
const req = newTestRequest({ method: 'POST', authCookie: 'the-key' }) | ||
return handleDestroyAllProjects(req, {}) | ||
} | ||
|
||
await fn() | ||
const got = await prisma.project.findMany({ where: { owner_id: 'bob' } }) | ||
expect(got.map((p) => p.proj_id)).toEqual(['one', 'four', 'five']) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import { ActionFunctionArgs } from '@remix-run/node' | ||
import { Params } from '@remix-run/react' | ||
import { ensure, handle, requireUser } from '../util/api.server' | ||
import { Status } from '../util/statusCodes.server' | ||
import { hardDeleteProject } from '../models/project.server' | ||
|
||
export async function action(args: ActionFunctionArgs) { | ||
return handle(args, { POST: handleDestroyProject }) | ||
} | ||
|
||
export async function handleDestroyProject(req: Request, params: Params<string>) { | ||
const user = await requireUser(req) | ||
|
||
const { id } = params | ||
ensure(id != null, 'id is null', Status.BAD_REQUEST) | ||
|
||
await hardDeleteProject({ id: id, userId: user.user_id }) | ||
|
||
return {} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import { ActionFunctionArgs } from '@remix-run/node' | ||
import { Params } from '@remix-run/react' | ||
import { hardDeleteAllProjects } from '../models/project.server' | ||
import { handle, requireUser } from '../util/api.server' | ||
|
||
export async function action(args: ActionFunctionArgs) { | ||
return handle(args, { POST: handleDestroyAllProjects }) | ||
} | ||
|
||
export async function handleDestroyAllProjects(req: Request, params: Params<string>) { | ||
const user = await requireUser(req) | ||
|
||
await hardDeleteAllProjects({ userId: user.user_id }) | ||
|
||
return {} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters