Skip to content

Commit

Permalink
remove uncommented code
Browse files Browse the repository at this point in the history
  • Loading branch information
Bregwin Jogi committed Jun 8, 2024
1 parent a0db446 commit 8e52709
Show file tree
Hide file tree
Showing 11 changed files with 21 additions and 96 deletions.
4 changes: 0 additions & 4 deletions src/auth/basic-auth.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@
// https://github.com/http-auth/http-auth-passport

const auth = require('http-auth');
//TEMP COMMENTED OUT FOR NOW
//const passport = require('passport');
const authPassport = require('http-auth-passport');
const authorize = require('./auth-middleware');

Expand All @@ -23,6 +21,4 @@ module.exports.strategy = () =>
})
);

//TEMP COMMENTED OUT FOR NOW
//module.exports.authenticate = () => passport.authenticate('http', { session: false });
module.exports.authenticate = () => authorize('http');
4 changes: 0 additions & 4 deletions src/auth/cognito.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@
// Identity Token provided by Cognito. The token will be
// parsed from the Authorization header (i.e., Bearer Token).

//TEMP COMMENTED OUT FOR NOW
// const passport = require('passport');
const BearerStrategy = require('passport-http-bearer').Strategy;
const { CognitoJwtVerifier } = require('aws-jwt-verify');

Expand Down Expand Up @@ -64,6 +62,4 @@ module.exports.strategy = () =>
}
});

//Commented out for now to use the authorize middleware, will use passport and cognito later
// module.exports.authenticate = () => passport.authenticate('bearer', { session: false });
module.exports.authenticate = () => authorize('bearer');
20 changes: 0 additions & 20 deletions src/model/fragment.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,20 +17,14 @@ const {
class Fragment {
constructor({ id, ownerId, created, updated, type, size = 0 }) {

//console.log('Constructor called with:', { id, ownerId, created, updated, type, size });

if(!ownerId ) {
//console.log('thrown! ownerId is required');
throw new Error('ownerId is required')
}else if (!type) {
//console.log('thrown! type is required');
throw new Error('type is required')
} else if(size < 0 || typeof size !== 'number') {
//console.log('thrown! size cannot be negative');
throw new Error('size cannot be negative')
}
else if (!Fragment.isSupportedType(type)) {
//console.log('thrown! unsupported media type');
throw new Error('unsupported media type');
}

Expand All @@ -41,7 +35,6 @@ class Fragment {
this.type = type;
this.size = size;

//console.log('Fragment created:', this);

}

Expand All @@ -52,7 +45,6 @@ class Fragment {
* @returns Promise<Array<Fragment>>
*/
static async byUser(ownerId, expand = false) {
// TODO
return await listFragments(ownerId, expand);
}

Expand All @@ -63,10 +55,6 @@ class Fragment {
* @returns Promise<Fragment>
*/
static async byId(ownerId, id) {

// if ((await this.byUser(ownerId).includes(id) )) {
// throw new Error('Fragment not found');
// }

const userFragments = await listFragments(ownerId, false);
if (!userFragments.includes(id)) {
Expand All @@ -90,7 +78,6 @@ class Fragment {
* @returns Promise<void>
*/
static delete(ownerId, id) {
// TODO
try {
return deleteFragment(ownerId, id);
}
Expand All @@ -104,7 +91,6 @@ class Fragment {
* @returns Promise<void>
*/
save() {
// TODO
this.updated = new Date().toISOString();
return writeFragment(this);
}
Expand All @@ -114,8 +100,6 @@ class Fragment {
* @returns Promise<Buffer>
*/
getData() {
// const fragment = this.byId(this.ownerId, this.id);
// return readFragmentData(fragment.ownerId, fragment.id);
return readFragmentData(this.ownerId, this.id);
}

Expand All @@ -125,7 +109,6 @@ class Fragment {
* @returns Promise<void>
*/
async setData(data) {
// TODO
if (!data || data.length === 0) {
throw new Error('data is required');
}
Expand All @@ -149,7 +132,6 @@ class Fragment {
* @returns {boolean} true if fragment's type is text/*
*/
get isText() {
// TODO
return this.mimeType.startsWith('text/');
}

Expand All @@ -158,7 +140,6 @@ class Fragment {
* @returns {Array<string>} list of supported mime types
*/
get formats() {
// TODO
return ['text/plain'];
}

Expand All @@ -168,7 +149,6 @@ class Fragment {
* @returns {boolean} true if we support this Content-Type (i.e., type/subtype)
*/
static isSupportedType(value) {
// TODO
const supportedTypes = ['text/plain', 'text/plain; charset=utf-8'];
return supportedTypes.includes(value);
}
Expand Down
10 changes: 1 addition & 9 deletions src/routes/api/get.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,10 @@ module.exports = (req, res, next) => {
res.status(415).json(createErrorResponse(415, 'Not allowed to convert to specified format'));
}
} else {
// Return the data with the original content type
// Return the data with original content type it had
res.status(200).send(data);
}

// res.status(200).send(
// data.toString()
// );

})})
.catch((err) => {
Expand All @@ -67,9 +64,4 @@ module.exports = (req, res, next) => {
});
}

// res.status(200).json(
// createSuccessResponse({
// fragments: [],
// })
// );
};
1 change: 0 additions & 1 deletion src/routes/api/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ const rawBody = () =>
// Define our first route, which will be: GET /v1/fragments
router.get('/fragments/:id?', require('./get'));
// Other routes (POST, DELETE, etc.) will go here later on...
// router.post('/fragments', require('./post'));

// Use a raw body parser for POST, which will give a `Buffer` Object or `{}` at `req.body`
// You can use Buffer.isBuffer(req.body) to test if it was parsed by the raw body parser.
Expand Down
14 changes: 4 additions & 10 deletions src/routes/api/post.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,20 +11,14 @@ module.exports = (req, res) => {
return;
}

// if (Fragment.isSupportedType(req.get('Content-Type')) === false) {
// res.status(400).json(createErrorResponse(415, 'unsupported media type'));
// return;
// }

const fragment = new Fragment({ ownerId: req.user, type: req.get('Content-Type') });

if (!Fragment.isSupportedType(fragment.type)) {

if (Fragment.isSupportedType(req.get('Content-Type')) === false) {
logger.error(`unsupported media type: ${fragment.type}`);
res.status(415).json(createErrorResponse(415, 'unsupported media type'));
res.status(400).json(createErrorResponse(415, 'unsupported media type'));
return;
}

const fragment = new Fragment({ ownerId: req.user, type: req.get('Content-Type') });

fragment
.setData(req.body)
.then(() => {
Expand Down
3 changes: 0 additions & 3 deletions tests/unit/app.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,6 @@ const request = require('supertest');
// Get our Express app object (we don't need the server part)
const app = require('../../src/app');

// const { createErrorResponse } = require('../../src/response');

// jest.mock('../../src/response');

describe('test 404 middleware', () => {
test('should return HTTP 404 response', async () => {
Expand Down
15 changes: 2 additions & 13 deletions tests/unit/auth.index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,25 +4,15 @@ const path = require('path');

describe('Authentication Module Selection', () => {
beforeAll(() => {
// Backup the original process.env object
// Backup original process.env
process.env = { ...process.env };
});

afterAll(() => {
// Restore the original process.env object
// Restore original process.env after testing
process.env = { ...process.env };
});

// test('should load Cognito auth module when AWS_COGNITO_POOL_ID and AWS_COGNITO_CLIENT_ID are set', () => {

// process.env.AWS_COGNITO_POOL_ID = 'fake-pool-id';
// process.env.AWS_COGNITO_CLIENT_ID = 'fake-client-id';

// const authModule = require('../../src/auth');
// // expect(authModule).toBe(require('../../src/auth/cognito'));
// expect(authModule).toThrow();

// });

test('should load Basic Auth module when HTPASSWD_FILE is set and not in production', () => {

Expand All @@ -45,7 +35,6 @@ describe('Authentication Module Selection', () => {

jest.resetModules();

// expect(() => require('../../src/auth')).toThrow('missing env vars: no authorization configuration found');
expect(() => require('../../src/auth')).toThrow('missing env vars: no authorization configuration found');
});

Expand Down
12 changes: 2 additions & 10 deletions tests/unit/logger.test.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
// logger.test.js
// const logger = require('../../src/logger');


describe('Logger', () => {
let originalEnv;

beforeAll(() => {
// Save the original process.env
// Save the original process.env to restore after test
originalEnv = { ...process.env };
});

Expand All @@ -16,22 +14,16 @@ describe('Logger', () => {
});

test('should use "info" as the default log level', () => {
delete process.env.LOG_LEVEL;
delete process.env.LOG_LEVEL;
const loggerInstance = require('../../src/logger');
expect(loggerInstance.level).toBe('info');
});

test('should set level to "debug" if LOG_LEVEL is set to "debug"', () => {

process.env.LOG_LEVEL = 'debug';

const loggerInstance = require('../../src/logger');

expect(loggerInstance.transport).toBeFalsy();

});




});
33 changes: 12 additions & 21 deletions tests/unit/memory.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,41 +8,32 @@ const {
} = require('../../src/model/data/memory/index');

describe('Fragment Data Management', () => {
// let fragmentMetadata;
// let fragmentData;

// beforeEach(() => {
// const fragmentMetadata = new MemoryDB();
// const fragmentData = new MemoryDB();
// });



test('should write and read fragment metadata', async () => {
const fragment = { ownerId: 'user1', id: 'frag1', data: 'test' };
const fragment = { ownerId: 'user1', id: 'fraggy1', data: 'test' };
await writeFragment(fragment);
const readData = await readFragment('user1', 'frag1');
const readData = await readFragment('user1', 'fraggy1');
expect(readData).toEqual(fragment);
});

test('should write and read fragment data', async () => {
const buffer = Buffer.from('test data');
await writeFragmentData('user1', 'frag1', buffer);
const readData = await readFragmentData('user1', 'frag1');
await writeFragmentData('user1', 'fraggy1', buffer);
const readData = await readFragmentData('user1', 'fraggy1');
expect(readData).toEqual(buffer);
});

test('should list fragments for a user', async () => {
const fragment1 = { ownerId: 'user1', id: 'frag1', data: 'test1' };
const fragment1 = { ownerId: 'user1', id: 'fraggy1', data: 'test1' };
const fragment2 = { ownerId: 'user1', id: 'frag2', data: 'test2' };
await writeFragment(fragment1);
await writeFragment(fragment2);
const fragments = await listFragments('user1');
expect(fragments).toEqual(['frag1', 'frag2']);
expect(fragments).toEqual(['fraggy1', 'frag2']);
});

test('should list expanded fragments for a user', async () => {
const fragment1 = { ownerId: 'user1', id: 'frag1', data: 'test1' };
const fragment1 = { ownerId: 'user1', id: 'fraggy1', data: 'test1' };
const fragment2 = { ownerId: 'user1', id: 'frag2', data: 'test2' };
await writeFragment(fragment1);
await writeFragment(fragment2);
Expand All @@ -51,12 +42,12 @@ describe('Fragment Data Management', () => {
});

test('should delete a fragment', async () => {
const fragment = { ownerId: 'user1', id: 'frag1', data: 'test' };
const fragment = { ownerId: 'user1', id: 'fraggy1', data: 'test' };
await writeFragment(fragment);
await writeFragmentData('user1', 'frag1', Buffer.from('test data'));
await deleteFragment('user1', 'frag1');
const readMetadata = await readFragment('user1', 'frag1');
const readData = await readFragmentData('user1', 'frag1');
await writeFragmentData('user1', 'fraggy1', Buffer.from('test data'));
await deleteFragment('user1', 'fraggy1');
const readMetadata = await readFragment('user1', 'fraggy1');
const readData = await readFragmentData('user1', 'fraggy1');
expect(readMetadata).toBeUndefined();
expect(readData).toBeUndefined();
});
Expand Down
1 change: 0 additions & 1 deletion tests/unit/post.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ describe('GET /v1/fragments', () => {
expect(res.body.fragment.created).toBeTruthy();
expect(res.body.fragment.updated).toBeTruthy();
expect(res.body.fragment.id).toBeTruthy();
// expect(Array.isArray(res.body.fragments)).toBe(true);
});

//testing a post request without data
Expand Down

0 comments on commit 8e52709

Please sign in to comment.