Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
it('Usage with a Hook', () => {
class SocialPost {
@Length(10, 20)
title: string;
@Contains('hello')
text: string;
}
class SocialPostController {
@Post()
@ValidateBodyFromClass(SocialPost, { /* options if relevant */ })
createSocialPost() {
// ...
return new HttpResponseCreated();
}
}
const app = createApp(SocialPostController);
return request(app)
.post('/')
.send({ text: 'foo' })
.expect(400)
.expect([
{
describe('Validation & Sanitization of HTTP Requests', () => {
// Test compilation
// tslint:disable-next-line:no-unused-variable
class MyController {
@Post('/user')
@ValidateBody({
additionalProperties: false,
properties: {
firstName: { type: 'string' },
lastName: { type: 'string' },
},
required: [ 'firstName', 'lastName' ],
type: 'object'
})
postUser(ctx: Context) {
// In this method we are sure that firstName and lastName
// are defined thanks to the above hook.
console.log(
ctx.request.body.firstName, ctx.request.body.lastName
);
return new HttpResponseOK();
}
async home(ctx: Context) {
// Normally in an HTML template
const response = new HttpResponseOK();
setCsrfCookie(response, await getCsrfToken());
return response;
}
}
const actualDocument = createOpenApiDocument(ApiController);
deepStrictEqual(actualDocument, expectedDocument);
// Test hook conflicts (Two calls of @JWT).
@ApiInfo({
title: 'My API',
version: '1.0.0'
})
class ApiController2 {
@Get('/products')
@JWTOptional()
readProducts() {}
@Post('/products')
@JWTRequired()
@ValidateBody({
properties: {
name: { type: 'string' }
},
type: 'object',
})
createProduct() {}
}
const yamlDocument2 = readFileSync(join(__dirname, './assets/openapi.hooks2.yml'), 'utf8');
const expectedDocument2 = parse(yamlDocument2);
const actualDocument2 = createOpenApiDocument(ApiController2);
deepStrictEqual(actualDocument2, expectedDocument2);
class AppController {
subControllers = [
MyController,
AuthController
];
}
await createConnection({
database: 'e2e_db.sqlite',
dropSchema: true,
entities: [ User, Permission, Group ],
synchronize: true,
type: 'sqlite',
});
const app = createApp(AppController);
/* Create a user */
await createConnection({
database: 'e2e_db.sqlite',
dropSchema: true,
entities: [ User, Permission, Group ],
name: 'create-connection',
synchronize: true,
type: 'sqlite',
});
const user = new User();
user.email = 'john@foalts.org';
user.password = await hashPassword('password');
await getRepository(User, 'create-connection').save(user);
text: string;
}
class SocialPostController {
@Post()
@ValidateBodyFromClass(SocialPost, { /* options if relevant */ })
createSocialPost() {
// ...
return new HttpResponseCreated();
}
}
const app = createApp(SocialPostController);
return request(app)
.post('/')
.send({ text: 'foo' })
.expect(400)
.expect([
{
children: [],
constraints: { length: 'title must be longer than or equal to 10 characters' },
property: 'title',
target: { text: 'foo' },
},
{
children: [],
constraints: { contains: 'text must contain a hello string' },
property: 'text',
password: string;
isAdmin: boolean;
}
// We need to call the model 'User2' here not to conflict with another test in this package.
const UserModel: Model = model('User2', UserSchema);
function AdminRequired() {
return Hook((ctx: Context) => {
if (!ctx.user.isAdmin) {
return new HttpResponseForbidden();
}
});
}
@TokenRequired({ user: fetchUser(UserModel), store: RedisStore })
class MyController {
@Get('/foo')
foo() {
return new HttpResponseOK();
}
@Get('/bar')
@AdminRequired()
bar() {
return new HttpResponseOK();
}
}
class AuthController {
@dependency
store: RedisStore;
}
}
@TokenRequired({
cookie: true,
store: TypeORMStore,
})
@CsrfTokenRequired()
class ApiController {
@Post('/products')
createProduct() {
return new HttpResponseCreated();
}
}
@TokenRequired({
cookie: true,
redirectTo: '/login',
store: TypeORMStore,
})
class PageController {
@Get('/home')
async home(ctx: Context) {
// Normally in an HTML template
return new HttpResponseOK({ csrfToken: await getCsrfToken(ctx.session) });
}
}
class AppController {
subControllers = [
AuthController,
PageController,
async function main() {
// Init Nuxt.js
const nuxt = new Nuxt(config);
// Build only in dev mode
if (config.dev) {
const builder = new Builder(nuxt);
await builder.build();
} else {
await nuxt.ready();
}
await createConnection();
const app = createApp(AppController, {
postMiddlewares: [
nuxt.render
]
});
const httpServer = http.createServer(app);
const port = Config.get('port', 3001);
httpServer.listen(port, () => {
console.log(`Listening on port ${port}...`);
});
}
it('should redirect the user to xxx/ if there is no trailing slash in the URL.', async () => {
// This way, the browser requests the assets at the correct path (the relative path).
const controller = new ConcreteClass();
const ctx = new Context({ path: 'xxx' });
const response = await controller.index(ctx);
if (!isHttpResponseMovedPermanently(response)) {
throw new Error('SwaggerController.index should return an HttpResponseMovedPermanently instance.');
}
strictEqual(response.path, ctx.request.path + '/');
});