Skip to content

Commit

Permalink
feat: adding unit tests fo all the services. fixing services various …
Browse files Browse the repository at this point in the history
…errors
  • Loading branch information
daniel78uk committed Nov 29, 2023
1 parent 33d3c31 commit 8a71015
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 40 deletions.
50 changes: 19 additions & 31 deletions server/services/redis.service.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ const get = async (request, key) => {

try {
const redisValue = await client.get(`${getSessionKey(request)}.${key}`)

let parsedValue = redisValue

if (isBooleanString(redisValue)) {
Expand All @@ -30,7 +29,7 @@ const get = async (request, key) => {
return parsedValue
} catch (err) {
console.error(err)
return false
throw new Error('Failed to retrieve value from Redis')
}
}

Expand All @@ -39,7 +38,9 @@ const set = async (request, key, value) => {
const keyWithSessionId = `${getSessionKey(request)}.${key}`

try {
return client.setex(keyWithSessionId, REDIS_TTL_IN_SECONDS, value)
await client.setex(keyWithSessionId, REDIS_TTL_IN_SECONDS, value)

return true
} catch (err) {
console.error(err)
return false
Expand All @@ -52,6 +53,8 @@ const deleteItem = async (request, key) => {

try {
await client.del(keyWithSessionId)

return true
} catch (err) {
console.error(err)
return false
Expand All @@ -66,55 +69,40 @@ const deleteSessionData = async (request) => {
for (const key of keys) {
await client.del(key)
}
return true
} catch (err) {
console.error(err)
return false
throw new Error('Failed to delete session data from Redis')
}
}
/**
* Checks a string value to see if it looks like a Json object i.e. begins and ends with curly brackets
* @param {*} value The string value to be chekced
* @returns True if the string looks like a Json object, otherwise false
*/

const isJsonString = (value) =>
value &&
value.length &&
((value.startsWith('{') && value.endsWith('}')) ||
(value.startsWith('[') && value.endsWith(']')))

/**
* Checks a string value to see if it contains a bolean i.e. 'true' or 'false'
* @param {*} value The string value to be chekced
* @returns True if the string contains a bolean, otherwise false
*/

const isBooleanString = (value) =>
value &&
value.length &&
(value.toLowerCase() === 'true' || value.toLowerCase() === 'false')

/**
* Scans the Redis cache for all keys matching the session key held in the session.
* The Redis scan function returns a cursor and a set of results. The scan function
* needs to be called repeatedly until the cursor is back to 0.
* @param {*} request The request containing the Redis cache
* @returns An array of Redis keys that are prefixed with the session key
*/
const getMatchingRedisKeys = async (request) => {
const client = getRedisClient(request)
const sessionKey = getSessionKey(request)

const keys = []

let scanResult = await client.scan('0', 'MATCH', `${sessionKey}.*`)
let cursor = scanResult[0]

keys.push(...scanResult[1]) //?

while (cursor !== '0') {
scanResult = await client.scan(cursor, 'MATCH', `${sessionKey}.*`)
cursor = scanResult[0]
keys.push(...scanResult[1])
try {
let cursor = '0'
do {
const scanResult = await client.scan(cursor, 'MATCH', `${sessionKey}.*`)
cursor = scanResult[0]
keys.push(...scanResult[1])
} while (cursor !== '0')
} catch (err) {
console.error(err)
throw new Error('Failed to get matching Redis keys')
}

return keys
Expand Down
19 changes: 10 additions & 9 deletions test/unit/services/redis.service.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ const mockClient = {
// Mock Redis get method
return tempKeys[key]
},
async setex(key, ttl, value) {
async setex(key, _ttl, value) {
// Mock Redis setex method
tempKeys[key] = value
},
Expand Down Expand Up @@ -50,6 +50,7 @@ const mockRequest = {
// Mock data
const mockValue = 'mockValue'
const mockTTLValue = 100
const mockTestKey = 'session_key.testKey'

describe('RedisService', () => {
afterEach(() => {
Expand All @@ -58,7 +59,7 @@ describe('RedisService', () => {

describe('get()', () => {
it('should get value from Redis', async () => {
await mockClient.setex('session_key.testKey', mockTTLValue, mockValue)
await mockClient.setex(mockTestKey, mockTTLValue, mockValue)

const result = await get(mockRequest, 'testKey')

Expand All @@ -70,33 +71,33 @@ describe('RedisService', () => {
it('should set value in Redis', async () => {
await set(mockRequest, 'testKey', mockValue)

const result = await mockClient.get('session_key.testKey')
const result = await mockClient.get(mockTestKey)

expect(result).toEqual(mockValue)
})
})

describe('delete()', () => {
it('should delete value from Redis', async () => {
await mockClient.setex('session_key.testKey', mockTTLValue, mockValue)
await mockClient.setex(mockTestKey, mockTTLValue, mockValue)

await deleteItem(mockRequest, 'testKey')

const result = await mockClient.get('session_key.testKey')
const result = await mockClient.get(mockTestKey)

expect(result).toBe(undefined)
})
})

describe('deleteSessionData()', () => {
it('should delete all session data from Redis', async () => {
await mockClient.setex('session_key.testKey1', mockTTLValue, mockValue)
await mockClient.setex('session_key.testKey2', mockTTLValue, mockValue)
await mockClient.setex(`${mockTestKey}1`, mockTTLValue, mockValue)
await mockClient.setex(`${mockTestKey}2`, mockTTLValue, mockValue)

await deleteSessionData(mockRequest)

const result1 = await mockClient.get('session_key.testKey1')
const result2 = await mockClient.get('session_key.testKey2')
const result1 = await mockClient.get(`${mockTestKey}1`)
const result2 = await mockClient.get(`${mockTestKey}2`)

expect(result1).toBe(undefined)
expect(result2).toBe(undefined)
Expand Down

0 comments on commit 8a71015

Please sign in to comment.