-
Notifications
You must be signed in to change notification settings - Fork 0
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
1 parent
2fe69af
commit 41b987e
Showing
11 changed files
with
120 additions
and
7 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,15 @@ | ||
import { getConnection } from 'typeorm' | ||
import { Project } from '../entities/project' | ||
import { Session } from '../entities/session' | ||
import { PolicyActionIndex } from './policy' | ||
|
||
export const canIndexProject: PolicyActionIndex = async function canIndexProject(session) { | ||
return !!session | ||
} | ||
|
||
export async function projectPolicyScope(session: Session) { | ||
return getConnection() | ||
.createQueryBuilder(Project, 'project') | ||
.innerJoin('project.users', 'user') | ||
.where('user.id = :userId', { userId: session.user.id }) | ||
} |
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 { FastifyInstance } from 'fastify' | ||
import { authorizeOfFail } from '../policies/policy' | ||
import { canIndexProject, projectPolicyScope } from '../policies/projects-policy' | ||
import { ProjectsIndexResponse } from '../schemas/types/projects.index.response' | ||
import * as projectsIndexResponseSchema from '../schemas/json/projects.index.response.json' | ||
|
||
export async function projectsRoutes(fastify: FastifyInstance) { | ||
fastify.get('/', { | ||
schema: { | ||
response: { | ||
200: projectsIndexResponseSchema | ||
} | ||
}, | ||
handler: async function index(request): Promise<ProjectsIndexResponse> { | ||
await authorizeOfFail(canIndexProject, request.session, null) | ||
const projects = (await projectPolicyScope(request.session!)).getMany() | ||
return projects | ||
} | ||
}) | ||
} |
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,14 @@ | ||
{ | ||
"$schema": "http://json-schema.org/schema", | ||
"type": "array", | ||
"items": { | ||
"type": "object", | ||
"properties": { | ||
"id": { "type": "number" }, | ||
"name": { "type": "string" }, | ||
"instructions": { "type": ["string", "null"] } | ||
}, | ||
"additionalProperties": false, | ||
"required": ["id", "name", "instructions"] | ||
} | ||
} |
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,12 @@ | ||
/* tslint:disable */ | ||
/** | ||
* This file was automatically generated by json-schema-to-typescript. | ||
* DO NOT MODIFY IT BY HAND. Instead, modify the source JSONSchema file, | ||
* and run json-schema-to-typescript to regenerate this file. | ||
*/ | ||
|
||
export type ProjectsIndexResponse = { | ||
id: number; | ||
name: string; | ||
instructions: string | 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
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,18 @@ | ||
import { getConnection } from 'typeorm' | ||
import { Project } from '../../entities/project' | ||
import { User } from '../../entities/user' | ||
import * as faker from 'faker' | ||
|
||
type ProjectFixtureOptions = { users?: User[] } | ||
|
||
export function buildProjectFixture(opts: ProjectFixtureOptions = {}) { | ||
const project = new Project() | ||
project.name = faker.company.companyName() | ||
project.instructions = faker.lorem.paragraphs(2) | ||
if (opts.users) project.users = Promise.resolve(opts.users) | ||
return project | ||
} | ||
|
||
export function createProjectFixture(opts: ProjectFixtureOptions = {}) { | ||
return getConnection().getRepository(Project).save(buildProjectFixture(opts)) | ||
} |
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,26 @@ | ||
import { expect } from 'chai' | ||
import { sign } from 'cookie-signature' | ||
import { COOKIE_NAME, COOKIE_SECRET } from '../../lib/dotenv' | ||
import { server } from '../../lib/fastify' | ||
import { ProjectsIndexResponse } from '../../schemas/types/projects.index.response' | ||
import { createProjectFixture } from '../fixtures/projects-fixtures' | ||
import { createSessionFixture } from '../fixtures/sessions-fixtures' | ||
|
||
describe('/projects', function () { | ||
describe('#index', function () { | ||
it('should return projects of the current user', async function () { | ||
const session = await createSessionFixture() | ||
const project1 = await createProjectFixture({ users: [session.user] }) | ||
const project2 = await createProjectFixture({ users: [session.user] }) | ||
const project3 = await createProjectFixture() | ||
|
||
const cookies = { [COOKIE_NAME]: sign(session.id, COOKIE_SECRET) } | ||
const response = await server.inject({ method: 'GET', url: '/projects', cookies }) | ||
expect(response.statusCode).to.eq(200) | ||
|
||
const json = response.json<ProjectsIndexResponse>() | ||
expect(json.map(_ => _.id)).to.have.members([project1.id, project2.id]) | ||
expect(json.map(_ => _.id)).to.not.include(project3.id) | ||
}) | ||
}) | ||
}) |