From 21081a405a72d32d8606b3bf694935a603f5e693 Mon Sep 17 00:00:00 2001 From: Huyen Nguyen <25715018+huyenltnguyen@users.noreply.github.com> Date: Sun, 22 Dec 2024 15:14:28 +0700 Subject: [PATCH] fix(api): /showCert not returning user full name (#57666) Co-authored-by: Oliver Eyton-Williams --- api/src/routes/public/certificate.test.ts | 79 ++++++++++++++++++++++- api/src/schemas/certificate/cert-slug.ts | 9 +-- 2 files changed, 79 insertions(+), 9 deletions(-) diff --git a/api/src/routes/public/certificate.test.ts b/api/src/routes/public/certificate.test.ts index 568c6155e96..e4f2163d075 100644 --- a/api/src/routes/public/certificate.test.ts +++ b/api/src/routes/public/certificate.test.ts @@ -7,11 +7,24 @@ import { } from '../../../jest.utils'; import { getFallbackFullStackDate } from '../helpers/certificate-utils'; +const DATE_NOW = Date.now(); + describe('certificate routes', () => { setupServer(); describe('Unauthenticated user', () => { - beforeAll(async () => resetDefaultUser()); + beforeAll(async () => { + await resetDefaultUser(); + + jest.useFakeTimers({ + doNotFake: ['nextTick'] + }); + jest.setSystemTime(DATE_NOW); + }); + + afterAll(() => { + jest.useRealTimers(); + }); describe('GET /certificate/showCert/:username/:certSlug', () => { beforeEach(async () => { @@ -204,6 +217,70 @@ describe('certificate routes', () => { expect(response.status).toBe(200); }); + test('should not return user full name if `showName` is `false`', async () => { + await fastifyTestInstance.prisma.user.update({ + where: { id: defaultUserId }, + data: { + profileUI: { + showTimeLine: true, + showCerts: true, + isLocked: false, + showName: false + }, + isJsAlgoDataStructCert: true, + completedChallenges: [ + { + id: '561abd10cb81ac38a17513bc', // Cert ID + completedDate: DATE_NOW + } + ] + } + }); + + const response = await superRequest( + '/certificate/showCert/foobar/javascript-algorithms-and-data-structures', + { + method: 'GET' + } + ); + + // TODO: delete this assertion once there's only one status 200 response + expect(response.body).toHaveProperty('username', 'foobar'); + expect(response.body).not.toHaveProperty('name'); + expect(response.status).toBe(200); + }); + + test('should return user full name if `showName` is `true`', async () => { + await fastifyTestInstance.prisma.user.update({ + where: { id: defaultUserId }, + data: { + profileUI: { + showTimeLine: true, + showCerts: true, + isLocked: false, + showName: true + }, + isJsAlgoDataStructCert: true, + completedChallenges: [ + { + id: '561abd10cb81ac38a17513bc', // Cert ID + completedDate: DATE_NOW + } + ] + } + }); + + const response = await superRequest( + '/certificate/showCert/foobar/javascript-algorithms-and-data-structures', + { + method: 'GET' + } + ); + + expect(response.body).toHaveProperty('name', 'foobar'); + expect(response.status).toBe(200); + }); + test('should return cert-not-found if there is no cert with that slug', async () => { const response = await superRequest( '/certificate/showCert/foobar/not-a-valid-cert-slug', diff --git a/api/src/schemas/certificate/cert-slug.ts b/api/src/schemas/certificate/cert-slug.ts index 402fb2fac40..9880b6f5232 100644 --- a/api/src/schemas/certificate/cert-slug.ts +++ b/api/src/schemas/certificate/cert-slug.ts @@ -85,14 +85,7 @@ export const certSlug = { certSlug: Type.Enum(Certification), certTitle: Type.String(), username: Type.String(), - date: Type.Number(), - completionTime: Type.Number() - }), - Type.Object({ - certSlug: Type.Enum(Certification), - certTitle: Type.String(), - username: Type.String(), - name: Type.String(), + name: Type.Optional(Type.String()), date: Type.Number(), completionTime: Type.Number() }),