Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
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 + '/');
});
it('should return an HttpResponseOK object with the testFooBar list.', async () => {
const ctx = new Context({ query: {} });
const response = await controller.get(ctx);
if (!isHttpResponseOK(response)) {
throw new Error('The returned value should be an HttpResponseOK object.');
}
if (!Array.isArray(response.body)) {
throw new Error('The response body should be an array of testFooBars.');
}
strictEqual(response.body.length, 2);
ok(response.body.find(testFooBar => testFooBar.text === testFooBar1.text));
ok(response.body.find(testFooBar => testFooBar.text === testFooBar2.text));
});
it('should return an HttpResponseNotFound object if the testFooBar was not found.', async () => {
const ctx = new Context({
params: {
testFooBarId: -1
}
});
const response = await controller.getById(ctx);
if (!isHttpResponseNotFound(response)) {
throw new Error('The returned value should be an HttpResponseNotFound object.');
}
});
+ 'an HttpResponseCreated object.', async () => {
const ctx = new Context({
body: {
text: 'TestFooBar 3',
}
});
const response = await controller.post(ctx);
if (!isHttpResponseCreated(response)) {
throw new Error('The returned value should be an HttpResponseCreated object.');
}
const testFooBar = await getRepository(TestFooBar).findOne({ text: 'TestFooBar 3' });
if (!testFooBar) {
throw new Error('No testFooBar 3 was found in the database.');
}
it('should return an HttpResponseNotFound if no controller is found with that name.', () => {
class ConcreteClass extends SwaggerController {
options = [
{ name: 'v1', url: 'http://example.com' },
];
}
const controller = new ConcreteClass();
let ctx = new Context({ query: { name: 'v2' } });
let response = controller.getOpenApiDefinition(ctx);
if (!isHttpResponseNotFound(response)) {
throw new Error('The response should be an instance of HttpResponseNotFound.');
}
ctx = new Context({ query: { name: 'v1' } });
response = controller.getOpenApiDefinition(ctx);
if (!isHttpResponseNotFound(response)) {
throw new Error('The response should be an instance of HttpResponseNotFound.');
}
});
it('should return an HttpResponseNotFound if the object does not exist.', async () => {
const ctx = new Context({
body: {
text: '',
},
params: {
id: -1
}
});
const response = await controller.putById(ctx);
if (!isHttpResponseNotFound(response)) {
throw new Error('The returned value should be an HttpResponseNotFound object.');
}
});
it('should let ctx.user equal undefined if the cookie does not exist.', async () => {
const hook = getHookFunction(JWT({ cookie: true }));
const ctx = new Context({ get(str: string) { return undefined; }, cookies: {} });
const response = await hook(ctx, services);
strictEqual(response, undefined);
strictEqual(ctx.user, undefined);
});
it('should return an HttpResponseOK object with the product list.', async () => {
const ctx = new Context({ query: {} });
const response = await controller.get(ctx);
if (!isHttpResponseOK(response)) {
throw new Error('The returned value should be an HttpResponseOK object.');
}
if (!Array.isArray(response.body)) {
throw new Error('The response body should be an array of products.');
}
strictEqual(response.body.length, 2);
ok(response.body.find(product => product.text === product1.text));
ok(response.body.find(product => product.text === product2.text));
});
it('should return an HttpResponseBadRequest if there are additional properties.', () => {
const preHook = validateEmailAndPasswordCredentialsFormat();
const ctx = new Context();
ctx.request.body = {
email: 'john@jack.com',
foo: 'bar',
password: 'myPassword',
};
const response = preHook(ctx, new ServiceManager());
expect(response).to.be.instanceOf(HttpResponseBadRequest);
expect((response as HttpResponseBadRequest).content).to.be.an('array').and.to.have.lengthOf(1);
expect((response as HttpResponseBadRequest).content[0]).to.deep.equal({
dataPath: '',
keyword: 'additionalProperties',
message: 'should NOT have additional properties',
params: {
additionalProperty: 'foo',
},
},
},
type: GraphQLString,
resolve(obj, args, context, info) {
return JSON.stringify(args);
}
}
},
name: 'RootQueryType',
})
});
}
controller = createController(ConcreteController);
const query = `query a($input: String) { hello(input: $input) }`;
const ctx = new Context({
query: { query, variables: '{"input":"foobar"}' }
});
const response = await controller.post(ctx);
if (!isHttpResponseOK(response)) {
throw new Error('The controller should have returned an HttpResponseOK instance.');
}
deepStrictEqual(response.body, {
data: {
hello: '{"input":"foobar"}'
}
});
});