Skip to content

Commit

Permalink
fix: auth.attempt proxy attempt method on the default guard
Browse files Browse the repository at this point in the history
Added tests to ensure all proxy properties are pointing
to the correct target property
  • Loading branch information
thetutlage committed Apr 30, 2020
1 parent 2e586db commit 8b37577
Show file tree
Hide file tree
Showing 3 changed files with 134 additions and 7 deletions.
2 changes: 1 addition & 1 deletion src/Auth/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ export class Auth implements AuthContract {
* Attempt to verify user credentials and perform login
*/
public async attempt (uid: string, password: string, ...args: any[]) {
return this.use().verifyCredentials(uid, password, ...args)
return this.use().attempt(uid, password, ...args)
}

/**
Expand Down
24 changes: 24 additions & 0 deletions test-helpers/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -302,3 +302,27 @@ export function unsignCookie (cookie: any, name: string) {

return encryption.verifier.unsign(cookieValue, name)
}

/**
* Mocks action on a object
*/
export function mockAction (collection: any, name: string, verifier: any) {
collection[name] = function (...args: any[]) {
verifier(...args)
delete collection[name]
}
}

/**
* Mocks property on a object
*/
export function mockProperty (collection: any, name: string, value: any) {
Object.defineProperty(collection, name, {
get () {
delete collection[name]
return value
},
enumerable: true,
configurable: true,
})
}
115 changes: 109 additions & 6 deletions test/auth.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
*/

import test from 'japa'
import 'reflect-metadata'
import { DatabaseContract } from '@ioc:Adonis/Lucid/Database'

import { AuthManager } from '../src/AuthManager'
Expand All @@ -21,8 +22,10 @@ import {
getDb,
getCtx,
cleanup,
container,
getModel,
container,
mockAction,
mockProperty,
getUserModel,
getLucidProviderConfig,
getDatabaseProviderConfig,
Expand Down Expand Up @@ -73,12 +76,16 @@ test.group('Auth', (group) => {
assert.instanceOf(mapping.provider, LucidProvider)
})

test('proxy method to the default driver', (assert) => {
test('proxy all methods to the default driver', async (assert) => {
const User = getUserModel(BaseModel)

const manager = new AuthManager(container, {
guard: 'session',
guard: 'custom',
list: {
custom: {
driver: 'custom',
provider: getLucidProviderConfig({ model: User }),
},
session: {
driver: 'session',
provider: getLucidProviderConfig({ model: User }),
Expand All @@ -88,13 +95,109 @@ test.group('Auth', (group) => {
provider: getDatabaseProviderConfig(),
},
},
})
} as any)

class CustomGuard {
}
const guardInstance = new CustomGuard()

const ctx = getCtx()
const auth = manager.getAuthForRequest(ctx)

assert.equal(auth.name, 'session')
assert.instanceOf(auth.provider, LucidProvider)
manager.extend('guard', 'custom', () => {
return guardInstance as any
})

/**
* Test attempt
*/
mockAction(guardInstance, 'attempt', function (uid: string, secret: string, rememberMe: boolean) {
assert.equal(uid, 'foo')
assert.equal(secret, 'secret')
assert.equal(rememberMe, true)
})
await auth.attempt('foo', 'secret', true)

/**
* Test verify credentails
*/
mockAction(guardInstance, 'verifyCredentials', function (uid: string, secret: string) {
assert.equal(uid, 'foo')
assert.equal(secret, 'secret')
})
await auth.verifyCredentials('foo', 'secret')

/**
* Test login
*/
mockAction(guardInstance, 'login', function (user: any, rememberMe: boolean) {
assert.deepEqual(user, { id: 1 })
assert.equal(rememberMe, true)
})
await auth.login({ id: 1 }, true)

/**
* Test loginViaId
*/
mockAction(guardInstance, 'loginViaId', function (id: number, rememberMe: boolean) {
assert.deepEqual(id, 1)
assert.equal(rememberMe, true)
})
await auth.loginViaId(1, true)

/**
* Test logout
*/
mockAction(guardInstance, 'logout', function (renewToken: boolean) {
assert.equal(renewToken, true)
})
await auth.logout(true)

/**
* Test authenticate
*/
mockAction(guardInstance, 'authenticate', function () {
assert.isTrue(true)
})
await auth.authenticate()

/**
* Test check
*/
mockAction(guardInstance, 'check', function () {
assert.isTrue(true)
})
await auth.check()

/**
* Test isGuest
*/
mockProperty(guardInstance, 'isGuest', false)
assert.isFalse(auth.isGuest)

/**
* Test user
*/
mockProperty(guardInstance, 'user', { id: 1 })
assert.deepEqual(auth.user, { id: 1 })

/**
* Test isLoggedIn
*/
mockProperty(guardInstance, 'isLoggedIn', true)
assert.isTrue(auth.isLoggedIn)

/**
* Test isLoggedOut
*/
mockProperty(guardInstance, 'isLoggedOut', true)
assert.isTrue(auth.isLoggedOut)

/**
* Test authenticationAttempted
*/
mockProperty(guardInstance, 'authenticationAttempted', true)
assert.isTrue(auth.authenticationAttempted)
})

test('update default guard', (assert) => {
Expand Down

0 comments on commit 8b37577

Please sign in to comment.