Skip to content

Commit

Permalink
fix(lib-classifier): expire classification session IDs
Browse files Browse the repository at this point in the history
- fix the session ID tests so that they fail properly.
- fix `getSessionID` to pass the tests.
- refresh the session TTL whenever an annotation is updated.
  • Loading branch information
eatyourgreens committed Nov 28, 2024
1 parent 1862ade commit d4fabee
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 3 deletions.
1 change: 1 addition & 0 deletions packages/lib-classifier/src/store/utils/session.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ const sessionUtils = {
getSessionID () {
const stored = (storage.getItem('session_id')) ? JSON.parse(storage.getItem('session_id')) : this.generateSessionID()
let { id, ttl } = stored
console.log(stored)

if (ttl < Date.now()) {
id = this.generateSessionID()
Expand Down
10 changes: 7 additions & 3 deletions packages/lib-classifier/src/store/utils/session.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import sinon from 'sinon'
import hash from 'hash.js'
import sessionUtils from './session'

describe('Store utils > sessionUtils', function () {
describe.only('Store utils > sessionUtils', function () {
describe('fiveMinutesFromNow', function () {
it('should return a Date object', function () {
const time = sessionUtils.fiveMinutesFromNow()
Expand Down Expand Up @@ -49,7 +49,9 @@ describe('Store utils > sessionUtils', function () {
})

it('it should retrieve id from session or local storage if it exists', function () {
const stored = { id: 'foobar', ttl: (Date.now() + 50000) }
const ttl = new Date()
ttl.setMinutes(ttl.getMinutes() + 5)
const stored = { id: 'foobar', ttl }
sessionStorage.setItem('session_id', JSON.stringify(stored))
sessionUtils.getSessionID()
expect(generateSessionIDSpy).to.have.not.been.called()
Expand All @@ -72,7 +74,9 @@ describe('Store utils > sessionUtils', function () {

describe('when the stored token has expired', function () {
before(function () {
const stored = { id: 'foobar', ttl: (Date.now() - 1) }
const ttl = new Date()
ttl.setMinutes(ttl.getMinutes() - 10)
const stored = { id: 'foobar', ttl }
sessionStorage.setItem('session_id', JSON.stringify(stored))
})

Expand Down

0 comments on commit d4fabee

Please sign in to comment.