-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
314 additions
and
108 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,154 @@ | ||
import { RedisClientType, createClient } from 'redis' | ||
|
||
import { RedisSessions } from '../../src/stores/redis' | ||
import { SessionUtils } from '../../src/utils' | ||
import { filter } from '../utils' | ||
|
||
// RedisSessions (best for production environment). | ||
const redis: RedisClientType = createClient({ | ||
socket: { | ||
host: 'localhost', | ||
port: 6379, | ||
}, | ||
password: 'redis', | ||
}) | ||
|
||
describe('reids', () => { | ||
test('correctly finds user from a given session id', async () => { | ||
const sessions = new RedisSessions<string, {}>({ redis }) | ||
|
||
const validSessionId = await sessions.createSession({ | ||
userId: 'user-id', | ||
label: 'test', | ||
meta: {}, | ||
}) | ||
|
||
await expect(sessions.getUserIdFromSession(validSessionId)).resolves.not.toBeNull() | ||
|
||
const invalidSessionId = SessionUtils.toSessionId('invalid-session-id') | ||
|
||
await expect(sessions.getUserIdFromSession(invalidSessionId)).resolves.toBeNull() | ||
}) | ||
|
||
test('correctly parses userId from a given session id', async () => { | ||
const sessions = new RedisSessions<string, {}>({ redis }) | ||
|
||
const userId = 'user-id' | ||
const sessionId = await sessions.createSession({ | ||
userId, | ||
label: 'test', | ||
meta: {}, | ||
}) | ||
|
||
await expect(sessions.getUserIdFromSession(sessionId)).resolves.toEqual(userId) | ||
}) | ||
|
||
test('correctly parses meta from a given session id', async () => { | ||
// NOTE: We want to test all serializable types here. | ||
|
||
const sessions = new RedisSessions< | ||
string, | ||
{ | ||
string: string | ||
number: number | ||
boolean: boolean | ||
array: string[] | ||
object: { [key: string]: string } | ||
} | ||
>({ redis }) | ||
|
||
const meta = { | ||
string: 'string', | ||
number: 1, | ||
boolean: true, | ||
array: ['string', 'string'], | ||
object: { | ||
string: 'string', | ||
}, | ||
} | ||
|
||
const sessionId = await sessions.createSession({ | ||
userId: 'user-id', | ||
label: 'test', | ||
meta, | ||
}) | ||
|
||
await expect(sessions.getSessionMeta(sessionId)).resolves.toEqual(meta) | ||
}) | ||
|
||
test('correctly destroys a session', async () => { | ||
const sessions = new RedisSessions<string, {}>({ redis }) | ||
|
||
const sessionId = await sessions.createSession({ | ||
userId: 'user-id', | ||
label: 'test', | ||
meta: {}, | ||
}) | ||
|
||
await sessions.destroySession(sessionId) | ||
|
||
await expect(sessions.getUserIdFromSession(sessionId)).resolves.toBeNull() | ||
}) | ||
|
||
test('correctly lists all sessions', async () => { | ||
const sessions = new RedisSessions<string, {}>({ redis }) | ||
|
||
await sessions.createSession({ userId: 'user-id', label: '#1', meta: {} }) | ||
await sessions.createSession({ userId: 'user-id', label: '#2', meta: {} }) | ||
await sessions.createSession({ userId: 'user-id', label: '#3', meta: {} }) | ||
|
||
await expect( | ||
// We filter out random values and dates. | ||
sessions.listSessions().then((r) => r.map((o) => filter(o, ['label', 'userId']))), | ||
).resolves.toMatchInlineSnapshot(` | ||
[ | ||
{ | ||
"label": "#1", | ||
"userId": "user-id", | ||
}, | ||
{ | ||
"label": "#2", | ||
"userId": "user-id", | ||
}, | ||
{ | ||
"label": "#3", | ||
"userId": "user-id", | ||
}, | ||
] | ||
`) | ||
}) | ||
|
||
test('correclty lists sessions for a given user', async () => { | ||
const sessions = new RedisSessions<string, {}>({ redis }) | ||
|
||
const userId = 'user-id' | ||
|
||
await sessions.createSession({ userId, label: '#1', meta: {} }) | ||
await sessions.createSession({ userId, label: '#2', meta: {} }) | ||
await sessions.createSession({ userId, label: '#3', meta: {} }) | ||
|
||
const otherUserId = 'other-user-id' | ||
await sessions.createSession({ userId: otherUserId, label: '#1', meta: {} }) | ||
await sessions.createSession({ userId: otherUserId, label: '#2', meta: {} }) | ||
|
||
await expect( | ||
// We filter out random values and dates. | ||
sessions.getSessionsForUser(userId).then((r) => r.map((o) => filter(o, ['label', 'userId']))), | ||
).resolves.toMatchInlineSnapshot(` | ||
[ | ||
{ | ||
"label": "#1", | ||
"userId": "user-id", | ||
}, | ||
{ | ||
"label": "#2", | ||
"userId": "user-id", | ||
}, | ||
{ | ||
"label": "#3", | ||
"userId": "user-id", | ||
}, | ||
] | ||
`) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.