Skip to content
This repository has been archived by the owner on Dec 30, 2021. It is now read-only.

fix(_imgurrequest): fix logical check for credits/search #125

Merged
merged 1 commit into from
Mar 13, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 26 additions & 13 deletions __tests__/imgurRequest.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,31 @@
const imgur = require('../lib/imgur.js'),
imgurTestId1 = 'mbgq7nd'; // Kitten
const imgur = require('../lib/imgur.js');

describe('_imgurRequest()', () => {
test('should fail with no input', () => {
const errMsg = 'Invalid argument';
beforeAll(() => imgur.setClientId('abc123'));

expect(imgur._imgurRequest()).rejects.toThrowError(errMsg);
});
test('should reject with invalid operation', () => {
expect.assertions(1);
return expect(imgur._imgurRequest()).rejects.toMatchInlineSnapshot(
`[Error: Invalid operation]`
);
});

test('should fail with an invalid operation specified', () => {
const errMsg = 'Invalid operation';
test('should reject with no payload', () => {
expect.assertions(1);
return expect(
imgur._imgurRequest('upload', null)
).rejects.toMatchInlineSnapshot(`[Error: No payload specified]`);
});

expect(imgur._imgurRequest('blah', imgurTestId1)).rejects.toThrowError(
errMsg
);
});
test('should resolve with no payload when operation is allowlisted', () => {
expect.assertions(1);
return expect(imgur._imgurRequest('credits', null)).resolves
.toMatchInlineSnapshot(`
Object {
"ClientLimit": 12500,
"ClientRemaining": 12500,
"UserLimit": 500,
"UserRemaining": 500,
"UserReset": 1615614380,
}
`);
});
15 changes: 9 additions & 6 deletions lib/imgur.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,14 +41,17 @@ imgur._imgurRequest = async (operation, payload, extraFormParams) => {
method: null,
encoding: 'utf8',
};
const noPayloadRequired = ['credits', 'search'];
let response = null;

if (
!operation ||
typeof operation !== 'string' ||
(!payload && operation !== ('credits' && 'search'))
) {
throw new Error('Invalid argument');
if (!operation || typeof operation !== 'string') {
throw new Error('Invalid operation');
}

if (!payload) {
if (!noPayloadRequired.includes(operation)) {
throw new Error('No payload specified');
}
}

switch (operation) {
Expand Down
33 changes: 33 additions & 0 deletions lib/mocks/handlers/credits.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
const AuthenticationRequiredResponse = {
data: {
error: 'Authentication required',
request: '/3/credits',
method: 'GET',
},
success: false,
status: 401,
};

const SuccessResponse = {
data: {
UserLimit: 500,
UserRemaining: 500,
UserReset: 1615614380,
ClientLimit: 12500,
ClientRemaining: 12500,
},
success: true,
status: 200,
};

function getHandler(req, res, ctx) {
if (!req.headers.has('authorization')) {
return res(ctx.status(401), ctx.json(AuthenticationRequiredResponse));
}

return res(ctx.json(SuccessResponse));
}

module.exports = {
getHandler,
};
4 changes: 4 additions & 0 deletions lib/mocks/handlers/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ const upload = require('./upload');
const authorize = require('./authorize');
const image = require('./image');
const gallery = require('./gallery');
const credits = require('./credits');

const handlers = [
//upload
Expand All @@ -22,6 +23,9 @@ const handlers = [
// authorize
rest.get('https://api.imgur.com/oauth2/authorize', authorize.getHandler),
rest.post('https://api.imgur.com/oauth2/authorize', authorize.postHandler),

// credits
rest.get('https://api.imgur.com/3/credits', credits.getHandler),
];

module.exports = {
Expand Down
6 changes: 3 additions & 3 deletions lib/mocks/handlers/upload.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,20 +21,20 @@ const SuccessfulUploadResponse = {
function postHandler(req, res, ctx) {
// image field is always required
if (!('image' in req.body)) {
return res(ctx.code(400), ctx.json(BadRequestErrorResponse));
return res(ctx.status(400), ctx.json(BadRequestErrorResponse));
}

// type is optional when uploading a file, but required
// for any other type
if ('type' in req.body) {
// only these types are allowed
if (!['file', 'url', 'base64'].includes(req.body.type)) {
return res(ctx.code(400), ctx.json(BadRequestErrorResponse));
return res(ctx.status(400), ctx.json(BadRequestErrorResponse));
}
// if type is not specified we assume we're uploading a file.
// but we need to make sure a file was sent in the image field
} else if (typeof req.body.image !== 'object') {
return res(ctx.code(400), ctx.json(BadRequestErrorResponse));
return res(ctx.status(400), ctx.json(BadRequestErrorResponse));
}

return res(ctx.json(SuccessfulUploadResponse));
Expand Down