mirror of
https://github.com/freeCodeCamp/freeCodeCamp.git
synced 2026-05-28 18:26:54 +00:00
feat: restrict card donation for users with little progress (#54529)
Co-authored-by: Naomi <nhcarrigan@gmail.com>
This commit is contained in:
@@ -1,9 +1,12 @@
|
||||
import type { Prisma } from '@prisma/client';
|
||||
import {
|
||||
createSuperRequest,
|
||||
devLogin,
|
||||
setupServer,
|
||||
superRequest
|
||||
superRequest,
|
||||
defaultUserEmail
|
||||
} from '../../jest.utils';
|
||||
import { createUserInput } from '../utils/create-user';
|
||||
|
||||
const chargeStripeCardReqBody = {
|
||||
paymentMethodId: 'UID',
|
||||
@@ -44,6 +47,39 @@ jest.mock('stripe', () => {
|
||||
});
|
||||
});
|
||||
|
||||
const userWithoutProgress: Prisma.userCreateInput =
|
||||
createUserInput(defaultUserEmail);
|
||||
|
||||
const userWithProgress: Prisma.userCreateInput = {
|
||||
...createUserInput(defaultUserEmail),
|
||||
completedChallenges: [
|
||||
{
|
||||
id: 'a6b0bb188d873cb2c8729495',
|
||||
completedDate: 1520002973119,
|
||||
solution: null,
|
||||
challengeType: 5
|
||||
},
|
||||
{
|
||||
id: '33b0bb188d873cb2c8729433',
|
||||
completedDate: 4420002973122,
|
||||
solution: null,
|
||||
challengeType: 5
|
||||
},
|
||||
{
|
||||
id: 'a5229172f011153519423690',
|
||||
completedDate: 1520440323273,
|
||||
solution: null,
|
||||
challengeType: 5
|
||||
},
|
||||
{
|
||||
id: 'a5229172f011153519423692',
|
||||
completedDate: 1520440323274,
|
||||
githubLink: '',
|
||||
challengeType: 5
|
||||
}
|
||||
]
|
||||
};
|
||||
|
||||
describe('Donate', () => {
|
||||
setupServer();
|
||||
|
||||
@@ -53,6 +89,10 @@ describe('Donate', () => {
|
||||
beforeEach(async () => {
|
||||
const setCookies = await devLogin();
|
||||
superPost = createSuperRequest({ method: 'POST', setCookies });
|
||||
await fastifyTestInstance.prisma.user.updateMany({
|
||||
where: { email: userWithProgress.email },
|
||||
data: userWithProgress
|
||||
});
|
||||
});
|
||||
|
||||
describe('POST /donate/charge-stripe-card', () => {
|
||||
@@ -133,6 +173,23 @@ describe('Donate', () => {
|
||||
error: 'Donation failed due to a server error.'
|
||||
});
|
||||
});
|
||||
|
||||
it('should return 400 if user has not completed challenges', async () => {
|
||||
await fastifyTestInstance.prisma.user.updateMany({
|
||||
where: { email: userWithProgress.email },
|
||||
data: userWithoutProgress
|
||||
});
|
||||
const failResponse = await superPost('/donate/charge-stripe-card').send(
|
||||
chargeStripeCardReqBody
|
||||
);
|
||||
expect(failResponse.body).toEqual({
|
||||
error: {
|
||||
type: 'MethodRestrictionError',
|
||||
message: `Donate using another method`
|
||||
}
|
||||
});
|
||||
expect(failResponse.status).toBe(400);
|
||||
});
|
||||
});
|
||||
|
||||
describe('POST /donate/add-donation', () => {
|
||||
|
||||
@@ -102,6 +102,17 @@ export const donateRoutes: FastifyPluginCallbackTypebox = (
|
||||
});
|
||||
|
||||
const { email, name } = user;
|
||||
const threeChallengesCompleted = user.completedChallenges.length >= 3;
|
||||
|
||||
if (!threeChallengesCompleted) {
|
||||
void reply.code(400);
|
||||
return {
|
||||
error: {
|
||||
type: 'MethodRestrictionError',
|
||||
message: `Donate using another method`
|
||||
}
|
||||
} as const;
|
||||
}
|
||||
|
||||
if (user.isDonating) {
|
||||
void reply.code(400);
|
||||
|
||||
@@ -14,7 +14,10 @@ export const chargeStripeCard = {
|
||||
400: Type.Object({
|
||||
error: Type.Object({
|
||||
message: Type.String(),
|
||||
type: Type.Literal('AlreadyDonatingError')
|
||||
type: Type.Union([
|
||||
Type.Literal('AlreadyDonatingError'),
|
||||
Type.Literal('MethodRestrictionError')
|
||||
])
|
||||
})
|
||||
}),
|
||||
402: Type.Object({
|
||||
|
||||
Reference in New Issue
Block a user