You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
97 lines
3.7 KiB
97 lines
3.7 KiB
import { expect, test } from 'bun:test'
|
|
import { runMigrationSqlite, getOrCreateSqlite } from '@nano/sqlite'
|
|
import SessionStore from './../src/sessionStore.ts'
|
|
import dayjs from 'dayjs'
|
|
|
|
import path from 'path'
|
|
import dotenv from 'dotenv'
|
|
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 () => {
|
|
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)
|
|
|
|
expect(result.user).toEqual(userHashResult)
|
|
})
|
|
|
|
test('session / find', 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 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 userSession = await session.get(result.user, result.token, request.agentChrome, request.language, request.ip)
|
|
|
|
expect(null).toEqual(userSession)
|
|
})
|
|
|
|
test('session / expired', 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)
|
|
|
|
// change manually session, and set expired_at to
|
|
const sessionStore = new SessionStore(db)
|
|
await sessionStore.update(1, { 'expired_at': dayjs().subtract(60, 'minute').toISOString() })
|
|
|
|
// getting user
|
|
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
|
|
})
|
|
|
|
test('session / destroy all', 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)
|
|
|
|
await session.create(1, request.agent, request.language, request.ip)
|
|
await session.create(1, request.agent, request.language, request.ip)
|
|
await session.create(1, request.agent, request.language, request.ip)
|
|
await session.create(1, request.agent, request.language, request.ip)
|
|
await session.create(1, request.agent, request.language, request.ip)
|
|
|
|
await session.destroyAll(1)
|
|
const result = db.query("SELECT * FROM sessions").all()
|
|
|
|
expect([]).toEqual(result)
|
|
})
|