Skip to content

Commit

Permalink
chore: add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
wa0x6e committed Oct 16, 2023
1 parent 9d5e12c commit 6176498
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 9 deletions.
21 changes: 16 additions & 5 deletions test/e2e/rpc.test.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,33 @@
import request from 'supertest';
import { get } from '../../src/aws';
import heavyPayload from './fixtures/too-heavy.json';

const HOST = `http://localhost:${process.env.PORT || 3003}`;

describe('POST /', () => {
describe('when the payload is valid', () => {
it('returns a 200 error', async () => {
const response = await request(HOST)
.post('/')
.send({ params: { test: 'value' } });
const content = { test: 'value' };
let response;

beforeAll(async () => {
response = await request(HOST).post('/').send({ params: content });
});

it('returns a 200 error', async () => {
expect(response.statusCode).toBe(200);
});

it('returns a payload', () => {
expect(response.body.result.cid.length).toBeGreaterThan(10);
expect(['4everland', 'infura', 'fleek', 'pinata']).toContain(response.body.result.provider);
});

it('caches the payload', async () => {
expect(await get(response.body.result.cid)).toEqual(Buffer.from(JSON.stringify(content)));
});
});

describe('when the payload is no valid', () => {
describe('when the payload is not valid', () => {
it('returns a 400 error on malformed json', async () => {
const response = await request(HOST).post('/').send({ test: 'value' });

Expand Down
17 changes: 13 additions & 4 deletions test/e2e/upload.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import path from 'path';
import fs from 'fs';
import request from 'supertest';
import { get } from '../../src/aws';

const HOST = `http://localhost:${process.env.PORT || 3003}`;

Expand Down Expand Up @@ -39,15 +41,22 @@ describe('POST /upload', () => {
});

describe('when the file is correct', () => {
it('uploads the file and returns a JSON-RPC response with the CID and its provider', async () => {
const response = await request(HOST)
.post('/upload')
.attach('file', path.join(__dirname, './fixtures/valid.png'));
const imagePath = path.join(__dirname, './fixtures/valid.webp');
let response;

beforeAll(async () => {
response = await request(HOST).post('/upload').attach('file', imagePath);
}, 10e3);

it('uploads the file and returns a JSON-RPC response with the CID and its provider', async () => {
expect(response.statusCode).toBe(200);
expect(response.body.jsonrpc).toBe('2.0');
expect(response.body.result.cid.length).toBeGreaterThan(10);
expect(['4everland', 'infura', 'fleek', 'pinata']).toContain(response.body.result.provider);
});

it('caches the payload', async () => {
expect(await get(response.body.result.cid)).toEqual(await fs.promises.readFile(imagePath));
});
});
});

0 comments on commit 6176498

Please sign in to comment.