Skip to content

Commit

Permalink
Merge pull request #3 from stitchng/develop
Browse files Browse the repository at this point in the history
Develop
  • Loading branch information
stitchng authored Mar 12, 2019
2 parents 6eb28bd + a4b62a2 commit 1b3eb91
Show file tree
Hide file tree
Showing 10 changed files with 145 additions and 28 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
<a name="0.2.0"></a>
# 0.2.0 (2019-03-12)

- Fixed Error (Session is not instatiated)
- Changed middleware type/category to **Global Middleware**
- Updated dependency `@bugsnag/js` from _5.0.1_ to _6.0.0_ in the package.json file

<a name="0.1.5"></a>
# 0.1.5 (2019-03-05)

Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ MIT

```bash

npm run lint
npm run lint

npm run test

Expand Down
6 changes: 3 additions & 3 deletions instructions.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@ const providers = [

## Registering middleware

Register the following middleware inside `start/kernel.js` file.
Register the following middleware inside `start/kernel.js` file. Place the bugsnag middleware after the 'Adonis/Middleware/AuthInit' middleware

```js
const serverMiddleware = [
'Adonis/Middleware/Static',
const globalMiddleware = [
...
'Adonis/Middleware/AuthInit',
'Adonis/Middleware/BugSnagUser'
]
```
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "adonisjs-bugsnag",
"version": "0.1.5",
"version": "0.2.0",
"description": "BugSnag addon/plugin package for AdonisJS 4.0+ apps",
"main": "index.js",
"files": [
Expand Down Expand Up @@ -44,7 +44,7 @@
},
"dependencies": {
"@adonisjs/generic-exceptions": "^2.0.1",
"@bugsnag/js": "^5.0.1",
"@bugsnag/js": "^6.0.0",
"debug": "^3.1.2"
},
"devDependencies": {
Expand Down
2 changes: 1 addition & 1 deletion providers/BugSnagProvider.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class BugSnagProvider extends ServiceProvider {

this.app.bind('Adonis/Middleware/BugSnagUser', (app) => {
let BugSnagUser = require('../src/BugSnag/Middleware/BugSnagUser')
return new BugSnagUser(app.use('Adonis/Src/Config'))
return new BugSnagUser(this.app.use('Adonis/Addons/BugSnag'))
})
}
}
Expand Down
17 changes: 12 additions & 5 deletions src/BugSnag/Middleware/BugSnagUser.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,23 @@
'use strict'

class BugSnagUser {
constructor (Config) {
this.appSecret = Config.get('app.appKey')
constructor (bugsnag) {
this.notifierWrapper = bugsnag
}

async handle ({ request, auth }, next) {
let _user = await (typeof (auth.getUser) === 'function' ? auth.getUser() : Promise.resolve(null))
async handle ({ request, session, auth }, next) {
try {
let _user = await (typeof (auth.getUser) === 'function' ? auth.getUser() : Promise.resolve(null))

request.user = _user
this.notifierWrapper.setAuthUser(_user)
this.notifierWrapper.setContext(request, session)
} catch (err) {
;
}

await next()

// this.notifierWrapper.leaveBreadcrumb('"name"', '{metaData}', '"type"', 'timestamp')
}
}

Expand Down
18 changes: 10 additions & 8 deletions src/BugSnag/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,25 +20,27 @@ class BugSnagAPIClient {
}
}

setAuthUser (user) {
setAuthUser (user = null) {
this.notifier.user = user
}

setContext (request) {
setContext (request = {}, session = {}) {
this.notifier.context = {
routeName: typeof request.currentRoute === 'function' ? request.currentRoute() : { middleware: [], verb: [] }
route: typeof request.currentRoute === 'function' ? request.currentRoute() : {},
cookies: typeof request.cookies === 'function' ? request.cookies() : {},
session: typeof session.all === 'function' ? session.all() : {}
}
}

addMetaData (metaData) {
addMetaData (metaData = {}) {
this.notifier.metaData = metaData
}

notify (error, request, metaData, extraMetaData) {
if (request.user) {
this.setAuthUser(request.user)
}
setDevice (device = {}) {
this.notifier.device = device
}

notify (error, request, metaData, extraMetaData) {
if (metaData) {
this.addMetaData(metaData)
}
Expand Down
26 changes: 26 additions & 0 deletions test/bugsnagnotifier.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ test.group('AdonisJS BugSnag Test(s)', (group) => {
const AdonisBugSnagNotifierInstance = new BugSnag(BugSnagJSNotifierStub, this.config, this.helpers, this.env)

assert.isTrue(typeof AdonisBugSnagNotifierInstance.setAuthUser === 'function')
assert.isTrue(typeof AdonisBugSnagNotifierInstance.setContext === 'function')
assert.isTrue(typeof AdonisBugSnagNotifierInstance.setDevice === 'function')
assert.isTrue(typeof AdonisBugSnagNotifierInstance.addMetaData === 'function')
assert.isTrue(typeof AdonisBugSnagNotifierInstance.notify === 'function')
assert.isTrue(AdonisBugSnagNotifierInstance.notifier !== null)
Expand Down Expand Up @@ -68,4 +70,28 @@ test.group('AdonisJS BugSnag Test(s)', (group) => {
assert.isTrue(!!AdonisBugSnagNotifierInstance.notifier.report)
assert.deepEqual(AdonisBugSnagNotifierInstance.notifier.report.request, { url: 'https://127.0.0.1:333/dashboard/user' })
})

test('device and context method works as expected', (assert) => {
this.config.set('bugsnag.apiKey', 'q24cd5317608c5353de0794576ee015q')
this.config.set('bugsnag.trackViaSession', false)
this.env.set('NODE_ENV', 'development')

const request = {
url: () => 'https://127.0.0.1:333/dashboard/user',
cookies: () => ({ oid: 11 }),
user: null
}

const session = {
all: () => ({ locked: 0 })
}

const AdonisBugSnagNotifierInstance = new BugSnag(BugSnagJSNotifierStub, this.config, this.helpers, this.env)
AdonisBugSnagNotifierInstance.setContext(request, session)
AdonisBugSnagNotifierInstance.setDevice({ os: 'Linux Darwin' })

assert.isTrue(!!AdonisBugSnagNotifierInstance.notifier.context)
assert.deepEqual(AdonisBugSnagNotifierInstance.notifier.context.session, { locked: 0 })
assert.deepEqual(AdonisBugSnagNotifierInstance.notifier.device, { os: 'Linux Darwin' })
})
})
43 changes: 35 additions & 8 deletions test/middleware.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,42 @@
*/

const test = require('japa')
const { Config, setupResolver } = require('@adonisjs/sink')
// const { resolver } = require('@adonisjs/fold')
const path = require('path')
const { Config, Env, Helpers } = require('@adonisjs/sink')
const { ioc } = require('@adonisjs/fold')
const NotifierStub = require('./setup/notifier-stub.js')
const BugSnagMiddleware = require('../src/BugSnag/Middleware/BugSnagUser.js')
const BugSnag = require('../src/BugSnag/index.js')

test.group('AdonisJS BugSnag Middleware Test(s)', (group) => {
group.before(() => {
setupResolver()
ioc.singleton('Adonis/Src/Config', () => {
let config = new Config()
config.set('bugsnag.apiKey', 'q24cd5317608c5353de0794576ee015q')
config.set('bugsnag.trackViaSession', false)
return config
})

ioc.singleton('Adonis/Src/Env', () => {
let env = new Env()
env.set('NODE_ENV', 'development')
return env
})

ioc.singleton('Adonis/Src/Helpers', () => {
let helpers = new Helpers(path.join(__dirname, '..'))
return helpers
})
})

test('setup the request user upon handling', async (assert) => {
const request = {
url: () => 'https://127.0.0.1:333/dashboard/user',
cookie: () => {}
cookies: () => ({ _gd1: 'opened' })
}

const session = {
all: () => ({ adonis_auth: '1' })
}

const auth = {
Expand All @@ -31,16 +54,20 @@ test.group('AdonisJS BugSnag Middleware Test(s)', (group) => {
}
}

const context = { request, auth }
const middleware = new BugSnagMiddleware(new Config())
const context = { request, auth, session }
const bugsnag = new BugSnag(NotifierStub, ioc.use('Adonis/Src/Config'), ioc.use('Adonis/Src/Helpers'), ioc.use('Adonis/Src/Env'))
const middleware = new BugSnagMiddleware(bugsnag)

middleware
.handle(context, async function () {
return true
})
.then(() => {
assert.isTrue(!!request.user)
assert.equal(request.user.id, 1)
assert.isTrue(!!bugsnag.notifier.user)
assert.isTrue(!!bugsnag.notifier.context)
assert.isTrue(!!bugsnag.notifier.context.session)
assert.deepEqual(bugsnag.notifier.context.cookies, { _gd1: 'opened' })
assert.equal(bugsnag.notifier.user.id, 1)
})
})
})
48 changes: 48 additions & 0 deletions test/provider.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
'use strict'

/*
* adonis-bugsnag
*
* (c) Harminder Virk <virk@adonisjs.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

const test = require('japa')
const path = require('path')
const { Config, Env, Helpers } = require('@adonisjs/sink')
const { ioc } = require('@adonisjs/fold')
const BugSnagMiddleware = require('../src/BugSnag/Middleware/BugSnagUser.js')
const BugSnagProvider = require('../providers/BugSnagProvider.js')
const BugSnag = require('../src/BugSnag/index.js')

test.group('AdonisJS BugSnag Provider Test(s)', (group) => {
group.before(() => {
ioc.singleton('Adonis/Src/Config', () => {
let config = new Config()
config.set('bugsnag.apiKey', 'q24cd5317608c5353de0794576ee015q')
config.set('bugsnag.trackViaSession', false)
return config
})

ioc.singleton('Env', () => {
let env = new Env()
env.set('NODE_ENV', 'development')
return env
})

ioc.singleton('Helpers', () => {
let helpers = new Helpers(path.join(__dirname, '..'))
return helpers
})
})

test('provider instance registers instance(s) as expected', async (assert) => {
let provider = new BugSnagProvider(ioc)
provider.register()

assert.instanceOf(ioc.use('Adonis/Addons/BugSnag'), BugSnag)
assert.instanceOf(ioc.use('Adonis/Middleware/BugSnagUser'), BugSnagMiddleware)
})
})

0 comments on commit 1b3eb91

Please sign in to comment.