Skip to content

Commit

Permalink
test(e2e): test file upload
Browse files Browse the repository at this point in the history
  • Loading branch information
sw-wayner committed Jan 9, 2024
1 parent ef3d1e5 commit 5c408f3
Show file tree
Hide file tree
Showing 4 changed files with 131 additions and 90 deletions.
89 changes: 1 addition & 88 deletions tests/lib/e2e/buckets/buckets.e2e-spec.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,9 @@
import { dataGenerator } from './../users.fixtures'
// import crypto from 'crypto';
import { createTestUser, deleteTestUser, getAuth, } from '../utils'
import { engine, testServer } from '../setup'
// import axios, { type AxiosStatic } from 'axios'
import { type User } from '../users.fixtures';


jest.mock('axios', () => ({ get: jest.fn() }))

describe('Bridge E2E Tests', () => {

let testUser: User
Expand Down Expand Up @@ -189,90 +185,7 @@ describe('Bridge E2E Tests', () => {
})
})

// describe('File Management', () => {

// describe('File upload v1', () => {

// it('Uploads and finishes correctly', async () => {

// const nodeID = engine._config.application.CLUSTER['0']

// const get = axios.get as jest.MockWithArgs<AxiosStatic>

// get.mockResolvedValue(Promise.resolve({ data: { result: 'http://fake-url' } } as any))

// await new Promise(resolve => engine.storage.models.Contact.record({
// nodeID,
// protocol: "1.2.0-INXT",
// address: "72.132.43.2", // this ip address is an example
// port: 43758,
// lastSeen: new Date(),
// }, resolve))

// const { body: { id: bucketId } } = await testServer
// .post('/buckets')
// .set('Authorization', getAuth(testUser))


// const response = await testServer.post(`/v2/buckets/${bucketId}/files/start`)
// .set('Authorization', getAuth(testUser))
// .send({ uploads: [{ index: 0, size: 1000, }, { index: 1, size: 10000, },], })


// console.log({ body: { ...response.body } });

// const { uploads } = response.body;

// for (const upload of uploads) {
// const { url, urls, index, uuid } = upload;
// expect(url).toBeDefined();
// expect(url).toContain('http');
// expect(url).toBe('http://fake-url')
// expect(urls).toBeNull();
// expect(uuid).toBeDefined();
// const file = crypto.randomBytes(50).toString('hex');
// // await axios.put(url, file, { headers: { 'Content-Type': 'application/octet-stream', }, });
// }

// 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, },
// ],
// });

// expect(responseComplete.status).toBe(200);

// const {
// bucket,
// created,
// filename,
// id,
// index: indexResponse,
// mimetype,
// renewal,
// size,
// version,
// } = responseComplete.body;

// expect(bucket).toEqual(bucketId);
// expect(created).toBeDefined();
// expect(filename).toBeDefined();
// expect(id).toBeDefined();
// expect(indexResponse).toEqual(index);
// expect(mimetype).toBeDefined();
// expect(renewal).toBeDefined();
// expect(size).toBeGreaterThan(0);
// expect(typeof size).toBe('number');
// expect(version).toBe(2);
// });
// })

// })


})

126 changes: 126 additions & 0 deletions tests/lib/e2e/buckets/files.e2e-spec.ts
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);
});

})

})



});
3 changes: 2 additions & 1 deletion tests/lib/e2e/global-setup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ export default async () => {
const db = client.db();
await Promise.all([
db.collection('users').deleteMany({}),
db.collection('buckets').deleteMany({})
db.collection('buckets').deleteMany({}),
db.collection('contacts').deleteMany({}),
]);

globalThis.mongoClient = client;
Expand Down
3 changes: 2 additions & 1 deletion tests/lib/e2e/global-teardown.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ export default async () => {

await Promise.all([
db.collection('users').deleteMany({}),
db.collection('buckets').deleteMany({})
db.collection('buckets').deleteMany({}),
db.collection('contacts').deleteMany({}),
]);

await client.close();
Expand Down

0 comments on commit 5c408f3

Please sign in to comment.