Skip to content

Commit

Permalink
test(core): unit validating clockskew offset is updated when client c…
Browse files Browse the repository at this point in the history
…lock back and forth (#12495)
  • Loading branch information
AllanZhengYP authored Nov 2, 2023
1 parent ecba1ec commit 1aa0c2b
Showing 1 changed file with 16 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,15 @@ 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(
offset
);
});

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);
Expand All @@ -32,12 +32,25 @@ 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);
expect(getUpdatedSystemClockOffset(clockTime.getTime(), 0)).toBe(
-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);
});
});

0 comments on commit 1aa0c2b

Please sign in to comment.