Skip to content

Commit

Permalink
support configuration to limit minimum value sipgatewa netmask can be…
Browse files Browse the repository at this point in the history
… used
  • Loading branch information
xquanluu committed Aug 19, 2024
1 parent ed71abd commit 341f917
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 0 deletions.
7 changes: 7 additions & 0 deletions lib/routes/api/sip-gateways.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ const checkUserScope = async(req, voip_carrier_sid) => {

const validate = async(req, sid) => {
const {lookupSipGatewayBySid} = req.app.locals;
const {netmask} = req.body;
let voip_carrier_sid;

if (sid) {
Expand All @@ -52,6 +53,12 @@ const validate = async(req, sid) => {
voip_carrier_sid = req.body.voip_carrier_sid;
if (!voip_carrier_sid) throw new DbErrorBadRequest('missing voip_carrier_sid');
}
if (netmask &&
process.env.JAMBONZ_MIN_GATEWAY_NETMASK &&
parseInt(netmask) < process.env.JAMBONZ_MIN_GATEWAY_NETMASK) {
throw new DbErrorBadRequest(
`netmask required to have value equal or greater than ${process.env.JAMBONZ_MIN_GATEWAY_NETMASK}`);
}
await checkUserScope(req, voip_carrier_sid);
};

Expand Down
47 changes: 47 additions & 0 deletions test/sip-gateways.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,53 @@ test('sip gateway tests', async(t) => {
let result;
const voip_carrier_sid = await createVoipCarrier(request);

/* add a invalid sip gateway */
const STORED_JAMBONZ_MIN_GATEWAY_NETMASK = process.env.JAMBONZ_MIN_GATEWAY_NETMASK;
process.env.JAMBONZ_MIN_GATEWAY_NETMASK = 24;

result = await request.post('/SipGateways', {
resolveWithFullResponse: true,
auth: authAdmin,
json: true,
simple: false,
body: {
voip_carrier_sid,
ipv4: '1.2.3.4',
netmask: 1,
inbound: true,
outbound: true,
protocol: 'tcp'
}
});
t.ok(result.statusCode === 400, 'successfully created sip gateway ');

result = await request.post('/SipGateways', {
resolveWithFullResponse: true,
auth: authAdmin,
json: true,
body: {
voip_carrier_sid,
ipv4: '1.2.3.4',
netmask: 24,
inbound: true,
outbound: true,
protocol: 'tcp'
}
});
t.ok(result.statusCode === 201, 'successfully created sip gateway ');

process.env.JAMBONZ_MIN_GATEWAY_NETMASK = STORED_JAMBONZ_MIN_GATEWAY_NETMASK;

/* delete sip gateways */
result = await request.delete(`/SipGateways/${result.body.sid}`, {
resolveWithFullResponse: true,
simple: false,
json: true,
auth: authAdmin
});
//console.log(`result: ${JSON.stringify(result)}`);
t.ok(result.statusCode === 204, 'successfully deleted sip gateway');

/* add a sip gateway */
result = await request.post('/SipGateways', {
resolveWithFullResponse: true,
Expand Down

0 comments on commit 341f917

Please sign in to comment.