-
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.
* remove pagination, return all projects * args interface * slugs * rename projects action * slugify dep * pin dep
- Loading branch information
Showing
33 changed files
with
295 additions
and
53 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
import { prisma } from '../db.server' | ||
import { | ||
createTestProject, | ||
createTestSession, | ||
createTestUser, | ||
newFormData, | ||
newTestRequest, | ||
truncateTables, | ||
} from '../test-util' | ||
import { ApiError } from '../util/api.server' | ||
import { handleRenameProject } from '../routes/projects.$id.rename' | ||
|
||
describe('handleRenameProject', () => { | ||
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 POST method', async () => { | ||
const fn = async () => handleRenameProject(newTestRequest(), {}) | ||
await expect(fn).rejects.toThrow(ApiError) | ||
await expect(fn).rejects.toThrow('invalid method') | ||
}) | ||
it('requires a user', async () => { | ||
const fn = async () => | ||
handleRenameProject(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 () => | ||
handleRenameProject(newTestRequest({ method: 'POST', authCookie: 'the-key' }), {}) | ||
await expect(fn).rejects.toThrow(ApiError) | ||
await expect(fn).rejects.toThrow('id is null') | ||
}) | ||
it('requires a valid title', async () => { | ||
const fn = (data: FormData) => async () => { | ||
const req = newTestRequest({ method: 'POST', authCookie: 'the-key', formData: data }) | ||
return handleRenameProject(req, { id: 'project-one' }) | ||
} | ||
|
||
await expect(fn(newFormData({}))).rejects.toThrow(ApiError) | ||
await expect(fn(newFormData({}))).rejects.toThrow('title is null') | ||
|
||
await expect(fn(newFormData({ title: '' }))).rejects.toThrow(ApiError) | ||
await expect(fn(newFormData({ title: '' }))).rejects.toThrow('title is too short') | ||
}) | ||
it('requires a valid project', async () => { | ||
const fn = (data: FormData) => async () => { | ||
const req = newTestRequest({ method: 'POST', authCookie: 'the-key', formData: data }) | ||
return handleRenameProject(req, { id: 'doesnt-exist' }) | ||
} | ||
|
||
await expect(fn(newFormData({ title: 'hello' }))).rejects.toThrow('Record to update not found') | ||
}) | ||
it('requires ownership of the project', async () => { | ||
const fn = (data: FormData) => async () => { | ||
const req = newTestRequest({ method: 'POST', authCookie: 'the-key', formData: data }) | ||
return handleRenameProject(req, { id: 'two' }) | ||
} | ||
|
||
await expect(fn(newFormData({ title: 'hello' }))).rejects.toThrow('Record to update not found') | ||
}) | ||
it('renames the project', async () => { | ||
const fn = async (data: FormData) => { | ||
const req = newTestRequest({ method: 'POST', authCookie: 'the-key', formData: data }) | ||
return handleRenameProject(req, { id: 'one' }) | ||
} | ||
|
||
await fn(newFormData({ title: 'hello' })) | ||
let project = await prisma.project.findFirst({ where: { proj_id: 'one' } }) | ||
|
||
expect(project?.title).toEqual('hello') | ||
|
||
await fn(newFormData({ title: 'hello AGAIN!' })) | ||
project = await prisma.project.findFirst({ where: { proj_id: 'one' } }) | ||
|
||
expect(project?.title).toEqual('hello-again') | ||
}) | ||
}) |
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,34 @@ | ||
import { ActionFunctionArgs } from '@remix-run/node' | ||
import { renameProject } from '../models/project.server' | ||
import { ensure, handle, requireUser } from '../util/api.server' | ||
import slugify from 'slugify' | ||
import { Status } from '../util/statusCodes.server' | ||
import { Params } from '@remix-run/react' | ||
|
||
export const SLUGIFY_OPTIONS = { lower: true, remove: /[^a-z0-9A-Z ]/ } | ||
|
||
export async function action(args: ActionFunctionArgs) { | ||
return handle(args, { POST: handleRenameProject }) | ||
} | ||
|
||
export async function handleRenameProject(req: Request, params: Params<string>) { | ||
ensure(req.method === 'POST', 'invalid method', Status.METHOD_NOT_ALLOWED) | ||
|
||
const user = await requireUser(req) | ||
|
||
const { id } = params | ||
ensure(id != null, 'id is null', Status.BAD_REQUEST) | ||
|
||
const formData = await req.formData() | ||
|
||
const title = formData.get('title') | ||
ensure(title != null, 'title is null', Status.BAD_REQUEST) | ||
ensure(typeof title === 'string', 'title is not a string', Status.BAD_REQUEST) | ||
|
||
const slug = slugify(title, SLUGIFY_OPTIONS) | ||
ensure(slug.length > 0, 'title is too short', Status.BAD_REQUEST) | ||
|
||
await renameProject({ id: id, userId: user.user_id, title: slug }) | ||
|
||
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
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
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
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
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
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
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
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.