Skip to content

Commit

Permalink
added more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Multivit4min committed Apr 14, 2019
1 parent 6a61178 commit 2664d20
Show file tree
Hide file tree
Showing 3 changed files with 98 additions and 70 deletions.
118 changes: 71 additions & 47 deletions test/integration.js
Original file line number Diff line number Diff line change
@@ -1,101 +1,125 @@
/*global describe it */
/*global describe after afterEach it */
const mock = require("mock-require")
const assert = require("assert")
const fs = require("fs")
const crc32 = require("crc-32")
const ResponseError = require("../exception/ResponseError")

let TeamSpeak3 = require("../TeamSpeak3.js")
mock.stop("../transport/TS3Query.js")
TeamSpeak3 = mock.reRequire("../TeamSpeak3.js")

describe("Integration Test", () => {
it("should connect to a TeamSpeak Server via RAW Query", done => {
let ts3 = null

after(async () => {
if (ts3 instanceof TeamSpeak3) await ts3.quit()
})

afterEach(() => {
if (ts3 instanceof TeamSpeak3) ts3.removeAllListeners()
})

it("should connect to a TeamSpeak Server via SSH Query", done => {
let error = null
const ts3 = new TeamSpeak3({
const ts3ssh = new TeamSpeak3({
protocol: "ssh",
host: "127.0.0.1",
queryport: 10011,
queryport: 10022,
serverport: 9987,
username: "serveradmin",
password: "abc123",
nickname: "NodeJS Query Framework"
nickname: "NodeJS SSH"
})

ts3.on("error", e => error = e)
ts3.on("close", () => done(error))
ts3ssh.on("error", e => error = e)
ts3ssh.on("close", () => done(error))

ts3.on("ready", async () => {
ts3ssh.on("ready", async () => {
try {
const [serverinfo, whoami] = await Promise.all([ts3.serverInfo(), ts3.whoami()])
const [serverinfo, whoami] = await Promise.all([ts3ssh.serverInfo(), ts3ssh.whoami()])
assert.equal("object", typeof serverinfo)
assert.equal("string", typeof serverinfo.virtualserver_name)
assert.equal("object", typeof whoami)
assert.equal("NodeJS Query Framework", whoami.client_nickname)
ts3.quit()
assert.equal("NodeJS SSH", whoami.client_nickname)
// eslint-disable-next-line no-underscore-dangle
ts3ssh._ts3.keepAlive()
ts3ssh.quit()
} catch (e) {
ts3.quit()
throw e
error = e
ts3ssh.quit()
}
})

}).timeout(5000)


it("should connect to a TeamSpeak Server via SSH Query", done => {
let error = null
const ts3 = new TeamSpeak3({
protocol: "ssh",
it("should connect to a TeamSpeak Server via RAW Query", done => {
ts3 = new TeamSpeak3({
host: "127.0.0.1",
queryport: 10022,
queryport: 10011,
serverport: 9987,
username: "serveradmin",
password: "abc123",
nickname: "NodeJS Query Framework"
nickname: "NodeJS RAW"
})

ts3.on("error", e => error = e)
ts3.on("close", () => done(error))
ts3.on("error", e => done(e))

ts3.on("ready", async () => {
try {
const [serverinfo, whoami] = await Promise.all([ts3.serverInfo(), ts3.whoami()])
assert.equal("object", typeof serverinfo)
assert.equal("string", typeof serverinfo.virtualserver_name)
assert.equal("object", typeof whoami)
assert.equal("NodeJS Query Framework", whoami.client_nickname)
ts3.quit()
assert.equal("NodeJS RAW", whoami.client_nickname)
// eslint-disable-next-line no-underscore-dangle
ts3._ts3.keepAlive()
try {
await ts3.execute("invalid_command")
done(new Error("should have handled an error"))
} catch (error) {
assert(error instanceof ResponseError)
}
done()
} catch (e) {
ts3.quit()
throw e
done(e)
}
})

}).timeout(5000)

it("should test upload and download of a file", done => {
let error = null
const ts3 = new TeamSpeak3({
host: "127.0.0.1",
queryport: 10011,
serverport: 9987,
username: "serveradmin",
password: "abc123",
nickname: "NodeJS Query Framework"
})

ts3.on("error", e => error = e)
ts3.on("close", () => done(error))
it("should test upload and download of a file", async () => {
if (!(ts3 instanceof TeamSpeak3))
throw new Error("can not run test, due to no valid connection")

const data = fs.readFileSync(`${__dirname}/mocks/filetransfer.png`)
const crc = crc32.buf(data)
await ts3.uploadFile(`/icon_${crc >>> 0}`, data, 0)
const download = await ts3.downloadIcon(`icon_${crc >>> 0}`)
assert.equal(crc, crc32.buf(download))

}).timeout(5000)

// eslint-disable-next-line arrow-body-style
it("should test receiving of an event", () => {
// eslint-disable-next-line no-async-promise-executor
return new Promise(async (fulfill, reject) => {
if (!(ts3 instanceof TeamSpeak3))
return reject(new Error("can not run test, due to no valid connection"))

const servername = `event ${Math.floor(Math.random() * 10000)}`

ts3.once("serveredit", ev => {
assert.deepEqual(ev.modified, { virtualserver_name: servername })
fulfill()
})

ts3.on("ready", async () => {
try {
const data = fs.readFileSync(`${__dirname}/mocks/filetransfer.png`)
const crc = crc32.buf(data)
await ts3.uploadFile(`/icon_${crc >>> 0}`, data, 0)
const download = await ts3.downloadIcon(`icon_${crc >>> 0}`)
assert.equal(crc, crc32.buf(download))
ts3.quit()
await ts3.registerEvent("server")
await ts3.serverEdit({ virtualserver_name: servername })
} catch (e) {
ts3.quit()
error = e
reject(e)
}
})

Expand Down
18 changes: 18 additions & 0 deletions test/ts3query.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/*global describe it */
const mock = require("mock-require")
const assert = require("assert")
mock.stop("../transport/TS3Query.js")
const TS3Query = require("../transport/TS3Query")


describe("TS3Query", () => {

it("should throw an error when the wrong protocol gets required", () => {
assert.throws(() => {
// eslint-disable-next-line no-new
new TS3Query({
protocol: "something false"
})
}, Error)
})
})
32 changes: 9 additions & 23 deletions transport/TS3Query.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ class TS3Query extends EventEmitter {
this.lastcmd = Date.now()
this.active = {}
this.connected = false
this.keepalive = false
this.keepalivetimer = null
this.preventDoubleEvents = true
this.floodTimeout = null
Expand Down Expand Up @@ -198,13 +197,15 @@ class TS3Query extends EventEmitter {
/**
* Sends keepalive to the TeamSpeak Server so the connection will not be closed
* @version 1.0
* @param {boolean} [b=true] - Parameter enables or disables the Keepalive Ping
*/
keepAlive(b = true) {
this._config.keepalive = b
if (!this._config.keepalive)
return clearTimeout(this.keepalivetimer)
this.refreshKeepAlive()
keepAlive() {
clearTimeout(this.keepalivetimer)
this.keepalivetimer = setTimeout(() => {
this.emit("debug", { type: "keepalive" })
this.lastcmd = Date.now()
this.socket.sendKeepAlive()
this.keepAlive()
}, 250 * 1000 - (Date.now() - this.lastcmd))
}


Expand All @@ -217,21 +218,6 @@ class TS3Query extends EventEmitter {
this.preventDoubleEvents = Boolean(b)
}

/**
* Refreshes the Keepalive Timer
* @version 1.0
* @private
*/
refreshKeepAlive() {
clearTimeout(this.keepalivetimer)
this.keepalivetimer = setTimeout(() => {
this.emit("debug", { type: "keepalive" })
this.lastcmd = Date.now()
this.socket.sendKeepAlive()
this.refreshKeepAlive()
}, 250 * 1000 - (Date.now() - this.lastcmd))
}


/**
* Sends a command to the TeamSpeak Server.
Expand Down Expand Up @@ -291,7 +277,7 @@ class TS3Query extends EventEmitter {
this.lastcmd = Date.now()
this.emit("debug", { type: "send", data: raw })
this.socket.send(raw)
this.refreshKeepAlive()
this.keepAlive()
}
}

Expand Down

0 comments on commit 2664d20

Please sign in to comment.