forked from storj-archived/bridge
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
131 additions
and
90 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,126 @@ | ||
import { ObjectId } from 'mongodb' | ||
import crypto from 'crypto' | ||
import axios from 'axios'; | ||
import { engine, testServer } from '../setup'; | ||
import { type User } from '../users.fixtures'; | ||
import { createTestUser, getAuth } from '../utils'; | ||
|
||
jest.mock('axios', () => ({ get: jest.fn(), post: jest.fn() })) | ||
|
||
describe('Bridge E2E Tests', () => { | ||
|
||
let testUser: User | ||
beforeAll(async () => { | ||
testUser = await createTestUser() | ||
|
||
// Create a contact | ||
const nodeID = engine._config.application.CLUSTER['0'] | ||
const payload = { nodeID, protocol: "1.2.0-INXT", address: "72.132.43.2", port: 43758, lastSeen: new Date(), } | ||
await new Promise(resolve => engine.storage.models.Contact.record(payload, resolve)) | ||
}) | ||
|
||
beforeEach(() => { | ||
jest.clearAllMocks() | ||
}) | ||
|
||
describe('File Management v1', () => { | ||
|
||
|
||
describe('Uploading a file', () => { | ||
|
||
it('When a user wants to upload a file, it should work for owned buckets and get a list of upload links one per each file part', async () => { | ||
// Arrange: Mock http calls | ||
const get = axios.get as jest.MockedFunction<typeof axios.get>; | ||
get.mockImplementation(async () => ({ data: { result: 'http://fake-url' } })) | ||
|
||
// Arrange: Create a bucket | ||
const { body: { id: bucketId } } = await testServer | ||
.post('/buckets') | ||
.set('Authorization', getAuth(testUser)) | ||
.expect(201) | ||
|
||
// Act: start the upload | ||
const response = await testServer.post(`/v2/buckets/${bucketId}/files/start`) | ||
.set('Authorization', getAuth(testUser)) | ||
.send({ uploads: [{ index: 0, size: 1000, }, { index: 1, size: 10000, },], }) | ||
|
||
// Assert | ||
expect(response.status).toBe(200); | ||
const { uploads } = response.body; | ||
|
||
expect(uploads).toHaveLength(2); | ||
|
||
const upload1 = uploads[0]; | ||
expect(upload1.url).toBe('http://fake-url') | ||
expect(upload1.urls).toBeNull(); | ||
expect(upload1.uuid).toBeDefined(); | ||
|
||
const upload2 = uploads[1]; | ||
expect(upload2.url).toBeDefined(); | ||
expect(upload2.url).toBe('http://fake-url') | ||
expect(upload2.urls).toBeNull(); | ||
expect(upload2.uuid).toBeDefined(); | ||
|
||
}) | ||
|
||
|
||
it('When a user finishes to upload a file, the user can finish the upload with a hash per each part uploaded', async () => { | ||
|
||
const get = axios.get as jest.MockedFunction<typeof axios.get>; | ||
get.mockImplementation(async (url: string) => { | ||
// Mock the upload link | ||
if (url.includes('/v2/upload/link')) return { data: { result: 'http://fake-url' } } | ||
// Mock the exists check | ||
if (url.includes('/exists')) return { status: 200 } | ||
// Fail for any other request | ||
throw new Error('Not implemented') | ||
}) | ||
|
||
// Arrange: Create a bucket | ||
const { body: { id: bucketId } } = await testServer | ||
.post('/buckets') | ||
.set('Authorization', getAuth(testUser)) | ||
|
||
// Arrange: start the upload | ||
const response = await testServer.post(`/v2/buckets/${bucketId}/files/start`) | ||
.set('Authorization', getAuth(testUser)) | ||
.send({ uploads: [{ index: 0, size: 1000, }, { index: 1, size: 10000, },], }) | ||
|
||
const { uploads } = response.body; | ||
|
||
// Act: finish the upload | ||
const index = crypto.randomBytes(32).toString('hex'); | ||
const responseComplete = await testServer.post(`/v2/buckets/${bucketId}/files/finish`) | ||
.set('Authorization', getAuth(testUser)) | ||
.send({ | ||
index, | ||
shards: [ | ||
{ hash: crypto.randomBytes(20).toString('hex'), uuid: uploads[0].uuid, }, | ||
{ hash: crypto.randomBytes(20).toString('hex'), uuid: uploads[1].uuid, }, | ||
], | ||
}); | ||
|
||
// Assert | ||
expect(responseComplete.status).toBe(200); | ||
|
||
const body = responseComplete.body; | ||
|
||
expect(body.bucket).toEqual(bucketId); | ||
expect(body.created).toBeDefined(); | ||
expect(body.filename).toBeDefined(); | ||
expect(body.id).toBeDefined(); | ||
expect(body.index).toEqual(index); | ||
expect(body.mimetype).toBeDefined(); | ||
expect(body.renewal).toBeDefined(); | ||
expect(body.size).toBeGreaterThan(0); | ||
expect(typeof body.size).toBe('number'); | ||
expect(body.version).toBe(2); | ||
}); | ||
|
||
}) | ||
|
||
}) | ||
|
||
|
||
|
||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters