fragments / tests / unit / getById.test.js
getById.test.js
Raw
const request = require('supertest');

const app = require('../../src/app');

const fs = require('fs');

describe('GET /v1/fragments/:id', () => {
	const testFilePath = `${__dirname}/assets/Portfolio.png`;

	test('unauthenticated requests are denied', () =>
		request(app).get('/v1/fragments/:id').expect(401));

	test('incorrect credentials are denied', () =>
		request(app)
			.post('/v1/fragments/:id')
			.auth('invalid@email.com', 'incorrect_password')
			.expect(401));

	// Testing a successful result with fragment data with given id by Using a valid username/password with no extension
	test('authenticated users get fragment data with the given id', async () => {
		const postRes = await request(app)
			.post('/v1/fragments')
			.auth('user1@email.com', 'password1')
			.set('Content-Type', 'text/plain')
			.send('This is fragment');
		const id  = postRes.body.fragments.id;
    
		const getRes = await request(app)
			.get(`/v1/fragments/${id}`)
			.auth('user1@email.com', 'password1');
		expect(getRes.statusCode).toBe(200);
		expect(getRes.text).toEqual('This is fragment');
	});

	test('if fragment cannot be converted to invalid random extension, return 415 error', async () => {
		const resPost = await request(app)
		  .post('/v1/fragments')
		  .auth('user1@email.com', 'password1')
		  .set('Content-Type', 'text/plain')
		  .send('fragment');
	
		const id = resPost.body.fragments.id;
	
		const res = await request(app)
		  .get(`/v1/fragments/${id}.randomext`)
		  .auth('user1@email.com', 'password1')
		 
		expect(res.status).toBe(415);
	  });

	test('if fragment cannot be converted to the type, return 415 error', async () => {
		const resPost = await request(app)
		  .post('/v1/fragments')
		  .auth('user1@email.com', 'password1')
		  .set('Content-Type', 'text/plain')
		  .send('fragment');
	
		const id = resPost.body.fragments.id;
	
		const res = await request(app)
		  .get(`/v1/fragments/${id}.js`)
		  .auth('user1@email.com', 'password1')
		 
		expect(res.status).toBe(415);
	  });

	test('Check markdown with ext can be converted to html', async () => {
		const resPost = await request(app)
		  .post('/v1/fragments')
		  .auth('user1@email.com', 'password1')
		  .set('Content-Type', 'text/markdown')
		  .send('fragment');
	
		const id = resPost.body.fragments.id;
	
		const res = await request(app)
		  .get(`/v1/fragments/${id}.html`)
		  .auth('user1@email.com', 'password1')
		  
		expect(res.status).toBe(200);
		expect(res.headers['content-type']).toEqual('text/html');
	});

	// Get PNG image fragment (.png)
	test('get the image fragment', async () => {
		const postRes = await request(app)
			.post('/v1/fragments')
			.auth('user1@email.com', 'password1')
			.set('Content-Type', 'image/png')
			.send(fs.readFileSync(testFilePath));

		expect(postRes.statusCode).toBe(201);

		const id = postRes.header.location.split('fragments/')[1];

		const getRes = await request(app)
			.get(`/v1/fragments/${id}`)
			.auth('user1@email.com', 'password1');

		expect(getRes.statusCode).toBe(200);
	});

	// Convert PNG image fragment (.png) to invalid type (.html or .txt or json)
	test('get the image fragment by converting it to INVALID image formats', async () => {
		const postRes = await request(app)
			.post('/v1/fragments')
			.auth('user1@email.com', 'password1')
			.set('Content-Type', 'image/png')
			.send(fs.readFileSync(testFilePath));

		expect(postRes.statusCode).toBe(201);

		const id = postRes.header.location.split('fragments/')[1];

		const getTxt = await request(app)
			.get(`/v1/fragments/${id}.txt`)
			.auth('user1@email.com', 'password1');

		expect(getTxt.statusCode).toBe(415);

		const getHTML = await request(app)
			.get(`/v1/fragments/${id}.html`)
			.auth('user1@email.com', 'password1');

		expect(getHTML.statusCode).toBe(415);

		const getJson = await request(app)
			.get(`/v1/fragments/${id}.json`)
			.auth('user1@email.com', 'password1');

		expect(getJson.statusCode).toBe(415);
	});

	// Convert PNG image fragment (.png) to JPEG, WEBP or GIF image formats (.jpeg, .webp, or .gif)
	test('get the image fragment PNG by converting it to other image formats', async () => {
		const postRes = await request(app)
			.post('/v1/fragments')
			.auth('user1@email.com', 'password1')
			.set('Content-Type', 'image/png')
			.send(fs.readFileSync(testFilePath));

		expect(postRes.statusCode).toBe(201);

		const id = postRes.header.location.split('fragments/')[1];

		const getResPng = await request(app)
			.get(`/v1/fragments/${id}.jpeg`)
			.auth('user1@email.com', 'password1');

		expect(getResPng.statusCode).toBe(200);

		const getResWebp = await request(app)
			.get(`/v1/fragments/${id}.webp`)
			.auth('user1@email.com', 'password1');

		expect(getResWebp.statusCode).toBe(200);

		const getResGif = await request(app)
			.get(`/v1/fragments/${id}.gif`)
			.auth('user1@email.com', 'password1');

		expect(getResGif.statusCode).toBe(200);
	});

	// Convert PNG image fragment (.JPEG) to PNG, WEBP or GIF image formats (.png, .webp, or .gif)
	test('get the image fragment JPEG by converting it to other image formats', async () => {
		const postRes = await request(app)
			.post('/v1/fragments')
			.auth('user1@email.com', 'password1')
			.set('Content-Type', 'image/jpeg')
			.send(fs.readFileSync(testFilePath));

		expect(postRes.statusCode).toBe(201);

		const id = postRes.header.location.split('fragments/')[1];

		const getResPng = await request(app)
			.get(`/v1/fragments/${id}.png`)
			.auth('user1@email.com', 'password1');

		expect(getResPng.statusCode).toBe(200);

		const getResWebp = await request(app)
			.get(`/v1/fragments/${id}.webp`)
			.auth('user1@email.com', 'password1');

		expect(getResWebp.statusCode).toBe(200);

		const getResGif = await request(app)
			.get(`/v1/fragments/${id}.gif`)
			.auth('user1@email.com', 'password1');

		expect(getResGif.statusCode).toBe(200);
	});

	// Convert PNG image fragment (.WEBP) to PNG, JPEG or GIF image formats (.png, .jpeg, or .gif)
	test('get the image fragment WEBP by converting it to other image formats', async () => {
		const postRes = await request(app)
			.post('/v1/fragments')
			.auth('user1@email.com', 'password1')
			.set('Content-Type', 'image/webp')
			.send(fs.readFileSync(testFilePath));

		expect(postRes.statusCode).toBe(201);

		const id = postRes.header.location.split('fragments/')[1];

		const getResPng = await request(app)
			.get(`/v1/fragments/${id}.png`)
			.auth('user1@email.com', 'password1');

		expect(getResPng.statusCode).toBe(200);

		const getResWebp = await request(app)
			.get(`/v1/fragments/${id}.jpeg`)
			.auth('user1@email.com', 'password1');

		expect(getResWebp.statusCode).toBe(200);

		const getResGif = await request(app)
			.get(`/v1/fragments/${id}.gif`)
			.auth('user1@email.com', 'password1');

		expect(getResGif.statusCode).toBe(200);
	});

	// Convert PNG image fragment (.GIF) to PNG, JPEG or WEBP image formats (.png, .jpeg, or .webp)
	test('get the image fragment GIF by converting it to other image formats', async () => {
		const postRes = await request(app)
			.post('/v1/fragments')
			.auth('user1@email.com', 'password1')
			.set('Content-Type', 'image/gif')
			.send(fs.readFileSync(testFilePath));

		expect(postRes.statusCode).toBe(201);

		const id = postRes.header.location.split('fragments/')[1];

		const getResPng = await request(app)
			.get(`/v1/fragments/${id}.png`)
			.auth('user1@email.com', 'password1');

		expect(getResPng.statusCode).toBe(200);

		const getResWebp = await request(app)
			.get(`/v1/fragments/${id}.jpeg`)
			.auth('user1@email.com', 'password1');

		expect(getResWebp.statusCode).toBe(200);

		const getResGif = await request(app)
			.get(`/v1/fragments/${id}.webp`)
			.auth('user1@email.com', 'password1');

		expect(getResGif.statusCode).toBe(200);
	});
});