Skip to content

Commit

Permalink
Wait for all NEW_USER events until emitting FINISHED_ISSUING_CERTIFIC…
Browse files Browse the repository at this point in the history
…ATES_FOR_ID (#2049)

* feat: wait for all NEW_USER events until emitting FINISHED_ISSUING_CERTIFICATES_FOR_ID

* fix: change it name
  • Loading branch information
Kacper-RF authored Nov 9, 2023
1 parent 97134c5 commit 0ad9275
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { Time } from 'pkijs'
import { issueCertificate, extractPendingCsrs } from './registration.functions'
import { jest } from '@jest/globals'
import { createTmpDir } from '../common/utils'
import { RegistrationEvents } from './registration.types'

describe('RegistrationService', () => {
let module: TestingModule
Expand Down Expand Up @@ -146,4 +147,37 @@ describe('RegistrationService', () => {
expect(pendingCsrs.length).toEqual(1)
expect(pendingCsrs[0]).toBe(userCsr.userCsr)
})

it('wait for all NEW_USER events until emitting FINISHED_ISSUING_CERTIFICATES_FOR_ID', async () => {
registrationService.permsData = permsData

const eventSpy = jest.spyOn(registrationService, 'emit')

const userCsr = await createUserCsr({
nickname: 'alice',
commonName: 'nqnw4kc4c77fb47lk52m5l57h4tcxceo7ymxekfn7yh5m66t4jv2olad.onion',
peerId: 'Qmf3ySkYqLET9xtAtDzvAr5Pp3egK1H3C5iJAZm1SpLEp6',
dmPublicKey: 'testdmPublicKey',
signAlg: configCrypto.signAlg,
hashAlg: configCrypto.hashAlg,
})
const userCsr2 = await createUserCsr({
nickname: 'karol',
commonName: 'nnnnnnc4c77fb47lk52m5l57h4tcxceo7ymxekfn7yh5m66t4jv2olad.onion',
peerId: 'QmffffffqLET9xtAtDzvAr5Pp3egK1H3C5iJAZm1SpLEp6',
dmPublicKey: 'testdmPublicKey',
signAlg: configCrypto.signAlg,
hashAlg: configCrypto.hashAlg,
})

const csrs: string[] = [userCsr.userCsr, userCsr2.userCsr]
// @ts-ignore - fn 'issueCertificates' is private
await registrationService.issueCertificates({ certificates: [], csrs, id: 1 })

expect(eventSpy).toHaveBeenLastCalledWith(RegistrationEvents.FINISHED_ISSUING_CERTIFICATES_FOR_ID, {
id: 1,
})

expect(eventSpy).toHaveBeenCalledTimes(3)
})
})
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,12 @@ export class RegistrationService extends EventEmitter implements OnModuleInit {
return
}
const pendingCsrs = await extractPendingCsrs(payload)
pendingCsrs.forEach(async csr => {
await this.registerUserCertificate(csr)
})

await Promise.all(
pendingCsrs.map(async csr => {
await this.registerUserCertificate(csr)
})
)

if (payload.id) this.emit(RegistrationEvents.FINISHED_ISSUING_CERTIFICATES_FOR_ID, { id: payload.id })
}
Expand Down

0 comments on commit 0ad9275

Please sign in to comment.