-
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
10 changed files
with
345 additions
and
15 deletions.
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
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,68 @@ | ||
import { prisma } from '../db.server' | ||
import { handleDeleteProject } from '../routes/projects.$id.delete' | ||
import { | ||
createTestProject, | ||
createTestSession, | ||
createTestUser, | ||
newTestRequest, | ||
truncateTables, | ||
} from '../test-util' | ||
import { ApiError } from '../util/api.server' | ||
|
||
describe('handleDeleteProject', () => { | ||
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: 'bar', title: 'project-two' }) | ||
}) | ||
|
||
it('requires a user', async () => { | ||
const fn = async () => | ||
handleDeleteProject(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 () => | ||
handleDeleteProject(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 handleDeleteProject(req, { id: 'doesnt-exist' }) | ||
} | ||
|
||
await expect(fn).rejects.toThrow('Record to update not found') | ||
}) | ||
it('requires ownership of the project', async () => { | ||
const fn = async () => { | ||
const req = newTestRequest({ method: 'POST', authCookie: 'the-key' }) | ||
return handleDeleteProject(req, { id: 'two' }) | ||
} | ||
|
||
await expect(fn).rejects.toThrow('Record to update not found') | ||
}) | ||
it('soft-deletes the project', async () => { | ||
const fn = async () => { | ||
const req = newTestRequest({ method: 'POST', authCookie: 'the-key' }) | ||
return handleDeleteProject(req, { id: 'one' }) | ||
} | ||
|
||
await fn() | ||
const project = await prisma.project.findFirst({ where: { proj_id: 'one' } }) | ||
expect(project?.deleted).toEqual(true) | ||
}) | ||
}) |
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,81 @@ | ||
import { prisma } from '../db.server' | ||
import { handleRestoreDeletedProject } from '../routes/projects.$id.restore' | ||
import { | ||
createTestProject, | ||
createTestSession, | ||
createTestUser, | ||
newTestRequest, | ||
truncateTables, | ||
} from '../test-util' | ||
import { ApiError } from '../util/api.server' | ||
|
||
describe('handleRestoreDeletedProject', () => { | ||
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', | ||
deleted: true, | ||
}) | ||
await createTestProject(prisma, { id: 'two', ownerId: 'foo', title: 'project-two' }) | ||
}) | ||
|
||
it('requires a user', async () => { | ||
const fn = async () => | ||
handleRestoreDeletedProject(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 () => | ||
handleRestoreDeletedProject(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 handleRestoreDeletedProject(req, { id: 'doesnt-exist' }) | ||
} | ||
|
||
await expect(fn).rejects.toThrow('Record to update not found') | ||
}) | ||
it('requires ownership of the project', async () => { | ||
const fn = async () => { | ||
const req = newTestRequest({ method: 'POST', authCookie: 'the-key' }) | ||
return handleRestoreDeletedProject(req, { id: 'two' }) | ||
} | ||
|
||
await expect(fn).rejects.toThrow('Record to update not found') | ||
}) | ||
it('requires a deleted project', async () => { | ||
const fn = async () => { | ||
const req = newTestRequest({ method: 'POST', authCookie: 'the-key' }) | ||
return handleRestoreDeletedProject(req, { id: 'two' }) | ||
} | ||
|
||
await expect(fn).rejects.toThrow('Record to update not found') | ||
}) | ||
it('restores the project', async () => { | ||
const fn = async () => { | ||
const req = newTestRequest({ method: 'POST', authCookie: 'the-key' }) | ||
return handleRestoreDeletedProject(req, { id: 'one' }) | ||
} | ||
|
||
await fn() | ||
const project = await prisma.project.findFirst({ where: { proj_id: 'one' } }) | ||
expect(project?.deleted).toEqual(null) | ||
}) | ||
}) |
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 { softDeleteProject } from '../models/project.server' | ||
import { ensure, handle, requireUser } from '../util/api.server' | ||
import { Status } from '../util/statusCodes.server' | ||
|
||
export async function action(args: ActionFunctionArgs) { | ||
return handle(args, { POST: handleDeleteProject }) | ||
} | ||
|
||
export async function handleDeleteProject(req: Request, params: Params<string>) { | ||
const user = await requireUser(req) | ||
|
||
const { id } = params | ||
ensure(id != null, 'id is null', Status.BAD_REQUEST) | ||
|
||
await softDeleteProject({ 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
Oops, something went wrong.