Skip to content

Commit

Permalink
added test cases for get-info and get routes, used markdown-it instea…
Browse files Browse the repository at this point in the history
…d of marked
  • Loading branch information
Bregwin Jogi committed Jul 8, 2024
1 parent 0306d14 commit c1696de
Show file tree
Hide file tree
Showing 7 changed files with 216 additions and 77 deletions.
2 changes: 1 addition & 1 deletion env.jest
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
PORT=8080

# Disable logs in tests. If you need to see more detail, change this to `debug`
LOG_LEVEL=silent
LOG_LEVEL=debug

# .htpasswd file to use in testing
HTPASSWD_FILE=tests/.htpasswd
60 changes: 51 additions & 9 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
"http-auth": "^4.2.0",
"http-auth-passport": "^1.0.7",
"js-yaml": "^4.1.0",
"marked": "^13.0.0",
"markdown-it": "^14.1.0",
"passport": "^0.7.0",
"passport-http-bearer": "^1.0.1",
"pino": "^9.0.0",
Expand Down
79 changes: 40 additions & 39 deletions src/routes/api/get.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@ const { createSuccessResponse } = require('../../response');
const { createErrorResponse } = require('../../response');
const logger = require('../../logger');
//conversion libraries
const marked = require('marked');
const yaml = require('js-yaml');
const markdownit = require('markdown-it');

//TODO: Add support for other text and JSON conversion
// const yaml = require('js-yaml');

const extensionToMimeType = {
// Text types
Expand Down Expand Up @@ -64,21 +66,21 @@ module.exports = (req, res, next) => {
convertFileType(fragment.mimeType, data, extension)
.then((convertedData) => {

const mimeType = getMimeType(extension);

res
const mimeType = getMimeType(extension)
res
.status(200)
.header('Content-Type', mimeType)
.header('Content-Disposition', `attachment; filename="fragment.${extension}"`)
.send(convertedData);

})
.catch((err) => {
logger.error(err);
res.status(415).json(createErrorResponse(415, 'Not allowed to convert to specified format'));
});
}
} else {

// Return the data with original content type it had
res.status(200).send(data.toString());
}
Expand Down Expand Up @@ -106,46 +108,45 @@ module.exports = (req, res, next) => {

const convertFileType = async (mimeType, data, extension) => {
data = data.toString();

try {
switch (mimeType) {
case 'text/plain':
if (extension === 'txt') return data;
break;
case 'text/markdown':
if (extension === 'html') {
return marked(data);
}
if (extension === 'txt') return data;
break;
case 'application/json':
if (extension === 'yaml' || extension === 'yml') {
const jsonObject = JSON.parse(data);
return yaml.dump(jsonObject);
}
if (extension === 'txt') return data;
break;
case 'application/yaml':
if (extension === 'json') {
const yamlObject = yaml.load(data);
return JSON.stringify(yamlObject);
}
if (extension === 'txt') return data;
break;

default:
throw new Error('Not allowed to convert to specified format');

if (mimeType === 'text/plain') {
if (extension === 'txt') return data;
} else if (mimeType === 'text/markdown') {
if (extension === 'html') {
const md = markdownit();
return md.render(data);
}
} catch (error) {
throw new Error(`Conversion failed: ${error.message}`);
if (extension === 'txt') return data;
}

//TODO: Add support for other text and JSON conversion
//Partial Implementation for Assigment 3
// else if (mimeType === 'application/json') {
// if (extension === 'yaml' || extension === 'yml') {
// const jsonObject = JSON.parse(data);
// return yaml.dump(jsonObject);
// }
// if (extension === 'txt') return data;
// } else if (mimeType === 'application/yaml') {
// if (extension === 'json') {
// const yamlObject = yaml.load(data);
// return JSON.stringify(yamlObject);
// }
// if (extension === 'txt') return data;
// }

else {
throw new Error(`Conversion failed: Unsupported conversion (${mimeType} to ${extension})`);
}



};


const getMimeType = (extension) => {
const mimeType = extensionToMimeType[extension.toLowerCase()];
if (!mimeType) {
throw new Error(`MIME type for extension ${extension} is not supported.`);
return null;
}
return mimeType;
};
Expand Down
13 changes: 5 additions & 8 deletions tests/unit/fragment.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,14 @@ const wait = async (ms = 10) => new Promise((resolve) => setTimeout(resolve, ms)

const validTypes = [
`text/plain`,
/*
Currently, only text/plain is supported. Others will be added later.
`text/markdown`,
`text/html`,
`application/json`,
`image/png`,
`image/jpeg`,
`image/webp`,
`image/gif`,
*/
// `image/png`,
// `image/jpeg`,
// `image/webp`,
// `image/gif`,

];

describe('Fragment class', () => {
Expand Down
51 changes: 51 additions & 0 deletions tests/unit/get-info.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
const request = require('supertest');
const { createSuccessResponse } = require('../../src/response');
const app = require('../../src/app');

describe('GET /v1/fragments/:id/info', () => {

test('GET /fragments/:id/info should return fragment info when found', async () => {
const postRequest = await request(app)
.post('/v1/fragments')
.auth('[email protected]', 'password1')
.set('Content-Type', 'text/plain')
.send(Buffer.from('hello world'));

const res = await request(app)
.get(`/v1/fragments/${postRequest.body.fragment.id}/info`)
.auth('[email protected]', 'password1');

expect(res.statusCode).toBe(200);
expect(res.text).toBe(
JSON.stringify(
createSuccessResponse({
fragment: {
id: postRequest.body.fragment.id,
ownerId: postRequest.body.fragment.ownerId,
created: postRequest.body.fragment.created,
updated: postRequest.body.fragment.updated,
type: 'text/plain',
size: 11,
},
})
)
);

});



test('GET /fragments/:id should return 404 when fragment not found', async () => {

const res = await request(app)
.get(`/v1/fragments/123123/info`)
.auth('[email protected]', 'password1');

expect(res.statusCode).toBe(404);
expect(res.body.status).toBe('error');
expect(res.body.error.message).toBe('Fragment not found');

});


});
Loading

0 comments on commit c1696de

Please sign in to comment.