From 61f57875f9f3b7f1fa04c691b805b2598bad8004 Mon Sep 17 00:00:00 2001 From: Michael Peters Date: Wed, 5 Oct 2022 22:33:27 -0700 Subject: [PATCH] primary with null, trusted rejects test --- src/client/tests/webapp/auto-verifier.test.ts | 61 ++++++++++++++++++- 1 file changed, 59 insertions(+), 2 deletions(-) diff --git a/src/client/tests/webapp/auto-verifier.test.ts b/src/client/tests/webapp/auto-verifier.test.ts index 8aa13ce..cede5dc 100644 --- a/src/client/tests/webapp/auto-verifier.test.ts +++ b/src/client/tests/webapp/auto-verifier.test.ts @@ -558,7 +558,7 @@ describe('fetchAndVerifyIfNeeded tests', () => { }); test('primary rejects - cache fail', async () => { - // expecting the promise to reject, the server to succeed, but the verify function not to be called + // expecting the promise to reject, the server to succeed, but the verify function to never be called const { nextPrimary, nextTrusted, primaryFunc, trustedFunc, ensureTrustedFuncReadyFunc, verifyFunc @@ -579,7 +579,7 @@ describe('fetchAndVerifyIfNeeded tests', () => { expect(trustedFunc).toHaveBeenCalled(); // TODO: Is this the source of the problem? - trustedFunc needs to wait to be called until ensureTrustedFuncReadyFunc is resolved expect(rejection).toBeUndefined(); - jest.spyOn(console, 'warn').mockImplementationOnce(() => {}); // suppress the warning + jest.spyOn(console, 'warn').mockImplementationOnce(() => {}); // suppress the warning primary.reject(new BasicWE(2)); // this will also 'unverify' the AutoVerifier await disjoint(); @@ -596,6 +596,63 @@ describe('fetchAndVerifyIfNeeded tests', () => { expect(verifyFunc).toHaveBeenCalledTimes(0); }); + test('primary with null, trusted rejects - cache miss, failed to ping server', async () => { + // expect the promise to reject, but the verify function to never be called + const { + nextPrimary, nextTrusted, nextEnsureTrustedFuncReady, + primaryFunc, trustedFunc, ensureTrustedFuncReadyFunc, verifyFunc + } = getManualsAndMocks(); + + const primary = nextPrimary(); + const trusted = nextTrusted(); + const ensureTrustedFuncReady = nextEnsureTrustedFuncReady(); + + const av = new AutoVerifier(primaryFunc, trustedFunc, ensureTrustedFuncReadyFunc, verifyFunc); + const resultPromise = av.fetchAndVerifyIfNeeded(); + let rejection: Error | undefined = undefined; + resultPromise.catch(value => { rejection = value; }); + + expect(av.primaryPromise).toBe(primary.promise); + expect(av.trustedPromise).toBe(trusted.promise); + expect(av.trustedStatus).toBe('fetching'); + expect(primaryFunc).toHaveBeenCalled(); + expect(trustedFunc).toHaveBeenCalled(); + + expect(ensureTrustedFuncReadyFunc).toHaveBeenCalledTimes(0); + primary.resolve(null); + await disjoint(); + + expect(ensureTrustedFuncReadyFunc).toHaveBeenCalled(); + expect(av.primaryPromise).toBe(null); + expect(av.trustedPromise).toBe(trusted.promise); + expect(av.trustedStatus).toBe('verifying'); + + ensureTrustedFuncReady.resolve(); + await disjoint(); + + expect(av.primaryPromise).toBe(null); + expect(av.trustedPromise).toBe(trusted.promise); + expect(av.trustedStatus).toBe('verifying'); + + expect(rejection).toBeUndefined(); + jest.spyOn(console, 'warn').mockImplementationOnce(() => {}); // suppress the warning + trusted.reject(new Error()); + await disjoint(); + + expect(rejection).toBeDefined(); + expect(av.primaryPromise).toBe(null); + expect(av.trustedPromise).toBe(null); + expect(av.trustedStatus).toBe('none'); + + await disjoint(); // sanity check + + expect(primaryFunc).toHaveBeenCalledTimes(1); + expect(trustedFunc).toHaveBeenCalledTimes(1); // notably, this server response will be thrown out + expect(ensureTrustedFuncReadyFunc).toHaveBeenCalledTimes(1); + expect(verifyFunc).toHaveBeenCalledTimes(0); + }); + // Make sure to do try/catch errors/rejections, verification failures, unverifies in the middle // Multiple tryResolveTrustedPromises + // Multiple fetchAndVerifyIfNeeded at the same time });