From 117a4e964b2c21dea9871290a745cff981cfe3bd Mon Sep 17 00:00:00 2001 From: Allan Zheng Date: Wed, 1 Nov 2023 17:38:57 -0700 Subject: [PATCH] test(core): add unit test to make sure clockskew offset is updated when client clock back and forth --- .../utils/getUpdatedSystemClockOffset.test.ts | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/packages/core/__tests__/clients/middleware/signing/utils/getUpdatedSystemClockOffset.test.ts b/packages/core/__tests__/clients/middleware/signing/utils/getUpdatedSystemClockOffset.test.ts index a7bf498885e..1861ded4eef 100644 --- a/packages/core/__tests__/clients/middleware/signing/utils/getUpdatedSystemClockOffset.test.ts +++ b/packages/core/__tests__/clients/middleware/signing/utils/getUpdatedSystemClockOffset.test.ts @@ -15,7 +15,7 @@ describe('getUpdatedSystemClockOffset', () => { Date.now = jest.fn(() => signingDate.valueOf()); }); - test('returns the current offset if not skewed', () => { + it('should return the current offset if not skewed', () => { mockIsClockSkewed.mockReturnValue(false); const offset = 1500; expect(getUpdatedSystemClockOffset(signingDate.getTime(), offset)).toBe( @@ -23,7 +23,7 @@ describe('getUpdatedSystemClockOffset', () => { ); }); - test('returns the updated offset if system clock is behind', () => { + it('should return the updated offset if system clock is behind', () => { mockIsClockSkewed.mockReturnValue(true); const clockTime = new Date(signingDate); clockTime.setMinutes(signingDate.getMinutes() + 15); @@ -32,7 +32,7 @@ describe('getUpdatedSystemClockOffset', () => { ); }); - test('returns the updated offset if system clock is ahead', () => { + it('should return the updated offset if system clock is ahead', () => { mockIsClockSkewed.mockReturnValue(true); const clockTime = new Date(signingDate); clockTime.setMinutes(signingDate.getMinutes() - 15); @@ -40,4 +40,17 @@ describe('getUpdatedSystemClockOffset', () => { -15 * 60 * 1000 ); }); + + // Addresses: https://github.com/aws-amplify/amplify-js/issues/12450#issuecomment-1787945008 + it('should return the updated offset if system clock is back and forth', () => { + // initialize client clock skew to be 15 mins behind + mockIsClockSkewed.mockReturnValue(true); + const clockTime = new Date(signingDate); + clockTime.setMinutes(signingDate.getMinutes() - 15); + let offset = getUpdatedSystemClockOffset(clockTime.getTime(), 0); + // client clock skew is now 15 mins ahead, making is sync with server clock + clockTime.setMinutes(signingDate.getMinutes() + 15); + offset = getUpdatedSystemClockOffset(clockTime.getTime(), offset); + expect(offset).toBe(0); + }); });