inviteCodes: explaination added, test for lower case

This commit is contained in:
Moriz Wahl 2021-01-18 17:23:55 +01:00
parent c6ff0723ee
commit a98170487b
3 changed files with 13 additions and 1 deletions

View File

@ -1,5 +1,8 @@
export default function generateInviteCode() {
// 6 random numbers in [ 0, 35 ] are 36 possible numbers (10 [0-9] + 26 [A-Z])
return Array.from({ length: 6 }, (n = Math.floor(Math.random() * 36)) => {
// n > 9: it is a letter (ASCII 65 is A) -> 10 + 55 = 65
// else: it is a number (ASCII 48 is 0) -> 0 + 48 = 48
return String.fromCharCode(n > 9 ? n + 55 : n + 48)
}).join('')
}

View File

@ -40,7 +40,7 @@ export default {
const session = context.driver.session()
const readTxResultPromise = session.readTransaction(async (txc) => {
const result = await txc.run(
`MATCH (ic:InviteCode { code: $code })
`MATCH (ic:InviteCode { code: toUpper($code) })
RETURN
CASE
WHEN ic.expiresAt IS NULL THEN true

View File

@ -163,6 +163,15 @@ describe('inviteCodes', () => {
expect(result.data.isValidInviteCode).toBeTruthy()
})
it('validates an invite code in lower case', async () => {
const unExpiringInviteCode = inviteCodes.filter((ic) => ic.expiresAt === null)[0].code
const result = await query({
query: isValidInviteCodeQuery,
variables: { code: unExpiringInviteCode.toLowerCase() },
})
expect(result.data.isValidInviteCode).toBeTruthy()
})
it('validates an invite code with expiresAt in the future', async () => {
const expiringInviteCode = inviteCodes.filter((ic) => ic.expiresAt !== null)[0].code
const result = await query({