mirror of
https://github.com/Ocelot-Social-Community/Ocelot-Social.git
synced 2025-12-13 07:46:06 +00:00
query for validation of invite code
This commit is contained in:
parent
0c141b631f
commit
f20e17dcc1
@ -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
|
||||
})
|
||||
|
||||
|
||||
@ -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?'
|
||||
|
||||
@ -107,6 +107,7 @@ export default shield(
|
||||
Donations: isAuthenticated,
|
||||
userData: isAuthenticated,
|
||||
MyInviteCodes: isAuthenticated,
|
||||
isValidInviteCode: allow,
|
||||
},
|
||||
Mutation: {
|
||||
'*': deny,
|
||||
|
||||
@ -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) => {
|
||||
|
||||
@ -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()
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
@ -12,5 +12,6 @@ type Mutation {
|
||||
}
|
||||
|
||||
type Query {
|
||||
MyInviteCodes: [InviteCode]
|
||||
MyInviteCodes: [InviteCode]
|
||||
isValidInviteCode(code: ID): Boolean
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user