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

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

describe('GET /v1/fragments/:id', () => {
	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
	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 fragmentData = postRes.text.fragment;

		const getRes = await request(app)
			.get(`/v1/fragments/${id}/info`)
			.auth('user1@email.com', 'password1');
		expect(getRes.statusCode).toBe(200);
		expect(getRes.body.text).toEqual(fragmentData);
	});

    // Check invalid id
	test('Returns an HTTP 404 with invalid id', async () => {
		await request(app)
			.post('/v1/fragments')
			.auth('user1@email.com', 'password1')
			.set('Content-Type', 'text/plain')
			.send('This is fragment');

		const getRes = await request(app)
			.get('/v1/fragments/invalidId/info')
			.auth('user1@email.com', 'password1');

		expect(getRes.status).toBe(404);
	});
});