Skip to content
This repository was archived by the owner on Aug 26, 2022. It is now read-only.
Open
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
1 change: 1 addition & 0 deletions sample.config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ swapExpirationDurationInSeconds = 3600
cookieSecret = '58da74ef560e5578cb46219b7818d7c2'
cookieMaxAgeMs = 86400000
simplePassword = '25ec02267950f537347b4a7c02b00ced'
bearer = '58da74ef560e5578cb46219b7818d7c2'

[threshold]
manualAboveFromAmountUsd = 5000
Expand Down
56 changes: 21 additions & 35 deletions src/api/routes/user.js
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down Expand Up @@ -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',
Expand Down
34 changes: 34 additions & 0 deletions test/auth.test.js
Original file line number Diff line number Diff line change
@@ -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)
})
})
})