Skip to content

Commit

Permalink
fix: broken extend logic for ally manager
Browse files Browse the repository at this point in the history
  • Loading branch information
thetutlage committed Jun 4, 2021
1 parent 745344f commit 1a722f8
Show file tree
Hide file tree
Showing 5 changed files with 112 additions and 3 deletions.
4 changes: 2 additions & 2 deletions providers/AllyProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ export default class AllyProvider {
* Register the binding
*/
public register() {
this.application.container.bind('Adonis/Addons/Ally', (container) => {
this.application.container.singleton('Adonis/Addons/Ally', (container) => {
const config = container.resolveBinding('Adonis/Core/Config').get('ally', {})
return new AllyManager(this.application, config)
})
Expand All @@ -38,7 +38,7 @@ export default class AllyProvider {
(HttpContext, Ally) => {
HttpContext.getter(
'ally',
function auth() {
function ally() {
return Ally.getAllyForRequest(this)
},
true
Expand Down
2 changes: 1 addition & 1 deletion src/AllyManager/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,6 @@ export class AllyManager implements AllyManagerContract {
throw new Exception('"Ally.extend" expects callback to be a function')
}

this.extendedDrivers[driverName] = callback
this.extendedDrivers.set(driverName, callback)
}
}
File renamed without changes.
41 changes: 41 additions & 0 deletions test-helpers/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*
* @adonisjs/ally
*
* (c) Harminder Virk <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

import { join } from 'path'
import { Filesystem } from '@poppinss/dev-utils'
import { Application } from '@adonisjs/core/build/standalone'

export const fs = new Filesystem(join(__dirname, '__app'))

export async function setup(setupProviders?: boolean) {
const application = new Application(fs.basePath, 'web', {
providers: ['@adonisjs/core', '../../providers/AllyProvider'],
})

await fs.add(
'config/app.ts',
`
export const profiler = { enabled: true }
export const appKey = 'averylongrandomsecretkey'
export const http = {
trustProxy: () => {},
cookie: {}
}
`
)

await application.setup()

if (setupProviders) {
await application.registerProviders()
await application.bootProviders()
}

return application
}
68 changes: 68 additions & 0 deletions test/ally-manager.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
/*
* @adonisjs/ally
*
* (c) Harminder Virk <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

import test from 'japa'
import { Ally } from '../src/Ally'
import { AllyManager } from '../src/AllyManager'
import { GithubDriver } from '../src/Drivers/Github'

import { setup, fs } from '../test-helpers'

test.group('AllyManager', (group) => {
group.after(async () => {
await fs.cleanup()
})

test('make instance of a mapping', async (assert) => {
const app = await setup(true)
const manager = new AllyManager(app, {
github: {
driver: 'github',
},
})

const HttpContext = app.container.resolveBinding('Adonis/Core/HttpContext')
assert.instanceOf(manager.makeMapping(HttpContext.create('/', {}), 'github'), GithubDriver)
})

test('register provider as singleton', async (assert) => {
const app = await setup(true)
assert.strictEqual(
app.container.resolveBinding('Adonis/Addons/Ally'),
app.container.resolveBinding('Adonis/Addons/Ally')
)
})

test('add ally getter to http context', async (assert) => {
const app = await setup(true)
const HttpContext = app.container.resolveBinding('Adonis/Core/HttpContext')

assert.instanceOf(HttpContext.create('/', {}).ally, Ally)
})

test('extend ally manager to add custom drivers', async (assert) => {
const app = await setup(true)
class MyCustomDriver {}

const manager = new AllyManager(app, {
foo: {
driver: 'foo',
},
})
manager.extend('foo', () => {
return new MyCustomDriver() as any
})

const HttpContext = app.container.resolveBinding('Adonis/Core/HttpContext')
assert.instanceOf(
manager.makeMapping(HttpContext.create('/', {}), 'foo' as any),
MyCustomDriver
)
})
})

0 comments on commit 1a722f8

Please sign in to comment.