Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
test.group('IoC', function () {
// Setup
ioc.fake('Adonis/Addons/Mongoose', () => require('mongoose'))
ioc.fake('Mongoose', () => ioc.use('Adonis/Addons/Mongoose'))
const BaseModel = require('../src/Model/Base')
const prov = new ServiceProvider(ioc)
prov._registerModel()
test('model should be instanciated property', function (assert) {
const Model = use('MongooseModel')
assert.exists(Model)
assert.equal(Model, BaseModel)
})
})
group.before(async () => {
await registrar.providers([
path.join(__dirname, '../providers/SwaggerProvider')
]).register()
// call in boot SwaggerProvider
ace.addCommand('Adonis/Commands/SwaggerExport')
ace.addCommand('Adonis/Commands/SwaggerExportDocs')
ace.addCommand('Adonis/Commands/SwaggerRemove')
ace.addCommand('Adonis/Commands/SwaggerRemoveDocs')
})
async _fetchTask (file) {
const filePath = path.join(this.tasksPath, file)
let task
try {
task = require(filePath)
} catch (e) {
if (e instanceof ReferenceError) {
debug('Unable to import task class <%s>. Is it a valid javascript class?', file)
return
} else {
throw e
}
}
// Get instance of task class
const taskInstance = ioc.make(task)
// Every task must expose a schedule
if (!('schedule' in task)) {
throw CE.RuntimeException.undefinedTaskSchedule(file)
}
// Every task must expose a handle function
if (!('handle' in taskInstance)) {
throw CE.RuntimeException.undefinedTaskHandle(file)
}
if (!(taskInstance instanceof Task)) {
throw CE.RuntimeException.undefinedInstanceTask(file)
}
// Track currently registered tasks in memory
// Every job must expose a key
if (!Job.key) {
throw new Error(`No key found for job: ${link}`)
}
// If job concurrency is not set, default to 1
if (Job.concurrency === undefined) {
Job.concurrency = 1
}
// If job concurrecny is set to an invalid value, throw error
if (typeof Job.concurrency !== 'number') {
throw new Error(`Job concurrency value must be a number but instead it is: <${typeof Job.concurrency}>`)
}
const jobInstance = ioc.make(Job)
// Every job must expose a handle function
if (!jobInstance.handle) {
throw new Error(`No handler found for job: ${link}`)
}
// Track currently registered jobs in memory
this.registeredJobs.push(Job)
// Register job handler
this.instance.process(Job.key, Job.concurrency, (job, done) => {
jobInstance.handle(job.data)
.then(result => {
done(null, result)
})
.catch(error => {
test('global middleware execution', assert => {
const Gql = ioc.use('Gql')
class Middleware {
async gqlHandle (resolve, root, args, context, info) {}
}
ioc.bind('Adonis/Middleware/Auth', () => {
return new Middleware()
})
class UserMiddlewareController {
user (parent, args, ctx) {}
users (parent, args, ctx) {}
}
ioc.bind('App/Controllers/Gql/UserMiddlewareController', () => {
return UserMiddlewareController
test('attach sideloaded data as meta', async (assert) => {
class Profile extends Model {
}
class User extends Model {
profile () {
return this.hasOne(Profile)
}
}
User._bootIfNotBooted()
Profile._bootIfNotBooted()
await ioc.use('Database').table('users').insert([{ username: 'virk' }, { username: 'nikk' }])
await ioc.use('Database').table('profiles').insert([{ user_id: 1, profile_name: 'virk' }, { user_id: 2, profile_name: 'nikk' }])
const users = await User.query().withCount('profile').paginate()
const json = users.toJSON()
assert.property(json, 'data')
assert.isArray(json.data)
assert.deepEqual(json.data[0].__meta__, { profile_count: helpers.formatNumber(1) })
assert.equal(json.page, 1)
assert.equal(json.total, 2)
assert.equal(json.perPage, 20)
assert.equal(json.lastPage, 1)
})
it('searchableAs returns prefixed model table', () => {
const model = new ModelStub()
ModelStub._bootIfNotBooted()
ioc.use('Config').set('scout.prefix', 'prefix_')
expect(model.searchableAs()).toBe('prefix_posts')
})
wire: async function () {
setupResolver()
ioc.bind('Adonis/Src/Helpers', () => new Helpers(path.join(__dirname, '..', 'app')))
await registrar.providers([
'@adonisjs/framework/providers/AppProvider',
'@adonisjs/lucid/providers/LucidProvider',
'@adonisjs/validator/providers/ValidatorProvider',
'@adonisjs/antl/providers/AntlProvider',
path.join(__dirname, '../providers/TranslatableProvider')
]).registerAndBoot()
ioc.singleton('App/Models/Role', (app) => {
const Model = app.use('Model')
class Role extends Model {
static boot () {
super.boot()
this.addTrait('Translatable', {
className: 'App/Models/RoleTranslation',
test('global middleware execution', assert => {
const Gql = ioc.use('Gql')
class Middleware {
async gqlHandle (resolve, root, args, context, info) {}
}
ioc.bind('Adonis/Middleware/Auth', () => {
return new Middleware()
})
class UserMiddlewareController {
user (parent, args, ctx) {}
users (parent, args, ctx) {}
}
ioc.bind('App/Controllers/Gql/UserMiddlewareController', () => {
return UserMiddlewareController
})
Gql.schema('UserMiddleware', () => {
Gql.query('UserMiddlewareController')
})
test('middleware registered when schema and resolver is defined and inside controller', assert => {
const Gql = ioc.use('Gql')
class Middleware {
async gqlHandle (resolve, root, args, context, info) {}
}
ioc.bind('Adonis/Middleware/Auth', () => {
return new Middleware()
})
ioc.bind('Adonis/Middleware/Acl', () => {
return new Middleware()
})
ioc.bind('Adonis/Middleware/SaaS', () => {
return new Middleware()
})
Gql.registerNamed({
auth: 'Adonis/Middleware/Auth',
acl: 'Adonis/Middleware/Acl',
saas: 'Adonis/Middleware/SaaS'
})
class UserMiddlewareController {
user (parent, args, ctx) {}
static middlewares () {
return {
all: ['auth'],
user: ['acl']