diff --git a/sample.config.toml b/sample.config.toml index 7bb290fb..14f2670a 100644 --- a/sample.config.toml +++ b/sample.config.toml @@ -17,6 +17,7 @@ swapExpirationDurationInSeconds = 3600 cookieSecret = '58da74ef560e5578cb46219b7818d7c2' cookieMaxAgeMs = 86400000 simplePassword = '25ec02267950f537347b4a7c02b00ced' +bearer = '58da74ef560e5578cb46219b7818d7c2' [threshold] manualAboveFromAmountUsd = 5000 diff --git a/src/api/routes/user.js b/src/api/routes/user.js index e196797a..06147096 100644 --- a/src/api/routes/user.js +++ b/src/api/routes/user.js @@ -6,6 +6,7 @@ const { parseArgsStringToArgv } = require('string-argv') const config = require('../../config') const Check = require('../../models/Check') const Order = require('../../models/Order') +const { getAtomicAgentQueue } = require('../../worker') const { safeCompare } = require('../../utils/crypto') const ensureAuth = require('../../middlewares/ensureAuth') @@ -108,46 +109,31 @@ router.get( }) ) -// router.post( -// '/order/retry', -// ensureAuth(401), -// asyncHandler(async (req, res) => { -// const { body } = req -// const { orderId, jobName } = body - -// if (!orderId) { -// return res.notOk(400, 'Order ID missing') -// } - -// if (!ALLOWED_RETRY_JOBS.find((job) => job.name === jobName)) { -// return res.notOk(400, `Invalid job name: ${jobName}`) -// } - -// const order = await Order.findOne({ orderId: orderId }).exec() -// if (!order) { -// return res.notOk(400, `Order not found: ${orderId}`) -// } - -// const index = ALLOWED_RETRY_JOBS.findIndex((job) => job.name === jobName) -// const jobsToBeRemoved = ALLOWED_RETRY_JOBS.slice(index).map((job) => job.name) +router.get( + '/order/retry', + asyncHandler(async (req, res) => { + const { query } = req + const { orderId } = query + const bearer = req.headers.authorization -// await agenda.cancel({ -// name: { -// $in: jobsToBeRemoved -// }, -// 'data.orderId': orderId -// }) + if (!safeCompare(bearer, config.auth.bearer)) { + return res.notOk(401, 'Unauthorised') + } -// order.status = ALLOWED_RETRY_JOBS[index].setStatus -// await order.save() + if (!orderId) { + return res.notOk(400, 'Order ID missing') + } -// await agenda.now(jobName, { orderId: order.orderId }) + const order = await Order.findOne({ orderId: orderId }).exec() + if (!order) { + return res.notOk(400, `Order not found: ${orderId}`) + } -// await order.log('RETRY', jobName) + await getAtomicAgentQueue().add({ orderId: order.orderId }, { jobId: order.orderId }) -// res.ok() -// }) -// ) + res.ok() + }) +) router.post( '/order/ignore', diff --git a/test/auth.test.js b/test/auth.test.js new file mode 100644 index 00000000..db44ea71 --- /dev/null +++ b/test/auth.test.js @@ -0,0 +1,34 @@ +/* eslint-env mocha */ +const chai = require('chai') +const chaiHttp = require('chai-http') +chai.use(chaiHttp) + +const { app } = require('../src/api') +const { prepare } = require('./lib/utils') +const config = require('../src/config') + +describe('Test Order retry Auth', () => { + before(async function () { + this.timeout(0) + await prepare() + }) + + it('Should return 401 without bearer token', async () => { + return chai + .request(app()) + .get('/api/user/order/retry?orderId=' + '123213234') + .then((res) => { + res.should.have.status(401) + }) + }) + + it('Should return Order not found for invalid orderId', async () => { + return chai + .request(app()) + .get('/api/user/order/retry?orderId=' + '123213234') + .set('Authorization', config.auth.bearer) + .then((res) => { + res.should.have.status(400) + }) + }) +})