Skip to content

Commit

Permalink
feat: add bree to context
Browse files Browse the repository at this point in the history
  • Loading branch information
shadowgate15 committed Aug 6, 2021
1 parent 5784525 commit 22763ca
Show file tree
Hide file tree
Showing 15 changed files with 130 additions and 28 deletions.
4 changes: 4 additions & 0 deletions .env.test
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#################
## api secrets ##
#################
JWT_SECRET=secret
9 changes: 2 additions & 7 deletions api.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ require('./config/env');

const API = require('@ladjs/api');
const Graceful = require('@ladjs/graceful');
const Mongoose = require('@ladjs/mongoose');
const ip = require('ip');

const logger = require('./helpers/logger');
Expand All @@ -12,10 +11,7 @@ const apiConfig = require('./config/api');
const api = new API(apiConfig);

if (!module.parent) {
const mongoose = new Mongoose({ ...api.config.mongoose, logger });

const graceful = new Graceful({
mongooses: [mongoose],
servers: [api],
redisClients: [api.client],
logger
Expand All @@ -30,9 +26,8 @@ if (!module.parent) {
logger.info(
`Lad API server listening on ${port} (LAN: ${ip.address()}:${port})`
);
await mongoose.connect();
} catch (err) {
logger.error(err);
} catch (error) {
logger.error(error);
// eslint-disable-next-line unicorn/no-process-exit
process.exit(1);
}
Expand Down
2 changes: 1 addition & 1 deletion app/controllers/api/v1/config.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
async function get(ctx) {
ctx.body = 'it worked';
ctx.body = { breeExists: ctx.bree };
}

module.exports = { get };
4 changes: 1 addition & 3 deletions app/controllers/api/v1/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,7 @@ const users = require('./users');
const config = require('./config');

const test = (ctx) => {
console.log('jungle');
console.log(ctx);
ctx.body = 'it worked';
ctx.body = { breeExists: Boolean(ctx.bree) };
};

module.exports = { log, users, config, test };
31 changes: 26 additions & 5 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,32 @@
const bree = require('./bree');
const api = require('./api');
const web = require('./web');
const proxy = require('./proxy');

const config = require('./config');

function plugin(opts, Bree) {
opts = {
port: config.port,
...opts
};

const oldInit = Bree.prototype.init;

Bree.prototype.init = function () {
oldInit.bind(this)();

// assign bree to the context
api.app.context.bree = this;

this.api = api;

this.api.listen(opts.port).catch((err) => {
throw err;
});
};
}

module.exports = {
bree,
api,
web,
proxy
proxy,
plugin
};
18 changes: 14 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@
"bitter-font": "^0.0.1",
"boolean": "^3.0.2",
"bootstrap": "4.6.0",
"bree": "^6.0.0",
"bree": "^6.2.1",
"bson-objectid": "^1.3.1",
"cabin": "^9.0.4",
"captain-hook": "^0.0.3",
Expand Down Expand Up @@ -177,6 +177,7 @@
"gulp-xo": "^0.25.0",
"husky": "^5.0.9",
"imagemin-pngquant": "^9.0.1",
"jsonwebtoken": "^8.5.1",
"lint-staged": "10.5.4",
"make-dir": "^3.1.0",
"ms": "^2.1.3",
Expand Down Expand Up @@ -217,8 +218,8 @@
"keywords": [
"api",
"lad",
"lad",
"lass"
"lass",
"bree"
],
"license": "Unlicensed",
"main": "index.js",
Expand Down Expand Up @@ -284,7 +285,16 @@
"unicorn/prevent-abbreviations": "off",
"unicorn/no-fn-reference-in-iterator": "off",
"import/extensions": "off",
"capitalized-comments": "off"
"capitalized-comments": "off",
"unicorn/catch-error-name": "off"
}
},
"ava": {
"files": [
"test/*.js",
"test/**/*.js",
"!test/jobs",
"!test/utils.js"
]
}
}
Empty file added routes/plugin
Empty file.
16 changes: 11 additions & 5 deletions test/api/v1.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
const test = require('ava');
const jwt = require('jsonwebtoken');

const config = require('../../config');
const phrases = require('../../config/phrases');

const utils = require('../utils');

test.beforeEach(async (t) => {
test.before(async (t) => {
await utils.setupApiServer(t);
t.context.token =
'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VyIjoiVGF5bG9yIFNjaGxleSIsImlhdCI6MTYyNzY4MDY1OCwiZXhwIjoxNjI3Njg0MjU4fQ.CA0sTEhWSSiJLHxNy8geWXf8b16mYDLb4kKI_tiI6fU';
t.context.token = jwt.sign({}, config.jwt.secret);
});

test('fails when no creds are presented', async (t) => {
Expand All @@ -21,6 +20,13 @@ test('works with jwt auth', async (t) => {
const { api, token } = t.context;
const res = await api.get('/v1/test').auth(token, { type: 'bearer' });

t.log(res);
t.is(200, res.status);
});

test('has bree in context', async (t) => {
const { api, token } = t.context;
const res = await api.get('/v1/test').auth(token, { type: 'bearer' });

t.is(200, res.status);
t.is(Boolean(res.body.breeExists), true);
});
13 changes: 13 additions & 0 deletions test/config/snapshots/utilities.js.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# Snapshot report for `test/config/utilities.js`

The actual snapshot is saved in `utilities.js.snap`.

Generated by [AVA](https://avajs.dev).

## returns JSON with 2 spaces

> Snapshot 1
`{␊
"ok": "hey"␊
}`
Binary file added test/config/snapshots/utilities.js.snap
Binary file not shown.
3 changes: 3 additions & 0 deletions test/jobs/basic.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
setTimeout(() => {
console.log('hello');
}, 100);
1 change: 1 addition & 0 deletions test/jobs/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

30 changes: 30 additions & 0 deletions test/plugin/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
const test = require('ava');

const { baseConfig } = require('../utils');

test.beforeEach((t) => {
t.context.Bree = require('bree');
t.context.plugin = require('../..').plugin;
});

test('api does not exist on bree constructor', (t) => {
const { Bree, plugin } = t.context;

Bree.extend(plugin);

t.is(typeof Bree.api, 'undefined');
});

test('api does exist on bree instance', (t) => {
const { Bree, plugin } = t.context;

Bree.extend(plugin);

const bree = new Bree(baseConfig);

t.log(bree);
// just to make sure this works correctly
t.is(typeof bree.start, 'function');
t.log(bree.api);
t.is(typeof bree.api, 'object');
});
25 changes: 23 additions & 2 deletions test/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ const mongoose = require('mongoose');
const request = require('supertest');
const { factory, MongooseAdapter } = require('factory-girl');
const getPort = require('get-port');
const path = require('path');

factory.setAdapter(new MongooseAdapter());

Expand Down Expand Up @@ -33,9 +34,16 @@ exports.setupWebServer = async (t) => {

exports.setupApiServer = async (t) => {
// Must require here in order to load changes made during setup
const { app } = require('../api');
const Bree = require('bree');
const { plugin } = require('../');

const port = await getPort();
t.context.api = request.agent(app.listen(port));

Bree.extend(plugin, { port });

const bree = new Bree(baseConfig);

t.context.api = request.agent(bree.api.server);
};

// Make sure to load the web server first using setupWebServer
Expand Down Expand Up @@ -77,3 +85,16 @@ exports.defineUserFactory = async () => {
return user;
});
};

const root = path.join(__dirname, 'jobs');
exports.root = root;

const baseConfig = {
root,
timeout: 0,
interval: 0,
hasSeconds: false,
defaultExtension: 'js',
jobs: ['basic']
};
exports.baseConfig = baseConfig;
2 changes: 1 addition & 1 deletion yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -3262,7 +3262,7 @@ braces@^3.0.1, braces@~3.0.2:
dependencies:
fill-range "^7.0.1"

bree@^6.0.0:
bree@^6.2.1:
version "6.2.1"
resolved "https://registry.yarnpkg.com/bree/-/bree-6.2.1.tgz#a8874d8d3e4fa081ada17c5c62a55cf30a1e81e9"
integrity sha512-apgOBJp/4onxroljplYKMUP1gxxUKhoftSh63h037avAuQLM7vrtJuyB8TYE9+dzduWkjb2CCKwfOkqEpMFdAg==
Expand Down

0 comments on commit 22763ca

Please sign in to comment.