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.
session/test/session.test.ts

97 lines
3.7 KiB

3 months ago
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'
3 months ago
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'
}
3 months ago
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)
3 months ago
const result = await session.create(1, request.agent, request.language, request.ip)
3 months ago
3 months ago
expect(result.user).toEqual(userHashResult)
3 months ago
})
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)
3 months ago
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)
3 months ago
3 months ago
const userSession = await session.get(result.user, result.token, request.agentChrome, request.language, request.ip)
3 months ago
3 months ago
expect(null).toEqual(userSession)
3 months ago
})
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)
3 months ago
const result = await session.create(1, request.agent, request.language, request.ip)
3 months ago
// change manually session, and set expired_at to
const sessionStore = new SessionStore(db)
3 months ago
await sessionStore.update(1, { 'expired_at': dayjs().subtract(60, 'minute').toISOString() })
3 months ago
// getting user
3 months ago
const user = await session.get(result.user, result.token, request.agent, request.language, request.ip)
3 months ago
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)
3 months ago
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)
3 months ago
await session.destroyAll(1)
const result = db.query("SELECT * FROM sessions").all()
expect([]).toEqual(result)
})