From cdd12ed3b380865fce35eacdba502798003c02d3 Mon Sep 17 00:00:00 2001 From: Melisa Anabella Rossi Date: Thu, 3 Aug 2023 18:53:47 -0300 Subject: [PATCH] add tests --- src/Project/Project.router.spec.ts | 126 +++++++++++++++++++++++++---- src/Project/Project.router.ts | 69 +++++++++++----- src/S3/S3Model.ts | 1 - src/S3/s3.ts | 1 - 4 files changed, 156 insertions(+), 41 deletions(-) diff --git a/src/Project/Project.router.spec.ts b/src/Project/Project.router.spec.ts index 5ae6639a..1ce04ab5 100644 --- a/src/Project/Project.router.spec.ts +++ b/src/Project/Project.router.spec.ts @@ -1,9 +1,28 @@ import supertest from 'supertest' import { buildURL, createAuthHeaders } from '../../spec/utils' import { SearchableModel } from '../Searchable/SearchableModel' -import { SearchableProject } from './SearchableProject' import { app } from '../server' +import * as s3Module from '../S3' +import { SearchableProject } from './SearchableProject' import { TemplateStatus } from './Project.types' +import { ManifestAttributes } from '../Manifest' +import { SDK7Scene } from '../Scene/SDK7Scene' +import { ContentMapping, Entity } from '@dcl/schemas' +import { COMPOSITE_FILE_HASH } from '../Scene/composite' + +jest.mock('../S3', () => { + return { + ...jest.requireActual('../S3'), + getProjectManifest: jest.fn() + } +}) + +jest.mock('../middleware', () => { + return { + ...jest.requireActual('../middleware'), + withModelExists: jest.fn().mockImplementation(() => jest.fn((_req, _res, next) => next())) + } +}) const server = supertest(app.getApp()) @@ -27,6 +46,10 @@ const mockEmptyResult = { describe('Project Router', () => { let url = '' + afterEach(() => { + jest.clearAllMocks() + }) + describe('when getting the projects of a user', () => { let mockResult: any describe('and the user has projects', () => { @@ -42,10 +65,6 @@ describe('Project Router', () => { .mockResolvedValueOnce(mockResult) }) - afterEach(() => { - jest.resetAllMocks() - }) - it('should return the projects', async () => { const response = await server .get(buildURL(url)) @@ -71,10 +90,6 @@ describe('Project Router', () => { .mockResolvedValueOnce(mockEmptyResult) }) - afterEach(() => { - jest.resetAllMocks() - }) - it('should return an empty array', async () => { const response = await server .get(buildURL(url)) @@ -111,10 +126,6 @@ describe('Project Router', () => { .mockResolvedValueOnce(mockResult) }) - afterEach(() => { - jest.resetAllMocks() - }) - it('should return the templates', async () => { const response = await server .get(buildURL(url)) @@ -138,10 +149,6 @@ describe('Project Router', () => { .mockResolvedValueOnce(mockEmptyResult) }) - afterEach(() => { - jest.resetAllMocks() - }) - it('should return an empty array', async () => { const response = await server .get(buildURL(url)) @@ -158,4 +165,89 @@ describe('Project Router', () => { }) }) }) + + describe('when getting scene contents', () => { + const projectId = 'project-id' + + describe('and getting project preview', () => { + describe('and project scene is in sdk6', () => { + beforeEach(() => { + (s3Module.getProjectManifest as jest.Mock).mockResolvedValueOnce({ + version: 1, + project: {}, + scene: { sdk6: { id: 'scene-id' }, sdk7: null } + } as unknown as ManifestAttributes) + }) + + it('should return error', async () => { + return await server + .get(buildURL(`/projects/${projectId}/contents/preview`)) + .expect(400) + }) + }) + + describe('and project scene is in sdk7', () => { + const entity = { id: 'entity-id', content: [] as ContentMapping[] } + beforeEach(() => { + (s3Module.getProjectManifest as jest.Mock).mockResolvedValueOnce({ + version: 1, + project: {}, + scene: { sdk6: null, sdk7: { id: 'scene-id' } } + } as unknown as ManifestAttributes) + jest.spyOn(SDK7Scene.prototype, 'getEntity').mockResolvedValueOnce(entity as Entity) + }) + + it('should return entity object', async () => { + const response = await server + .get(buildURL(`/projects/${projectId}/contents/preview`)) + .expect(200) + expect(response.body).toEqual(entity) + }) + }) + }) + + describe('and getting scene composite', () => { + describe('and project scene is in sdk6', () => { + beforeEach(() => { + (s3Module.getProjectManifest as jest.Mock).mockResolvedValueOnce({ + version: 1, + project: {}, + scene: { sdk6: { id: 'scene-id' }, sdk7: null } + } as unknown as ManifestAttributes) + }) + + it('should return error', async () => { + return await server + .get(buildURL(`/projects/${projectId}/contents/${COMPOSITE_FILE_HASH}`)) + .expect(400) + }) + }) + + describe('and project scene is in sdk7', () => { + const composite = { components: [] } + beforeEach(() => { + (s3Module.getProjectManifest as jest.Mock).mockResolvedValueOnce({ + version: 1, + project: {}, + scene: { sdk6: null, sdk7: { id: 'scene-id', composite } } + } as unknown as ManifestAttributes) + }) + + it('should return composite definition', async () => { + const response = await server + .get(buildURL(`/projects/${projectId}/contents/${COMPOSITE_FILE_HASH}`)) + .expect(200) + expect(response.body).toEqual(composite) + }) + }) + }) + + describe('and getting file hash', () => { + it('should redirect to s3 bucket', async () => { + return await server + .get(buildURL(`/projects/${projectId}/contents/some-hash`)) + .expect(301) + }) + }) + }) }) diff --git a/src/Project/Project.router.ts b/src/Project/Project.router.ts index 7c42088f..c72cd998 100644 --- a/src/Project/Project.router.ts +++ b/src/Project/Project.router.ts @@ -142,7 +142,7 @@ export class ProjectRouter extends Router { this.router.get( '/projects/:id/contents/:content', withProjectExists, - server.handleRequest(this.getContents) + this.getContents ) } @@ -235,8 +235,8 @@ export class ProjectRouter extends Router { addInmutableCacheControlHeader(res) return res.redirect( - `${getBucketURL()}/${project.getFileKey(filename)}`, - 301 + 301, + `${getBucketURL()}/${project.getFileKey(filename)}` ) } @@ -272,30 +272,55 @@ export class ProjectRouter extends Router { // when content is preview, return entity object if (content === 'preview') { - const { scene, project } = await getProjectManifest(projectId) - - if (scene.sdk6) { - throw new Error("Can't preview sdk6 scenes") + try { + const { scene, project } = await getProjectManifest(projectId) + if (scene.sdk6) { + return res + .status(STATUS_CODES.badRequest) + .send( + server.sendError( + { projectId: project.id }, + "Can't preview projects with sdk6 scene" + ) + ) + } + + const entity = await new SDK7Scene(scene.sdk7).getEntity(project) + + // Add composite file + entity.content = [ + { file: 'composite.json', hash: COMPOSITE_FILE_HASH }, + ...entity.content, + ] + + return res.json(entity) + } catch(error) { + return res + .status(STATUS_CODES.notFound) + .send(server.sendError({ projectId }, (error as Error)?.message)) } - - const entity = await new SDK7Scene(scene.sdk7).getEntity(project) - - // Add composite file - entity.content = [ - { file: 'composite.json', hash: COMPOSITE_FILE_HASH }, - ...entity.content, - ] - - return entity } // when content is composite, return scene composite if (content === COMPOSITE_FILE_HASH) { - const { scene } = await getProjectManifest(projectId) - if (scene.sdk6) { - throw new Error("SDK6 scene don't have composite definition") + try { + const { scene } = await getProjectManifest(projectId) + if (scene.sdk6) { + return res + .status(STATUS_CODES.badRequest) + .send( + server.sendError( + { projectId }, + "SDK6 scene don't have composite definition" + ) + ) + } + return res.json(scene.sdk7.composite) + } catch (error: any) { + return res + .status(STATUS_CODES.notFound) + .send(server.sendError({ projectId }, (error as Error)?.message)) } - return scene.sdk7.composite } // redirect to content in s3 @@ -304,6 +329,6 @@ export class ProjectRouter extends Router { content )}${ts ? `?ts=${ts}` : ''}` - return res.redirect(redirectPath, 301) + return res.redirect(301, redirectPath) } } diff --git a/src/S3/S3Model.ts b/src/S3/S3Model.ts index 18f7f7ba..23ab6bd0 100644 --- a/src/S3/S3Model.ts +++ b/src/S3/S3Model.ts @@ -31,7 +31,6 @@ export class S3Model { let file try { const key = this.getFileKey(filename) - console.log(key) file = await readFile(key) } catch (error) { // No previous entity diff --git a/src/S3/s3.ts b/src/S3/s3.ts index d8ad6353..cf92d79f 100644 --- a/src/S3/s3.ts +++ b/src/S3/s3.ts @@ -63,7 +63,6 @@ export function readFile(key: string): Promise { Key: key, } log.info(`Reading file "${key}"`) - console.log(BUCKET_NAME) return utils.promisify(s3.getObject.bind(s3))(params) }