mirror of
https://github.com/freeCodeCamp/freeCodeCamp.git
synced 2026-08-31 01:14:08 +08:00
fix(api): dcc empty collections return 200 not 404 (#68890)
This commit is contained in:
committed by
GitHub
parent
7c68382a6e
commit
7f7379efeb
@@ -493,7 +493,7 @@ describe('/daily-coding-challenge', () => {
|
||||
expect(res.status).toBe(200);
|
||||
});
|
||||
|
||||
it('should return 404 when no challenges exist for the given month', async () => {
|
||||
it('should return 200 with an empty array when no challenges exist for the given month', async () => {
|
||||
const count = vi.fn();
|
||||
const originalSentry = fastifyTestInstance.Sentry;
|
||||
fastifyTestInstance.Sentry = {
|
||||
@@ -505,12 +505,9 @@ describe('/daily-coding-challenge', () => {
|
||||
method: 'GET'
|
||||
}).send({});
|
||||
|
||||
expect(res.status).toBe(404);
|
||||
expect(res.body).toEqual({
|
||||
type: 'error',
|
||||
message: 'No challenges found.'
|
||||
});
|
||||
expect(count).toHaveBeenCalledWith('dcc.challenge_not_found', 1, {
|
||||
expect(res.status).toBe(200);
|
||||
expect(res.body).toEqual([]);
|
||||
expect(count).toHaveBeenCalledWith('dcc.empty_result', 1, {
|
||||
attributes: { route: '/daily-coding-challenge/month/:month' }
|
||||
});
|
||||
|
||||
@@ -569,7 +566,7 @@ describe('/daily-coding-challenge', () => {
|
||||
fastifyTestInstance.Sentry = originalSentry;
|
||||
});
|
||||
|
||||
it('should return 404 when no challenges exist', async () => {
|
||||
it('should return 200 with an empty array when no challenges exist', async () => {
|
||||
await fastifyTestInstance.prisma.dailyCodingChallenges.deleteMany();
|
||||
|
||||
const count = vi.fn();
|
||||
@@ -583,12 +580,9 @@ describe('/daily-coding-challenge', () => {
|
||||
method: 'GET'
|
||||
}).send({});
|
||||
|
||||
expect(res.status).toBe(404);
|
||||
expect(res.body).toEqual({
|
||||
type: 'error',
|
||||
message: 'No challenges found.'
|
||||
});
|
||||
expect(count).toHaveBeenCalledWith('dcc.challenge_not_found', 1, {
|
||||
expect(res.status).toBe(200);
|
||||
expect(res.body).toEqual([]);
|
||||
expect(count).toHaveBeenCalledWith('dcc.empty_result', 1, {
|
||||
attributes: { route: '/daily-coding-challenge/all' }
|
||||
});
|
||||
|
||||
|
||||
@@ -260,13 +260,10 @@ export const dailyCodingChallengeRoutes: FastifyPluginCallbackTypebox = (
|
||||
});
|
||||
|
||||
if (!challenges || challenges.length === 0) {
|
||||
req.log.warn({ month }, 'No challenges found for month');
|
||||
fastify.Sentry?.metrics?.count('dcc.challenge_not_found', 1, {
|
||||
fastify.Sentry?.metrics?.count('dcc.empty_result', 1, {
|
||||
attributes: { route: '/daily-coding-challenge/month/:month' }
|
||||
});
|
||||
return reply
|
||||
.status(404)
|
||||
.send({ type: 'error', message: 'No challenges found.' });
|
||||
return reply.send([]);
|
||||
}
|
||||
|
||||
const response = challenges.map(challenge => ({
|
||||
@@ -322,13 +319,10 @@ export const dailyCodingChallengeRoutes: FastifyPluginCallbackTypebox = (
|
||||
});
|
||||
|
||||
if (!allChallenges || allChallenges.length === 0) {
|
||||
req.log.warn({ date: today }, 'No challenges found.');
|
||||
fastify.Sentry?.metrics?.count('dcc.challenge_not_found', 1, {
|
||||
fastify.Sentry?.metrics?.count('dcc.empty_result', 1, {
|
||||
attributes: { route: '/daily-coding-challenge/all' }
|
||||
});
|
||||
return reply
|
||||
.status(404)
|
||||
.send({ type: 'error', message: 'No challenges found.' });
|
||||
return reply.send([]);
|
||||
}
|
||||
|
||||
const response = allChallenges.map(challenge => ({
|
||||
|
||||
@@ -100,10 +100,6 @@ const month = {
|
||||
type: Type.Literal('error'),
|
||||
message: Type.Literal('Invalid date format. Please use YYYY-MM.')
|
||||
}),
|
||||
404: Type.Object({
|
||||
type: Type.Literal('error'),
|
||||
message: Type.Literal('No challenges found.')
|
||||
}),
|
||||
500: Type.Object({
|
||||
type: Type.Literal('error'),
|
||||
message: Type.Literal('Internal server error.')
|
||||
@@ -114,10 +110,6 @@ const month = {
|
||||
const all = {
|
||||
response: {
|
||||
200: manyChallengesResponse,
|
||||
404: Type.Object({
|
||||
type: Type.Literal('error'),
|
||||
message: Type.Literal('No challenges found.')
|
||||
}),
|
||||
500: Type.Object({
|
||||
type: Type.Literal('error'),
|
||||
message: Type.Literal('Internal server error.')
|
||||
|
||||
@@ -151,7 +151,7 @@ function DailyCodingChallengeCalendar({
|
||||
const response = await fetch(`${apiLocation}/daily-coding-challenge/all`);
|
||||
const challenges = (await response.json()) as AllDailyChallengeFromDb[];
|
||||
|
||||
if (Array.isArray(challenges)) {
|
||||
if (Array.isArray(challenges) && challenges.length > 0) {
|
||||
// Todo: validate shape of challenges
|
||||
|
||||
const newDailyChallengesMap = new Map() as DailyChallengesMap;
|
||||
|
||||
@@ -124,4 +124,18 @@ describe('<DailyCodingChallengeArchive />', () => {
|
||||
})
|
||||
).toBeInTheDocument();
|
||||
});
|
||||
|
||||
it('renders the not found page when there are no challenges', async () => {
|
||||
vi.spyOn(globalThis, 'fetch').mockResolvedValue({
|
||||
json: vi.fn().mockResolvedValue([])
|
||||
} as unknown as Response);
|
||||
|
||||
renderArchive();
|
||||
|
||||
expect(
|
||||
await screen.findByRole('heading', {
|
||||
name: 'daily-coding-challenges.not-found'
|
||||
})
|
||||
).toBeInTheDocument();
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user