diff --git a/src/controllers/main.ts b/src/controllers/main.ts index 80d6a56..a531066 100644 --- a/src/controllers/main.ts +++ b/src/controllers/main.ts @@ -16,10 +16,34 @@ class MainController extends DefaultController { request: express.Request, response: express.Response ) => { - const token = `${request.query.token}` || `${request.body.text}`; - const data = await getSingle(token); - - response.status(200).send(data); + if (request.query.token || request.body.text) { + const token = `${request.query.token}` || `${request.body.text}`; + // check to see if token is at least 3 characters + if (token.length > 2) { + const data = await getSingle(token); + response.status(200).send(data); + } else { + response.status(400).send({ + data: { + message: + "Please use the endpoint with a get param of 'token'. example https://cryptoget.herokuapp.com/?token=eth", + }, + meta: { + status: 400, + }, + }); + } + } else { + response.status(400).send({ + data: { + message: + "Please use the endpoint with a get param of 'token'. example https://cryptoget.herokuapp.com/?token=eth", + }, + meta: { + status: 400, + }, + }); + } }; } diff --git a/src/controllers/slack.ts b/src/controllers/slack.ts index 2dc1c2a..ef19706 100644 --- a/src/controllers/slack.ts +++ b/src/controllers/slack.ts @@ -16,13 +16,28 @@ class SlackController extends DefaultController { request: express.Request, response: express.Response ) => { - const token = `${request.query.token}` || `${request.body.text}`; - const data = await getSingle(token); + if (request.query.token || request.body.text) { + const token = `${request.query.token}` || `${request.body.text}`; + // check to see if token is at least 3 characters + if (token.length > 2) { + const data = await getSingle(token); - response.status(200).send({ - response_type: "in_channel", - text: `${data.data.message}`, - }); + response.status(200).send({ + response_type: "in_channel", + text: `${data.data.message}`, + }); + } else { + response.status(400).send({ + data: { + message: + "Please use the endpoint with a get param of 'token'. example https://cryptoget.herokuapp.com/?token=eth", + }, + meta: { + status: 400, + }, + }); + } + } }; } diff --git a/src/middleware/validation.ts b/src/middleware/validation.ts index 96b25fb..9dd44ae 100644 --- a/src/middleware/validation.ts +++ b/src/middleware/validation.ts @@ -28,41 +28,6 @@ function validateTokenMiddleware( }, }); } - - // check to see if token exists in request - try { - if (request) { - if (request.query.token || request.body.text) { - const args = `${request.query.token}` || `${request.body.text}`; - // check to see if token is at least 3 characters - if (args.length > 2) { - next(); - } else { - logger.info("Token is invalid, Please use a valid token"); - response.status(400).send({ - data: { - message: - "Please use the endpoint with a get param of 'token'. example https://cryptoget.herokuapp.com/?token=eth", - }, - meta: { - status: 400, - }, - }); - } - } - } - } catch (e) { - logger.info("Token is invalid, Please use a valid token"); - response.status(400).send({ - data: { - message: - "Please use the endpoint with a get param of 'token'. example https://cryptoget.herokuapp.com/?token=eth", - }, - meta: { - status: 400, - }, - }); - } next(); } diff --git a/tests/middleware/validation.test.ts b/tests/middleware/validation.test.ts deleted file mode 100644 index bba2dbc..0000000 --- a/tests/middleware/validation.test.ts +++ /dev/null @@ -1,53 +0,0 @@ -import dotenv from "dotenv"; -import { NextFunction, Request, Response } from "express"; -dotenv.config(); - -import { - validateTokenMiddleware -} from '../../src/middleware/validation'; - -describe('/middleware', () => { - describe('/validation.ts', () => { - describe('validateTokenMiddleware method', () => { - let mockEmptyRequest: Request; - let mockValidRequest: Request; - let mockResponse: Response; - let mockInvalidRequest: Request; - let mockInvalidRequestSlack: Request; - let nextFunction: NextFunction = jest.fn(); - - beforeEach(() => { - mockEmptyRequest = { - query: {}, - } as Request; - mockValidRequest = { - query: {}, - } as Request; - mockInvalidRequest = { - query: {}, - } as Request; - mockInvalidRequestSlack = { - query: {}, - body: {}, - } as Request; - mockValidRequest.query = { - token: 'valid_token', - }; - mockInvalidRequest.query = { - token: 'invalid_token', - }; - mockInvalidRequestSlack.body = { - text: 'invalid_token', - }; - mockResponse = {} as Response; - mockResponse.send = jest.fn(); - mockResponse.header = jest.fn(); - mockResponse.status = jest.fn(() => mockResponse); - }); - it( 'should return 400 from no token', () => { - validateTokenMiddleware(mockEmptyRequest, mockResponse, nextFunction); - expect(mockResponse.status).toHaveBeenCalledWith(400); - }); - }); - }); -}); \ No newline at end of file