Skip to content

Instantly share code, notes, and snippets.

@zanechua
Created December 16, 2017 01:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zanechua/e238e2d8fb697b975099803ede7b9712 to your computer and use it in GitHub Desktop.
Save zanechua/e238e2d8fb697b975099803ede7b9712 to your computer and use it in GitHub Desktop.
AdonisJS Authentication Test
'use strict'
const { Config } = require('@adonisjs/sink')
const Auth = use('Adonis/Src/Auth')
const User = use('App/Models/User')
const { before, beforeEach, after, afterEach, test } = use('Test/Suite')('Authentication Test')
test('Ensure Authentication Works', async ({ assert }) => {
let httpSession = null
const username = 'test'
const email = 'foo@bar.com'
const password = 'secret'
const config = new Config()
config.set('auth', {
authenticator: 'session',
session: {
serializer: 'lucid',
scheme: 'session',
model: User,
uid: 'email',
password: 'password'
}
})
const auth = new Auth({
session: {
put (key, value) {
httpSession = { key, value }
}
},
response: { cookie: function () {} },
request: { cookie: function () {} }
}, config)
try {
await User.create({ username: username, email: email, password: password, status: '0' })
const user = await auth.attempt(email, password)
assert.instanceOf(user, User)
assert.deepEqual(httpSession, { key: 'adonis-auth', value: 1 })
}
catch (message)
{
console.log(message);
//assert.equal(message, 'ERROR')
}
})
before(async () => {
// executed before all the tests for a given suite
})
beforeEach(async () => {
// executed before each test inside a given suite
})
after(async () => {
// executed after all the tests for a given suite
})
afterEach(async () => {
// executed after each test inside a given suite
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment