Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
it('can create an instance of the related model', async () => {
const order = await customerRepo.orders(existingCustomerId).create({
description: 'order 1',
});
expect(toJSON(order)).containDeep(
toJSON({
customerId: existingCustomerId,
description: 'order 1',
}),
);
const persisted = await orderRepo.findById(order.id);
expect(toJSON(persisted)).to.deepEqual(
toJSON({
...order,
isShipped: features.emptyValue,
// eslint-disable-next-line @typescript-eslint/camelcase
shipment_id: features.emptyValue,
}),
);
});
it('gives a warning if called on an app without RepositoryMixin', async () => {
const normalApp = new Application();
await sandbox.copyFile(
resolve(__dirname, '../../fixtures/multiple.artifact.js'),
);
const booterInst = new RepositoryBooter(
normalApp as ApplicationWithRepositories,
SANDBOX_PATH,
);
// Load uses discovered property
booterInst.discovered = [resolve(SANDBOX_PATH, 'multiple.artifact.js')];
await booterInst.load();
sinon.assert.calledOnce(stub);
sinon.assert.calledWith(
stub,
'app.repository() function is needed for RepositoryBooter. You can add it ' +
'to your Application using RepositoryMixin from @loopback/repository.',
);
});
describe('datasource booter unit tests', () => {
const SANDBOX_PATH = resolve(__dirname, '../../../.sandbox');
const sandbox = new TestSandbox(SANDBOX_PATH);
const DATASOURCES_PREFIX = 'datasources';
const DATASOURCES_TAG = 'datasource';
class AppWithRepo extends RepositoryMixin(Application) {}
let app: AppWithRepo;
// eslint-disable-next-line @typescript-eslint/no-explicit-any
let stub: sinon.SinonStub<[any?, ...any[]], void>;
beforeEach('reset sandbox', () => sandbox.reset());
beforeEach(getApp);
beforeEach(createStub);
afterEach(restoreStub);
it('gives a warning if called on an app without RepositoryMixin', async () => {
describe('repository booter unit tests', () => {
const SANDBOX_PATH = resolve(__dirname, '../../../.sandbox');
const sandbox = new TestSandbox(SANDBOX_PATH);
const REPOSITORIES_PREFIX = 'repositories';
const REPOSITORIES_TAG = 'repository';
class RepoApp extends RepositoryMixin(Application) {}
let app: RepoApp;
// eslint-disable-next-line @typescript-eslint/no-explicit-any
let stub: sinon.SinonStub<[any?, ...any[]], void>;
beforeEach('reset sandbox', () => sandbox.reset());
beforeEach(getApp);
beforeEach(createStub);
afterEach(restoreStub);
it('gives a warning if called on an app without RepositoryMixin', async () => {
describe('repository booter integration tests', () => {
const SANDBOX_PATH = resolve(__dirname, '../../.sandbox');
const sandbox = new TestSandbox(SANDBOX_PATH);
// Remnants from Refactor -- need to add these to core
const REPOSITORIES_PREFIX = 'repositories';
const REPOSITORIES_TAG = 'repository';
let app: BooterApp;
beforeEach('reset sandbox', () => sandbox.reset());
beforeEach(getApp);
it('boots repositories when app.boot() is called', async () => {
const expectedBindings = [
`${REPOSITORIES_PREFIX}.ArtifactOne`,
`${REPOSITORIES_PREFIX}.ArtifactTwo`,
];
it('throws an error when the target does not have any primary key', () => {
class Product extends Entity {
static definition = new ModelDefinition(
'Product',
).addProperty('categoryId', {type: Number});
}
class Category extends Entity {
static definition = new ModelDefinition('Category');
}
const productRepo = createStubInstance(DefaultCrudRepository);
const categoryRepo = createStubInstance(DefaultCrudRepository);
const relationMeta: BelongsToDefinition = {
type: RelationType.belongsTo,
targetsMany: false,
name: 'category',
source: Product,
target: () => Category,
keyFrom: 'categoryId',
// Let the relation to look up keyTo as the primary key of Category
// (which is not defined!)
keyTo: undefined,
};
expect(() =>
createBelongsToAccessor(
relationMeta,
it('throws an error when the target does not have any primary key', () => {
class Product extends Entity {
static definition = new ModelDefinition(
'Product',
).addProperty('categoryId', {type: Number});
}
class Category extends Entity {
static definition = new ModelDefinition('Category');
}
const productRepo = createStubInstance(DefaultCrudRepository);
const categoryRepo = createStubInstance(DefaultCrudRepository);
const relationMeta: BelongsToDefinition = {
type: RelationType.belongsTo,
targetsMany: false,
name: 'category',
source: Product,
target: () => Category,
keyFrom: 'categoryId',
// Let the relation to look up keyTo as the primary key of Category
// (which is not defined!)
keyTo: undefined,
};
expect(() =>
createBelongsToAccessor(
const invocationCtx = givenInvocationContext();
const keys = invocationCtx.getGlobalInterceptorBindingKeys();
expect(keys).to.eql([
'globalInterceptors.authInterceptor',
'globalInterceptors.logInterceptor',
]);
});
// See https://v8.dev/blog/array-sort
function isSortStable() {
// v8 7.0 or above
return +process.versions.v8.split('.')[0] >= 7;
}
skipIf(
!isSortStable(),
it,
'sorts by binding order without group tags',
async () => {
ctx
.bind('globalInterceptors.authInterceptor')
.to(authInterceptor)
.apply(asGlobalInterceptor());
ctx
.bind('globalInterceptors.logInterceptor')
.to(logInterceptor)
.apply(asGlobalInterceptor());
const invocationCtx = givenInvocationContext();
realWorldAPIs = process.env.APIS.split(/\s+/)
.filter(Boolean)
.map(url => ({
swaggerUrl: url,
name: '',
version: '',
}));
return;
}
// This hook sometimes takes several seconds, due to the large download
// eslint-disable-next-line no-invalid-this
this.timeout(10000);
// Download a list of over 1500 real-world Swagger APIs from apis.guru
const res = await supertest('https://api.apis.guru')
.get('/v2/list.json')
.expect(200);
if (!res.ok) {
throw new Error('Unable to download API listing from apis.guru');
}
// Remove certain APIs that are known to cause problems
const apis = res.body;
// GitHub's CORS policy blocks this request
delete apis['googleapis.com:adsense'];
// These APIs cause infinite loops in json-schema-ref-parser. Still investigating.
// https://github.com/BigstickCarpet/json-schema-ref-parser/issues/56
delete apis['bungie.net'];
delete apis['stripe.com'];
it('parses query without decorated rest query params', async () => {
// This handler responds with the query object (which is expected to
// be parsed by Express)
function requestWithQueryHandler({request, response}: RequestContext) {
response.json(request.query);
response.end();
}
// See https://github.com/strongloop/loopback-next/issues/2088
const server = await givenAServer();
server.handler(requestWithQueryHandler);
await server.start();
await supertest(server.url)
.get('/?x=1&y[a]=2')
.expect(200, {x: '1', y: {a: '2'}});
await server.stop();
});