mirror of
https://github.com/freeCodeCamp/freeCodeCamp.git
synced 2026-04-11 07:00:41 -04:00
feat(api): add charge-stripe and create-stripe-payment-intent endpoints (#54545)
Co-authored-by: Oliver Eyton-Williams <ojeytonwilliams@gmail.com>
This commit is contained in:
29
api/src/utils/validate-donation.test.ts
Normal file
29
api/src/utils/validate-donation.test.ts
Normal file
@@ -0,0 +1,29 @@
|
||||
import { inLastFiveMinutes } from './validate-donation';
|
||||
|
||||
describe('inLastFiveMinutes', () => {
|
||||
beforeAll(() => {
|
||||
jest.useFakeTimers();
|
||||
});
|
||||
|
||||
afterAll(() => {
|
||||
jest.useRealTimers();
|
||||
});
|
||||
|
||||
it('should return true if the timestamp is within the last five minutes', () => {
|
||||
const currentTimestamp = Math.floor(Date.now() / 1000);
|
||||
const recentTimestamp = currentTimestamp - 100;
|
||||
expect(inLastFiveMinutes(recentTimestamp)).toBe(true);
|
||||
});
|
||||
|
||||
it('should return false if the timestamp is more than five minutes ago', () => {
|
||||
const currentTimestamp = Math.floor(Date.now() / 1000);
|
||||
const oldTimestamp = currentTimestamp - 400;
|
||||
expect(inLastFiveMinutes(oldTimestamp)).toBe(false);
|
||||
});
|
||||
|
||||
it('should return true if the timestamp is exactly five minutes ago', () => {
|
||||
const currentTimestamp = Math.floor(Date.now() / 1000);
|
||||
const exactTimestamp = currentTimestamp - 300;
|
||||
expect(inLastFiveMinutes(exactTimestamp)).toBe(true);
|
||||
});
|
||||
});
|
||||
10
api/src/utils/validate-donation.ts
Normal file
10
api/src/utils/validate-donation.ts
Normal file
@@ -0,0 +1,10 @@
|
||||
/**
|
||||
* Checks if a timestamp was created within five minutes.
|
||||
* @param unixTimestamp - A unix timestamp .
|
||||
* @returns - The generated email template.
|
||||
*/
|
||||
export const inLastFiveMinutes = (unixTimestamp: number) => {
|
||||
const currentTimestamp = Math.floor(Date.now() / 1000);
|
||||
const timeDifference = currentTimestamp - unixTimestamp;
|
||||
return timeDifference <= 300; // 300 seconds is 5 minutes
|
||||
};
|
||||
Reference in New Issue
Block a user