Skip to content

Commit

Permalink
fix(getWanStatus): use ConnectionStatus instead of deprecated method
Browse files Browse the repository at this point in the history
fix #1
  • Loading branch information
nioc committed Nov 29, 2023
1 parent a3e5007 commit 0303dd7
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 8 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ Node.js wrapper for Synology SRM API.
## Key features
- authentication
- get WAN status,
- get WAN connection (ip, status, interface name),
- get network utilization,
- get devices with status, IP, etc... ,
- get wifi devices with link quality, signal strength, max rate, band used, etc... ,
Expand Down
21 changes: 16 additions & 5 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -160,17 +160,28 @@ class SrmClient {
}

/**
* Retrieve WAN status
* Retrieve WAN connection status
*
* @return {Promise<Object>} WAN connection (status, ip, interface name)
*
* @return {Promise<Boolean>} Does WAN is ok
*/
async getWanStatus () {
async getWanConnectionStatus () {
const data = {
method: 'get',
version: 1,
api: 'SYNO.Mesh.Network.WANStatus',
api: 'SYNO.Core.Network.Router.ConnectionStatus',
}
return (await this.request('/webapi/entry.cgi', data)).wan_connected
return (await this.request('/webapi/entry.cgi', data))
}

/**
* Retrieve WAN status
*
* @return {Promise<Boolean>} Does WAN is ok
*/
async getWanStatus () {
const getWanConnectionStatus = await this.getWanConnectionStatus()
return getWanConnectionStatus.ipv4.conn_status === 'normal' || getWanConnectionStatus.ipv6.conn_status === 'normal'
}

/**
Expand Down
35 changes: 32 additions & 3 deletions test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -217,15 +217,44 @@ describe('Logout', () => {
})
})

describe('get WAN connection status', () => {
it('should return an object with ipv4 and ipv6 attributes', async () => {
nock(baseUrl)
.post(pathEntry, 'method=get&version=1&api=SYNO.Core.Network.Router.ConnectionStatus')
.reply(200, { data: { ipv4: { conn_status: 'normal', ifname: 'eth0', ip: '192.168.1.16', pppoe: false, vpn_profile: '' }, ipv6: { conn_status: 'not available', ifname: '', ip: '', pppoe: true } }, success: true })
const client = new SrmClient(baseUrl, sid)
const result = await client.getWanConnectionStatus()
assert.strictEqual(result.ipv4.ip, '192.168.1.16')
assert.strictEqual(result.ipv4.conn_status, 'normal')
assert.strictEqual(result.ipv6.conn_status, 'not available')
})
})

describe('get WAN status', () => {
it('should return a boolean', async () => {
it('should return true if there is an IPv4 connection', async () => {
nock(baseUrl)
.post(pathEntry, 'method=get&version=1&api=SYNO.Core.Network.Router.ConnectionStatus')
.reply(200, { data: { ipv4: { conn_status: 'normal', ifname: 'eth0', ip: '192.168.1.16', pppoe: false, vpn_profile: '' }, ipv6: { conn_status: 'not available', ifname: '', ip: '', pppoe: true } }, success: true })
const client = new SrmClient(baseUrl, sid)
const result = await client.getWanStatus()
assert.strictEqual(result, true)
})
it('should return true if there is an IPv6 connection', async () => {
nock(baseUrl)
.post(pathEntry, 'method=get&version=1&api=SYNO.Mesh.Network.WANStatus')
.reply(200, { data: { wan_connected: true }, success: true })
.post(pathEntry, 'method=get&version=1&api=SYNO.Core.Network.Router.ConnectionStatus')
.reply(200, { data: { ipv4: { conn_status: 'not available', ifname: '', ip: '', pppoe: true }, ipv6: { conn_status: 'normal', ifname: 'eth0', ip: '2001:0db8:0000:85a3:0000:0000:ac1f:8001', pppoe: false, vpn_profile: '' } }, success: true })
const client = new SrmClient(baseUrl, sid)
const result = await client.getWanStatus()
assert.strictEqual(result, true)
})
it('should return false if there is no connection', async () => {
nock(baseUrl)
.post(pathEntry, 'method=get&version=1&api=SYNO.Core.Network.Router.ConnectionStatus')
.reply(200, { data: { ipv4: { conn_status: 'not available', ifname: '', ip: '', pppoe: true }, ipv6: { conn_status: 'not available', ifname: '', ip: '', pppoe: true } }, success: true })
const client = new SrmClient(baseUrl, sid)
const result = await client.getWanStatus()
assert.strictEqual(result, false)
})
})

describe('get traffic', () => {
Expand Down

0 comments on commit 0303dd7

Please sign in to comment.