query for validation of invite code

This commit is contained in:
Moriz Wahl 2021-01-12 21:25:25 +01:00
parent 0c141b631f
commit f20e17dcc1
6 changed files with 67 additions and 7 deletions

View File

@ -235,9 +235,7 @@ Factory.define('inviteCode')
neode.create('InviteCode', buildObject), neode.create('InviteCode', buildObject),
options.generatedBy, options.generatedBy,
]) ])
await Promise.all([ await Promise.all([inviteCode.relateTo(generatedBy, 'generated')])
inviteCode.relateTo(generatedBy, 'generated'),
])
return inviteCode return inviteCode
}) })

View File

@ -107,6 +107,7 @@ export default shield(
Donations: isAuthenticated, Donations: isAuthenticated,
userData: isAuthenticated, userData: isAuthenticated,
MyInviteCodes: isAuthenticated, MyInviteCodes: isAuthenticated,
isValidInviteCode: allow,
}, },
Mutation: { Mutation: {
'*': deny, '*': deny,

View File

@ -34,6 +34,31 @@ export default {
session.close() session.close()
} }
}, },
isValidInviteCode: async (_parent, args, context, _resolveInfo) => {
const { code } = args
if (!code) return false
const session = context.driver.session()
const readTxResultPromise = session.readTransaction(async (txc) => {
const result = await txc.run(
`MATCH (ic:InviteCode { code: $code })
RETURN
CASE
WHEN ic.expiresAt IS NULL THEN true
WHEN datetime(ic.expiresAt) >= datetime() THEN true
ELSE false END AS result`,
{
code,
},
)
return result.records.map((record) => record.get('result'))
})
try {
const txResult = await readTxResultPromise
return !!txResult[0]
} finally {
session.close()
}
},
}, },
Mutation: { Mutation: {
GenerateInviteCode: async (_parent, args, context, _resolveInfo) => { GenerateInviteCode: async (_parent, args, context, _resolveInfo) => {

View File

@ -30,6 +30,12 @@ const myInviteCodesQuery = gql`
} }
` `
const isValidInviteCodeQuery = gql`
query($code: ID) {
isValidInviteCode(code: $code)
}
`
beforeAll(async () => { beforeAll(async () => {
await cleanDatabase() await cleanDatabase()
const { server } = createServer({ const { server } = createServer({
@ -141,7 +147,36 @@ describe('inviteCodes', () => {
expect(inviteCodes).toHaveLength(2) expect(inviteCodes).toHaveLength(2)
}) })
// const expiringInviteCode = inviteCodes.filter((ic) => ic.expiresAt !== null) it('does not returns the created invite codes of other users when queried', async () => {
// const unExpiringInviteCode = inviteCodes.filter((ic) => ic.expiresAt === null) await Factory.build('inviteCode')
const response = await query({ query: myInviteCodesQuery })
inviteCodes = response.data.MyInviteCodes
expect(inviteCodes).toHaveLength(2)
})
it('validates an invite code without expiresAt', async () => {
const unExpiringInviteCode = inviteCodes.filter((ic) => ic.expiresAt === null)[0].code
expect(
query({ query: isValidInviteCodeQuery, variables: { code: unExpiringInviteCode } }),
).resolves.toBeTruthy()
})
it('validates an invite code with expiresAt in the future', async () => {
const expiringInviteCode = inviteCodes.filter((ic) => ic.expiresAt !== null)[0].code
expect(
query({ query: isValidInviteCodeQuery, variables: { code: expiringInviteCode } }),
).resolves.toBeTruthy()
})
it.skip('does not validate an invite code which expired in the past', async () => {
const lastWeek = new Date()
lastWeek.setDate(lastWeek.getDate() - 7)
const code = await Factory.build('inviteCode', {
expiresAt: lastWeek.toISOString(),
})
expect(
query({ query: isValidInviteCodeQuery, variables: { code: code.code } }),
).resolves.toBeFalsy()
})
}) })
}) })

View File

@ -13,4 +13,5 @@ type Mutation {
type Query { type Query {
MyInviteCodes: [InviteCode] MyInviteCodes: [InviteCode]
isValidInviteCode(code: ID): Boolean
} }