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),
options.generatedBy,
])
await Promise.all([
inviteCode.relateTo(generatedBy, 'generated'),
])
await Promise.all([inviteCode.relateTo(generatedBy, 'generated')])
return inviteCode
})

View File

@ -550,7 +550,7 @@ const languages = ['de', 'en', 'es', 'fr', 'it', 'pt', 'pl']
generatedBy: jennyRostock,
},
)
authenticatedUser = await louie.toJson()
const mention1 =
'Hey <a class="mention" data-mention-id="u3" href="/profile/u3">@jenny-rostock</a>, what\'s up?'

View File

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

View File

@ -34,6 +34,31 @@ export default {
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: {
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 () => {
await cleanDatabase()
const { server } = createServer({
@ -141,7 +147,36 @@ describe('inviteCodes', () => {
expect(inviteCodes).toHaveLength(2)
})
// const expiringInviteCode = inviteCodes.filter((ic) => ic.expiresAt !== null)
// const unExpiringInviteCode = inviteCodes.filter((ic) => ic.expiresAt === null)
it('does not returns the created invite codes of other users when queried', async () => {
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

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