Skip to content

Commit

Permalink
add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Melisa Anabella Rossi committed Aug 3, 2023
1 parent 9cc4a66 commit cdd12ed
Show file tree
Hide file tree
Showing 4 changed files with 156 additions and 41 deletions.
126 changes: 109 additions & 17 deletions src/Project/Project.router.spec.ts
Original file line number Diff line number Diff line change
@@ -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())

Expand All @@ -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', () => {
Expand All @@ -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))
Expand All @@ -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))
Expand Down Expand Up @@ -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))
Expand All @@ -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))
Expand All @@ -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)
})
})
})
})
69 changes: 47 additions & 22 deletions src/Project/Project.router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ export class ProjectRouter extends Router {
this.router.get(
'/projects/:id/contents/:content',
withProjectExists,
server.handleRequest(this.getContents)
this.getContents
)
}

Expand Down Expand Up @@ -235,8 +235,8 @@ export class ProjectRouter extends Router {

addInmutableCacheControlHeader(res)
return res.redirect(
`${getBucketURL()}/${project.getFileKey(filename)}`,
301
301,
`${getBucketURL()}/${project.getFileKey(filename)}`
)
}

Expand Down Expand Up @@ -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
Expand All @@ -304,6 +329,6 @@ export class ProjectRouter extends Router {
content
)}${ts ? `?ts=${ts}` : ''}`

return res.redirect(redirectPath, 301)
return res.redirect(301, redirectPath)
}
}
1 change: 0 additions & 1 deletion src/S3/S3Model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 0 additions & 1 deletion src/S3/s3.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,6 @@ export function readFile(key: string): Promise<AWS.S3.GetObjectOutput> {
Key: key,
}
log.info(`Reading file "${key}"`)
console.log(BUCKET_NAME)
return utils.promisify<AWS.S3.GetObjectOutput>(s3.getObject.bind(s3))(params)
}

Expand Down

0 comments on commit cdd12ed

Please sign in to comment.