|
|
@ -7,6 +7,15 @@ import path from 'path'
|
|
|
|
import dotenv from 'dotenv'
|
|
|
|
import dotenv from 'dotenv'
|
|
|
|
import Session from './../src/session.ts'
|
|
|
|
import Session from './../src/session.ts'
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const userHashResult = '6e340b9cffb37a989ca544e6bb780a2c78901d3fb33738768511a30617afa01d'
|
|
|
|
|
|
|
|
const userHmacResult = 'qMMo5J3Kq0q3+IPadNretbmitllWakjUTD/6HnTRdSNoifrjkLEf5p6sc0qx2q0bSGTeqXI4OaP5VuUQk0EzuQ=='
|
|
|
|
|
|
|
|
const request = {
|
|
|
|
|
|
|
|
agent: 'Mozilla/2.0 (X11; Windows x86;) Gecko/20100101 Firefox/120.0',
|
|
|
|
|
|
|
|
agentChrome: 'Mozilla/2.0 (X11; Windows x86;) Gecko/20100101 Chrome/121.0',
|
|
|
|
|
|
|
|
language: 'de,en-US;q=0.8,en;q=0.10',
|
|
|
|
|
|
|
|
ip: '1.1.1.1'
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
test('session / create', async () => {
|
|
|
|
test('session / create', async () => {
|
|
|
|
const db = getOrCreateSqlite({ 'name': ':memory:', 'create': true, 'readwrite': true })
|
|
|
|
const db = getOrCreateSqlite({ 'name': ':memory:', 'create': true, 'readwrite': true })
|
|
|
|
await runMigrationSqlite(path.resolve(__dirname, './../src/migration'), db)
|
|
|
|
await runMigrationSqlite(path.resolve(__dirname, './../src/migration'), db)
|
|
|
@ -14,9 +23,9 @@ test('session / create', async () => {
|
|
|
|
dotenv.config('./../.env.test')
|
|
|
|
dotenv.config('./../.env.test')
|
|
|
|
|
|
|
|
|
|
|
|
const session = new Session(db)
|
|
|
|
const session = new Session(db)
|
|
|
|
const result = await session.create(1, 'Mozilla/2.0 (X11; Windows x86;) Gecko/20100101 Firefox/120.0', 'de,en-US;q=0.7,en;q=0.3', '1.1.1.1')
|
|
|
|
const result = await session.create(1, request.agent, request.language, request.ip)
|
|
|
|
|
|
|
|
|
|
|
|
expect('3qC9SK5Z8XC0DXQXs3Fs5CbK5ZS9YOVZziNT8Ulzc2dehXh9qJNMyoVl6jGJJnbbd77/qPdID2wRDFEzqtKshg==').toEqual(result.user)
|
|
|
|
expect(result.user).toEqual(userHashResult)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
|
|
test('session / find', async () => {
|
|
|
|
test('session / find', async () => {
|
|
|
@ -26,11 +35,25 @@ test('session / find', async () => {
|
|
|
|
dotenv.config('./../.env.test')
|
|
|
|
dotenv.config('./../.env.test')
|
|
|
|
|
|
|
|
|
|
|
|
const session = new Session(db)
|
|
|
|
const session = new Session(db)
|
|
|
|
const result = await session.create(1, 'Mozilla/2.0 (X11; Windows x86;) Gecko/20100101 Firefox/120.0', 'de,en-US;q=0.7,en;q=0.3', '1.1.1.1')
|
|
|
|
const result = await session.create(1, request.agent, request.language, request.ip)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const userSession = await session.get(result.user, result.token, request.agent, request.language, request.ip)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
expect(userHmacResult).toEqual(userSession.user)
|
|
|
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
test('session / find / change browser', async () => {
|
|
|
|
|
|
|
|
const db = getOrCreateSqlite({ 'name': ':memory:', 'create': true, 'readwrite': true })
|
|
|
|
|
|
|
|
await runMigrationSqlite(path.resolve(__dirname, './../src/migration'), db)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
dotenv.config('./../.env.test')
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const session = new Session(db)
|
|
|
|
|
|
|
|
const result = await session.create(1, request.agent, request.language, request.ip)
|
|
|
|
|
|
|
|
|
|
|
|
const user = await session.get(1, 'Mozilla/2.0 (X11; Windows x86;) Gecko/20100101 Firefox/120.0', 'de,en-US;q=0.7,en;q=0.3', '1.1.1.1', result.token)
|
|
|
|
const userSession = await session.get(result.user, result.token, request.agentChrome, request.language, request.ip)
|
|
|
|
|
|
|
|
|
|
|
|
expect('3qC9SK5Z8XC0DXQXs3Fs5CbK5ZS9YOVZziNT8Ulzc2dehXh9qJNMyoVl6jGJJnbbd77/qPdID2wRDFEzqtKshg==').toEqual(user.user)
|
|
|
|
expect(null).toEqual(userSession)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
|
|
test('session / expired', async () => {
|
|
|
|
test('session / expired', async () => {
|
|
|
@ -40,14 +63,14 @@ test('session / expired', async () => {
|
|
|
|
dotenv.config('./../.env.test')
|
|
|
|
dotenv.config('./../.env.test')
|
|
|
|
|
|
|
|
|
|
|
|
const session = new Session(db)
|
|
|
|
const session = new Session(db)
|
|
|
|
const result = await session.create(1, 'Mozilla/2.0 (X11; Windows x86;) Gecko/20100101 Firefox/120.0', 'de,en-US;q=0.7,en;q=0.3', '1.1.1.1')
|
|
|
|
const result = await session.create(1, request.agent, request.language, request.ip)
|
|
|
|
|
|
|
|
|
|
|
|
// change manually session, and set expired_at to
|
|
|
|
// change manually session, and set expired_at to
|
|
|
|
const sessionStore = new SessionStore(db)
|
|
|
|
const sessionStore = new SessionStore(db)
|
|
|
|
await sessionStore.update(1, { 'expired_at': dayjs().toISOString() })
|
|
|
|
await sessionStore.update(1, { 'expired_at': dayjs().subtract(60, 'minute').toISOString() })
|
|
|
|
|
|
|
|
|
|
|
|
// getting user
|
|
|
|
// getting user
|
|
|
|
const user = await session.get(1, 'Mozilla/2.0 (X11; Windows x86;) Gecko/20100101 Firefox/120.0', 'de,en-US;q=0.7,en;q=0.3', '1.1.1.1', result.token)
|
|
|
|
const user = await session.get(result.user, result.token, request.agent, request.language, request.ip)
|
|
|
|
|
|
|
|
|
|
|
|
expect(null).toEqual(user) // user not found, because expired user is deleted
|
|
|
|
expect(null).toEqual(user) // user not found, because expired user is deleted
|
|
|
|
})
|
|
|
|
})
|
|
|
@ -60,11 +83,11 @@ test('session / destroy all', async () => {
|
|
|
|
|
|
|
|
|
|
|
|
const session = new Session(db)
|
|
|
|
const session = new Session(db)
|
|
|
|
|
|
|
|
|
|
|
|
await session.create(1, 'Mozilla/2.0 (X11; Windows x86;) Gecko/20100101 Firefox/120.0', 'de,en-US;q=0.7,en;q=0.3', '1.1.1.1')
|
|
|
|
await session.create(1, request.agent, request.language, request.ip)
|
|
|
|
await session.create(1, 'Mozilla/2.0 (X11; Windows x86;) Gecko/20100101 Firefox/120.0', 'de,en-US;q=0.7,en;q=0.3', '1.1.1.2')
|
|
|
|
await session.create(1, request.agent, request.language, request.ip)
|
|
|
|
await session.create(1, 'Mozilla/2.0 (X11; Windows x86;) Gecko/20100101 Firefox/120.0', 'de,en-US;q=0.7,en;q=0.3', '1.1.1.3')
|
|
|
|
await session.create(1, request.agent, request.language, request.ip)
|
|
|
|
await session.create(1, 'Mozilla/2.0 (X11; Windows x86;) Gecko/20100101 Firefox/120.0', 'de,en-US;q=0.7,en;q=0.3', '1.1.1.4')
|
|
|
|
await session.create(1, request.agent, request.language, request.ip)
|
|
|
|
await session.create(1, 'Mozilla/2.0 (X11; Windows x86;) Gecko/20100101 Firefox/120.0', 'de,en-US;q=0.7,en;q=0.3', '1.1.1.5')
|
|
|
|
await session.create(1, request.agent, request.language, request.ip)
|
|
|
|
|
|
|
|
|
|
|
|
await session.destroyAll(1)
|
|
|
|
await session.destroyAll(1)
|
|
|
|
const result = db.query("SELECT * FROM sessions").all()
|
|
|
|
const result = db.query("SELECT * FROM sessions").all()
|
|
|
|